org.el 842 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t)
  74. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  75. ;; some places -- e.g. see the macro org-with-limited-levels.
  76. ;;
  77. ;; In Org buffers, the value of `outline-regexp' is that of
  78. ;; `org-outline-regexp'. The only function still directly relying on
  79. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  80. ;; job when `orgstruct-mode' is active.
  81. (defvar org-outline-regexp "\\*+ "
  82. "Regexp to match Org headlines.")
  83. (defvar org-outline-regexp-bol "^\\*+ "
  84. "Regexp to match Org headlines.
  85. This is similar to `org-outline-regexp' but additionally makes
  86. sure that we are at the beginning of the line.")
  87. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  88. "Matches an headline, putting stars and text into groups.
  89. Stars are put in group 1 and the trimmed body in group 2.")
  90. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  91. (when (fboundp 'defvaralias)
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (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-at-clock-log-p "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" ())
  106. (declare-function org-clock-timestamps-down "org-clock" ())
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "org-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-id-get-create "org-id" (&optional force))
  114. (declare-function org-id-find-id-file "org-id" (id))
  115. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  116. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  117. (declare-function org-table-align "org-table" ())
  118. (declare-function org-table-paste-rectangle "org-table" ())
  119. (declare-function org-table-maybe-eval-formula "org-table" ())
  120. (declare-function org-table-maybe-recalculate-line "org-table" ())
  121. ;; load languages based on value of `org-babel-load-languages'
  122. (defvar org-babel-load-languages)
  123. ;;;###autoload
  124. (defun org-babel-do-load-languages (sym value)
  125. "Load the languages defined in `org-babel-load-languages'."
  126. (set-default sym value)
  127. (mapc (lambda (pair)
  128. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  129. (if active
  130. (progn
  131. (require (intern (concat "ob-" lang))))
  132. (progn
  133. (funcall 'fmakunbound
  134. (intern (concat "org-babel-execute:" lang)))
  135. (funcall 'fmakunbound
  136. (intern (concat "org-babel-expand-body:" lang)))))))
  137. org-babel-load-languages))
  138. (defcustom org-babel-load-languages '((emacs-lisp . t))
  139. "Languages which can be evaluated in Org-mode buffers.
  140. This list can be used to load support for any of the languages
  141. below, note that each language will depend on a different set of
  142. system executables and/or Emacs modes. When a language is
  143. \"loaded\", then code blocks in that language can be evaluated
  144. with `org-babel-execute-src-block' bound by default to C-c
  145. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  146. be set to remove code block evaluation from the C-c C-c
  147. keybinding. By default only Emacs Lisp (which has no
  148. requirements) is loaded."
  149. :group 'org-babel
  150. :set 'org-babel-do-load-languages
  151. :version "24.1"
  152. :type '(alist :tag "Babel Languages"
  153. :key-type
  154. (choice
  155. (const :tag "Awk" awk)
  156. (const :tag "C" C)
  157. (const :tag "R" R)
  158. (const :tag "Asymptote" asymptote)
  159. (const :tag "Calc" calc)
  160. (const :tag "Clojure" clojure)
  161. (const :tag "CSS" css)
  162. (const :tag "Ditaa" ditaa)
  163. (const :tag "Dot" dot)
  164. (const :tag "Emacs Lisp" emacs-lisp)
  165. (const :tag "Fortran" fortran)
  166. (const :tag "Gnuplot" gnuplot)
  167. (const :tag "Haskell" haskell)
  168. (const :tag "IO" io)
  169. (const :tag "Java" java)
  170. (const :tag "Javascript" js)
  171. (const :tag "LaTeX" latex)
  172. (const :tag "Ledger" ledger)
  173. (const :tag "Lilypond" lilypond)
  174. (const :tag "Lisp" lisp)
  175. (const :tag "Maxima" maxima)
  176. (const :tag "Matlab" matlab)
  177. (const :tag "Mscgen" mscgen)
  178. (const :tag "Ocaml" ocaml)
  179. (const :tag "Octave" octave)
  180. (const :tag "Org" org)
  181. (const :tag "Perl" perl)
  182. (const :tag "Pico Lisp" picolisp)
  183. (const :tag "PlantUML" plantuml)
  184. (const :tag "Python" python)
  185. (const :tag "Ruby" ruby)
  186. (const :tag "Sass" sass)
  187. (const :tag "Scala" scala)
  188. (const :tag "Scheme" scheme)
  189. (const :tag "Screen" screen)
  190. (const :tag "Shell Script" sh)
  191. (const :tag "Shen" shen)
  192. (const :tag "Sql" sql)
  193. (const :tag "Sqlite" sqlite))
  194. :value-type (boolean :tag "Activate" :value t)))
  195. ;;;; Customization variables
  196. (defcustom org-clone-delete-id nil
  197. "Remove ID property of clones of a subtree.
  198. When non-nil, clones of a subtree don't inherit the ID property.
  199. Otherwise they inherit the ID property with a new unique
  200. identifier."
  201. :type 'boolean
  202. :version "24.1"
  203. :group 'org-id)
  204. ;;; Version
  205. (require 'org-compat)
  206. (org-check-version)
  207. ;;;###autoload
  208. (defun org-version (&optional here full message)
  209. "Show the org-mode version in the echo area.
  210. With prefix argument HERE, insert it at point.
  211. When FULL is non-nil, use a verbose version string.
  212. When MESSAGE is non-nil, display a message with the version."
  213. (interactive "P")
  214. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  215. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs.el")))
  216. (org-trash (or
  217. (and (fboundp 'org-release) (fboundp 'org-git-version))
  218. (load (concat org-dir "org-version.el")
  219. 'noerror 'nomessage 'nosuffix)))
  220. (org-version (org-release))
  221. (git-version (org-git-version))
  222. (version (format "Org-mode version %s (%s @ %s)"
  223. org-version
  224. git-version
  225. (if org-install-dir
  226. (if (string= org-dir org-install-dir)
  227. org-install-dir
  228. (concat "mixed installation! " org-install-dir " and " org-dir))
  229. "org-loaddefs.el can not be found!")))
  230. (_version (if full version org-version)))
  231. (if (org-called-interactively-p 'interactive)
  232. (if here
  233. (insert version)
  234. (message version))
  235. (if message (message _version))
  236. _version)))
  237. (defconst org-version (org-version))
  238. ;;; Compatibility constants
  239. ;;; The custom variables
  240. (defgroup org nil
  241. "Outline-based notes management and organizer."
  242. :tag "Org"
  243. :group 'outlines
  244. :group 'calendar)
  245. (defcustom org-mode-hook nil
  246. "Mode hook for Org-mode, run after the mode was turned on."
  247. :group 'org
  248. :type 'hook)
  249. (defcustom org-load-hook nil
  250. "Hook that is run after org.el has been loaded."
  251. :group 'org
  252. :type 'hook)
  253. (defcustom org-log-buffer-setup-hook nil
  254. "Hook that is run after an Org log buffer is created."
  255. :group 'org
  256. :version "24.1"
  257. :type 'hook)
  258. (defvar org-modules) ; defined below
  259. (defvar org-modules-loaded nil
  260. "Have the modules been loaded already?")
  261. (defun org-load-modules-maybe (&optional force)
  262. "Load all extensions listed in `org-modules'."
  263. (when (or force (not org-modules-loaded))
  264. (mapc (lambda (ext)
  265. (condition-case nil (require ext)
  266. (error (message "Problems while trying to load feature `%s'" ext))))
  267. org-modules)
  268. (setq org-modules-loaded t)))
  269. (defun org-set-modules (var value)
  270. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  271. (set var value)
  272. (when (featurep 'org)
  273. (org-load-modules-maybe 'force)))
  274. (when (org-bound-and-true-p org-modules)
  275. (let ((a (member 'org-infojs org-modules)))
  276. (and a (setcar a 'org-jsinfo))))
  277. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  278. "Modules that should always be loaded together with org.el.
  279. If a description starts with <C>, the file is not part of Emacs
  280. and loading it will require that you have downloaded and properly installed
  281. the org-mode distribution.
  282. You can also use this system to load external packages (i.e. neither Org
  283. core modules, nor modules from the CONTRIB directory). Just add symbols
  284. to the end of the list. If the package is called org-xyz.el, then you need
  285. to add the symbol `xyz', and the package must have a call to
  286. (provide 'org-xyz)"
  287. :group 'org
  288. :set 'org-set-modules
  289. :type
  290. '(set :greedy t
  291. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  292. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  293. (const :tag " crypt: Encryption of subtrees" org-crypt)
  294. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  295. (const :tag " docview: Links to doc-view buffers" org-docview)
  296. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  297. (const :tag " id: Global IDs for identifying entries" org-id)
  298. (const :tag " info: Links to Info nodes" org-info)
  299. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  300. (const :tag " habit: Track your consistency with habits" org-habit)
  301. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  302. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  303. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  304. (const :tag " mew Links to Mew folders/messages" org-mew)
  305. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  306. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  307. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  308. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  309. (const :tag " vm: Links to VM folders/messages" org-vm)
  310. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  311. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  312. (const :tag " mouse: Additional mouse support" org-mouse)
  313. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  314. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  315. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  316. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  317. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  318. (const :tag "C collector: Collect properties into tables" org-collector)
  319. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  320. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  321. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  322. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  323. (const :tag "C eval: Include command output as text" org-eval)
  324. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  325. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  326. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  327. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  328. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  329. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  330. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  331. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  332. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  333. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  334. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  335. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  336. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  337. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  338. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  339. (const :tag "C registry: A registry for Org-mode links" org-registry)
  340. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  341. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  342. (const :tag "C secretary: Team management with org-mode" org-secretary)
  343. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  344. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  345. (const :tag "C track: Keep up with Org-mode development" org-track)
  346. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  347. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  348. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  349. (defcustom org-support-shift-select nil
  350. "Non-nil means make shift-cursor commands select text when possible.
  351. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  352. start selecting a region, or enlarge regions started in this way.
  353. In Org-mode, in special contexts, these same keys are used for
  354. other purposes, important enough to compete with shift selection.
  355. Org tries to balance these needs by supporting `shift-select-mode'
  356. outside these special contexts, under control of this variable.
  357. The default of this variable is nil, to avoid confusing behavior. Shifted
  358. cursor keys will then execute Org commands in the following contexts:
  359. - on a headline, changing TODO state (left/right) and priority (up/down)
  360. - on a time stamp, changing the time
  361. - in a plain list item, changing the bullet type
  362. - in a property definition line, switching between allowed values
  363. - in the BEGIN line of a clock table (changing the time block).
  364. Outside these contexts, the commands will throw an error.
  365. When this variable is t and the cursor is not in a special
  366. context, Org-mode will support shift-selection for making and
  367. enlarging regions. To make this more effective, the bullet
  368. cycling will no longer happen anywhere in an item line, but only
  369. if the cursor is exactly on the bullet.
  370. If you set this variable to the symbol `always', then the keys
  371. will not be special in headlines, property lines, and item lines,
  372. to make shift selection work there as well. If this is what you
  373. want, you can use the following alternative commands: `C-c C-t'
  374. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  375. can be used to switch TODO sets, `C-c -' to cycle item bullet
  376. types, and properties can be edited by hand or in column view.
  377. However, when the cursor is on a timestamp, shift-cursor commands
  378. will still edit the time stamp - this is just too good to give up.
  379. XEmacs user should have this variable set to nil, because
  380. `shift-select-mode' is in Emacs 23 or later only."
  381. :group 'org
  382. :type '(choice
  383. (const :tag "Never" nil)
  384. (const :tag "When outside special context" t)
  385. (const :tag "Everywhere except timestamps" always)))
  386. (defcustom org-loop-over-headlines-in-active-region nil
  387. "Shall some commands act upon headlines in the active region?
  388. When set to `t', some commands will be performed in all headlines
  389. within the active region.
  390. When set to `start-level', some commands will be performed in all
  391. headlines within the active region, provided that these headlines
  392. are of the same level than the first one.
  393. When set to a string, those commands will be performed on the
  394. matching headlines within the active region. Such string must be
  395. a tags/property/todo match as it is used in the agenda tags view.
  396. The list of commands is: `org-schedule', `org-deadline',
  397. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  398. `org-archive-to-archive-sibling'. The archiving commands skip
  399. already archived entries."
  400. :type '(choice (const :tag "Don't loop" nil)
  401. (const :tag "All headlines in active region" t)
  402. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  403. (string :tag "Tags/Property/Todo matcher"))
  404. :version "24.1"
  405. :group 'org-todo
  406. :group 'org-archive)
  407. (defgroup org-startup nil
  408. "Options concerning startup of Org-mode."
  409. :tag "Org Startup"
  410. :group 'org)
  411. (defcustom org-startup-folded t
  412. "Non-nil means entering Org-mode will switch to OVERVIEW.
  413. This can also be configured on a per-file basis by adding one of
  414. the following lines anywhere in the buffer:
  415. #+STARTUP: fold (or `overview', this is equivalent)
  416. #+STARTUP: nofold (or `showall', this is equivalent)
  417. #+STARTUP: content
  418. #+STARTUP: showeverything"
  419. :group 'org-startup
  420. :type '(choice
  421. (const :tag "nofold: show all" nil)
  422. (const :tag "fold: overview" t)
  423. (const :tag "content: all headlines" content)
  424. (const :tag "show everything, even drawers" showeverything)))
  425. (defcustom org-startup-truncated t
  426. "Non-nil means entering Org-mode will set `truncate-lines'.
  427. This is useful since some lines containing links can be very long and
  428. uninteresting. Also tables look terrible when wrapped."
  429. :group 'org-startup
  430. :type 'boolean)
  431. (defcustom org-startup-indented nil
  432. "Non-nil means turn on `org-indent-mode' on startup.
  433. This can also be configured on a per-file basis by adding one of
  434. the following lines anywhere in the buffer:
  435. #+STARTUP: indent
  436. #+STARTUP: noindent"
  437. :group 'org-structure
  438. :type '(choice
  439. (const :tag "Not" nil)
  440. (const :tag "Globally (slow on startup in large files)" t)))
  441. (defcustom org-use-sub-superscripts t
  442. "Non-nil means interpret \"_\" and \"^\" for export.
  443. When this option is turned on, you can use TeX-like syntax for sub- and
  444. superscripts. Several characters after \"_\" or \"^\" will be
  445. considered as a single item - so grouping with {} is normally not
  446. needed. For example, the following things will be parsed as single
  447. sub- or superscripts.
  448. 10^24 or 10^tau several digits will be considered 1 item.
  449. 10^-12 or 10^-tau a leading sign with digits or a word
  450. x^2-y^3 will be read as x^2 - y^3, because items are
  451. terminated by almost any nonword/nondigit char.
  452. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  453. Still, ambiguity is possible - so when in doubt use {} to enclose the
  454. sub/superscript. If you set this variable to the symbol `{}',
  455. the braces are *required* in order to trigger interpretations as
  456. sub/superscript. This can be helpful in documents that need \"_\"
  457. frequently in plain text.
  458. Not all export backends support this, but HTML does.
  459. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  460. :group 'org-startup
  461. :group 'org-export-translation
  462. :version "24.1"
  463. :type '(choice
  464. (const :tag "Always interpret" t)
  465. (const :tag "Only with braces" {})
  466. (const :tag "Never interpret" nil)))
  467. (if (fboundp 'defvaralias)
  468. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  469. (defcustom org-startup-with-beamer-mode nil
  470. "Non-nil means turn on `org-beamer-mode' on startup.
  471. This can also be configured on a per-file basis by adding one of
  472. the following lines anywhere in the buffer:
  473. #+STARTUP: beamer"
  474. :group 'org-startup
  475. :version "24.1"
  476. :type 'boolean)
  477. (defcustom org-startup-align-all-tables nil
  478. "Non-nil means align all tables when visiting a file.
  479. This is useful when the column width in tables is forced with <N> cookies
  480. in table fields. Such tables will look correct only after the first re-align.
  481. This can also be configured on a per-file basis by adding one of
  482. the following lines anywhere in the buffer:
  483. #+STARTUP: align
  484. #+STARTUP: noalign"
  485. :group 'org-startup
  486. :type 'boolean)
  487. (defcustom org-startup-with-inline-images nil
  488. "Non-nil means show inline images when loading a new Org file.
  489. This can also be configured on a per-file basis by adding one of
  490. the following lines anywhere in the buffer:
  491. #+STARTUP: inlineimages
  492. #+STARTUP: noinlineimages"
  493. :group 'org-startup
  494. :version "24.1"
  495. :type 'boolean)
  496. (defcustom org-insert-mode-line-in-empty-file nil
  497. "Non-nil means insert the first line setting Org-mode in empty files.
  498. When the function `org-mode' is called interactively in an empty file, this
  499. normally means that the file name does not automatically trigger Org-mode.
  500. To ensure that the file will always be in Org-mode in the future, a
  501. line enforcing Org-mode will be inserted into the buffer, if this option
  502. has been set."
  503. :group 'org-startup
  504. :type 'boolean)
  505. (defcustom org-replace-disputed-keys nil
  506. "Non-nil means use alternative key bindings for some keys.
  507. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  508. These keys are also used by other packages like shift-selection-mode'
  509. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  510. If you want to use Org-mode together with one of these other modes,
  511. or more generally if you would like to move some Org-mode commands to
  512. other keys, set this variable and configure the keys with the variable
  513. `org-disputed-keys'.
  514. This option is only relevant at load-time of Org-mode, and must be set
  515. *before* org.el is loaded. Changing it requires a restart of Emacs to
  516. become effective."
  517. :group 'org-startup
  518. :type 'boolean)
  519. (defcustom org-use-extra-keys nil
  520. "Non-nil means use extra key sequence definitions for certain commands.
  521. This happens automatically if you run XEmacs or if `window-system'
  522. is nil. This variable lets you do the same manually. You must
  523. set it before loading org.
  524. Example: on Carbon Emacs 22 running graphically, with an external
  525. keyboard on a Powerbook, the default way of setting M-left might
  526. not work for either Alt or ESC. Setting this variable will make
  527. it work for ESC."
  528. :group 'org-startup
  529. :type 'boolean)
  530. (if (fboundp 'defvaralias)
  531. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  532. (defcustom org-disputed-keys
  533. '(([(shift up)] . [(meta p)])
  534. ([(shift down)] . [(meta n)])
  535. ([(shift left)] . [(meta -)])
  536. ([(shift right)] . [(meta +)])
  537. ([(control shift right)] . [(meta shift +)])
  538. ([(control shift left)] . [(meta shift -)]))
  539. "Keys for which Org-mode and other modes compete.
  540. This is an alist, cars are the default keys, second element specifies
  541. the alternative to use when `org-replace-disputed-keys' is t.
  542. Keys can be specified in any syntax supported by `define-key'.
  543. The value of this option takes effect only at Org-mode's startup,
  544. therefore you'll have to restart Emacs to apply it after changing."
  545. :group 'org-startup
  546. :type 'alist)
  547. (defun org-key (key)
  548. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  549. Or return the original if not disputed.
  550. Also apply the translations defined in `org-xemacs-key-equivalents'."
  551. (when org-replace-disputed-keys
  552. (let* ((nkey (key-description key))
  553. (x (org-find-if (lambda (x)
  554. (equal (key-description (car x)) nkey))
  555. org-disputed-keys)))
  556. (setq key (if x (cdr x) key))))
  557. (when (featurep 'xemacs)
  558. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  559. key)
  560. (defun org-find-if (predicate seq)
  561. (catch 'exit
  562. (while seq
  563. (if (funcall predicate (car seq))
  564. (throw 'exit (car seq))
  565. (pop seq)))))
  566. (defun org-defkey (keymap key def)
  567. "Define a key, possibly translated, as returned by `org-key'."
  568. (define-key keymap (org-key key) def))
  569. (defcustom org-ellipsis nil
  570. "The ellipsis to use in the Org-mode outline.
  571. When nil, just use the standard three dots. When a string, use that instead,
  572. When a face, use the standard 3 dots, but with the specified face.
  573. The change affects only Org-mode (which will then use its own display table).
  574. Changing this requires executing `M-x org-mode' in a buffer to become
  575. effective."
  576. :group 'org-startup
  577. :type '(choice (const :tag "Default" nil)
  578. (face :tag "Face" :value org-warning)
  579. (string :tag "String" :value "...#")))
  580. (defvar org-display-table nil
  581. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  582. (defgroup org-keywords nil
  583. "Keywords in Org-mode."
  584. :tag "Org Keywords"
  585. :group 'org)
  586. (defcustom org-deadline-string "DEADLINE:"
  587. "String to mark deadline entries.
  588. A deadline is this string, followed by a time stamp. Should be a word,
  589. terminated by a colon. You can insert a schedule keyword and
  590. a timestamp with \\[org-deadline].
  591. Changes become only effective after restarting Emacs."
  592. :group 'org-keywords
  593. :type 'string)
  594. (defcustom org-scheduled-string "SCHEDULED:"
  595. "String to mark scheduled TODO entries.
  596. A schedule is this string, followed by a time stamp. Should be a word,
  597. terminated by a colon. You can insert a schedule keyword and
  598. a timestamp with \\[org-schedule].
  599. Changes become only effective after restarting Emacs."
  600. :group 'org-keywords
  601. :type 'string)
  602. (defcustom org-closed-string "CLOSED:"
  603. "String used as the prefix for timestamps logging closing a TODO entry."
  604. :group 'org-keywords
  605. :type 'string)
  606. (defcustom org-clock-string "CLOCK:"
  607. "String used as prefix for timestamps clocking work hours on an item."
  608. :group 'org-keywords
  609. :type 'string)
  610. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  611. org-scheduled-string "\\|"
  612. org-deadline-string "\\|"
  613. org-closed-string "\\|"
  614. org-clock-string "\\)")
  615. "Matches a line with planning or clock info.")
  616. (defcustom org-comment-string "COMMENT"
  617. "Entries starting with this keyword will never be exported.
  618. An entry can be toggled between COMMENT and normal with
  619. \\[org-toggle-comment].
  620. Changes become only effective after restarting Emacs."
  621. :group 'org-keywords
  622. :type 'string)
  623. (defcustom org-quote-string "QUOTE"
  624. "Entries starting with this keyword will be exported in fixed-width font.
  625. Quoting applies only to the text in the entry following the headline, and does
  626. not extend beyond the next headline, even if that is lower level.
  627. An entry can be toggled between QUOTE and normal with
  628. \\[org-toggle-fixed-width-section]."
  629. :group 'org-keywords
  630. :type 'string)
  631. (defconst org-repeat-re
  632. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  633. "Regular expression for specifying repeated events.
  634. After a match, group 1 contains the repeat expression.")
  635. (defgroup org-structure nil
  636. "Options concerning the general structure of Org-mode files."
  637. :tag "Org Structure"
  638. :group 'org)
  639. (defgroup org-reveal-location nil
  640. "Options about how to make context of a location visible."
  641. :tag "Org Reveal Location"
  642. :group 'org-structure)
  643. (defconst org-context-choice
  644. '(choice
  645. (const :tag "Always" t)
  646. (const :tag "Never" nil)
  647. (repeat :greedy t :tag "Individual contexts"
  648. (cons
  649. (choice :tag "Context"
  650. (const agenda)
  651. (const org-goto)
  652. (const occur-tree)
  653. (const tags-tree)
  654. (const link-search)
  655. (const mark-goto)
  656. (const bookmark-jump)
  657. (const isearch)
  658. (const default))
  659. (boolean))))
  660. "Contexts for the reveal options.")
  661. (defcustom org-show-hierarchy-above '((default . t))
  662. "Non-nil means show full hierarchy when revealing a location.
  663. Org-mode often shows locations in an org-mode file which might have
  664. been invisible before. When this is set, the hierarchy of headings
  665. above the exposed location is shown.
  666. Turning this off for example for sparse trees makes them very compact.
  667. Instead of t, this can also be an alist specifying this option for different
  668. contexts. Valid contexts are
  669. agenda when exposing an entry from the agenda
  670. org-goto when using the command `org-goto' on key C-c C-j
  671. occur-tree when using the command `org-occur' on key C-c /
  672. tags-tree when constructing a sparse tree based on tags matches
  673. link-search when exposing search matches associated with a link
  674. mark-goto when exposing the jump goal of a mark
  675. bookmark-jump when exposing a bookmark location
  676. isearch when exiting from an incremental search
  677. default default for all contexts not set explicitly"
  678. :group 'org-reveal-location
  679. :type org-context-choice)
  680. (defcustom org-show-following-heading '((default . nil))
  681. "Non-nil means show following heading when revealing a location.
  682. Org-mode often shows locations in an org-mode file which might have
  683. been invisible before. When this is set, the heading following the
  684. match is shown.
  685. Turning this off for example for sparse trees makes them very compact,
  686. but makes it harder to edit the location of the match. In such a case,
  687. use the command \\[org-reveal] to show more context.
  688. Instead of t, this can also be an alist specifying this option for different
  689. contexts. See `org-show-hierarchy-above' for valid contexts."
  690. :group 'org-reveal-location
  691. :type org-context-choice)
  692. (defcustom org-show-siblings '((default . nil) (isearch t))
  693. "Non-nil means show all sibling heading when revealing a location.
  694. Org-mode often shows locations in an org-mode file which might have
  695. been invisible before. When this is set, the sibling of the current entry
  696. heading are all made visible. If `org-show-hierarchy-above' is t,
  697. the same happens on each level of the hierarchy above the current entry.
  698. By default this is on for the isearch context, off for all other contexts.
  699. Turning this off for example for sparse trees makes them very compact,
  700. but makes it harder to edit the location of the match. In such a case,
  701. use the command \\[org-reveal] to show more context.
  702. Instead of t, this can also be an alist specifying this option for different
  703. contexts. See `org-show-hierarchy-above' for valid contexts."
  704. :group 'org-reveal-location
  705. :type org-context-choice)
  706. (defcustom org-show-entry-below '((default . nil))
  707. "Non-nil means show the entry below a headline when revealing a location.
  708. Org-mode often shows locations in an org-mode file which might have
  709. been invisible before. When this is set, the text below the headline that is
  710. exposed is also shown.
  711. By default this is off for all contexts.
  712. Instead of t, this can also be an alist specifying this option for different
  713. contexts. See `org-show-hierarchy-above' for valid contexts."
  714. :group 'org-reveal-location
  715. :type org-context-choice)
  716. (defcustom org-indirect-buffer-display 'other-window
  717. "How should indirect tree buffers be displayed?
  718. This applies to indirect buffers created with the commands
  719. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  720. Valid values are:
  721. current-window Display in the current window
  722. other-window Just display in another window.
  723. dedicated-frame Create one new frame, and re-use it each time.
  724. new-frame Make a new frame each time. Note that in this case
  725. previously-made indirect buffers are kept, and you need to
  726. kill these buffers yourself."
  727. :group 'org-structure
  728. :group 'org-agenda-windows
  729. :type '(choice
  730. (const :tag "In current window" current-window)
  731. (const :tag "In current frame, other window" other-window)
  732. (const :tag "Each time a new frame" new-frame)
  733. (const :tag "One dedicated frame" dedicated-frame)))
  734. (defcustom org-use-speed-commands nil
  735. "Non-nil means activate single letter commands at beginning of a headline.
  736. This may also be a function to test for appropriate locations where speed
  737. commands should be active."
  738. :group 'org-structure
  739. :type '(choice
  740. (const :tag "Never" nil)
  741. (const :tag "At beginning of headline stars" t)
  742. (function)))
  743. (defcustom org-speed-commands-user nil
  744. "Alist of additional speed commands.
  745. This list will be checked before `org-speed-commands-default'
  746. when the variable `org-use-speed-commands' is non-nil
  747. and when the cursor is at the beginning of a headline.
  748. The car if each entry is a string with a single letter, which must
  749. be assigned to `self-insert-command' in the global map.
  750. The cdr is either a command to be called interactively, a function
  751. to be called, or a form to be evaluated.
  752. An entry that is just a list with a single string will be interpreted
  753. as a descriptive headline that will be added when listing the speed
  754. commands in the Help buffer using the `?' speed command."
  755. :group 'org-structure
  756. :type '(repeat :value ("k" . ignore)
  757. (choice :value ("k" . ignore)
  758. (list :tag "Descriptive Headline" (string :tag "Headline"))
  759. (cons :tag "Letter and Command"
  760. (string :tag "Command letter")
  761. (choice
  762. (function)
  763. (sexp))))))
  764. (defgroup org-cycle nil
  765. "Options concerning visibility cycling in Org-mode."
  766. :tag "Org Cycle"
  767. :group 'org-structure)
  768. (defcustom org-cycle-skip-children-state-if-no-children t
  769. "Non-nil means skip CHILDREN state in entries that don't have any."
  770. :group 'org-cycle
  771. :type 'boolean)
  772. (defcustom org-cycle-max-level nil
  773. "Maximum level which should still be subject to visibility cycling.
  774. Levels higher than this will, for cycling, be treated as text, not a headline.
  775. When `org-odd-levels-only' is set, a value of N in this variable actually
  776. means 2N-1 stars as the limiting headline.
  777. When nil, cycle all levels.
  778. Note that the limiting level of cycling is also influenced by
  779. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  780. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  781. than its value."
  782. :group 'org-cycle
  783. :type '(choice
  784. (const :tag "No limit" nil)
  785. (integer :tag "Maximum level")))
  786. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  787. "Names of drawers. Drawers are not opened by cycling on the headline above.
  788. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  789. this:
  790. :DRAWERNAME:
  791. .....
  792. :END:
  793. The drawer \"PROPERTIES\" is special for capturing properties through
  794. the property API.
  795. Drawers can be defined on the per-file basis with a line like:
  796. #+DRAWERS: HIDDEN STATE PROPERTIES"
  797. :group 'org-structure
  798. :group 'org-cycle
  799. :type '(repeat (string :tag "Drawer Name")))
  800. (defcustom org-hide-block-startup nil
  801. "Non-nil means entering Org-mode will fold all blocks.
  802. This can also be set in on a per-file basis with
  803. #+STARTUP: hideblocks
  804. #+STARTUP: showblocks"
  805. :group 'org-startup
  806. :group 'org-cycle
  807. :type 'boolean)
  808. (defcustom org-cycle-global-at-bob nil
  809. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  810. This makes it possible to do global cycling without having to use S-TAB or
  811. \\[universal-argument] TAB. For this special case to work, the first line
  812. of the buffer must not be a headline -- it may be empty or some other text.
  813. When used in this way, `org-cycle-hook' is disabled temporarily to make
  814. sure the cursor stays at the beginning of the buffer. When this option is
  815. nil, don't do anything special at the beginning of the buffer."
  816. :group 'org-cycle
  817. :type 'boolean)
  818. (defcustom org-cycle-level-after-item/entry-creation t
  819. "Non-nil means cycle entry level or item indentation in new empty entries.
  820. When the cursor is at the end of an empty headline, i.e with only stars
  821. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  822. and then all possible ancestor states, before returning to the original state.
  823. This makes data entry extremely fast: M-RET to create a new headline,
  824. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  825. When the cursor is at the end of an empty plain list item, one TAB will
  826. make it a subitem, two or more tabs will back up to make this an item
  827. higher up in the item hierarchy."
  828. :group 'org-cycle
  829. :type 'boolean)
  830. (defcustom org-cycle-emulate-tab t
  831. "Where should `org-cycle' emulate TAB.
  832. nil Never
  833. white Only in completely white lines
  834. whitestart Only at the beginning of lines, before the first non-white char
  835. t Everywhere except in headlines
  836. exc-hl-bol Everywhere except at the start of a headline
  837. If TAB is used in a place where it does not emulate TAB, the current subtree
  838. visibility is cycled."
  839. :group 'org-cycle
  840. :type '(choice (const :tag "Never" nil)
  841. (const :tag "Only in completely white lines" white)
  842. (const :tag "Before first char in a line" whitestart)
  843. (const :tag "Everywhere except in headlines" t)
  844. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  845. ))
  846. (defcustom org-cycle-separator-lines 2
  847. "Number of empty lines needed to keep an empty line between collapsed trees.
  848. If you leave an empty line between the end of a subtree and the following
  849. headline, this empty line is hidden when the subtree is folded.
  850. Org-mode will leave (exactly) one empty line visible if the number of
  851. empty lines is equal or larger to the number given in this variable.
  852. So the default 2 means at least 2 empty lines after the end of a subtree
  853. are needed to produce free space between a collapsed subtree and the
  854. following headline.
  855. If the number is negative, and the number of empty lines is at least -N,
  856. all empty lines are shown.
  857. Special case: when 0, never leave empty lines in collapsed view."
  858. :group 'org-cycle
  859. :type 'integer)
  860. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  861. (defcustom org-pre-cycle-hook nil
  862. "Hook that is run before visibility cycling is happening.
  863. The function(s) in this hook must accept a single argument which indicates
  864. the new state that will be set right after running this hook. The
  865. argument is a symbol. Before a global state change, it can have the values
  866. `overview', `content', or `all'. Before a local state change, it can have
  867. the values `folded', `children', or `subtree'."
  868. :group 'org-cycle
  869. :type 'hook)
  870. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  871. org-cycle-hide-drawers
  872. org-cycle-show-empty-lines
  873. org-optimize-window-after-visibility-change)
  874. "Hook that is run after `org-cycle' has changed the buffer visibility.
  875. The function(s) in this hook must accept a single argument which indicates
  876. the new state that was set by the most recent `org-cycle' command. The
  877. argument is a symbol. After a global state change, it can have the values
  878. `overview', `contents', or `all'. After a local state change, it can have
  879. the values `folded', `children', or `subtree'."
  880. :group 'org-cycle
  881. :type 'hook)
  882. (defgroup org-edit-structure nil
  883. "Options concerning structure editing in Org-mode."
  884. :tag "Org Edit Structure"
  885. :group 'org-structure)
  886. (defcustom org-odd-levels-only nil
  887. "Non-nil means skip even levels and only use odd levels for the outline.
  888. This has the effect that two stars are being added/taken away in
  889. promotion/demotion commands. It also influences how levels are
  890. handled by the exporters.
  891. Changing it requires restart of `font-lock-mode' to become effective
  892. for fontification also in regions already fontified.
  893. You may also set this on a per-file basis by adding one of the following
  894. lines to the buffer:
  895. #+STARTUP: odd
  896. #+STARTUP: oddeven"
  897. :group 'org-edit-structure
  898. :group 'org-appearance
  899. :type 'boolean)
  900. (defcustom org-adapt-indentation t
  901. "Non-nil means adapt indentation to outline node level.
  902. When this variable is set, Org assumes that you write outlines by
  903. indenting text in each node to align with the headline (after the stars).
  904. The following issues are influenced by this variable:
  905. - When this is set and the *entire* text in an entry is indented, the
  906. indentation is increased by one space in a demotion command, and
  907. decreased by one in a promotion command. If any line in the entry
  908. body starts with text at column 0, indentation is not changed at all.
  909. - Property drawers and planning information is inserted indented when
  910. this variable s set. When nil, they will not be indented.
  911. - TAB indents a line relative to context. The lines below a headline
  912. will be indented when this variable is set.
  913. Note that this is all about true indentation, by adding and removing
  914. space characters. See also `org-indent.el' which does level-dependent
  915. indentation in a virtual way, i.e. at display time in Emacs."
  916. :group 'org-edit-structure
  917. :type 'boolean)
  918. (defcustom org-special-ctrl-a/e nil
  919. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  920. When t, `C-a' will bring back the cursor to the beginning of the
  921. headline text, i.e. after the stars and after a possible TODO
  922. keyword. In an item, this will be the position after bullet and
  923. check-box, if any. When the cursor is already at that position,
  924. another `C-a' will bring it to the beginning of the line.
  925. `C-e' will jump to the end of the headline, ignoring the presence
  926. of tags in the headline. A second `C-e' will then jump to the
  927. true end of the line, after any tags. This also means that, when
  928. this variable is non-nil, `C-e' also will never jump beyond the
  929. end of the heading of a folded section, i.e. not after the
  930. ellipses.
  931. When set to the symbol `reversed', the first `C-a' or `C-e' works
  932. normally, going to the true line boundary first. Only a directly
  933. following, identical keypress will bring the cursor to the
  934. special positions.
  935. This may also be a cons cell where the behavior for `C-a' and
  936. `C-e' is set separately."
  937. :group 'org-edit-structure
  938. :type '(choice
  939. (const :tag "off" nil)
  940. (const :tag "on: after stars/bullet and before tags first" t)
  941. (const :tag "reversed: true line boundary first" reversed)
  942. (cons :tag "Set C-a and C-e separately"
  943. (choice :tag "Special C-a"
  944. (const :tag "off" nil)
  945. (const :tag "on: after stars/bullet first" t)
  946. (const :tag "reversed: before stars/bullet first" reversed))
  947. (choice :tag "Special C-e"
  948. (const :tag "off" nil)
  949. (const :tag "on: before tags first" t)
  950. (const :tag "reversed: after tags first" reversed)))))
  951. (if (fboundp 'defvaralias)
  952. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  953. (defcustom org-special-ctrl-k nil
  954. "Non-nil means `C-k' will behave specially in headlines.
  955. When nil, `C-k' will call the default `kill-line' command.
  956. When t, the following will happen while the cursor is in the headline:
  957. - When the cursor is at the beginning of a headline, kill the entire
  958. line and possible the folded subtree below the line.
  959. - When in the middle of the headline text, kill the headline up to the tags.
  960. - When after the headline text, kill the tags."
  961. :group 'org-edit-structure
  962. :type 'boolean)
  963. (defcustom org-ctrl-k-protect-subtree nil
  964. "Non-nil means, do not delete a hidden subtree with C-k.
  965. When set to the symbol `error', simply throw an error when C-k is
  966. used to kill (part-of) a headline that has hidden text behind it.
  967. Any other non-nil value will result in a query to the user, if it is
  968. OK to kill that hidden subtree. When nil, kill without remorse."
  969. :group 'org-edit-structure
  970. :version "24.1"
  971. :type '(choice
  972. (const :tag "Do not protect hidden subtrees" nil)
  973. (const :tag "Protect hidden subtrees with a security query" t)
  974. (const :tag "Never kill a hidden subtree with C-k" error)))
  975. (defcustom org-catch-invisible-edits nil
  976. "Check if in invisible region before inserting or deleting a character.
  977. Valid values are:
  978. nil Do not check, so just do invisible edits.
  979. error Throw an error and do nothing.
  980. show Make point visible, and do the requested edit.
  981. show-and-error Make point visible, then throw an error and abort the edit.
  982. smart Make point visible, and do insertion/deletion if it is
  983. adjacent to visible text and the change feels predictable.
  984. Never delete a previously invisible character or add in the
  985. middle or right after an invisible region. Basically, this
  986. allows insertion and backward-delete right before ellipses.
  987. FIXME: maybe in this case we should not even show?"
  988. :group 'org-edit-structure
  989. :version "24.1"
  990. :type '(choice
  991. (const :tag "Do not check" nil)
  992. (const :tag "Throw error when trying to edit" error)
  993. (const :tag "Unhide, but do not do the edit" show-and-error)
  994. (const :tag "Show invisible part and do the edit" show)
  995. (const :tag "Be smart and do the right thing" smart)))
  996. (defcustom org-yank-folded-subtrees t
  997. "Non-nil means when yanking subtrees, fold them.
  998. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  999. it starts with a heading and all other headings in it are either children
  1000. or siblings, then fold all the subtrees. However, do this only if no
  1001. text after the yank would be swallowed into a folded tree by this action."
  1002. :group 'org-edit-structure
  1003. :type 'boolean)
  1004. (defcustom org-yank-adjusted-subtrees nil
  1005. "Non-nil means when yanking subtrees, adjust the level.
  1006. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1007. this function for details."
  1008. :group 'org-edit-structure
  1009. :type 'boolean)
  1010. (defcustom org-M-RET-may-split-line '((default . t))
  1011. "Non-nil means M-RET will split the line at the cursor position.
  1012. When nil, it will go to the end of the line before making a
  1013. new line.
  1014. You may also set this option in a different way for different
  1015. contexts. Valid contexts are:
  1016. headline when creating a new headline
  1017. item when creating a new item
  1018. table in a table field
  1019. default the value to be used for all contexts not explicitly
  1020. customized"
  1021. :group 'org-structure
  1022. :group 'org-table
  1023. :type '(choice
  1024. (const :tag "Always" t)
  1025. (const :tag "Never" nil)
  1026. (repeat :greedy t :tag "Individual contexts"
  1027. (cons
  1028. (choice :tag "Context"
  1029. (const headline)
  1030. (const item)
  1031. (const table)
  1032. (const default))
  1033. (boolean)))))
  1034. (defcustom org-insert-heading-respect-content nil
  1035. "Non-nil means insert new headings after the current subtree.
  1036. When nil, the new heading is created directly after the current line.
  1037. The commands \\[org-insert-heading-respect-content] and
  1038. \\[org-insert-todo-heading-respect-content] turn this variable on
  1039. for the duration of the command."
  1040. :group 'org-structure
  1041. :type 'boolean)
  1042. (defcustom org-blank-before-new-entry '((heading . auto)
  1043. (plain-list-item . auto))
  1044. "Should `org-insert-heading' leave a blank line before new heading/item?
  1045. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1046. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1047. which case Org will look at the surrounding headings/items and try to
  1048. make an intelligent decision whether to insert a blank line or not.
  1049. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1050. set, the setting here is ignored and no empty line is inserted, to avoid
  1051. breaking the list structure."
  1052. :group 'org-edit-structure
  1053. :type '(list
  1054. (cons (const heading)
  1055. (choice (const :tag "Never" nil)
  1056. (const :tag "Always" t)
  1057. (const :tag "Auto" auto)))
  1058. (cons (const plain-list-item)
  1059. (choice (const :tag "Never" nil)
  1060. (const :tag "Always" t)
  1061. (const :tag "Auto" auto)))))
  1062. (defcustom org-insert-heading-hook nil
  1063. "Hook being run after inserting a new heading."
  1064. :group 'org-edit-structure
  1065. :type 'hook)
  1066. (defcustom org-enable-fixed-width-editor t
  1067. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1068. This currently only means they are never auto-wrapped.
  1069. When nil, such lines will be treated like ordinary lines.
  1070. See also the QUOTE keyword."
  1071. :group 'org-edit-structure
  1072. :type 'boolean)
  1073. (defcustom org-goto-auto-isearch t
  1074. "Non-nil means typing characters in `org-goto' starts incremental search.
  1075. When nil, you can use these keybindings to navigate the buffer:
  1076. q Quit the org-goto interface
  1077. n Go to the next visible heading
  1078. p Go to the previous visible heading
  1079. f Go one heading forward on same level
  1080. b Go one heading backward on same level
  1081. u Go one heading up"
  1082. :group 'org-edit-structure
  1083. :type 'boolean)
  1084. (defgroup org-sparse-trees nil
  1085. "Options concerning sparse trees in Org-mode."
  1086. :tag "Org Sparse Trees"
  1087. :group 'org-structure)
  1088. (defcustom org-highlight-sparse-tree-matches t
  1089. "Non-nil means highlight all matches that define a sparse tree.
  1090. The highlights will automatically disappear the next time the buffer is
  1091. changed by an edit command."
  1092. :group 'org-sparse-trees
  1093. :type 'boolean)
  1094. (defcustom org-remove-highlights-with-change t
  1095. "Non-nil means any change to the buffer will remove temporary highlights.
  1096. Such highlights are created by `org-occur' and `org-clock-display'.
  1097. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1098. The highlights created by `org-preview-latex-fragment' always need
  1099. `C-c C-c' to be removed."
  1100. :group 'org-sparse-trees
  1101. :group 'org-time
  1102. :type 'boolean)
  1103. (defcustom org-occur-hook '(org-first-headline-recenter)
  1104. "Hook that is run after `org-occur' has constructed a sparse tree.
  1105. This can be used to recenter the window to show as much of the structure
  1106. as possible."
  1107. :group 'org-sparse-trees
  1108. :type 'hook)
  1109. (defgroup org-imenu-and-speedbar nil
  1110. "Options concerning imenu and speedbar in Org-mode."
  1111. :tag "Org Imenu and Speedbar"
  1112. :group 'org-structure)
  1113. (defcustom org-imenu-depth 2
  1114. "The maximum level for Imenu access to Org-mode headlines.
  1115. This also applied for speedbar access."
  1116. :group 'org-imenu-and-speedbar
  1117. :type 'integer)
  1118. (defgroup org-table nil
  1119. "Options concerning tables in Org-mode."
  1120. :tag "Org Table"
  1121. :group 'org)
  1122. (defcustom org-enable-table-editor 'optimized
  1123. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1124. When nil, such lines will be treated like ordinary lines.
  1125. When equal to the symbol `optimized', the table editor will be optimized to
  1126. do the following:
  1127. - Automatic overwrite mode in front of whitespace in table fields.
  1128. This makes the structure of the table stay in tact as long as the edited
  1129. field does not exceed the column width.
  1130. - Minimize the number of realigns. Normally, the table is aligned each time
  1131. TAB or RET are pressed to move to another field. With optimization this
  1132. happens only if changes to a field might have changed the column width.
  1133. Optimization requires replacing the functions `self-insert-command',
  1134. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1135. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1136. very good at guessing when a re-align will be necessary, but you can always
  1137. force one with \\[org-ctrl-c-ctrl-c].
  1138. If you would like to use the optimized version in Org-mode, but the
  1139. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1140. This variable can be used to turn on and off the table editor during a session,
  1141. but in order to toggle optimization, a restart is required.
  1142. See also the variable `org-table-auto-blank-field'."
  1143. :group 'org-table
  1144. :type '(choice
  1145. (const :tag "off" nil)
  1146. (const :tag "on" t)
  1147. (const :tag "on, optimized" optimized)))
  1148. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1149. (version<= emacs-version "24.1"))
  1150. "Non-nil means cluster self-insert commands for undo when possible.
  1151. If this is set, then, like in the Emacs command loop, 20 consecutive
  1152. characters will be undone together.
  1153. This is configurable, because there is some impact on typing performance."
  1154. :group 'org-table
  1155. :type 'boolean)
  1156. (defcustom org-table-tab-recognizes-table.el t
  1157. "Non-nil means TAB will automatically notice a table.el table.
  1158. When it sees such a table, it moves point into it and - if necessary -
  1159. calls `table-recognize-table'."
  1160. :group 'org-table-editing
  1161. :type 'boolean)
  1162. (defgroup org-link nil
  1163. "Options concerning links in Org-mode."
  1164. :tag "Org Link"
  1165. :group 'org)
  1166. (defvar org-link-abbrev-alist-local nil
  1167. "Buffer-local version of `org-link-abbrev-alist', which see.
  1168. The value of this is taken from the #+LINK lines.")
  1169. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1170. (defcustom org-link-abbrev-alist nil
  1171. "Alist of link abbreviations.
  1172. The car of each element is a string, to be replaced at the start of a link.
  1173. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1174. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1175. [[linkkey:tag][description]]
  1176. The 'linkkey' must be a word word, starting with a letter, followed
  1177. by letters, numbers, '-' or '_'.
  1178. If REPLACE is a string, the tag will simply be appended to create the link.
  1179. If the string contains \"%s\", the tag will be inserted there. If the string
  1180. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1181. at that point (see the function `url-hexify-string'). If the string contains
  1182. the specifier \"%(my-function)\", then the custom function `my-function' will
  1183. be invoked: this function takes the tag as its only argument and must return
  1184. a string.
  1185. REPLACE may also be a function that will be called with the tag as the
  1186. only argument to create the link, which should be returned as a string.
  1187. See the manual for examples."
  1188. :group 'org-link
  1189. :type '(repeat
  1190. (cons
  1191. (string :tag "Protocol")
  1192. (choice
  1193. (string :tag "Format")
  1194. (function)))))
  1195. (defcustom org-descriptive-links t
  1196. "Non-nil means Org will display descriptive links.
  1197. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1198. \"Org Website\", hiding the link itself and just displaying its
  1199. description. When set to `nil', Org will display the full links
  1200. literally.
  1201. You can interactively set the value of this variable by calling
  1202. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1203. :group 'org-link
  1204. :type 'boolean)
  1205. (defcustom org-link-file-path-type 'adaptive
  1206. "How the path name in file links should be stored.
  1207. Valid values are:
  1208. relative Relative to the current directory, i.e. the directory of the file
  1209. into which the link is being inserted.
  1210. absolute Absolute path, if possible with ~ for home directory.
  1211. noabbrev Absolute path, no abbreviation of home directory.
  1212. adaptive Use relative path for files in the current directory and sub-
  1213. directories of it. For other files, use an absolute path."
  1214. :group 'org-link
  1215. :type '(choice
  1216. (const relative)
  1217. (const absolute)
  1218. (const noabbrev)
  1219. (const adaptive)))
  1220. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1221. "Types of links that should be activated in Org-mode files.
  1222. This is a list of symbols, each leading to the activation of a certain link
  1223. type. In principle, it does not hurt to turn on most link types - there may
  1224. be a small gain when turning off unused link types. The types are:
  1225. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1226. angle Links in angular brackets that may contain whitespace like
  1227. <bbdb:Carsten Dominik>.
  1228. plain Plain links in normal text, no whitespace, like http://google.com.
  1229. radio Text that is matched by a radio target, see manual for details.
  1230. tag Tag settings in a headline (link to tag search).
  1231. date Time stamps (link to calendar).
  1232. footnote Footnote labels.
  1233. Changing this variable requires a restart of Emacs to become effective."
  1234. :group 'org-link
  1235. :type '(set :greedy t
  1236. (const :tag "Double bracket links" bracket)
  1237. (const :tag "Angular bracket links" angle)
  1238. (const :tag "Plain text links" plain)
  1239. (const :tag "Radio target matches" radio)
  1240. (const :tag "Tags" tag)
  1241. (const :tag "Timestamps" date)
  1242. (const :tag "Footnotes" footnote)))
  1243. (defcustom org-make-link-description-function nil
  1244. "Function to use for generating link descriptions from links.
  1245. When nil, the link location will be used. This function must take
  1246. two parameters: the first one is the link, the second one is the
  1247. description generated by `org-insert-link'. The function should
  1248. return the description to use."
  1249. :group 'org-link
  1250. :type 'function)
  1251. (defgroup org-link-store nil
  1252. "Options concerning storing links in Org-mode."
  1253. :tag "Org Store Link"
  1254. :group 'org-link)
  1255. (defcustom org-url-hexify-p t
  1256. "When non-nil, hexify URL when creating a link."
  1257. :type 'boolean
  1258. :version "24.3"
  1259. :group 'org-link-store)
  1260. (defcustom org-email-link-description-format "Email %c: %.30s"
  1261. "Format of the description part of a link to an email or usenet message.
  1262. The following %-escapes will be replaced by corresponding information:
  1263. %F full \"From\" field
  1264. %f name, taken from \"From\" field, address if no name
  1265. %T full \"To\" field
  1266. %t first name in \"To\" field, address if no name
  1267. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1268. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1269. %s subject
  1270. %d date
  1271. %m message-id.
  1272. You may use normal field width specification between the % and the letter.
  1273. This is for example useful to limit the length of the subject.
  1274. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1275. :group 'org-link-store
  1276. :type 'string)
  1277. (defcustom org-from-is-user-regexp
  1278. (let (r1 r2)
  1279. (when (and user-mail-address (not (string= user-mail-address "")))
  1280. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1281. (when (and user-full-name (not (string= user-full-name "")))
  1282. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1283. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1284. "Regexp matched against the \"From:\" header of an email or usenet message.
  1285. It should match if the message is from the user him/herself."
  1286. :group 'org-link-store
  1287. :type 'regexp)
  1288. (defcustom org-context-in-file-links t
  1289. "Non-nil means file links from `org-store-link' contain context.
  1290. A search string will be added to the file name with :: as separator and
  1291. used to find the context when the link is activated by the command
  1292. `org-open-at-point'. When this option is t, the entire active region
  1293. will be placed in the search string of the file link. If set to a
  1294. positive integer, only the first n lines of context will be stored.
  1295. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1296. negates this setting for the duration of the command."
  1297. :group 'org-link-store
  1298. :type '(choice boolean integer))
  1299. (defcustom org-keep-stored-link-after-insertion nil
  1300. "Non-nil means keep link in list for entire session.
  1301. The command `org-store-link' adds a link pointing to the current
  1302. location to an internal list. These links accumulate during a session.
  1303. The command `org-insert-link' can be used to insert links into any
  1304. Org-mode file (offering completion for all stored links). When this
  1305. option is nil, every link which has been inserted once using \\[org-insert-link]
  1306. will be removed from the list, to make completing the unused links
  1307. more efficient."
  1308. :group 'org-link-store
  1309. :type 'boolean)
  1310. (defgroup org-link-follow nil
  1311. "Options concerning following links in Org-mode."
  1312. :tag "Org Follow Link"
  1313. :group 'org-link)
  1314. (defcustom org-link-translation-function nil
  1315. "Function to translate links with different syntax to Org syntax.
  1316. This can be used to translate links created for example by the Planner
  1317. or emacs-wiki packages to Org syntax.
  1318. The function must accept two parameters, a TYPE containing the link
  1319. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1320. which is everything after the link protocol. It should return a cons
  1321. with possibly modified values of type and path.
  1322. Org contains a function for this, so if you set this variable to
  1323. `org-translate-link-from-planner', you should be able follow many
  1324. links created by planner."
  1325. :group 'org-link-follow
  1326. :type 'function)
  1327. (defcustom org-follow-link-hook nil
  1328. "Hook that is run after a link has been followed."
  1329. :group 'org-link-follow
  1330. :type 'hook)
  1331. (defcustom org-tab-follows-link nil
  1332. "Non-nil means on links TAB will follow the link.
  1333. Needs to be set before org.el is loaded.
  1334. This really should not be used, it does not make sense, and the
  1335. implementation is bad."
  1336. :group 'org-link-follow
  1337. :type 'boolean)
  1338. (defcustom org-return-follows-link nil
  1339. "Non-nil means on links RET will follow the link."
  1340. :group 'org-link-follow
  1341. :type 'boolean)
  1342. (defcustom org-mouse-1-follows-link
  1343. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1344. "Non-nil means mouse-1 on a link will follow the link.
  1345. A longer mouse click will still set point. Does not work on XEmacs.
  1346. Needs to be set before org.el is loaded."
  1347. :group 'org-link-follow
  1348. :type 'boolean)
  1349. (defcustom org-mark-ring-length 4
  1350. "Number of different positions to be recorded in the ring.
  1351. Changing this requires a restart of Emacs to work correctly."
  1352. :group 'org-link-follow
  1353. :type 'integer)
  1354. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1355. "Non-nil means internal links in Org files must exactly match a headline.
  1356. When nil, the link search tries to match a phrase with all words
  1357. in the search text."
  1358. :group 'org-link-follow
  1359. :version "24.1"
  1360. :type '(choice
  1361. (const :tag "Use fuzzy text search" nil)
  1362. (const :tag "Match only exact headline" t)
  1363. (const :tag "Match exact headline or query to create it"
  1364. query-to-create)))
  1365. (defcustom org-link-frame-setup
  1366. '((vm . vm-visit-folder-other-frame)
  1367. (vm-imap . vm-visit-imap-folder-other-frame)
  1368. (gnus . org-gnus-no-new-news)
  1369. (file . find-file-other-window)
  1370. (wl . wl-other-frame))
  1371. "Setup the frame configuration for following links.
  1372. When following a link with Emacs, it may often be useful to display
  1373. this link in another window or frame. This variable can be used to
  1374. set this up for the different types of links.
  1375. For VM, use any of
  1376. `vm-visit-folder'
  1377. `vm-visit-folder-other-window'
  1378. `vm-visit-folder-other-frame'
  1379. For Gnus, use any of
  1380. `gnus'
  1381. `gnus-other-frame'
  1382. `org-gnus-no-new-news'
  1383. For FILE, use any of
  1384. `find-file'
  1385. `find-file-other-window'
  1386. `find-file-other-frame'
  1387. For Wanderlust use any of
  1388. `wl'
  1389. `wl-other-frame'
  1390. For the calendar, use the variable `calendar-setup'.
  1391. For BBDB, it is currently only possible to display the matches in
  1392. another window."
  1393. :group 'org-link-follow
  1394. :type '(list
  1395. (cons (const vm)
  1396. (choice
  1397. (const vm-visit-folder)
  1398. (const vm-visit-folder-other-window)
  1399. (const vm-visit-folder-other-frame)))
  1400. (cons (const gnus)
  1401. (choice
  1402. (const gnus)
  1403. (const gnus-other-frame)
  1404. (const org-gnus-no-new-news)))
  1405. (cons (const file)
  1406. (choice
  1407. (const find-file)
  1408. (const find-file-other-window)
  1409. (const find-file-other-frame)))
  1410. (cons (const wl)
  1411. (choice
  1412. (const wl)
  1413. (const wl-other-frame)))))
  1414. (defcustom org-display-internal-link-with-indirect-buffer nil
  1415. "Non-nil means use indirect buffer to display infile links.
  1416. Activating internal links (from one location in a file to another location
  1417. in the same file) normally just jumps to the location. When the link is
  1418. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1419. is displayed in
  1420. another window. When this option is set, the other window actually displays
  1421. an indirect buffer clone of the current buffer, to avoid any visibility
  1422. changes to the current buffer."
  1423. :group 'org-link-follow
  1424. :type 'boolean)
  1425. (defcustom org-open-non-existing-files nil
  1426. "Non-nil means `org-open-file' will open non-existing files.
  1427. When nil, an error will be generated.
  1428. This variable applies only to external applications because they
  1429. might choke on non-existing files. If the link is to a file that
  1430. will be opened in Emacs, the variable is ignored."
  1431. :group 'org-link-follow
  1432. :type 'boolean)
  1433. (defcustom org-open-directory-means-index-dot-org nil
  1434. "Non-nil means a link to a directory really means to index.org.
  1435. When nil, following a directory link will run dired or open a finder/explorer
  1436. window on that directory."
  1437. :group 'org-link-follow
  1438. :type 'boolean)
  1439. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1440. "Function and arguments to call for following mailto links.
  1441. This is a list with the first element being a Lisp function, and the
  1442. remaining elements being arguments to the function. In string arguments,
  1443. %a will be replaced by the address, and %s will be replaced by the subject
  1444. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1445. :group 'org-link-follow
  1446. :type '(choice
  1447. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1448. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1449. (const :tag "message-mail" (message-mail "%a" "%s"))
  1450. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1451. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1452. "Non-nil means ask for confirmation before executing shell links.
  1453. Shell links can be dangerous: just think about a link
  1454. [[shell:rm -rf ~/*][Google Search]]
  1455. This link would show up in your Org-mode document as \"Google Search\",
  1456. but really it would remove your entire home directory.
  1457. Therefore we advise against setting this variable to nil.
  1458. Just change it to `y-or-n-p' if you want to confirm with a
  1459. single keystroke rather than having to type \"yes\"."
  1460. :group 'org-link-follow
  1461. :type '(choice
  1462. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1463. (const :tag "with y-or-n (faster)" y-or-n-p)
  1464. (const :tag "no confirmation (dangerous)" nil)))
  1465. (put 'org-confirm-shell-link-function
  1466. 'safe-local-variable
  1467. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1468. (defcustom org-confirm-shell-link-not-regexp ""
  1469. "A regexp to skip confirmation for shell links."
  1470. :group 'org-link-follow
  1471. :version "24.1"
  1472. :type 'regexp)
  1473. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1474. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1475. Elisp links can be dangerous: just think about a link
  1476. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1477. This link would show up in your Org-mode document as \"Google Search\",
  1478. but really it would remove your entire home directory.
  1479. Therefore we advise against setting this variable to nil.
  1480. Just change it to `y-or-n-p' if you want to confirm with a
  1481. single keystroke rather than having to type \"yes\"."
  1482. :group 'org-link-follow
  1483. :type '(choice
  1484. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1485. (const :tag "with y-or-n (faster)" y-or-n-p)
  1486. (const :tag "no confirmation (dangerous)" nil)))
  1487. (put 'org-confirm-shell-link-function
  1488. 'safe-local-variable
  1489. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1490. (defcustom org-confirm-elisp-link-not-regexp ""
  1491. "A regexp to skip confirmation for Elisp links."
  1492. :group 'org-link-follow
  1493. :version "24.1"
  1494. :type 'regexp)
  1495. (defconst org-file-apps-defaults-gnu
  1496. '((remote . emacs)
  1497. (system . mailcap)
  1498. (t . mailcap))
  1499. "Default file applications on a UNIX or GNU/Linux system.
  1500. See `org-file-apps'.")
  1501. (defconst org-file-apps-defaults-macosx
  1502. '((remote . emacs)
  1503. (t . "open %s")
  1504. (system . "open %s")
  1505. ("ps.gz" . "gv %s")
  1506. ("eps.gz" . "gv %s")
  1507. ("dvi" . "xdvi %s")
  1508. ("fig" . "xfig %s"))
  1509. "Default file applications on a MacOS X system.
  1510. The system \"open\" is known as a default, but we use X11 applications
  1511. for some files for which the OS does not have a good default.
  1512. See `org-file-apps'.")
  1513. (defconst org-file-apps-defaults-windowsnt
  1514. (list
  1515. '(remote . emacs)
  1516. (cons t
  1517. (list (if (featurep 'xemacs)
  1518. 'mswindows-shell-execute
  1519. 'w32-shell-execute)
  1520. "open" 'file))
  1521. (cons 'system
  1522. (list (if (featurep 'xemacs)
  1523. 'mswindows-shell-execute
  1524. 'w32-shell-execute)
  1525. "open" 'file)))
  1526. "Default file applications on a Windows NT system.
  1527. The system \"open\" is used for most files.
  1528. See `org-file-apps'.")
  1529. (defcustom org-file-apps
  1530. '(
  1531. (auto-mode . emacs)
  1532. ("\\.mm\\'" . default)
  1533. ("\\.x?html?\\'" . default)
  1534. ("\\.pdf\\'" . default)
  1535. )
  1536. "External applications for opening `file:path' items in a document.
  1537. Org-mode uses system defaults for different file types, but
  1538. you can use this variable to set the application for a given file
  1539. extension. The entries in this list are cons cells where the car identifies
  1540. files and the cdr the corresponding command. Possible values for the
  1541. file identifier are
  1542. \"string\" A string as a file identifier can be interpreted in different
  1543. ways, depending on its contents:
  1544. - Alphanumeric characters only:
  1545. Match links with this file extension.
  1546. Example: (\"pdf\" . \"evince %s\")
  1547. to open PDFs with evince.
  1548. - Regular expression: Match links where the
  1549. filename matches the regexp. If you want to
  1550. use groups here, use shy groups.
  1551. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1552. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1553. to open *.html and *.xhtml with firefox.
  1554. - Regular expression which contains (non-shy) groups:
  1555. Match links where the whole link, including \"::\", and
  1556. anything after that, matches the regexp.
  1557. In a custom command string, %1, %2, etc. are replaced with
  1558. the parts of the link that were matched by the groups.
  1559. For backwards compatibility, if a command string is given
  1560. that does not use any of the group matches, this case is
  1561. handled identically to the second one (i.e. match against
  1562. file name only).
  1563. In a custom lisp form, you can access the group matches with
  1564. (match-string n link).
  1565. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1566. to open [[file:document.pdf::5]] with evince at page 5.
  1567. `directory' Matches a directory
  1568. `remote' Matches a remote file, accessible through tramp or efs.
  1569. Remote files most likely should be visited through Emacs
  1570. because external applications cannot handle such paths.
  1571. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1572. so all files Emacs knows how to handle. Using this with
  1573. command `emacs' will open most files in Emacs. Beware that this
  1574. will also open html files inside Emacs, unless you add
  1575. (\"html\" . default) to the list as well.
  1576. t Default for files not matched by any of the other options.
  1577. `system' The system command to open files, like `open' on Windows
  1578. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1579. that will be selected if you call `C-c C-o' with a double
  1580. \\[universal-argument] \\[universal-argument] prefix.
  1581. Possible values for the command are:
  1582. `emacs' The file will be visited by the current Emacs process.
  1583. `default' Use the default application for this file type, which is the
  1584. association for t in the list, most likely in the system-specific
  1585. part.
  1586. This can be used to overrule an unwanted setting in the
  1587. system-specific variable.
  1588. `system' Use the system command for opening files, like \"open\".
  1589. This command is specified by the entry whose car is `system'.
  1590. Most likely, the system-specific version of this variable
  1591. does define this command, but you can overrule/replace it
  1592. here.
  1593. string A command to be executed by a shell; %s will be replaced
  1594. by the path to the file.
  1595. sexp A Lisp form which will be evaluated. The file path will
  1596. be available in the Lisp variable `file'.
  1597. For more examples, see the system specific constants
  1598. `org-file-apps-defaults-macosx'
  1599. `org-file-apps-defaults-windowsnt'
  1600. `org-file-apps-defaults-gnu'."
  1601. :group 'org-link-follow
  1602. :type '(repeat
  1603. (cons (choice :value ""
  1604. (string :tag "Extension")
  1605. (const :tag "System command to open files" system)
  1606. (const :tag "Default for unrecognized files" t)
  1607. (const :tag "Remote file" remote)
  1608. (const :tag "Links to a directory" directory)
  1609. (const :tag "Any files that have Emacs modes"
  1610. auto-mode))
  1611. (choice :value ""
  1612. (const :tag "Visit with Emacs" emacs)
  1613. (const :tag "Use default" default)
  1614. (const :tag "Use the system command" system)
  1615. (string :tag "Command")
  1616. (sexp :tag "Lisp form")))))
  1617. (defcustom org-doi-server-url "http://dx.doi.org/"
  1618. "The URL of the DOI server."
  1619. :type 'string
  1620. :version "24.3"
  1621. :group 'org-link-follow)
  1622. (defgroup org-refile nil
  1623. "Options concerning refiling entries in Org-mode."
  1624. :tag "Org Refile"
  1625. :group 'org)
  1626. (defcustom org-directory "~/org"
  1627. "Directory with org files.
  1628. This is just a default location to look for Org files. There is no need
  1629. at all to put your files into this directory. It is only used in the
  1630. following situations:
  1631. 1. When a capture template specifies a target file that is not an
  1632. absolute path. The path will then be interpreted relative to
  1633. `org-directory'
  1634. 2. When a capture note is filed away in an interactive way (when exiting the
  1635. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1636. with `org-directory' as the default path."
  1637. :group 'org-refile
  1638. :group 'org-remember
  1639. :group 'org-capture
  1640. :type 'directory)
  1641. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1642. "Default target for storing notes.
  1643. Used as a fall back file for org-remember.el and org-capture.el, for
  1644. templates that do not specify a target file."
  1645. :group 'org-refile
  1646. :group 'org-remember
  1647. :group 'org-capture
  1648. :type '(choice
  1649. (const :tag "Default from remember-data-file" nil)
  1650. file))
  1651. (defcustom org-goto-interface 'outline
  1652. "The default interface to be used for `org-goto'.
  1653. Allowed values are:
  1654. outline The interface shows an outline of the relevant file
  1655. and the correct heading is found by moving through
  1656. the outline or by searching with incremental search.
  1657. outline-path-completion Headlines in the current buffer are offered via
  1658. completion. This is the interface also used by
  1659. the refile command."
  1660. :group 'org-refile
  1661. :type '(choice
  1662. (const :tag "Outline" outline)
  1663. (const :tag "Outline-path-completion" outline-path-completion)))
  1664. (defcustom org-goto-max-level 5
  1665. "Maximum target level when running `org-goto' with refile interface."
  1666. :group 'org-refile
  1667. :type 'integer)
  1668. (defcustom org-reverse-note-order nil
  1669. "Non-nil means store new notes at the beginning of a file or entry.
  1670. When nil, new notes will be filed to the end of a file or entry.
  1671. This can also be a list with cons cells of regular expressions that
  1672. are matched against file names, and values."
  1673. :group 'org-remember
  1674. :group 'org-capture
  1675. :group 'org-refile
  1676. :type '(choice
  1677. (const :tag "Reverse always" t)
  1678. (const :tag "Reverse never" nil)
  1679. (repeat :tag "By file name regexp"
  1680. (cons regexp boolean))))
  1681. (defcustom org-log-refile nil
  1682. "Information to record when a task is refiled.
  1683. Possible values are:
  1684. nil Don't add anything
  1685. time Add a time stamp to the task
  1686. note Prompt for a note and add it with template `org-log-note-headings'
  1687. This option can also be set with on a per-file-basis with
  1688. #+STARTUP: nologrefile
  1689. #+STARTUP: logrefile
  1690. #+STARTUP: lognoterefile
  1691. You can have local logging settings for a subtree by setting the LOGGING
  1692. property to one or more of these keywords.
  1693. When bulk-refiling from the agenda, the value `note' is forbidden and
  1694. will temporarily be changed to `time'."
  1695. :group 'org-refile
  1696. :group 'org-progress
  1697. :version "24.1"
  1698. :type '(choice
  1699. (const :tag "No logging" nil)
  1700. (const :tag "Record timestamp" time)
  1701. (const :tag "Record timestamp with note." note)))
  1702. (defcustom org-refile-targets nil
  1703. "Targets for refiling entries with \\[org-refile].
  1704. This is a list of cons cells. Each cell contains:
  1705. - a specification of the files to be considered, either a list of files,
  1706. or a symbol whose function or variable value will be used to retrieve
  1707. a file name or a list of file names. If you use `org-agenda-files' for
  1708. that, all agenda files will be scanned for targets. Nil means consider
  1709. headings in the current buffer.
  1710. - A specification of how to find candidate refile targets. This may be
  1711. any of:
  1712. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1713. This tag has to be present in all target headlines, inheritance will
  1714. not be considered.
  1715. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1716. todo keyword.
  1717. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1718. headlines that are refiling targets.
  1719. - a cons cell (:level . N). Any headline of level N is considered a target.
  1720. Note that, when `org-odd-levels-only' is set, level corresponds to
  1721. order in hierarchy, not to the number of stars.
  1722. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1723. Note that, when `org-odd-levels-only' is set, level corresponds to
  1724. order in hierarchy, not to the number of stars.
  1725. Each element of this list generates a set of possible targets.
  1726. The union of these sets is presented (with completion) to
  1727. the user by `org-refile'.
  1728. You can set the variable `org-refile-target-verify-function' to a function
  1729. to verify each headline found by the simple criteria above.
  1730. When this variable is nil, all top-level headlines in the current buffer
  1731. are used, equivalent to the value `((nil . (:level . 1))'."
  1732. :group 'org-refile
  1733. :type '(repeat
  1734. (cons
  1735. (choice :value org-agenda-files
  1736. (const :tag "All agenda files" org-agenda-files)
  1737. (const :tag "Current buffer" nil)
  1738. (function) (variable) (file))
  1739. (choice :tag "Identify target headline by"
  1740. (cons :tag "Specific tag" (const :value :tag) (string))
  1741. (cons :tag "TODO keyword" (const :value :todo) (string))
  1742. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1743. (cons :tag "Level number" (const :value :level) (integer))
  1744. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1745. (defcustom org-refile-target-verify-function nil
  1746. "Function to verify if the headline at point should be a refile target.
  1747. The function will be called without arguments, with point at the
  1748. beginning of the headline. It should return t and leave point
  1749. where it is if the headline is a valid target for refiling.
  1750. If the target should not be selected, the function must return nil.
  1751. In addition to this, it may move point to a place from where the search
  1752. should be continued. For example, the function may decide that the entire
  1753. subtree of the current entry should be excluded and move point to the end
  1754. of the subtree."
  1755. :group 'org-refile
  1756. :type 'function)
  1757. (defcustom org-refile-use-cache nil
  1758. "Non-nil means cache refile targets to speed up the process.
  1759. The cache for a particular file will be updated automatically when
  1760. the buffer has been killed, or when any of the marker used for flagging
  1761. refile targets no longer points at a live buffer.
  1762. If you have added new entries to a buffer that might themselves be targets,
  1763. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1764. find that easier, `C-u C-u C-u C-c C-w'."
  1765. :group 'org-refile
  1766. :version "24.1"
  1767. :type 'boolean)
  1768. (defcustom org-refile-use-outline-path nil
  1769. "Non-nil means provide refile targets as paths.
  1770. So a level 3 headline will be available as level1/level2/level3.
  1771. When the value is `file', also include the file name (without directory)
  1772. into the path. In this case, you can also stop the completion after
  1773. the file name, to get entries inserted as top level in the file.
  1774. When `full-file-path', include the full file path."
  1775. :group 'org-refile
  1776. :type '(choice
  1777. (const :tag "Not" nil)
  1778. (const :tag "Yes" t)
  1779. (const :tag "Start with file name" file)
  1780. (const :tag "Start with full file path" full-file-path)))
  1781. (defcustom org-outline-path-complete-in-steps t
  1782. "Non-nil means complete the outline path in hierarchical steps.
  1783. When Org-mode uses the refile interface to select an outline path
  1784. \(see variable `org-refile-use-outline-path'), the completion of
  1785. the path can be done is a single go, or if can be done in steps down
  1786. the headline hierarchy. Going in steps is probably the best if you
  1787. do not use a special completion package like `ido' or `icicles'.
  1788. However, when using these packages, going in one step can be very
  1789. fast, while still showing the whole path to the entry."
  1790. :group 'org-refile
  1791. :type 'boolean)
  1792. (defcustom org-refile-allow-creating-parent-nodes nil
  1793. "Non-nil means allow to create new nodes as refile targets.
  1794. New nodes are then created by adding \"/new node name\" to the completion
  1795. of an existing node. When the value of this variable is `confirm',
  1796. new node creation must be confirmed by the user (recommended)
  1797. When nil, the completion must match an existing entry.
  1798. Note that, if the new heading is not seen by the criteria
  1799. listed in `org-refile-targets', multiple instances of the same
  1800. heading would be created by trying again to file under the new
  1801. heading."
  1802. :group 'org-refile
  1803. :type '(choice
  1804. (const :tag "Never" nil)
  1805. (const :tag "Always" t)
  1806. (const :tag "Prompt for confirmation" confirm)))
  1807. (defcustom org-refile-active-region-within-subtree nil
  1808. "Non-nil means also refile active region within a subtree.
  1809. By default `org-refile' doesn't allow refiling regions if they
  1810. don't contain a set of subtrees, but it might be convenient to
  1811. do so sometimes: in that case, the first line of the region is
  1812. converted to a headline before refiling."
  1813. :group 'org-refile
  1814. :version "24.1"
  1815. :type 'boolean)
  1816. (defgroup org-todo nil
  1817. "Options concerning TODO items in Org-mode."
  1818. :tag "Org TODO"
  1819. :group 'org)
  1820. (defgroup org-progress nil
  1821. "Options concerning Progress logging in Org-mode."
  1822. :tag "Org Progress"
  1823. :group 'org-time)
  1824. (defvar org-todo-interpretation-widgets
  1825. '((:tag "Sequence (cycling hits every state)" sequence)
  1826. (:tag "Type (cycling directly to DONE)" type))
  1827. "The available interpretation symbols for customizing `org-todo-keywords'.
  1828. Interested libraries should add to this list.")
  1829. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1830. "List of TODO entry keyword sequences and their interpretation.
  1831. \\<org-mode-map>This is a list of sequences.
  1832. Each sequence starts with a symbol, either `sequence' or `type',
  1833. indicating if the keywords should be interpreted as a sequence of
  1834. action steps, or as different types of TODO items. The first
  1835. keywords are states requiring action - these states will select a headline
  1836. for inclusion into the global TODO list Org-mode produces. If one of
  1837. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1838. signify that no further action is necessary. If \"|\" is not found,
  1839. the last keyword is treated as the only DONE state of the sequence.
  1840. The command \\[org-todo] cycles an entry through these states, and one
  1841. additional state where no keyword is present. For details about this
  1842. cycling, see the manual.
  1843. TODO keywords and interpretation can also be set on a per-file basis with
  1844. the special #+SEQ_TODO and #+TYP_TODO lines.
  1845. Each keyword can optionally specify a character for fast state selection
  1846. \(in combination with the variable `org-use-fast-todo-selection')
  1847. and specifiers for state change logging, using the same syntax that
  1848. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1849. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1850. indicates to record a time stamp each time this state is selected.
  1851. Each keyword may also specify if a timestamp or a note should be
  1852. recorded when entering or leaving the state, by adding additional
  1853. characters in the parenthesis after the keyword. This looks like this:
  1854. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1855. record only the time of the state change. With X and Y being either
  1856. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1857. Y when leaving the state if and only if the *target* state does not
  1858. define X. You may omit any of the fast-selection key or X or /Y,
  1859. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1860. For backward compatibility, this variable may also be just a list
  1861. of keywords. In this case the interpretation (sequence or type) will be
  1862. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1863. :group 'org-todo
  1864. :group 'org-keywords
  1865. :type '(choice
  1866. (repeat :tag "Old syntax, just keywords"
  1867. (string :tag "Keyword"))
  1868. (repeat :tag "New syntax"
  1869. (cons
  1870. (choice
  1871. :tag "Interpretation"
  1872. ;;Quick and dirty way to see
  1873. ;;`org-todo-interpretations'. This takes the
  1874. ;;place of item arguments
  1875. :convert-widget
  1876. (lambda (widget)
  1877. (widget-put widget
  1878. :args (mapcar
  1879. #'(lambda (x)
  1880. (widget-convert
  1881. (cons 'const x)))
  1882. org-todo-interpretation-widgets))
  1883. widget))
  1884. (repeat
  1885. (string :tag "Keyword"))))))
  1886. (defvar org-todo-keywords-1 nil
  1887. "All TODO and DONE keywords active in a buffer.")
  1888. (make-variable-buffer-local 'org-todo-keywords-1)
  1889. (defvar org-todo-keywords-for-agenda nil)
  1890. (defvar org-done-keywords-for-agenda nil)
  1891. (defvar org-drawers-for-agenda nil)
  1892. (defvar org-todo-keyword-alist-for-agenda nil)
  1893. (defvar org-tag-alist-for-agenda nil)
  1894. (defvar org-agenda-contributing-files nil)
  1895. (defvar org-not-done-keywords nil)
  1896. (make-variable-buffer-local 'org-not-done-keywords)
  1897. (defvar org-done-keywords nil)
  1898. (make-variable-buffer-local 'org-done-keywords)
  1899. (defvar org-todo-heads nil)
  1900. (make-variable-buffer-local 'org-todo-heads)
  1901. (defvar org-todo-sets nil)
  1902. (make-variable-buffer-local 'org-todo-sets)
  1903. (defvar org-todo-log-states nil)
  1904. (make-variable-buffer-local 'org-todo-log-states)
  1905. (defvar org-todo-kwd-alist nil)
  1906. (make-variable-buffer-local 'org-todo-kwd-alist)
  1907. (defvar org-todo-key-alist nil)
  1908. (make-variable-buffer-local 'org-todo-key-alist)
  1909. (defvar org-todo-key-trigger nil)
  1910. (make-variable-buffer-local 'org-todo-key-trigger)
  1911. (defcustom org-todo-interpretation 'sequence
  1912. "Controls how TODO keywords are interpreted.
  1913. This variable is in principle obsolete and is only used for
  1914. backward compatibility, if the interpretation of todo keywords is
  1915. not given already in `org-todo-keywords'. See that variable for
  1916. more information."
  1917. :group 'org-todo
  1918. :group 'org-keywords
  1919. :type '(choice (const sequence)
  1920. (const type)))
  1921. (defcustom org-use-fast-todo-selection t
  1922. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1923. This variable describes if and under what circumstances the cycling
  1924. mechanism for TODO keywords will be replaced by a single-key, direct
  1925. selection scheme.
  1926. When nil, fast selection is never used.
  1927. When the symbol `prefix', it will be used when `org-todo' is called
  1928. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1929. `C-u t' in an agenda buffer.
  1930. When t, fast selection is used by default. In this case, the prefix
  1931. argument forces cycling instead.
  1932. In all cases, the special interface is only used if access keys have
  1933. actually been assigned by the user, i.e. if keywords in the configuration
  1934. are followed by a letter in parenthesis, like TODO(t)."
  1935. :group 'org-todo
  1936. :type '(choice
  1937. (const :tag "Never" nil)
  1938. (const :tag "By default" t)
  1939. (const :tag "Only with C-u C-c C-t" prefix)))
  1940. (defcustom org-provide-todo-statistics t
  1941. "Non-nil means update todo statistics after insert and toggle.
  1942. ALL-HEADLINES means update todo statistics by including headlines
  1943. with no TODO keyword as well, counting them as not done.
  1944. A list of TODO keywords means the same, but skip keywords that are
  1945. not in this list.
  1946. When this is set, todo statistics is updated in the parent of the
  1947. current entry each time a todo state is changed."
  1948. :group 'org-todo
  1949. :type '(choice
  1950. (const :tag "Yes, only for TODO entries" t)
  1951. (const :tag "Yes, including all entries" 'all-headlines)
  1952. (repeat :tag "Yes, for TODOs in this list"
  1953. (string :tag "TODO keyword"))
  1954. (other :tag "No TODO statistics" nil)))
  1955. (defcustom org-hierarchical-todo-statistics t
  1956. "Non-nil means TODO statistics covers just direct children.
  1957. When nil, all entries in the subtree are considered.
  1958. This has only an effect if `org-provide-todo-statistics' is set.
  1959. To set this to nil for only a single subtree, use a COOKIE_DATA
  1960. property and include the word \"recursive\" into the value."
  1961. :group 'org-todo
  1962. :type 'boolean)
  1963. (defcustom org-after-todo-state-change-hook nil
  1964. "Hook which is run after the state of a TODO item was changed.
  1965. The new state (a string with a TODO keyword, or nil) is available in the
  1966. Lisp variable `org-state'."
  1967. :group 'org-todo
  1968. :type 'hook)
  1969. (defvar org-blocker-hook nil
  1970. "Hook for functions that are allowed to block a state change.
  1971. Functions in this hook should not modify the buffer.
  1972. Each function gets as its single argument a property list,
  1973. see `org-trigger-hook' for more information about this list.
  1974. If any of the functions in this hook returns nil, the state change
  1975. is blocked.")
  1976. (defvar org-trigger-hook nil
  1977. "Hook for functions that are triggered by a state change.
  1978. Each function gets as its single argument a property list with at
  1979. least the following elements:
  1980. (:type type-of-change :position pos-at-entry-start
  1981. :from old-state :to new-state)
  1982. Depending on the type, more properties may be present.
  1983. This mechanism is currently implemented for:
  1984. TODO state changes
  1985. ------------------
  1986. :type todo-state-change
  1987. :from previous state (keyword as a string), or nil, or a symbol
  1988. 'todo' or 'done', to indicate the general type of state.
  1989. :to new state, like in :from")
  1990. (defcustom org-enforce-todo-dependencies nil
  1991. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1992. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1993. be blocked if any prior sibling is not yet done.
  1994. Finally, if the parent is blocked because of ordered siblings of its own,
  1995. the child will also be blocked."
  1996. :set (lambda (var val)
  1997. (set var val)
  1998. (if val
  1999. (add-hook 'org-blocker-hook
  2000. 'org-block-todo-from-children-or-siblings-or-parent)
  2001. (remove-hook 'org-blocker-hook
  2002. 'org-block-todo-from-children-or-siblings-or-parent)))
  2003. :group 'org-todo
  2004. :type 'boolean)
  2005. (defcustom org-enforce-todo-checkbox-dependencies nil
  2006. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2007. When this is nil, checkboxes have no influence on switching TODO states.
  2008. When non-nil, you first need to check off all check boxes before the TODO
  2009. entry can be switched to DONE.
  2010. This variable needs to be set before org.el is loaded, and you need to
  2011. restart Emacs after a change to make the change effective. The only way
  2012. to change is while Emacs is running is through the customize interface."
  2013. :set (lambda (var val)
  2014. (set var val)
  2015. (if val
  2016. (add-hook 'org-blocker-hook
  2017. 'org-block-todo-from-checkboxes)
  2018. (remove-hook 'org-blocker-hook
  2019. 'org-block-todo-from-checkboxes)))
  2020. :group 'org-todo
  2021. :type 'boolean)
  2022. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2023. "Non-nil means inserting a TODO heading is treated as state change.
  2024. So when the command \\[org-insert-todo-heading] is used, state change
  2025. logging will apply if appropriate. When nil, the new TODO item will
  2026. be inserted directly, and no logging will take place."
  2027. :group 'org-todo
  2028. :type 'boolean)
  2029. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2030. "Non-nil means switching TODO states with S-cursor counts as state change.
  2031. This is the default behavior. However, setting this to nil allows a
  2032. convenient way to select a TODO state and bypass any logging associated
  2033. with that."
  2034. :group 'org-todo
  2035. :type 'boolean)
  2036. (defcustom org-todo-state-tags-triggers nil
  2037. "Tag changes that should be triggered by TODO state changes.
  2038. This is a list. Each entry is
  2039. (state-change (tag . flag) .......)
  2040. State-change can be a string with a state, and empty string to indicate the
  2041. state that has no TODO keyword, or it can be one of the symbols `todo'
  2042. or `done', meaning any not-done or done state, respectively."
  2043. :group 'org-todo
  2044. :group 'org-tags
  2045. :type '(repeat
  2046. (cons (choice :tag "When changing to"
  2047. (const :tag "Not-done state" todo)
  2048. (const :tag "Done state" done)
  2049. (string :tag "State"))
  2050. (repeat
  2051. (cons :tag "Tag action"
  2052. (string :tag "Tag")
  2053. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2054. (defcustom org-log-done nil
  2055. "Information to record when a task moves to the DONE state.
  2056. Possible values are:
  2057. nil Don't add anything, just change the keyword
  2058. time Add a time stamp to the task
  2059. note Prompt for a note and add it with template `org-log-note-headings'
  2060. This option can also be set with on a per-file-basis with
  2061. #+STARTUP: nologdone
  2062. #+STARTUP: logdone
  2063. #+STARTUP: lognotedone
  2064. You can have local logging settings for a subtree by setting the LOGGING
  2065. property to one or more of these keywords."
  2066. :group 'org-todo
  2067. :group 'org-progress
  2068. :type '(choice
  2069. (const :tag "No logging" nil)
  2070. (const :tag "Record CLOSED timestamp" time)
  2071. (const :tag "Record CLOSED timestamp with note." note)))
  2072. ;; Normalize old uses of org-log-done.
  2073. (cond
  2074. ((eq org-log-done t) (setq org-log-done 'time))
  2075. ((and (listp org-log-done) (memq 'done org-log-done))
  2076. (setq org-log-done 'note)))
  2077. (defcustom org-log-reschedule nil
  2078. "Information to record when the scheduling date of a tasks is modified.
  2079. Possible values are:
  2080. nil Don't add anything, just change the date
  2081. time Add a time stamp to the task
  2082. note Prompt for a note and add it with template `org-log-note-headings'
  2083. This option can also be set with on a per-file-basis with
  2084. #+STARTUP: nologreschedule
  2085. #+STARTUP: logreschedule
  2086. #+STARTUP: lognotereschedule"
  2087. :group 'org-todo
  2088. :group 'org-progress
  2089. :type '(choice
  2090. (const :tag "No logging" nil)
  2091. (const :tag "Record timestamp" time)
  2092. (const :tag "Record timestamp with note." note)))
  2093. (defcustom org-log-redeadline nil
  2094. "Information to record when the deadline date of a tasks is modified.
  2095. Possible values are:
  2096. nil Don't add anything, just change the date
  2097. time Add a time stamp to the task
  2098. note Prompt for a note and add it with template `org-log-note-headings'
  2099. This option can also be set with on a per-file-basis with
  2100. #+STARTUP: nologredeadline
  2101. #+STARTUP: logredeadline
  2102. #+STARTUP: lognoteredeadline
  2103. You can have local logging settings for a subtree by setting the LOGGING
  2104. property to one or more of these keywords."
  2105. :group 'org-todo
  2106. :group 'org-progress
  2107. :type '(choice
  2108. (const :tag "No logging" nil)
  2109. (const :tag "Record timestamp" time)
  2110. (const :tag "Record timestamp with note." note)))
  2111. (defcustom org-log-note-clock-out nil
  2112. "Non-nil means record a note when clocking out of an item.
  2113. This can also be configured on a per-file basis by adding one of
  2114. the following lines anywhere in the buffer:
  2115. #+STARTUP: lognoteclock-out
  2116. #+STARTUP: nolognoteclock-out"
  2117. :group 'org-todo
  2118. :group 'org-progress
  2119. :type 'boolean)
  2120. (defcustom org-log-done-with-time t
  2121. "Non-nil means the CLOSED time stamp will contain date and time.
  2122. When nil, only the date will be recorded."
  2123. :group 'org-progress
  2124. :type 'boolean)
  2125. (defcustom org-log-note-headings
  2126. '((done . "CLOSING NOTE %t")
  2127. (state . "State %-12s from %-12S %t")
  2128. (note . "Note taken on %t")
  2129. (reschedule . "Rescheduled from %S on %t")
  2130. (delschedule . "Not scheduled, was %S on %t")
  2131. (redeadline . "New deadline from %S on %t")
  2132. (deldeadline . "Removed deadline, was %S on %t")
  2133. (refile . "Refiled on %t")
  2134. (clock-out . ""))
  2135. "Headings for notes added to entries.
  2136. The value is an alist, with the car being a symbol indicating the note
  2137. context, and the cdr is the heading to be used. The heading may also be the
  2138. empty string.
  2139. %t in the heading will be replaced by a time stamp.
  2140. %T will be an active time stamp instead the default inactive one
  2141. %d will be replaced by a short-format time stamp.
  2142. %D will be replaced by an active short-format time stamp.
  2143. %s will be replaced by the new TODO state, in double quotes.
  2144. %S will be replaced by the old TODO state, in double quotes.
  2145. %u will be replaced by the user name.
  2146. %U will be replaced by the full user name.
  2147. In fact, it is not a good idea to change the `state' entry, because
  2148. agenda log mode depends on the format of these entries."
  2149. :group 'org-todo
  2150. :group 'org-progress
  2151. :type '(list :greedy t
  2152. (cons (const :tag "Heading when closing an item" done) string)
  2153. (cons (const :tag
  2154. "Heading when changing todo state (todo sequence only)"
  2155. state) string)
  2156. (cons (const :tag "Heading when just taking a note" note) string)
  2157. (cons (const :tag "Heading when clocking out" clock-out) string)
  2158. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2159. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2160. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2161. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2162. (cons (const :tag "Heading when refiling" refile) string)))
  2163. (unless (assq 'note org-log-note-headings)
  2164. (push '(note . "%t") org-log-note-headings))
  2165. (defcustom org-log-into-drawer nil
  2166. "Non-nil means insert state change notes and time stamps into a drawer.
  2167. When nil, state changes notes will be inserted after the headline and
  2168. any scheduling and clock lines, but not inside a drawer.
  2169. The value of this variable should be the name of the drawer to use.
  2170. LOGBOOK is proposed as the default drawer for this purpose, you can
  2171. also set this to a string to define the drawer of your choice.
  2172. A value of t is also allowed, representing \"LOGBOOK\".
  2173. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2174. will be ignored.
  2175. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2176. a subtree."
  2177. :group 'org-todo
  2178. :group 'org-progress
  2179. :type '(choice
  2180. (const :tag "Not into a drawer" nil)
  2181. (const :tag "LOGBOOK" t)
  2182. (string :tag "Other")))
  2183. (if (fboundp 'defvaralias)
  2184. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2185. (defun org-log-into-drawer ()
  2186. "Return the value of `org-log-into-drawer', but let properties overrule.
  2187. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2188. used instead of the default value."
  2189. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2190. (cond
  2191. ((not p) org-log-into-drawer)
  2192. ((equal p "nil") nil)
  2193. ((equal p "t") "LOGBOOK")
  2194. (t p))))
  2195. (defcustom org-log-state-notes-insert-after-drawers nil
  2196. "Non-nil means insert state change notes after any drawers in entry.
  2197. Only the drawers that *immediately* follow the headline and the
  2198. deadline/scheduled line are skipped.
  2199. When nil, insert notes right after the heading and perhaps the line
  2200. with deadline/scheduling if present.
  2201. This variable will have no effect if `org-log-into-drawer' is
  2202. set."
  2203. :group 'org-todo
  2204. :group 'org-progress
  2205. :type 'boolean)
  2206. (defcustom org-log-states-order-reversed t
  2207. "Non-nil means the latest state note will be directly after heading.
  2208. When nil, the state change notes will be ordered according to time."
  2209. :group 'org-todo
  2210. :group 'org-progress
  2211. :type 'boolean)
  2212. (defcustom org-todo-repeat-to-state nil
  2213. "The TODO state to which a repeater should return the repeating task.
  2214. By default this is the first task in a TODO sequence, or the previous state
  2215. in a TODO_TYP set. But you can specify another task here.
  2216. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2217. :group 'org-todo
  2218. :version "24.1"
  2219. :type '(choice (const :tag "Head of sequence" nil)
  2220. (string :tag "Specific state")))
  2221. (defcustom org-log-repeat 'time
  2222. "Non-nil means record moving through the DONE state when triggering repeat.
  2223. An auto-repeating task is immediately switched back to TODO when
  2224. marked DONE. If you are not logging state changes (by adding \"@\"
  2225. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2226. record a closing note, there will be no record of the task moving
  2227. through DONE. This variable forces taking a note anyway.
  2228. nil Don't force a record
  2229. time Record a time stamp
  2230. note Prompt for a note and add it with template `org-log-note-headings'
  2231. This option can also be set with on a per-file-basis with
  2232. #+STARTUP: nologrepeat
  2233. #+STARTUP: logrepeat
  2234. #+STARTUP: lognoterepeat
  2235. You can have local logging settings for a subtree by setting the LOGGING
  2236. property to one or more of these keywords."
  2237. :group 'org-todo
  2238. :group 'org-progress
  2239. :type '(choice
  2240. (const :tag "Don't force a record" nil)
  2241. (const :tag "Force recording the DONE state" time)
  2242. (const :tag "Force recording a note with the DONE state" note)))
  2243. (defgroup org-priorities nil
  2244. "Priorities in Org-mode."
  2245. :tag "Org Priorities"
  2246. :group 'org-todo)
  2247. (defcustom org-enable-priority-commands t
  2248. "Non-nil means priority commands are active.
  2249. When nil, these commands will be disabled, so that you never accidentally
  2250. set a priority."
  2251. :group 'org-priorities
  2252. :type 'boolean)
  2253. (defcustom org-highest-priority ?A
  2254. "The highest priority of TODO items. A character like ?A, ?B etc.
  2255. Must have a smaller ASCII number than `org-lowest-priority'."
  2256. :group 'org-priorities
  2257. :type 'character)
  2258. (defcustom org-lowest-priority ?C
  2259. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2260. Must have a larger ASCII number than `org-highest-priority'."
  2261. :group 'org-priorities
  2262. :type 'character)
  2263. (defcustom org-default-priority ?B
  2264. "The default priority of TODO items.
  2265. This is the priority an item gets if no explicit priority is given.
  2266. When starting to cycle on an empty priority the first step in the cycle
  2267. depends on `org-priority-start-cycle-with-default'. The resulting first
  2268. step priority must not exceed the range from `org-highest-priority' to
  2269. `org-lowest-priority' which means that `org-default-priority' has to be
  2270. in this range exclusive or inclusive the range boundaries. Else the
  2271. first step refuses to set the default and the second will fall back
  2272. to (depending on the command used) the highest or lowest priority."
  2273. :group 'org-priorities
  2274. :type 'character)
  2275. (defcustom org-priority-start-cycle-with-default t
  2276. "Non-nil means start with default priority when starting to cycle.
  2277. When this is nil, the first step in the cycle will be (depending on the
  2278. command used) one higher or lower than the default priority.
  2279. See also `org-default-priority'."
  2280. :group 'org-priorities
  2281. :type 'boolean)
  2282. (defcustom org-get-priority-function nil
  2283. "Function to extract the priority from a string.
  2284. The string is normally the headline. If this is nil Org computes the
  2285. priority from the priority cookie like [#A] in the headline. It returns
  2286. an integer, increasing by 1000 for each priority level.
  2287. The user can set a different function here, which should take a string
  2288. as an argument and return the numeric priority."
  2289. :group 'org-priorities
  2290. :version "24.1"
  2291. :type 'function)
  2292. (defgroup org-time nil
  2293. "Options concerning time stamps and deadlines in Org-mode."
  2294. :tag "Org Time"
  2295. :group 'org)
  2296. (defcustom org-insert-labeled-timestamps-at-point nil
  2297. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2298. When nil, these labeled time stamps are forces into the second line of an
  2299. entry, just after the headline. When scheduling from the global TODO list,
  2300. the time stamp will always be forced into the second line."
  2301. :group 'org-time
  2302. :type 'boolean)
  2303. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2304. "Formats for `format-time-string' which are used for time stamps.
  2305. It is not recommended to change this constant.")
  2306. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2307. "Number of minutes to round time stamps to.
  2308. These are two values, the first applies when first creating a time stamp.
  2309. The second applies when changing it with the commands `S-up' and `S-down'.
  2310. When changing the time stamp, this means that it will change in steps
  2311. of N minutes, as given by the second value.
  2312. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2313. numbers should be factors of 60, so for example 5, 10, 15.
  2314. When this is larger than 1, you can still force an exact time stamp by using
  2315. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2316. and by using a prefix arg to `S-up/down' to specify the exact number
  2317. of minutes to shift."
  2318. :group 'org-time
  2319. :get #'(lambda (var) ; Make sure both elements are there
  2320. (if (integerp (default-value var))
  2321. (list (default-value var) 5)
  2322. (default-value var)))
  2323. :type '(list
  2324. (integer :tag "when inserting times")
  2325. (integer :tag "when modifying times")))
  2326. ;; Normalize old customizations of this variable.
  2327. (when (integerp org-time-stamp-rounding-minutes)
  2328. (setq org-time-stamp-rounding-minutes
  2329. (list org-time-stamp-rounding-minutes
  2330. org-time-stamp-rounding-minutes)))
  2331. (defcustom org-display-custom-times nil
  2332. "Non-nil means overlay custom formats over all time stamps.
  2333. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2334. To turn this on on a per-file basis, insert anywhere in the file:
  2335. #+STARTUP: customtime"
  2336. :group 'org-time
  2337. :set 'set-default
  2338. :type 'sexp)
  2339. (make-variable-buffer-local 'org-display-custom-times)
  2340. (defcustom org-time-stamp-custom-formats
  2341. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2342. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2343. These are overlaid over the default ISO format if the variable
  2344. `org-display-custom-times' is set. Time like %H:%M should be at the
  2345. end of the second format. The custom formats are also honored by export
  2346. commands, if custom time display is turned on at the time of export."
  2347. :group 'org-time
  2348. :type 'sexp)
  2349. (defun org-time-stamp-format (&optional long inactive)
  2350. "Get the right format for a time string."
  2351. (let ((f (if long (cdr org-time-stamp-formats)
  2352. (car org-time-stamp-formats))))
  2353. (if inactive
  2354. (concat "[" (substring f 1 -1) "]")
  2355. f)))
  2356. (defcustom org-time-clocksum-format "%d:%02d"
  2357. "The format string used when creating CLOCKSUM lines.
  2358. This is also used when org-mode generates a time duration."
  2359. :group 'org-time
  2360. :type 'string)
  2361. (defcustom org-time-clocksum-use-fractional nil
  2362. "If non-nil, \\[org-clock-display] uses fractional times.
  2363. org-mode generates a time duration."
  2364. :group 'org-time
  2365. :type 'boolean)
  2366. (defcustom org-time-clocksum-fractional-format "%.2f"
  2367. "The format string used when creating CLOCKSUM lines, or when
  2368. org-mode generates a time duration."
  2369. :group 'org-time
  2370. :type 'string)
  2371. (defcustom org-deadline-warning-days 14
  2372. "No. of days before expiration during which a deadline becomes active.
  2373. This variable governs the display in sparse trees and in the agenda.
  2374. When 0 or negative, it means use this number (the absolute value of it)
  2375. even if a deadline has a different individual lead time specified.
  2376. Custom commands can set this variable in the options section."
  2377. :group 'org-time
  2378. :group 'org-agenda-daily/weekly
  2379. :type 'integer)
  2380. (defcustom org-read-date-prefer-future t
  2381. "Non-nil means assume future for incomplete date input from user.
  2382. This affects the following situations:
  2383. 1. The user gives a month but not a year.
  2384. For example, if it is April and you enter \"feb 2\", this will be read
  2385. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2386. 2. The user gives a day, but no month.
  2387. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2388. read this as the third of *next* month. However, if you enter \"17\",
  2389. it will be considered as *this* month.
  2390. If you set this variable to the symbol `time', then also the following
  2391. will work:
  2392. 3. If the user gives a time.
  2393. If the time is before now, it will be interpreted as tomorrow.
  2394. Currently none of this works for ISO week specifications.
  2395. When this option is nil, the current day, month and year will always be
  2396. used as defaults.
  2397. See also `org-agenda-jump-prefer-future'."
  2398. :group 'org-time
  2399. :type '(choice
  2400. (const :tag "Never" nil)
  2401. (const :tag "Check month and day" t)
  2402. (const :tag "Check month, day, and time" time)))
  2403. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2404. "Should the agenda jump command prefer the future for incomplete dates?
  2405. The default is to do the same as configured in `org-read-date-prefer-future'.
  2406. But you can also set a deviating value here.
  2407. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2408. :group 'org-agenda
  2409. :group 'org-time
  2410. :version "24.1"
  2411. :type '(choice
  2412. (const :tag "Use org-read-date-prefer-future"
  2413. org-read-date-prefer-future)
  2414. (const :tag "Never" nil)
  2415. (const :tag "Always" t)))
  2416. (defcustom org-read-date-force-compatible-dates t
  2417. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2418. Depending on the system Emacs is running on, certain dates cannot
  2419. be represented with the type used internally to represent time.
  2420. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2421. correctly. Some systems allow for earlier dates, some for later,
  2422. some for both. One way to find out it to insert any date into an
  2423. Org buffer, putting the cursor on the year and hitting S-up and
  2424. S-down to test the range.
  2425. When this variable is set to t, the date/time prompt will not let
  2426. you specify dates outside the 1970-2037 range, so it is certain that
  2427. these dates will work in whatever version of Emacs you are
  2428. running, and also that you can move a file from one Emacs implementation
  2429. to another. WHenever Org is forcing the year for you, it will display
  2430. a message and beep.
  2431. When this variable is nil, Org will check if the date is
  2432. representable in the specific Emacs implementation you are using.
  2433. If not, it will force a year, usually the current year, and beep
  2434. to remind you. Currently this setting is not recommended because
  2435. the likelihood that you will open your Org files in an Emacs that
  2436. has limited date range is not negligible.
  2437. A workaround for this problem is to use diary sexp dates for time
  2438. stamps outside of this range."
  2439. :group 'org-time
  2440. :version "24.1"
  2441. :type 'boolean)
  2442. (defcustom org-read-date-display-live t
  2443. "Non-nil means display current interpretation of date prompt live.
  2444. This display will be in an overlay, in the minibuffer."
  2445. :group 'org-time
  2446. :type 'boolean)
  2447. (defcustom org-read-date-popup-calendar t
  2448. "Non-nil means pop up a calendar when prompting for a date.
  2449. In the calendar, the date can be selected with mouse-1. However, the
  2450. minibuffer will also be active, and you can simply enter the date as well.
  2451. When nil, only the minibuffer will be available."
  2452. :group 'org-time
  2453. :type 'boolean)
  2454. (if (fboundp 'defvaralias)
  2455. (defvaralias 'org-popup-calendar-for-date-prompt
  2456. 'org-read-date-popup-calendar))
  2457. (defcustom org-read-date-minibuffer-setup-hook nil
  2458. "Hook to be used to set up keys for the date/time interface.
  2459. Add key definitions to `minibuffer-local-map', which will be a temporary
  2460. copy."
  2461. :group 'org-time
  2462. :type 'hook)
  2463. (defcustom org-extend-today-until 0
  2464. "The hour when your day really ends. Must be an integer.
  2465. This has influence for the following applications:
  2466. - When switching the agenda to \"today\". It it is still earlier than
  2467. the time given here, the day recognized as TODAY is actually yesterday.
  2468. - When a date is read from the user and it is still before the time given
  2469. here, the current date and time will be assumed to be yesterday, 23:59.
  2470. Also, timestamps inserted in capture templates follow this rule.
  2471. IMPORTANT: This is a feature whose implementation is and likely will
  2472. remain incomplete. Really, it is only here because past midnight seems to
  2473. be the favorite working time of John Wiegley :-)"
  2474. :group 'org-time
  2475. :type 'integer)
  2476. (defcustom org-use-effective-time nil
  2477. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2478. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2479. \"effective time\" of any timestamps between midnight and 8am will be
  2480. 23:59 of the previous day."
  2481. :group 'org-time
  2482. :version "24.1"
  2483. :type 'boolean)
  2484. (defcustom org-edit-timestamp-down-means-later nil
  2485. "Non-nil means S-down will increase the time in a time stamp.
  2486. When nil, S-up will increase."
  2487. :group 'org-time
  2488. :type 'boolean)
  2489. (defcustom org-calendar-follow-timestamp-change t
  2490. "Non-nil means make the calendar window follow timestamp changes.
  2491. When a timestamp is modified and the calendar window is visible, it will be
  2492. moved to the new date."
  2493. :group 'org-time
  2494. :type 'boolean)
  2495. (defgroup org-tags nil
  2496. "Options concerning tags in Org-mode."
  2497. :tag "Org Tags"
  2498. :group 'org)
  2499. (defcustom org-tag-alist nil
  2500. "List of tags allowed in Org-mode files.
  2501. When this list is nil, Org-mode will base TAG input on what is already in the
  2502. buffer.
  2503. The value of this variable is an alist, the car of each entry must be a
  2504. keyword as a string, the cdr may be a character that is used to select
  2505. that tag through the fast-tag-selection interface.
  2506. See the manual for details."
  2507. :group 'org-tags
  2508. :type '(repeat
  2509. (choice
  2510. (cons (string :tag "Tag name")
  2511. (character :tag "Access char"))
  2512. (list :tag "Start radio group"
  2513. (const :startgroup)
  2514. (option (string :tag "Group description")))
  2515. (list :tag "End radio group"
  2516. (const :endgroup)
  2517. (option (string :tag "Group description")))
  2518. (const :tag "New line" (:newline)))))
  2519. (defcustom org-tag-persistent-alist nil
  2520. "List of tags that will always appear in all Org-mode files.
  2521. This is in addition to any in buffer settings or customizations
  2522. of `org-tag-alist'.
  2523. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2524. The value of this variable is an alist, the car of each entry must be a
  2525. keyword as a string, the cdr may be a character that is used to select
  2526. that tag through the fast-tag-selection interface.
  2527. See the manual for details.
  2528. To disable these tags on a per-file basis, insert anywhere in the file:
  2529. #+STARTUP: noptag"
  2530. :group 'org-tags
  2531. :type '(repeat
  2532. (choice
  2533. (cons (string :tag "Tag name")
  2534. (character :tag "Access char"))
  2535. (const :tag "Start radio group" (:startgroup))
  2536. (const :tag "End radio group" (:endgroup))
  2537. (const :tag "New line" (:newline)))))
  2538. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2539. "If non-nil, always offer completion for all tags of all agenda files.
  2540. Instead of customizing this variable directly, you might want to
  2541. set it locally for capture buffers, because there no list of
  2542. tags in that file can be created dynamically (there are none).
  2543. (add-hook 'org-capture-mode-hook
  2544. (lambda ()
  2545. (set (make-local-variable
  2546. 'org-complete-tags-always-offer-all-agenda-tags)
  2547. t)))"
  2548. :group 'org-tags
  2549. :version "24.1"
  2550. :type 'boolean)
  2551. (defvar org-file-tags nil
  2552. "List of tags that can be inherited by all entries in the file.
  2553. The tags will be inherited if the variable `org-use-tag-inheritance'
  2554. says they should be.
  2555. This variable is populated from #+FILETAGS lines.")
  2556. (defcustom org-use-fast-tag-selection 'auto
  2557. "Non-nil means use fast tag selection scheme.
  2558. This is a special interface to select and deselect tags with single keys.
  2559. When nil, fast selection is never used.
  2560. When the symbol `auto', fast selection is used if and only if selection
  2561. characters for tags have been configured, either through the variable
  2562. `org-tag-alist' or through a #+TAGS line in the buffer.
  2563. When t, fast selection is always used and selection keys are assigned
  2564. automatically if necessary."
  2565. :group 'org-tags
  2566. :type '(choice
  2567. (const :tag "Always" t)
  2568. (const :tag "Never" nil)
  2569. (const :tag "When selection characters are configured" 'auto)))
  2570. (defcustom org-fast-tag-selection-single-key nil
  2571. "Non-nil means fast tag selection exits after first change.
  2572. When nil, you have to press RET to exit it.
  2573. During fast tag selection, you can toggle this flag with `C-c'.
  2574. This variable can also have the value `expert'. In this case, the window
  2575. displaying the tags menu is not even shown, until you press C-c again."
  2576. :group 'org-tags
  2577. :type '(choice
  2578. (const :tag "No" nil)
  2579. (const :tag "Yes" t)
  2580. (const :tag "Expert" expert)))
  2581. (defvar org-fast-tag-selection-include-todo nil
  2582. "Non-nil means fast tags selection interface will also offer TODO states.
  2583. This is an undocumented feature, you should not rely on it.")
  2584. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2585. "The column to which tags should be indented in a headline.
  2586. If this number is positive, it specifies the column. If it is negative,
  2587. it means that the tags should be flushright to that column. For example,
  2588. -80 works well for a normal 80 character screen.
  2589. When 0, place tags directly after headline text, with only one space in
  2590. between."
  2591. :group 'org-tags
  2592. :type 'integer)
  2593. (defcustom org-auto-align-tags t
  2594. "Non-nil keeps tags aligned when modifying headlines.
  2595. Some operations (i.e. demoting) change the length of a headline and
  2596. therefore shift the tags around. With this option turned on, after
  2597. each such operation the tags are again aligned to `org-tags-column'."
  2598. :group 'org-tags
  2599. :type 'boolean)
  2600. (defcustom org-use-tag-inheritance t
  2601. "Non-nil means tags in levels apply also for sublevels.
  2602. When nil, only the tags directly given in a specific line apply there.
  2603. This may also be a list of tags that should be inherited, or a regexp that
  2604. matches tags that should be inherited. Additional control is possible
  2605. with the variable `org-tags-exclude-from-inheritance' which gives an
  2606. explicit list of tags to be excluded from inheritance., even if the value of
  2607. `org-use-tag-inheritance' would select it for inheritance.
  2608. If this option is t, a match early-on in a tree can lead to a large
  2609. number of matches in the subtree when constructing the agenda or creating
  2610. a sparse tree. If you only want to see the first match in a tree during
  2611. a search, check out the variable `org-tags-match-list-sublevels'."
  2612. :group 'org-tags
  2613. :type '(choice
  2614. (const :tag "Not" nil)
  2615. (const :tag "Always" t)
  2616. (repeat :tag "Specific tags" (string :tag "Tag"))
  2617. (regexp :tag "Tags matched by regexp")))
  2618. (defcustom org-tags-exclude-from-inheritance nil
  2619. "List of tags that should never be inherited.
  2620. This is a way to exclude a few tags from inheritance. For way to do
  2621. the opposite, to actively allow inheritance for selected tags,
  2622. see the variable `org-use-tag-inheritance'."
  2623. :group 'org-tags
  2624. :type '(repeat (string :tag "Tag")))
  2625. (defun org-tag-inherit-p (tag)
  2626. "Check if TAG is one that should be inherited."
  2627. (cond
  2628. ((member tag org-tags-exclude-from-inheritance) nil)
  2629. ((eq org-use-tag-inheritance t) t)
  2630. ((not org-use-tag-inheritance) nil)
  2631. ((stringp org-use-tag-inheritance)
  2632. (string-match org-use-tag-inheritance tag))
  2633. ((listp org-use-tag-inheritance)
  2634. (member tag org-use-tag-inheritance))
  2635. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2636. (defcustom org-tags-match-list-sublevels t
  2637. "Non-nil means list also sublevels of headlines matching a search.
  2638. This variable applies to tags/property searches, and also to stuck
  2639. projects because this search is based on a tags match as well.
  2640. When set to the symbol `indented', sublevels are indented with
  2641. leading dots.
  2642. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2643. the sublevels of a headline matching a tag search often also match
  2644. the same search. Listing all of them can create very long lists.
  2645. Setting this variable to nil causes subtrees of a match to be skipped.
  2646. This variable is semi-obsolete and probably should always be true. It
  2647. is better to limit inheritance to certain tags using the variables
  2648. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2649. :group 'org-tags
  2650. :type '(choice
  2651. (const :tag "No, don't list them" nil)
  2652. (const :tag "Yes, do list them" t)
  2653. (const :tag "List them, indented with leading dots" indented)))
  2654. (defcustom org-tags-sort-function nil
  2655. "When set, tags are sorted using this function as a comparator."
  2656. :group 'org-tags
  2657. :type '(choice
  2658. (const :tag "No sorting" nil)
  2659. (const :tag "Alphabetical" string<)
  2660. (const :tag "Reverse alphabetical" string>)
  2661. (function :tag "Custom function" nil)))
  2662. (defvar org-tags-history nil
  2663. "History of minibuffer reads for tags.")
  2664. (defvar org-last-tags-completion-table nil
  2665. "The last used completion table for tags.")
  2666. (defvar org-after-tags-change-hook nil
  2667. "Hook that is run after the tags in a line have changed.")
  2668. (defgroup org-properties nil
  2669. "Options concerning properties in Org-mode."
  2670. :tag "Org Properties"
  2671. :group 'org)
  2672. (defcustom org-property-format "%-10s %s"
  2673. "How property key/value pairs should be formatted by `indent-line'.
  2674. When `indent-line' hits a property definition, it will format the line
  2675. according to this format, mainly to make sure that the values are
  2676. lined-up with respect to each other."
  2677. :group 'org-properties
  2678. :type 'string)
  2679. (defcustom org-properties-postprocess-alist nil
  2680. "Alist of properties and functions to adjust inserted values.
  2681. Elements of this alist must be of the form
  2682. ([string] [function])
  2683. where [string] must be a property name and [function] must be a
  2684. lambda expression: this lambda expression must take one argument,
  2685. the value to adjust, and return the new value as a string.
  2686. For example, this element will allow the property \"Remaining\"
  2687. to be updated wrt the relation between the \"Effort\" property
  2688. and the clock summary:
  2689. ((\"Remaining\" (lambda(value)
  2690. (let ((clocksum (org-clock-sum-current-item))
  2691. (effort (org-duration-string-to-minutes
  2692. (org-entry-get (point) \"Effort\"))))
  2693. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2694. :group 'org-properties
  2695. :version "24.1"
  2696. :type '(alist :key-type (string :tag "Property")
  2697. :value-type (function :tag "Function")))
  2698. (defcustom org-use-property-inheritance nil
  2699. "Non-nil means properties apply also for sublevels.
  2700. This setting is chiefly used during property searches. Turning it on can
  2701. cause significant overhead when doing a search, which is why it is not
  2702. on by default.
  2703. When nil, only the properties directly given in the current entry count.
  2704. When t, every property is inherited. The value may also be a list of
  2705. properties that should have inheritance, or a regular expression matching
  2706. properties that should be inherited.
  2707. However, note that some special properties use inheritance under special
  2708. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2709. and the properties ending in \"_ALL\" when they are used as descriptor
  2710. for valid values of a property.
  2711. Note for programmers:
  2712. When querying an entry with `org-entry-get', you can control if inheritance
  2713. should be used. By default, `org-entry-get' looks only at the local
  2714. properties. You can request inheritance by setting the inherit argument
  2715. to t (to force inheritance) or to `selective' (to respect the setting
  2716. in this variable)."
  2717. :group 'org-properties
  2718. :type '(choice
  2719. (const :tag "Not" nil)
  2720. (const :tag "Always" t)
  2721. (repeat :tag "Specific properties" (string :tag "Property"))
  2722. (regexp :tag "Properties matched by regexp")))
  2723. (defun org-property-inherit-p (property)
  2724. "Check if PROPERTY is one that should be inherited."
  2725. (cond
  2726. ((eq org-use-property-inheritance t) t)
  2727. ((not org-use-property-inheritance) nil)
  2728. ((stringp org-use-property-inheritance)
  2729. (string-match org-use-property-inheritance property))
  2730. ((listp org-use-property-inheritance)
  2731. (member property org-use-property-inheritance))
  2732. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2733. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2734. "The default column format, if no other format has been defined.
  2735. This variable can be set on the per-file basis by inserting a line
  2736. #+COLUMNS: %25ITEM ....."
  2737. :group 'org-properties
  2738. :type 'string)
  2739. (defcustom org-columns-ellipses ".."
  2740. "The ellipses to be used when a field in column view is truncated.
  2741. When this is the empty string, as many characters as possible are shown,
  2742. but then there will be no visual indication that the field has been truncated.
  2743. When this is a string of length N, the last N characters of a truncated
  2744. field are replaced by this string. If the column is narrower than the
  2745. ellipses string, only part of the ellipses string will be shown."
  2746. :group 'org-properties
  2747. :type 'string)
  2748. (defcustom org-columns-modify-value-for-display-function nil
  2749. "Function that modifies values for display in column view.
  2750. For example, it can be used to cut out a certain part from a time stamp.
  2751. The function must take 2 arguments:
  2752. column-title The title of the column (*not* the property name)
  2753. value The value that should be modified.
  2754. The function should return the value that should be displayed,
  2755. or nil if the normal value should be used."
  2756. :group 'org-properties
  2757. :type 'function)
  2758. (defcustom org-effort-property "Effort"
  2759. "The property that is being used to keep track of effort estimates.
  2760. Effort estimates given in this property need to have the format H:MM."
  2761. :group 'org-properties
  2762. :group 'org-progress
  2763. :type '(string :tag "Property"))
  2764. (defconst org-global-properties-fixed
  2765. '(("VISIBILITY_ALL" . "folded children content all")
  2766. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2767. "List of property/value pairs that can be inherited by any entry.
  2768. These are fixed values, for the preset properties. The user variable
  2769. that can be used to add to this list is `org-global-properties'.
  2770. The entries in this list are cons cells where the car is a property
  2771. name and cdr is a string with the value. If the value represents
  2772. multiple items like an \"_ALL\" property, separate the items by
  2773. spaces.")
  2774. (defcustom org-global-properties nil
  2775. "List of property/value pairs that can be inherited by any entry.
  2776. This list will be combined with the constant `org-global-properties-fixed'.
  2777. The entries in this list are cons cells where the car is a property
  2778. name and cdr is a string with the value.
  2779. You can set buffer-local values for the same purpose in the variable
  2780. `org-file-properties' this by adding lines like
  2781. #+PROPERTY: NAME VALUE"
  2782. :group 'org-properties
  2783. :type '(repeat
  2784. (cons (string :tag "Property")
  2785. (string :tag "Value"))))
  2786. (defvar org-file-properties nil
  2787. "List of property/value pairs that can be inherited by any entry.
  2788. Valid for the current buffer.
  2789. This variable is populated from #+PROPERTY lines.")
  2790. (make-variable-buffer-local 'org-file-properties)
  2791. (defgroup org-agenda nil
  2792. "Options concerning agenda views in Org-mode."
  2793. :tag "Org Agenda"
  2794. :group 'org)
  2795. (defvar org-category nil
  2796. "Variable used by org files to set a category for agenda display.
  2797. Such files should use a file variable to set it, for example
  2798. # -*- mode: org; org-category: \"ELisp\"
  2799. or contain a special line
  2800. #+CATEGORY: ELisp
  2801. If the file does not specify a category, then file's base name
  2802. is used instead.")
  2803. (make-variable-buffer-local 'org-category)
  2804. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2805. (defcustom org-agenda-files nil
  2806. "The files to be used for agenda display.
  2807. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2808. \\[org-remove-file]. You can also use customize to edit the list.
  2809. If an entry is a directory, all files in that directory that are matched by
  2810. `org-agenda-file-regexp' will be part of the file list.
  2811. If the value of the variable is not a list but a single file name, then
  2812. the list of agenda files is actually stored and maintained in that file, one
  2813. agenda file per line. In this file paths can be given relative to
  2814. `org-directory'. Tilde expansion and environment variable substitution
  2815. are also made."
  2816. :group 'org-agenda
  2817. :type '(choice
  2818. (repeat :tag "List of files and directories" file)
  2819. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2820. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2821. "Regular expression to match files for `org-agenda-files'.
  2822. If any element in the list in that variable contains a directory instead
  2823. of a normal file, all files in that directory that are matched by this
  2824. regular expression will be included."
  2825. :group 'org-agenda
  2826. :type 'regexp)
  2827. (defcustom org-agenda-text-search-extra-files nil
  2828. "List of extra files to be searched by text search commands.
  2829. These files will be search in addition to the agenda files by the
  2830. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2831. Note that these files will only be searched for text search commands,
  2832. not for the other agenda views like todo lists, tag searches or the weekly
  2833. agenda. This variable is intended to list notes and possibly archive files
  2834. that should also be searched by these two commands.
  2835. In fact, if the first element in the list is the symbol `agenda-archives',
  2836. than all archive files of all agenda files will be added to the search
  2837. scope."
  2838. :group 'org-agenda
  2839. :type '(set :greedy t
  2840. (const :tag "Agenda Archives" agenda-archives)
  2841. (repeat :inline t (file))))
  2842. (if (fboundp 'defvaralias)
  2843. (defvaralias 'org-agenda-multi-occur-extra-files
  2844. 'org-agenda-text-search-extra-files))
  2845. (defcustom org-agenda-skip-unavailable-files nil
  2846. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2847. A nil value means to remove them, after a query, from the list."
  2848. :group 'org-agenda
  2849. :type 'boolean)
  2850. (defcustom org-calendar-to-agenda-key [?c]
  2851. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2852. The command `org-calendar-goto-agenda' will be bound to this key. The
  2853. default is the character `c' because then `c' can be used to switch back and
  2854. forth between agenda and calendar."
  2855. :group 'org-agenda
  2856. :type 'sexp)
  2857. (defcustom org-calendar-insert-diary-entry-key [?i]
  2858. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2859. This option is irrelevant until `org-agenda-diary-file' has been configured
  2860. to point to an Org-mode file. When that is the case, the command
  2861. `org-agenda-diary-entry' will be bound to the key given here, by default
  2862. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2863. if you want to continue doing this, you need to change this to a different
  2864. key."
  2865. :group 'org-agenda
  2866. :type 'sexp)
  2867. (defcustom org-agenda-diary-file 'diary-file
  2868. "File to which to add new entries with the `i' key in agenda and calendar.
  2869. When this is the symbol `diary-file', the functionality in the Emacs
  2870. calendar will be used to add entries to the `diary-file'. But when this
  2871. points to a file, `org-agenda-diary-entry' will be used instead."
  2872. :group 'org-agenda
  2873. :type '(choice
  2874. (const :tag "The standard Emacs diary file" diary-file)
  2875. (file :tag "Special Org file diary entries")))
  2876. (eval-after-load "calendar"
  2877. '(progn
  2878. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2879. 'org-calendar-goto-agenda)
  2880. (add-hook 'calendar-mode-hook
  2881. (lambda ()
  2882. (unless (eq org-agenda-diary-file 'diary-file)
  2883. (define-key calendar-mode-map
  2884. org-calendar-insert-diary-entry-key
  2885. 'org-agenda-diary-entry))))))
  2886. (defgroup org-latex nil
  2887. "Options for embedding LaTeX code into Org-mode."
  2888. :tag "Org LaTeX"
  2889. :group 'org)
  2890. (defcustom org-format-latex-options
  2891. '(:foreground default :background default :scale 1.0
  2892. :html-foreground "Black" :html-background "Transparent"
  2893. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2894. "Options for creating images from LaTeX fragments.
  2895. This is a property list with the following properties:
  2896. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2897. `default' means use the foreground of the default face.
  2898. :background the background color, or \"Transparent\".
  2899. `default' means use the background of the default face.
  2900. :scale a scaling factor for the size of the images, to get more pixels
  2901. :html-foreground, :html-background, :html-scale
  2902. the same numbers for HTML export.
  2903. :matchers a list indicating which matchers should be used to
  2904. find LaTeX fragments. Valid members of this list are:
  2905. \"begin\" find environments
  2906. \"$1\" find single characters surrounded by $.$
  2907. \"$\" find math expressions surrounded by $...$
  2908. \"$$\" find math expressions surrounded by $$....$$
  2909. \"\\(\" find math expressions surrounded by \\(...\\)
  2910. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2911. :group 'org-latex
  2912. :type 'plist)
  2913. (defcustom org-format-latex-signal-error t
  2914. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2915. When nil, just push out a message."
  2916. :group 'org-latex
  2917. :version "24.1"
  2918. :type 'boolean)
  2919. (defcustom org-latex-to-mathml-jar-file nil
  2920. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2921. Use this to specify additional executable file say a jar file.
  2922. When using MathToWeb as the converter, specify the full-path to
  2923. your mathtoweb.jar file."
  2924. :group 'org-latex
  2925. :version "24.1"
  2926. :type '(choice
  2927. (const :tag "None" nil)
  2928. (file :tag "JAR file" :must-match t)))
  2929. (defcustom org-latex-to-mathml-convert-command nil
  2930. "Command to convert LaTeX fragments to MathML.
  2931. Replace format-specifiers in the command as noted below and use
  2932. `shell-command' to convert LaTeX to MathML.
  2933. %j: Executable file in fully expanded form as specified by
  2934. `org-latex-to-mathml-jar-file'.
  2935. %I: Input LaTeX file in fully expanded form
  2936. %o: Output MathML file
  2937. This command is used by `org-create-math-formula'.
  2938. When using MathToWeb as the converter, set this to
  2939. \"java -jar %j -unicode -force -df %o %I\"."
  2940. :group 'org-latex
  2941. :version "24.1"
  2942. :type '(choice
  2943. (const :tag "None" nil)
  2944. (string :tag "\nShell command")))
  2945. (defcustom org-latex-create-formula-image-program 'dvipng
  2946. "Program to convert LaTeX fragments with.
  2947. dvipng Process the LaTeX fragments to dvi file, then convert
  2948. dvi files to png files using dvipng.
  2949. This will also include processing of non-math environments.
  2950. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2951. to convert pdf files to png files"
  2952. :group 'org-latex
  2953. :version "24.1"
  2954. :type '(choice
  2955. (const :tag "dvipng" dvipng)
  2956. (const :tag "imagemagick" imagemagick)))
  2957. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  2958. "Path to store latex preview images. A relative path here creates many
  2959. directories relative to the processed org files paths. An absolute path
  2960. puts all preview images at the same place."
  2961. :group 'org-latex
  2962. :version "24.3"
  2963. :type 'string)
  2964. (defun org-format-latex-mathml-available-p ()
  2965. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2966. (save-match-data
  2967. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2968. org-latex-to-mathml-convert-command)
  2969. (let ((executable (car (split-string
  2970. org-latex-to-mathml-convert-command))))
  2971. (when (executable-find executable)
  2972. (if (string-match
  2973. "%j" org-latex-to-mathml-convert-command)
  2974. (file-readable-p org-latex-to-mathml-jar-file)
  2975. t))))))
  2976. (defcustom org-format-latex-header "\\documentclass{article}
  2977. \\usepackage[usenames]{color}
  2978. \\usepackage{amsmath}
  2979. \\usepackage[mathscr]{eucal}
  2980. \\pagestyle{empty} % do not remove
  2981. \[PACKAGES]
  2982. \[DEFAULT-PACKAGES]
  2983. % The settings below are copied from fullpage.sty
  2984. \\setlength{\\textwidth}{\\paperwidth}
  2985. \\addtolength{\\textwidth}{-3cm}
  2986. \\setlength{\\oddsidemargin}{1.5cm}
  2987. \\addtolength{\\oddsidemargin}{-2.54cm}
  2988. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2989. \\setlength{\\textheight}{\\paperheight}
  2990. \\addtolength{\\textheight}{-\\headheight}
  2991. \\addtolength{\\textheight}{-\\headsep}
  2992. \\addtolength{\\textheight}{-\\footskip}
  2993. \\addtolength{\\textheight}{-3cm}
  2994. \\setlength{\\topmargin}{1.5cm}
  2995. \\addtolength{\\topmargin}{-2.54cm}"
  2996. "The document header used for processing LaTeX fragments.
  2997. It is imperative that this header make sure that no page number
  2998. appears on the page. The package defined in the variables
  2999. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3000. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3001. will be appended."
  3002. :group 'org-latex
  3003. :type 'string)
  3004. (defvar org-format-latex-header-extra nil)
  3005. (defun org-set-packages-alist (var val)
  3006. "Set the packages alist and make sure it has 3 elements per entry."
  3007. (set var (mapcar (lambda (x)
  3008. (if (and (consp x) (= (length x) 2))
  3009. (list (car x) (nth 1 x) t)
  3010. x))
  3011. val)))
  3012. (defun org-get-packages-alist (var)
  3013. "Get the packages alist and make sure it has 3 elements per entry."
  3014. (mapcar (lambda (x)
  3015. (if (and (consp x) (= (length x) 2))
  3016. (list (car x) (nth 1 x) t)
  3017. x))
  3018. (default-value var)))
  3019. ;; The following variables are defined here because is it also used
  3020. ;; when formatting latex fragments. Originally it was part of the
  3021. ;; LaTeX exporter, which is why the name includes "export".
  3022. (defcustom org-export-latex-default-packages-alist
  3023. '(("AUTO" "inputenc" t)
  3024. ("T1" "fontenc" t)
  3025. ("" "fixltx2e" nil)
  3026. ("" "graphicx" t)
  3027. ("" "longtable" nil)
  3028. ("" "float" nil)
  3029. ("" "wrapfig" nil)
  3030. ("" "soul" t)
  3031. ("" "textcomp" t)
  3032. ("" "marvosym" t)
  3033. ("" "wasysym" t)
  3034. ("" "latexsym" t)
  3035. ("" "amssymb" t)
  3036. ("" "hyperref" nil)
  3037. "\\tolerance=1000"
  3038. )
  3039. "Alist of default packages to be inserted in the header.
  3040. Change this only if one of the packages here causes an incompatibility
  3041. with another package you are using.
  3042. The packages in this list are needed by one part or another of Org-mode
  3043. to function properly.
  3044. - inputenc, fontenc: for basic font and character selection
  3045. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3046. for interpreting the entities in `org-entities'. You can skip some of these
  3047. packages if you don't use any of the symbols in it.
  3048. - graphicx: for including images
  3049. - float, wrapfig: for figure placement
  3050. - longtable: for long tables
  3051. - hyperref: for cross references
  3052. Therefore you should not modify this variable unless you know what you
  3053. are doing. The one reason to change it anyway is that you might be loading
  3054. some other package that conflicts with one of the default packages.
  3055. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3056. If SNIPPET-FLAG is t, the package also needs to be included when
  3057. compiling LaTeX snippets into images for inclusion into HTML."
  3058. :group 'org-export-latex
  3059. :set 'org-set-packages-alist
  3060. :get 'org-get-packages-alist
  3061. :version "24.1"
  3062. :type '(repeat
  3063. (choice
  3064. (list :tag "options/package pair"
  3065. (string :tag "options")
  3066. (string :tag "package")
  3067. (boolean :tag "Snippet"))
  3068. (string :tag "A line of LaTeX"))))
  3069. (defcustom org-export-latex-packages-alist nil
  3070. "Alist of packages to be inserted in every LaTeX header.
  3071. These will be inserted after `org-export-latex-default-packages-alist'.
  3072. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3073. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3074. turning LaTeX snippets into images for inclusion into HTML.
  3075. Make sure that you only list packages here which:
  3076. - you want in every file
  3077. - do not conflict with the default packages in
  3078. `org-export-latex-default-packages-alist'
  3079. - do not conflict with the setup in `org-format-latex-header'."
  3080. :group 'org-export-latex
  3081. :set 'org-set-packages-alist
  3082. :get 'org-get-packages-alist
  3083. :type '(repeat
  3084. (choice
  3085. (list :tag "options/package pair"
  3086. (string :tag "options")
  3087. (string :tag "package")
  3088. (boolean :tag "Snippet"))
  3089. (string :tag "A line of LaTeX"))))
  3090. (defgroup org-appearance nil
  3091. "Settings for Org-mode appearance."
  3092. :tag "Org Appearance"
  3093. :group 'org)
  3094. (defcustom org-level-color-stars-only nil
  3095. "Non-nil means fontify only the stars in each headline.
  3096. When nil, the entire headline is fontified.
  3097. Changing it requires restart of `font-lock-mode' to become effective
  3098. also in regions already fontified."
  3099. :group 'org-appearance
  3100. :type 'boolean)
  3101. (defcustom org-hide-leading-stars nil
  3102. "Non-nil means hide the first N-1 stars in a headline.
  3103. This works by using the face `org-hide' for these stars. This
  3104. face is white for a light background, and black for a dark
  3105. background. You may have to customize the face `org-hide' to
  3106. make this work.
  3107. Changing it requires restart of `font-lock-mode' to become effective
  3108. also in regions already fontified.
  3109. You may also set this on a per-file basis by adding one of the following
  3110. lines to the buffer:
  3111. #+STARTUP: hidestars
  3112. #+STARTUP: showstars"
  3113. :group 'org-appearance
  3114. :type 'boolean)
  3115. (defcustom org-hidden-keywords nil
  3116. "List of symbols corresponding to keywords to be hidden the org buffer.
  3117. For example, a value '(title) for this list will make the document's title
  3118. appear in the buffer without the initial #+TITLE: keyword."
  3119. :group 'org-appearance
  3120. :version "24.1"
  3121. :type '(set (const :tag "#+AUTHOR" author)
  3122. (const :tag "#+DATE" date)
  3123. (const :tag "#+EMAIL" email)
  3124. (const :tag "#+TITLE" title)))
  3125. (defcustom org-custom-properties nil
  3126. "List of properties (as strings) with a special meaning.
  3127. The default use of these custom properties is to let the user
  3128. hide them with `org-toggle-custom-properties-visibility'."
  3129. :group 'org-properties
  3130. :group 'org-appearance
  3131. :version "24.3"
  3132. :type '(repeat (string :tag "Property Name")))
  3133. (defcustom org-fontify-done-headline nil
  3134. "Non-nil means change the face of a headline if it is marked DONE.
  3135. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3136. When this is non-nil, the headline after the keyword is set to the
  3137. `org-headline-done' as an additional indication."
  3138. :group 'org-appearance
  3139. :type 'boolean)
  3140. (defcustom org-fontify-emphasized-text t
  3141. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3142. Changing this variable requires a restart of Emacs to take effect."
  3143. :group 'org-appearance
  3144. :type 'boolean)
  3145. (defcustom org-fontify-whole-heading-line nil
  3146. "Non-nil means fontify the whole line for headings.
  3147. This is useful when setting a background color for the
  3148. org-level-* faces."
  3149. :group 'org-appearance
  3150. :type 'boolean)
  3151. (defcustom org-highlight-latex-fragments-and-specials nil
  3152. "Non-nil means fontify what is treated specially by the exporters."
  3153. :group 'org-appearance
  3154. :type 'boolean)
  3155. (defcustom org-hide-emphasis-markers nil
  3156. "Non-nil mean font-lock should hide the emphasis marker characters."
  3157. :group 'org-appearance
  3158. :type 'boolean)
  3159. (defcustom org-pretty-entities nil
  3160. "Non-nil means show entities as UTF8 characters.
  3161. When nil, the \\name form remains in the buffer."
  3162. :group 'org-appearance
  3163. :version "24.1"
  3164. :type 'boolean)
  3165. (defcustom org-pretty-entities-include-sub-superscripts t
  3166. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3167. :group 'org-appearance
  3168. :version "24.1"
  3169. :type 'boolean)
  3170. (defvar org-emph-re nil
  3171. "Regular expression for matching emphasis.
  3172. After a match, the match groups contain these elements:
  3173. 0 The match of the full regular expression, including the characters
  3174. before and after the proper match
  3175. 1 The character before the proper match, or empty at beginning of line
  3176. 2 The proper match, including the leading and trailing markers
  3177. 3 The leading marker like * or /, indicating the type of highlighting
  3178. 4 The text between the emphasis markers, not including the markers
  3179. 5 The character after the match, empty at the end of a line")
  3180. (defvar org-verbatim-re nil
  3181. "Regular expression for matching verbatim text.")
  3182. (defvar org-emphasis-regexp-components) ; defined just below
  3183. (defvar org-emphasis-alist) ; defined just below
  3184. (defun org-set-emph-re (var val)
  3185. "Set variable and compute the emphasis regular expression."
  3186. (set var val)
  3187. (when (and (boundp 'org-emphasis-alist)
  3188. (boundp 'org-emphasis-regexp-components)
  3189. org-emphasis-alist org-emphasis-regexp-components)
  3190. (let* ((e org-emphasis-regexp-components)
  3191. (pre (car e))
  3192. (post (nth 1 e))
  3193. (border (nth 2 e))
  3194. (body (nth 3 e))
  3195. (nl (nth 4 e))
  3196. (body1 (concat body "*?"))
  3197. (markers (mapconcat 'car org-emphasis-alist ""))
  3198. (vmarkers (mapconcat
  3199. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3200. org-emphasis-alist "")))
  3201. ;; make sure special characters appear at the right position in the class
  3202. (if (string-match "\\^" markers)
  3203. (setq markers (concat (replace-match "" t t markers) "^")))
  3204. (if (string-match "-" markers)
  3205. (setq markers (concat (replace-match "" t t markers) "-")))
  3206. (if (string-match "\\^" vmarkers)
  3207. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3208. (if (string-match "-" vmarkers)
  3209. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3210. (if (> nl 0)
  3211. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3212. (int-to-string nl) "\\}")))
  3213. ;; Make the regexp
  3214. (setq org-emph-re
  3215. (concat "\\([" pre "]\\|^\\)"
  3216. "\\("
  3217. "\\([" markers "]\\)"
  3218. "\\("
  3219. "[^" border "]\\|"
  3220. "[^" border "]"
  3221. body1
  3222. "[^" border "]"
  3223. "\\)"
  3224. "\\3\\)"
  3225. "\\([" post "]\\|$\\)"))
  3226. (setq org-verbatim-re
  3227. (concat "\\([" pre "]\\|^\\)"
  3228. "\\("
  3229. "\\([" vmarkers "]\\)"
  3230. "\\("
  3231. "[^" border "]\\|"
  3232. "[^" border "]"
  3233. body1
  3234. "[^" border "]"
  3235. "\\)"
  3236. "\\3\\)"
  3237. "\\([" post "]\\|$\\)")))))
  3238. (defcustom org-emphasis-regexp-components
  3239. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3240. "Components used to build the regular expression for emphasis.
  3241. This is a list with five entries. Terminology: In an emphasis string
  3242. like \" *strong word* \", we call the initial space PREMATCH, the final
  3243. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3244. and \"trong wor\" is the body. The different components in this variable
  3245. specify what is allowed/forbidden in each part:
  3246. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3247. post Chars allowed as postmatch. End of line will be allowed too.
  3248. border The chars *forbidden* as border characters.
  3249. body-regexp A regexp like \".\" to match a body character. Don't use
  3250. non-shy groups here, and don't allow newline here.
  3251. newline The maximum number of newlines allowed in an emphasis exp.
  3252. Use customize to modify this, or restart Emacs after changing it."
  3253. :group 'org-appearance
  3254. :set 'org-set-emph-re
  3255. :type '(list
  3256. (sexp :tag "Allowed chars in pre ")
  3257. (sexp :tag "Allowed chars in post ")
  3258. (sexp :tag "Forbidden chars in border ")
  3259. (sexp :tag "Regexp for body ")
  3260. (integer :tag "number of newlines allowed")
  3261. (option (boolean :tag "Please ignore this button"))))
  3262. (defcustom org-emphasis-alist
  3263. `(("*" bold "<b>" "</b>")
  3264. ("/" italic "<i>" "</i>")
  3265. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3266. ("=" org-code "<code>" "</code>" verbatim)
  3267. ("~" org-verbatim "<code>" "</code>" verbatim)
  3268. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3269. "<del>" "</del>")
  3270. )
  3271. "Special syntax for emphasized text.
  3272. Text starting and ending with a special character will be emphasized, for
  3273. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3274. characters, the face to be used by font-lock for highlighting in Org-mode
  3275. Emacs buffers, and the HTML tags to be used for this.
  3276. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3277. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3278. Use customize to modify this, or restart Emacs after changing it."
  3279. :group 'org-appearance
  3280. :set 'org-set-emph-re
  3281. :type '(repeat
  3282. (list
  3283. (string :tag "Marker character")
  3284. (choice
  3285. (face :tag "Font-lock-face")
  3286. (plist :tag "Face property list"))
  3287. (string :tag "HTML start tag")
  3288. (string :tag "HTML end tag")
  3289. (option (const verbatim)))))
  3290. (defvar org-protecting-blocks
  3291. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3292. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3293. This is needed for font-lock setup.")
  3294. ;;; Miscellaneous options
  3295. (defgroup org-completion nil
  3296. "Completion in Org-mode."
  3297. :tag "Org Completion"
  3298. :group 'org)
  3299. (defcustom org-completion-use-ido nil
  3300. "Non-nil means use ido completion wherever possible.
  3301. Note that `ido-mode' must be active for this variable to be relevant.
  3302. If you decide to turn this variable on, you might well want to turn off
  3303. `org-outline-path-complete-in-steps'.
  3304. See also `org-completion-use-iswitchb'."
  3305. :group 'org-completion
  3306. :type 'boolean)
  3307. (defcustom org-completion-use-iswitchb nil
  3308. "Non-nil means use iswitchb completion wherever possible.
  3309. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3310. If you decide to turn this variable on, you might well want to turn off
  3311. `org-outline-path-complete-in-steps'.
  3312. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3313. :group 'org-completion
  3314. :type 'boolean)
  3315. (defcustom org-completion-fallback-command 'hippie-expand
  3316. "The expansion command called by \\[pcomplete] in normal context.
  3317. Normal means, no org-mode-specific context."
  3318. :group 'org-completion
  3319. :type 'function)
  3320. ;;; Functions and variables from their packages
  3321. ;; Declared here to avoid compiler warnings
  3322. ;; XEmacs only
  3323. (defvar outline-mode-menu-heading)
  3324. (defvar outline-mode-menu-show)
  3325. (defvar outline-mode-menu-hide)
  3326. (defvar zmacs-regions) ; XEmacs regions
  3327. ;; Emacs only
  3328. (defvar mark-active)
  3329. ;; Various packages
  3330. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3331. (declare-function calendar-forward-day "cal-move" (arg))
  3332. (declare-function calendar-goto-date "cal-move" (date))
  3333. (declare-function calendar-goto-today "cal-move" ())
  3334. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3335. (defvar calc-embedded-close-formula)
  3336. (defvar calc-embedded-open-formula)
  3337. (declare-function cdlatex-tab "ext:cdlatex" ())
  3338. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3339. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3340. (defvar font-lock-unfontify-region-function)
  3341. (declare-function iswitchb-read-buffer "iswitchb"
  3342. (prompt &optional default require-match start matches-set))
  3343. (defvar iswitchb-temp-buflist)
  3344. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3345. (defvar org-agenda-tags-todo-honor-ignore-options)
  3346. (declare-function org-agenda-skip "org-agenda" ())
  3347. (declare-function
  3348. org-agenda-format-item "org-agenda"
  3349. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3350. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3351. (declare-function org-agenda-change-all-lines "org-agenda"
  3352. (newhead hdmarker &optional fixface just-this))
  3353. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3354. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3355. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3356. (beg end))
  3357. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3358. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3359. "org-agenda" (&optional end))
  3360. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3361. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3362. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3363. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3364. (declare-function org-indent-mode "org-indent" (&optional arg))
  3365. (declare-function parse-time-string "parse-time" (string))
  3366. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3367. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3368. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3369. (defvar remember-data-file)
  3370. (defvar texmathp-why)
  3371. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3372. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3373. (defvar w3m-current-url)
  3374. (defvar w3m-current-title)
  3375. (defvar org-latex-regexps)
  3376. ;;; Autoload and prepare some org modules
  3377. ;; Some table stuff that needs to be defined here, because it is used
  3378. ;; by the functions setting up org-mode or checking for table context.
  3379. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3380. "Detect an org-type or table-type table.")
  3381. (defconst org-table-line-regexp "^[ \t]*|"
  3382. "Detect an org-type table line.")
  3383. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3384. "Detect an org-type table line.")
  3385. (defconst org-table-hline-regexp "^[ \t]*|-"
  3386. "Detect an org-type table hline.")
  3387. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3388. "Detect a table-type table hline.")
  3389. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3390. "Detect the first line outside a table when searching from within it.
  3391. This works for both table types.")
  3392. ;; Autoload the functions in org-table.el that are needed by functions here.
  3393. (eval-and-compile
  3394. (org-autoload "org-table"
  3395. '(org-table-begin org-table-blank-field org-table-end)))
  3396. ;;;###autoload
  3397. (defun turn-on-orgtbl ()
  3398. "Unconditionally turn on `orgtbl-mode'."
  3399. (require 'org-table)
  3400. (orgtbl-mode 1))
  3401. (defun org-at-table-p (&optional table-type)
  3402. "Return t if the cursor is inside an org-type table.
  3403. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3404. (if org-enable-table-editor
  3405. (save-excursion
  3406. (beginning-of-line 1)
  3407. (looking-at (if table-type org-table-any-line-regexp
  3408. org-table-line-regexp)))
  3409. nil))
  3410. (defsubst org-table-p () (org-at-table-p))
  3411. (defun org-at-table.el-p ()
  3412. "Return t if and only if we are at a table.el table."
  3413. (and (org-at-table-p 'any)
  3414. (save-excursion
  3415. (goto-char (org-table-begin 'any))
  3416. (looking-at org-table1-hline-regexp))))
  3417. (defun org-table-recognize-table.el ()
  3418. "If there is a table.el table nearby, recognize it and move into it."
  3419. (if org-table-tab-recognizes-table.el
  3420. (if (org-at-table.el-p)
  3421. (progn
  3422. (beginning-of-line 1)
  3423. (if (looking-at org-table-dataline-regexp)
  3424. nil
  3425. (if (looking-at org-table1-hline-regexp)
  3426. (progn
  3427. (beginning-of-line 2)
  3428. (if (looking-at org-table-any-border-regexp)
  3429. (beginning-of-line -1)))))
  3430. (if (re-search-forward "|" (org-table-end t) t)
  3431. (progn
  3432. (require 'table)
  3433. (if (table--at-cell-p (point))
  3434. t
  3435. (message "recognizing table.el table...")
  3436. (table-recognize-table)
  3437. (message "recognizing table.el table...done")))
  3438. (error "This should not happen"))
  3439. t)
  3440. nil)
  3441. nil))
  3442. (defun org-at-table-hline-p ()
  3443. "Return t if the cursor is inside a hline in a table."
  3444. (if org-enable-table-editor
  3445. (save-excursion
  3446. (beginning-of-line 1)
  3447. (looking-at org-table-hline-regexp))
  3448. nil))
  3449. (defvar org-table-clean-did-remove-column nil)
  3450. (defun org-table-map-tables (function &optional quietly)
  3451. "Apply FUNCTION to the start of all tables in the buffer."
  3452. (save-excursion
  3453. (save-restriction
  3454. (widen)
  3455. (goto-char (point-min))
  3456. (while (re-search-forward org-table-any-line-regexp nil t)
  3457. (unless quietly
  3458. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3459. (beginning-of-line 1)
  3460. (when (and (looking-at org-table-line-regexp)
  3461. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3462. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3463. (save-excursion (funcall function))
  3464. (or (looking-at org-table-line-regexp)
  3465. (forward-char 1)))
  3466. (re-search-forward org-table-any-border-regexp nil 1))))
  3467. (unless quietly (message "Mapping tables: done")))
  3468. ;; Declare and autoload functions from org-exp.el & Co
  3469. (declare-function org-default-export-plist "org-exp")
  3470. (declare-function org-infile-export-plist "org-exp")
  3471. (declare-function org-get-current-options "org-exp")
  3472. ;; Declare and autoload functions from org-agenda.el
  3473. (eval-and-compile
  3474. (org-autoload "org-agenda"
  3475. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3476. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3477. (declare-function org-clock-update-mode-line "org-clock" ())
  3478. (declare-function org-resolve-clocks "org-clock"
  3479. (&optional also-non-dangling-p prompt last-valid))
  3480. (defvar org-clock-start-time)
  3481. (defvar org-clock-marker (make-marker)
  3482. "Marker recording the last clock-in.")
  3483. (defvar org-clock-hd-marker (make-marker)
  3484. "Marker recording the last clock-in, but the headline position.")
  3485. (defvar org-clock-heading ""
  3486. "The heading of the current clock entry.")
  3487. (defun org-clock-is-active ()
  3488. "Return non-nil if clock is currently running.
  3489. The return value is actually the clock marker."
  3490. (marker-buffer org-clock-marker))
  3491. (eval-and-compile
  3492. (org-autoload "org-clock" '(org-clock-remove-overlays
  3493. org-clock-update-time-maybe
  3494. org-clocktable-shift)))
  3495. (defun org-check-running-clock ()
  3496. "Check if the current buffer contains the running clock.
  3497. If yes, offer to stop it and to save the buffer with the changes."
  3498. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3499. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3500. (buffer-name))))
  3501. (org-clock-out)
  3502. (when (y-or-n-p "Save changed buffer?")
  3503. (save-buffer))))
  3504. (defun org-clocktable-try-shift (dir n)
  3505. "Check if this line starts a clock table, if yes, shift the time block."
  3506. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3507. (org-clocktable-shift dir n)))
  3508. ;;;###autoload
  3509. (defun org-clock-persistence-insinuate ()
  3510. "Set up hooks for clock persistence."
  3511. (require 'org-clock)
  3512. (add-hook 'org-mode-hook 'org-clock-load)
  3513. (add-hook 'kill-emacs-hook 'org-clock-save))
  3514. ;; Define the variable already here, to make sure we have it.
  3515. (defvar org-indent-mode nil
  3516. "Non-nil if Org-Indent mode is enabled.
  3517. Use the command `org-indent-mode' to change this variable.")
  3518. ;; Autoload archiving code
  3519. ;; The stuff that is needed for cycling and tags has to be defined here.
  3520. (defgroup org-archive nil
  3521. "Options concerning archiving in Org-mode."
  3522. :tag "Org Archive"
  3523. :group 'org-structure)
  3524. (defcustom org-archive-location "%s_archive::"
  3525. "The location where subtrees should be archived.
  3526. The value of this variable is a string, consisting of two parts,
  3527. separated by a double-colon. The first part is a filename and
  3528. the second part is a headline.
  3529. When the filename is omitted, archiving happens in the same file.
  3530. %s in the filename will be replaced by the current file
  3531. name (without the directory part). Archiving to a different file
  3532. is useful to keep archived entries from contributing to the
  3533. Org-mode Agenda.
  3534. The archived entries will be filed as subtrees of the specified
  3535. headline. When the headline is omitted, the subtrees are simply
  3536. filed away at the end of the file, as top-level entries. Also in
  3537. the heading you can use %s to represent the file name, this can be
  3538. useful when using the same archive for a number of different files.
  3539. Here are a few examples:
  3540. \"%s_archive::\"
  3541. If the current file is Projects.org, archive in file
  3542. Projects.org_archive, as top-level trees. This is the default.
  3543. \"::* Archived Tasks\"
  3544. Archive in the current file, under the top-level headline
  3545. \"* Archived Tasks\".
  3546. \"~/org/archive.org::\"
  3547. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3548. \"~/org/archive.org::* From %s\"
  3549. Archive in file ~/org/archive.org (absolute path), under headlines
  3550. \"From FILENAME\" where file name is the current file name.
  3551. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3552. The \"datetree/\" string is special, signifying to archive
  3553. items to the datetree. Items are placed in either the CLOSED
  3554. date of the item, or the current date if there is no CLOSED date.
  3555. The heading will be a subentry to the current date. There doesn't
  3556. need to be a heading, but there always needs to be a slash after
  3557. datetree. For example, to store archived items directly in the
  3558. datetree, use \"~/org/datetree.org::datetree/\".
  3559. \"basement::** Finished Tasks\"
  3560. Archive in file ./basement (relative path), as level 3 trees
  3561. below the level 2 heading \"** Finished Tasks\".
  3562. You may set this option on a per-file basis by adding to the buffer a
  3563. line like
  3564. #+ARCHIVE: basement::** Finished Tasks
  3565. You may also define it locally for a subtree by setting an ARCHIVE property
  3566. in the entry. If such a property is found in an entry, or anywhere up
  3567. the hierarchy, it will be used."
  3568. :group 'org-archive
  3569. :type 'string)
  3570. (defcustom org-archive-tag "ARCHIVE"
  3571. "The tag that marks a subtree as archived.
  3572. An archived subtree does not open during visibility cycling, and does
  3573. not contribute to the agenda listings.
  3574. After changing this, font-lock must be restarted in the relevant buffers to
  3575. get the proper fontification."
  3576. :group 'org-archive
  3577. :group 'org-keywords
  3578. :type 'string)
  3579. (defcustom org-agenda-skip-archived-trees t
  3580. "Non-nil means the agenda will skip any items located in archived trees.
  3581. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3582. variable is no longer recommended, you should leave it at the value t.
  3583. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3584. :group 'org-archive
  3585. :group 'org-agenda-skip
  3586. :type 'boolean)
  3587. (defcustom org-columns-skip-archived-trees t
  3588. "Non-nil means ignore archived trees when creating column view."
  3589. :group 'org-archive
  3590. :group 'org-properties
  3591. :type 'boolean)
  3592. (defcustom org-cycle-open-archived-trees nil
  3593. "Non-nil means `org-cycle' will open archived trees.
  3594. An archived tree is a tree marked with the tag ARCHIVE.
  3595. When nil, archived trees will stay folded. You can still open them with
  3596. normal outline commands like `show-all', but not with the cycling commands."
  3597. :group 'org-archive
  3598. :group 'org-cycle
  3599. :type 'boolean)
  3600. (defcustom org-sparse-tree-open-archived-trees nil
  3601. "Non-nil means sparse tree construction shows matches in archived trees.
  3602. When nil, matches in these trees are highlighted, but the trees are kept in
  3603. collapsed state."
  3604. :group 'org-archive
  3605. :group 'org-sparse-trees
  3606. :type 'boolean)
  3607. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3608. "The default date type when building a sparse tree.
  3609. When this is nil, a date is a scheduled or a deadline timestamp.
  3610. Otherwise, these types are allowed:
  3611. all: all timestamps
  3612. active: only active timestamps (<...>)
  3613. inactive: only inactive timestamps (<...)
  3614. scheduled: only scheduled timestamps
  3615. deadline: only deadline timestamps"
  3616. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3617. (const :tag "All timestamps" all)
  3618. (const :tag "Only active timestamps" active)
  3619. (const :tag "Only inactive timestamps" inactive)
  3620. (const :tag "Only scheduled timestamps" scheduled)
  3621. (const :tag "Only deadline timestamps" deadline))
  3622. :version "24.3"
  3623. :group 'org-sparse-trees)
  3624. (defun org-cycle-hide-archived-subtrees (state)
  3625. "Re-hide all archived subtrees after a visibility state change."
  3626. (when (and (not org-cycle-open-archived-trees)
  3627. (not (memq state '(overview folded))))
  3628. (save-excursion
  3629. (let* ((globalp (memq state '(contents all)))
  3630. (beg (if globalp (point-min) (point)))
  3631. (end (if globalp (point-max) (org-end-of-subtree t))))
  3632. (org-hide-archived-subtrees beg end)
  3633. (goto-char beg)
  3634. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3635. (message "%s" (substitute-command-keys
  3636. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3637. (defun org-force-cycle-archived ()
  3638. "Cycle subtree even if it is archived."
  3639. (interactive)
  3640. (setq this-command 'org-cycle)
  3641. (let ((org-cycle-open-archived-trees t))
  3642. (call-interactively 'org-cycle)))
  3643. (defun org-hide-archived-subtrees (beg end)
  3644. "Re-hide all archived subtrees after a visibility state change."
  3645. (save-excursion
  3646. (let* ((re (concat ":" org-archive-tag ":")))
  3647. (goto-char beg)
  3648. (while (re-search-forward re end t)
  3649. (when (org-at-heading-p)
  3650. (org-flag-subtree t)
  3651. (org-end-of-subtree t))))))
  3652. (declare-function outline-end-of-heading "outline" ())
  3653. (declare-function outline-flag-region "outline" (from to flag))
  3654. (defun org-flag-subtree (flag)
  3655. (save-excursion
  3656. (org-back-to-heading t)
  3657. (outline-end-of-heading)
  3658. (outline-flag-region (point)
  3659. (progn (org-end-of-subtree t) (point))
  3660. flag)))
  3661. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3662. (eval-and-compile
  3663. (org-autoload "org-archive"
  3664. '(org-add-archive-files)))
  3665. ;; Autoload Column View Code
  3666. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3667. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3668. (declare-function org-columns-compute "org-colview" (property))
  3669. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3670. '(org-columns-number-to-string
  3671. org-columns-get-format-and-top-level
  3672. org-columns-compute
  3673. org-columns-remove-overlays))
  3674. ;; Autoload ID code
  3675. (declare-function org-id-store-link "org-id")
  3676. (declare-function org-id-locations-load "org-id")
  3677. (declare-function org-id-locations-save "org-id")
  3678. (defvar org-id-track-globally)
  3679. (org-autoload "org-id"
  3680. '(org-id-new
  3681. org-id-copy
  3682. org-id-get-with-outline-path-completion
  3683. org-id-get-with-outline-drilling))
  3684. ;;; Variables for pre-computed regular expressions, all buffer local
  3685. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3686. "Matches first line of a hidden block.")
  3687. (make-variable-buffer-local 'org-drawer-regexp)
  3688. (defvar org-todo-regexp nil
  3689. "Matches any of the TODO state keywords.")
  3690. (make-variable-buffer-local 'org-todo-regexp)
  3691. (defvar org-not-done-regexp nil
  3692. "Matches any of the TODO state keywords except the last one.")
  3693. (make-variable-buffer-local 'org-not-done-regexp)
  3694. (defvar org-not-done-heading-regexp nil
  3695. "Matches a TODO headline that is not done.")
  3696. (make-variable-buffer-local 'org-not-done-regexp)
  3697. (defvar org-todo-line-regexp nil
  3698. "Matches a headline and puts TODO state into group 2 if present.")
  3699. (make-variable-buffer-local 'org-todo-line-regexp)
  3700. (defvar org-complex-heading-regexp nil
  3701. "Matches a headline and puts everything into groups:
  3702. group 1: the stars
  3703. group 2: The todo keyword, maybe
  3704. group 3: Priority cookie
  3705. group 4: True headline
  3706. group 5: Tags")
  3707. (make-variable-buffer-local 'org-complex-heading-regexp)
  3708. (defvar org-complex-heading-regexp-format nil
  3709. "Printf format to make regexp to match an exact headline.
  3710. This regexp will match the headline of any node which has the
  3711. exact headline text that is put into the format, but may have any
  3712. TODO state, priority and tags.")
  3713. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3714. (defvar org-todo-line-tags-regexp nil
  3715. "Matches a headline and puts TODO state into group 2 if present.
  3716. Also put tags into group 4 if tags are present.")
  3717. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3718. (defvar org-ds-keyword-length 12
  3719. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3720. (make-variable-buffer-local 'org-ds-keyword-length)
  3721. (defvar org-deadline-regexp nil
  3722. "Matches the DEADLINE keyword.")
  3723. (make-variable-buffer-local 'org-deadline-regexp)
  3724. (defvar org-deadline-time-regexp nil
  3725. "Matches the DEADLINE keyword together with a time stamp.")
  3726. (make-variable-buffer-local 'org-deadline-time-regexp)
  3727. (defvar org-deadline-line-regexp nil
  3728. "Matches the DEADLINE keyword and the rest of the line.")
  3729. (make-variable-buffer-local 'org-deadline-line-regexp)
  3730. (defvar org-scheduled-regexp nil
  3731. "Matches the SCHEDULED keyword.")
  3732. (make-variable-buffer-local 'org-scheduled-regexp)
  3733. (defvar org-scheduled-time-regexp nil
  3734. "Matches the SCHEDULED keyword together with a time stamp.")
  3735. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3736. (defvar org-closed-time-regexp nil
  3737. "Matches the CLOSED keyword together with a time stamp.")
  3738. (make-variable-buffer-local 'org-closed-time-regexp)
  3739. (defvar org-keyword-time-regexp nil
  3740. "Matches any of the 4 keywords, together with the time stamp.")
  3741. (make-variable-buffer-local 'org-keyword-time-regexp)
  3742. (defvar org-keyword-time-not-clock-regexp nil
  3743. "Matches any of the 3 keywords, together with the time stamp.")
  3744. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3745. (defvar org-maybe-keyword-time-regexp nil
  3746. "Matches a timestamp, possibly preceded by a keyword.")
  3747. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3748. (defvar org-all-time-keywords nil
  3749. "List of time keywords.")
  3750. (make-variable-buffer-local 'org-all-time-keywords)
  3751. (defconst org-plain-time-of-day-regexp
  3752. (concat
  3753. "\\(\\<[012]?[0-9]"
  3754. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3755. "\\(--?"
  3756. "\\(\\<[012]?[0-9]"
  3757. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3758. "\\)?")
  3759. "Regular expression to match a plain time or time range.
  3760. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3761. groups carry important information:
  3762. 0 the full match
  3763. 1 the first time, range or not
  3764. 8 the second time, if it is a range.")
  3765. (defconst org-plain-time-extension-regexp
  3766. (concat
  3767. "\\(\\<[012]?[0-9]"
  3768. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3769. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3770. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3771. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3772. groups carry important information:
  3773. 0 the full match
  3774. 7 hours of duration
  3775. 9 minutes of duration")
  3776. (defconst org-stamp-time-of-day-regexp
  3777. (concat
  3778. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3779. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3780. "\\(--?"
  3781. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3782. "Regular expression to match a timestamp time or time range.
  3783. After a match, the following groups carry important information:
  3784. 0 the full match
  3785. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3786. 2 the first time, range or not
  3787. 4 the second time, if it is a range.")
  3788. (defconst org-startup-options
  3789. '(("fold" org-startup-folded t)
  3790. ("overview" org-startup-folded t)
  3791. ("nofold" org-startup-folded nil)
  3792. ("showall" org-startup-folded nil)
  3793. ("showeverything" org-startup-folded showeverything)
  3794. ("content" org-startup-folded content)
  3795. ("indent" org-startup-indented t)
  3796. ("noindent" org-startup-indented nil)
  3797. ("hidestars" org-hide-leading-stars t)
  3798. ("showstars" org-hide-leading-stars nil)
  3799. ("odd" org-odd-levels-only t)
  3800. ("oddeven" org-odd-levels-only nil)
  3801. ("align" org-startup-align-all-tables t)
  3802. ("noalign" org-startup-align-all-tables nil)
  3803. ("inlineimages" org-startup-with-inline-images t)
  3804. ("noinlineimages" org-startup-with-inline-images nil)
  3805. ("customtime" org-display-custom-times t)
  3806. ("logdone" org-log-done time)
  3807. ("lognotedone" org-log-done note)
  3808. ("nologdone" org-log-done nil)
  3809. ("lognoteclock-out" org-log-note-clock-out t)
  3810. ("nolognoteclock-out" org-log-note-clock-out nil)
  3811. ("logrepeat" org-log-repeat state)
  3812. ("lognoterepeat" org-log-repeat note)
  3813. ("nologrepeat" org-log-repeat nil)
  3814. ("logreschedule" org-log-reschedule time)
  3815. ("lognotereschedule" org-log-reschedule note)
  3816. ("nologreschedule" org-log-reschedule nil)
  3817. ("logredeadline" org-log-redeadline time)
  3818. ("lognoteredeadline" org-log-redeadline note)
  3819. ("nologredeadline" org-log-redeadline nil)
  3820. ("logrefile" org-log-refile time)
  3821. ("lognoterefile" org-log-refile note)
  3822. ("nologrefile" org-log-refile nil)
  3823. ("fninline" org-footnote-define-inline t)
  3824. ("nofninline" org-footnote-define-inline nil)
  3825. ("fnlocal" org-footnote-section nil)
  3826. ("fnauto" org-footnote-auto-label t)
  3827. ("fnprompt" org-footnote-auto-label nil)
  3828. ("fnconfirm" org-footnote-auto-label confirm)
  3829. ("fnplain" org-footnote-auto-label plain)
  3830. ("fnadjust" org-footnote-auto-adjust t)
  3831. ("nofnadjust" org-footnote-auto-adjust nil)
  3832. ("constcgs" constants-unit-system cgs)
  3833. ("constSI" constants-unit-system SI)
  3834. ("noptag" org-tag-persistent-alist nil)
  3835. ("hideblocks" org-hide-block-startup t)
  3836. ("nohideblocks" org-hide-block-startup nil)
  3837. ("beamer" org-startup-with-beamer-mode t)
  3838. ("entitiespretty" org-pretty-entities t)
  3839. ("entitiesplain" org-pretty-entities nil))
  3840. "Variable associated with STARTUP options for org-mode.
  3841. Each element is a list of three items: the startup options (as written
  3842. in the #+STARTUP line), the corresponding variable, and the value to set
  3843. this variable to if the option is found. An optional forth element PUSH
  3844. means to push this value onto the list in the variable.")
  3845. (defun org-update-property-plist (key val props)
  3846. "Update PROPS with KEY and VAL."
  3847. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3848. (key (if appending (substring key 0 (- (length key) 1)) key))
  3849. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3850. (previous (cdr (assoc key props))))
  3851. (if appending
  3852. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3853. (cons (cons key val) remainder))))
  3854. (defconst org-block-regexp
  3855. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3856. "Regular expression for hiding blocks.")
  3857. (defconst org-heading-keyword-regexp-format
  3858. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3859. "Printf format for a regexp matching an headline with some keyword.
  3860. This regexp will match the headline of any node which has the
  3861. exact keyword that is put into the format. The keyword isn't in
  3862. any group by default, but the stars and the body are.")
  3863. (defconst org-heading-keyword-maybe-regexp-format
  3864. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3865. "Printf format for a regexp matching an headline, possibly with some keyword.
  3866. This regexp can match any headline with the specified keyword, or
  3867. without a keyword. The keyword isn't in any group by default,
  3868. but the stars and the body are.")
  3869. (defun org-set-regexps-and-options ()
  3870. "Precompute regular expressions for current buffer."
  3871. (when (derived-mode-p 'org-mode)
  3872. (org-set-local 'org-todo-kwd-alist nil)
  3873. (org-set-local 'org-todo-key-alist nil)
  3874. (org-set-local 'org-todo-key-trigger nil)
  3875. (org-set-local 'org-todo-keywords-1 nil)
  3876. (org-set-local 'org-done-keywords nil)
  3877. (org-set-local 'org-todo-heads nil)
  3878. (org-set-local 'org-todo-sets nil)
  3879. (org-set-local 'org-todo-log-states nil)
  3880. (org-set-local 'org-file-properties nil)
  3881. (org-set-local 'org-file-tags nil)
  3882. (let ((re (org-make-options-regexp
  3883. '("CATEGORY" "TODO" "COLUMNS"
  3884. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3885. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3886. "OPTIONS")
  3887. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3888. (splitre "[ \t]+")
  3889. (scripts org-use-sub-superscripts)
  3890. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3891. tail sep kws1 prio props ftags drawers beamer-p
  3892. ext-setup-or-nil setup-contents (start 0))
  3893. (save-excursion
  3894. (save-restriction
  3895. (widen)
  3896. (goto-char (point-min))
  3897. (while (or (and ext-setup-or-nil
  3898. (string-match re ext-setup-or-nil start)
  3899. (setq start (match-end 0)))
  3900. (and (setq ext-setup-or-nil nil start 0)
  3901. (re-search-forward re nil t)))
  3902. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3903. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3904. (if (stringp value) (setq value (org-trim value)))
  3905. (cond
  3906. ((equal key "CATEGORY")
  3907. (setq cat value))
  3908. ((member key '("SEQ_TODO" "TODO"))
  3909. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3910. ((equal key "TYP_TODO")
  3911. (push (cons 'type (org-split-string value splitre)) kwds))
  3912. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3913. ;; general TODO-like setup
  3914. (push (cons (intern (downcase (match-string 1 key)))
  3915. (org-split-string value splitre)) kwds))
  3916. ((equal key "TAGS")
  3917. (setq tags (append tags (if tags '("\\n") nil)
  3918. (org-split-string value splitre))))
  3919. ((equal key "COLUMNS")
  3920. (org-set-local 'org-columns-default-format value))
  3921. ((equal key "LINK")
  3922. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3923. (push (cons (match-string 1 value)
  3924. (org-trim (match-string 2 value)))
  3925. links)))
  3926. ((equal key "PRIORITIES")
  3927. (setq prio (org-split-string value " +")))
  3928. ((equal key "PROPERTY")
  3929. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3930. (setq props (org-update-property-plist (match-string 1 value)
  3931. (match-string 2 value)
  3932. props))))
  3933. ((equal key "FILETAGS")
  3934. (when (string-match "\\S-" value)
  3935. (setq ftags
  3936. (append
  3937. ftags
  3938. (apply 'append
  3939. (mapcar (lambda (x) (org-split-string x ":"))
  3940. (org-split-string value)))))))
  3941. ((equal key "DRAWERS")
  3942. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  3943. ((equal key "CONSTANTS")
  3944. (setq const (append const (org-split-string value splitre))))
  3945. ((equal key "STARTUP")
  3946. (let ((opts (org-split-string value splitre))
  3947. l var val)
  3948. (while (setq l (pop opts))
  3949. (when (setq l (assoc l org-startup-options))
  3950. (setq var (nth 1 l) val (nth 2 l))
  3951. (if (not (nth 3 l))
  3952. (set (make-local-variable var) val)
  3953. (if (not (listp (symbol-value var)))
  3954. (set (make-local-variable var) nil))
  3955. (set (make-local-variable var) (symbol-value var))
  3956. (add-to-list var val))))))
  3957. ((equal key "ARCHIVE")
  3958. (setq arch value)
  3959. (remove-text-properties 0 (length arch)
  3960. '(face t fontified t) arch))
  3961. ((equal key "LATEX_CLASS")
  3962. (setq beamer-p (equal value "beamer")))
  3963. ((equal key "OPTIONS")
  3964. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3965. (setq scripts (read (match-string 2 value)))))
  3966. ((equal key "SETUPFILE")
  3967. (setq setup-contents (org-file-contents
  3968. (expand-file-name
  3969. (org-remove-double-quotes value))
  3970. 'noerror))
  3971. (if (not ext-setup-or-nil)
  3972. (setq ext-setup-or-nil setup-contents start 0)
  3973. (setq ext-setup-or-nil
  3974. (concat (substring ext-setup-or-nil 0 start)
  3975. "\n" setup-contents "\n"
  3976. (substring ext-setup-or-nil start)))))))
  3977. ;; search for property blocks
  3978. (goto-char (point-min))
  3979. (while (re-search-forward org-block-regexp nil t)
  3980. (when (equal "PROPERTY" (upcase (match-string 1)))
  3981. (setq value (replace-regexp-in-string
  3982. "[\n\r]" " " (match-string 4)))
  3983. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3984. (setq props (org-update-property-plist (match-string 1 value)
  3985. (match-string 2 value)
  3986. props)))))))
  3987. (org-set-local 'org-use-sub-superscripts scripts)
  3988. (when cat
  3989. (org-set-local 'org-category (intern cat))
  3990. (push (cons "CATEGORY" cat) props))
  3991. (when prio
  3992. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3993. (setq prio (mapcar 'string-to-char prio))
  3994. (org-set-local 'org-highest-priority (nth 0 prio))
  3995. (org-set-local 'org-lowest-priority (nth 1 prio))
  3996. (org-set-local 'org-default-priority (nth 2 prio)))
  3997. (and props (org-set-local 'org-file-properties (nreverse props)))
  3998. (and ftags (org-set-local 'org-file-tags
  3999. (mapcar 'org-add-prop-inherited ftags)))
  4000. (and drawers (org-set-local 'org-drawers drawers))
  4001. (and arch (org-set-local 'org-archive-location arch))
  4002. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4003. ;; Process the TODO keywords
  4004. (unless kwds
  4005. ;; Use the global values as if they had been given locally.
  4006. (setq kwds (default-value 'org-todo-keywords))
  4007. (if (stringp (car kwds))
  4008. (setq kwds (list (cons org-todo-interpretation
  4009. (default-value 'org-todo-keywords)))))
  4010. (setq kwds (reverse kwds)))
  4011. (setq kwds (nreverse kwds))
  4012. (let (inter kws kw)
  4013. (while (setq kws (pop kwds))
  4014. (let ((kws (or
  4015. (run-hook-with-args-until-success
  4016. 'org-todo-setup-filter-hook kws)
  4017. kws)))
  4018. (setq inter (pop kws) sep (member "|" kws)
  4019. kws0 (delete "|" (copy-sequence kws))
  4020. kwsa nil
  4021. kws1 (mapcar
  4022. (lambda (x)
  4023. ;; 1 2
  4024. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4025. (progn
  4026. (setq kw (match-string 1 x)
  4027. key (and (match-end 2) (match-string 2 x))
  4028. log (org-extract-log-state-settings x))
  4029. (push (cons kw (and key (string-to-char key))) kwsa)
  4030. (and log (push log org-todo-log-states))
  4031. kw)
  4032. (error "Invalid TODO keyword %s" x)))
  4033. kws0)
  4034. kwsa (if kwsa (append '((:startgroup))
  4035. (nreverse kwsa)
  4036. '((:endgroup))))
  4037. hw (car kws1)
  4038. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4039. tail (list inter hw (car dws) (org-last dws))))
  4040. (add-to-list 'org-todo-heads hw 'append)
  4041. (push kws1 org-todo-sets)
  4042. (setq org-done-keywords (append org-done-keywords dws nil))
  4043. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4044. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4045. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4046. (setq org-todo-sets (nreverse org-todo-sets)
  4047. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4048. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4049. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4050. ;; Process the constants
  4051. (when const
  4052. (let (e cst)
  4053. (while (setq e (pop const))
  4054. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4055. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4056. (setq org-table-formula-constants-local cst)))
  4057. ;; Process the tags.
  4058. (when tags
  4059. (let (e tgs)
  4060. (while (setq e (pop tags))
  4061. (cond
  4062. ((equal e "{") (push '(:startgroup) tgs))
  4063. ((equal e "}") (push '(:endgroup) tgs))
  4064. ((equal e "\\n") (push '(:newline) tgs))
  4065. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4066. (push (cons (match-string 1 e)
  4067. (string-to-char (match-string 2 e)))
  4068. tgs))
  4069. (t (push (list e) tgs))))
  4070. (org-set-local 'org-tag-alist nil)
  4071. (while (setq e (pop tgs))
  4072. (or (and (stringp (car e))
  4073. (assoc (car e) org-tag-alist))
  4074. (push e org-tag-alist)))))
  4075. ;; Compute the regular expressions and other local variables.
  4076. ;; Using `org-outline-regexp-bol' would complicate them much,
  4077. ;; because of the fixed white space at the end of that string.
  4078. (if (not org-done-keywords)
  4079. (setq org-done-keywords (and org-todo-keywords-1
  4080. (list (org-last org-todo-keywords-1)))))
  4081. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4082. (length org-scheduled-string)
  4083. (length org-clock-string)
  4084. (length org-closed-string)))
  4085. org-drawer-regexp
  4086. (concat "^[ \t]*:\\("
  4087. (mapconcat 'regexp-quote org-drawers "\\|")
  4088. "\\):[ \t]*$")
  4089. org-not-done-keywords
  4090. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4091. org-todo-regexp
  4092. (concat "\\("
  4093. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4094. "\\)")
  4095. org-not-done-regexp
  4096. (concat "\\("
  4097. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4098. "\\)")
  4099. org-not-done-heading-regexp
  4100. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4101. org-todo-line-regexp
  4102. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4103. org-complex-heading-regexp
  4104. (concat "^\\(\\*+\\)"
  4105. "\\(?: +" org-todo-regexp "\\)?"
  4106. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4107. "\\(?: +\\(.*?\\)\\)??"
  4108. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4109. "[ \t]*$")
  4110. org-complex-heading-regexp-format
  4111. (concat "^\\(\\*+\\)"
  4112. "\\(?: +" org-todo-regexp "\\)?"
  4113. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4114. "\\(?: +"
  4115. ;; Stats cookies can be stuck to body.
  4116. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4117. "\\(%s\\)"
  4118. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4119. "\\)"
  4120. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4121. "[ \t]*$")
  4122. org-todo-line-tags-regexp
  4123. (concat "^\\(\\*+\\)"
  4124. "\\(?: +" org-todo-regexp "\\)?"
  4125. "\\(?: +\\(.*?\\)\\)??"
  4126. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4127. "[ \t]*$")
  4128. org-deadline-regexp (concat "\\<" org-deadline-string)
  4129. org-deadline-time-regexp
  4130. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4131. org-deadline-line-regexp
  4132. (concat "\\<\\(" org-deadline-string "\\).*")
  4133. org-scheduled-regexp
  4134. (concat "\\<" org-scheduled-string)
  4135. org-scheduled-time-regexp
  4136. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4137. org-closed-time-regexp
  4138. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4139. org-keyword-time-regexp
  4140. (concat "\\<\\(" org-scheduled-string
  4141. "\\|" org-deadline-string
  4142. "\\|" org-closed-string
  4143. "\\|" org-clock-string "\\)"
  4144. " *[[<]\\([^]>]+\\)[]>]")
  4145. org-keyword-time-not-clock-regexp
  4146. (concat "\\<\\(" org-scheduled-string
  4147. "\\|" org-deadline-string
  4148. "\\|" org-closed-string
  4149. "\\)"
  4150. " *[[<]\\([^]>]+\\)[]>]")
  4151. org-maybe-keyword-time-regexp
  4152. (concat "\\(\\<\\(" org-scheduled-string
  4153. "\\|" org-deadline-string
  4154. "\\|" org-closed-string
  4155. "\\|" org-clock-string "\\)\\)?"
  4156. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4157. org-all-time-keywords
  4158. (mapcar (lambda (w) (substring w 0 -1))
  4159. (list org-scheduled-string org-deadline-string
  4160. org-clock-string org-closed-string))
  4161. )
  4162. (org-compute-latex-and-specials-regexp)
  4163. (org-set-font-lock-defaults))))
  4164. (defun org-file-contents (file &optional noerror)
  4165. "Return the contents of FILE, as a string."
  4166. (if (or (not file)
  4167. (not (file-readable-p file)))
  4168. (if noerror
  4169. (progn
  4170. (message "Cannot read file \"%s\"" file)
  4171. (ding) (sit-for 2)
  4172. "")
  4173. (error "Cannot read file \"%s\"" file))
  4174. (with-temp-buffer
  4175. (insert-file-contents file)
  4176. (buffer-string))))
  4177. (defun org-extract-log-state-settings (x)
  4178. "Extract the log state setting from a TODO keyword string.
  4179. This will extract info from a string like \"WAIT(w@/!)\"."
  4180. (let (kw key log1 log2)
  4181. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4182. (setq kw (match-string 1 x)
  4183. key (and (match-end 2) (match-string 2 x))
  4184. log1 (and (match-end 3) (match-string 3 x))
  4185. log2 (and (match-end 4) (match-string 4 x)))
  4186. (and (or log1 log2)
  4187. (list kw
  4188. (and log1 (if (equal log1 "!") 'time 'note))
  4189. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4190. (defun org-remove-keyword-keys (list)
  4191. "Remove a pair of parenthesis at the end of each string in LIST."
  4192. (mapcar (lambda (x)
  4193. (if (string-match "(.*)$" x)
  4194. (substring x 0 (match-beginning 0))
  4195. x))
  4196. list))
  4197. (defun org-assign-fast-keys (alist)
  4198. "Assign fast keys to a keyword-key alist.
  4199. Respect keys that are already there."
  4200. (let (new e (alt ?0))
  4201. (while (setq e (pop alist))
  4202. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4203. (cdr e)) ;; Key already assigned.
  4204. (push e new)
  4205. (let ((clist (string-to-list (downcase (car e))))
  4206. (used (append new alist)))
  4207. (when (= (car clist) ?@)
  4208. (pop clist))
  4209. (while (and clist (rassoc (car clist) used))
  4210. (pop clist))
  4211. (unless clist
  4212. (while (rassoc alt used)
  4213. (incf alt)))
  4214. (push (cons (car e) (or (car clist) alt)) new))))
  4215. (nreverse new)))
  4216. ;;; Some variables used in various places
  4217. (defvar org-window-configuration nil
  4218. "Used in various places to store a window configuration.")
  4219. (defvar org-selected-window nil
  4220. "Used in various places to store a window configuration.")
  4221. (defvar org-finish-function nil
  4222. "Function to be called when `C-c C-c' is used.
  4223. This is for getting out of special buffers like capture.")
  4224. ;; FIXME: Occasionally check by commenting these, to make sure
  4225. ;; no other functions uses these, forgetting to let-bind them.
  4226. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4227. (defvar org-last-state)
  4228. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4229. ;; Defined somewhere in this file, but used before definition.
  4230. (defvar org-entities) ;; defined in org-entities.el
  4231. (defvar org-struct-menu)
  4232. (defvar org-org-menu)
  4233. (defvar org-tbl-menu)
  4234. ;;;; Define the Org-mode
  4235. ;; We use a before-change function to check if a table might need
  4236. ;; an update.
  4237. (defvar org-table-may-need-update t
  4238. "Indicates that a table might need an update.
  4239. This variable is set by `org-before-change-function'.
  4240. `org-table-align' sets it back to nil.")
  4241. (defun org-before-change-function (beg end)
  4242. "Every change indicates that a table might need an update."
  4243. (setq org-table-may-need-update t))
  4244. (defvar org-mode-map)
  4245. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4246. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4247. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4248. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4249. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4250. (defvar org-table-buffer-is-an nil)
  4251. (defvar bidi-paragraph-direction)
  4252. (defvar buffer-face-mode-face)
  4253. (require 'outline)
  4254. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4255. (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"))
  4256. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4257. ;; Other stuff we need.
  4258. (require 'time-date)
  4259. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4260. (require 'easymenu)
  4261. (require 'overlay)
  4262. (require 'org-macs)
  4263. (require 'org-entities)
  4264. ;; (require 'org-compat) moved higher up in the file before it is first used
  4265. (require 'org-faces)
  4266. (require 'org-list)
  4267. (require 'org-pcomplete)
  4268. (require 'org-src)
  4269. (require 'org-footnote)
  4270. ;; babel
  4271. (require 'ob)
  4272. (require 'ob-table)
  4273. (require 'ob-lob)
  4274. (require 'ob-ref)
  4275. (require 'ob-tangle)
  4276. (require 'ob-comint)
  4277. (require 'ob-keys)
  4278. ;;;###autoload
  4279. (define-derived-mode org-mode outline-mode "Org"
  4280. "Outline-based notes management and organizer, alias
  4281. \"Carsten's outline-mode for keeping track of everything.\"
  4282. Org-mode develops organizational tasks around a NOTES file which
  4283. contains information about projects as plain text. Org-mode is
  4284. implemented on top of outline-mode, which is ideal to keep the content
  4285. of large files well structured. It supports ToDo items, deadlines and
  4286. time stamps, which magically appear in the diary listing of the Emacs
  4287. calendar. Tables are easily created with a built-in table editor.
  4288. Plain text URL-like links connect to websites, emails (VM), Usenet
  4289. messages (Gnus), BBDB entries, and any files related to the project.
  4290. For printing and sharing of notes, an Org-mode file (or a part of it)
  4291. can be exported as a structured ASCII or HTML file.
  4292. The following commands are available:
  4293. \\{org-mode-map}"
  4294. ;; Get rid of Outline menus, they are not needed
  4295. ;; Need to do this here because define-derived-mode sets up
  4296. ;; the keymap so late. Still, it is a waste to call this each time
  4297. ;; we switch another buffer into org-mode.
  4298. (if (featurep 'xemacs)
  4299. (when (boundp 'outline-mode-menu-heading)
  4300. ;; Assume this is Greg's port, it uses easymenu
  4301. (easy-menu-remove outline-mode-menu-heading)
  4302. (easy-menu-remove outline-mode-menu-show)
  4303. (easy-menu-remove outline-mode-menu-hide))
  4304. (define-key org-mode-map [menu-bar headings] 'undefined)
  4305. (define-key org-mode-map [menu-bar hide] 'undefined)
  4306. (define-key org-mode-map [menu-bar show] 'undefined))
  4307. (org-load-modules-maybe)
  4308. (easy-menu-add org-org-menu)
  4309. (easy-menu-add org-tbl-menu)
  4310. (org-install-agenda-files-menu)
  4311. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4312. (add-to-invisibility-spec '(org-cwidth))
  4313. (add-to-invisibility-spec '(org-hide-block . t))
  4314. (when (featurep 'xemacs)
  4315. (org-set-local 'line-move-ignore-invisible t))
  4316. (org-set-local 'outline-regexp org-outline-regexp)
  4317. (org-set-local 'outline-level 'org-outline-level)
  4318. (setq bidi-paragraph-direction 'left-to-right)
  4319. (when (and org-ellipsis
  4320. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4321. (fboundp 'make-glyph-code))
  4322. (unless org-display-table
  4323. (setq org-display-table (make-display-table)))
  4324. (set-display-table-slot
  4325. org-display-table 4
  4326. (vconcat (mapcar
  4327. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4328. org-ellipsis)))
  4329. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4330. (setq buffer-display-table org-display-table))
  4331. (org-set-regexps-and-options)
  4332. (when (and org-tag-faces (not org-tags-special-faces-re))
  4333. ;; tag faces set outside customize.... force initialization.
  4334. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4335. ;; Calc embedded
  4336. (org-set-local 'calc-embedded-open-mode "# ")
  4337. (modify-syntax-entry ?@ "w")
  4338. (if org-startup-truncated (setq truncate-lines t))
  4339. (org-set-local 'font-lock-unfontify-region-function
  4340. 'org-unfontify-region)
  4341. ;; Activate before-change-function
  4342. (org-set-local 'org-table-may-need-update t)
  4343. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4344. 'local)
  4345. ;; Check for running clock before killing a buffer
  4346. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4347. ;; Indentation.
  4348. (org-set-local 'indent-line-function 'org-indent-line)
  4349. (org-set-local 'indent-region-function 'org-indent-region)
  4350. ;; Initialize radio targets.
  4351. (org-update-radio-target-regexp)
  4352. ;; Filling and auto-filling.
  4353. (org-setup-filling)
  4354. ;; Comments.
  4355. (org-setup-comments-handling)
  4356. ;; Beginning/end of defun
  4357. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4358. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4359. ;; Next error for sparse trees
  4360. (org-set-local 'next-error-function 'org-occur-next-match)
  4361. ;; Make sure dependence stuff works reliably, even for users who set it
  4362. ;; too late :-(
  4363. (if org-enforce-todo-dependencies
  4364. (add-hook 'org-blocker-hook
  4365. 'org-block-todo-from-children-or-siblings-or-parent)
  4366. (remove-hook 'org-blocker-hook
  4367. 'org-block-todo-from-children-or-siblings-or-parent))
  4368. (if org-enforce-todo-checkbox-dependencies
  4369. (add-hook 'org-blocker-hook
  4370. 'org-block-todo-from-checkboxes)
  4371. (remove-hook 'org-blocker-hook
  4372. 'org-block-todo-from-checkboxes))
  4373. ;; Align options lines
  4374. (org-set-local
  4375. 'align-mode-rules-list
  4376. '((org-in-buffer-settings
  4377. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4378. (modes . '(org-mode)))))
  4379. ;; Imenu
  4380. (org-set-local 'imenu-create-index-function
  4381. 'org-imenu-get-tree)
  4382. ;; Make isearch reveal context
  4383. (if (or (featurep 'xemacs)
  4384. (not (boundp 'outline-isearch-open-invisible-function)))
  4385. ;; Emacs 21 and XEmacs make use of the hook
  4386. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4387. ;; Emacs 22 deals with this through a special variable
  4388. (org-set-local 'outline-isearch-open-invisible-function
  4389. (lambda (&rest ignore) (org-show-context 'isearch))))
  4390. ;; Turn on org-beamer-mode?
  4391. (and org-startup-with-beamer-mode (org-beamer-mode))
  4392. ;; Setup the pcomplete hooks
  4393. (set (make-local-variable 'pcomplete-command-completion-function)
  4394. 'org-pcomplete-initial)
  4395. (set (make-local-variable 'pcomplete-command-name-function)
  4396. 'org-command-at-point)
  4397. (set (make-local-variable 'pcomplete-default-completion-function)
  4398. 'ignore)
  4399. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4400. 'org-parse-arguments)
  4401. (set (make-local-variable 'pcomplete-termination-string) "")
  4402. (when (>= emacs-major-version 23)
  4403. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4404. ;; If empty file that did not turn on org-mode automatically, make it to.
  4405. (if (and org-insert-mode-line-in-empty-file
  4406. (org-called-interactively-p 'any)
  4407. (= (point-min) (point-max)))
  4408. (insert "# -*- mode: org -*-\n\n"))
  4409. (unless org-inhibit-startup
  4410. (when org-startup-align-all-tables
  4411. (let ((bmp (buffer-modified-p)))
  4412. (org-table-map-tables 'org-table-align 'quietly)
  4413. (set-buffer-modified-p bmp)))
  4414. (when org-startup-with-inline-images
  4415. (org-display-inline-images))
  4416. (when org-startup-indented
  4417. (require 'org-indent)
  4418. (org-indent-mode 1))
  4419. (unless org-inhibit-startup-visibility-stuff
  4420. (org-set-startup-visibility)))
  4421. ;; Try to set org-hide correctly
  4422. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4423. (when (fboundp 'abbrev-table-put)
  4424. (abbrev-table-put org-mode-abbrev-table
  4425. :parents (list text-mode-abbrev-table)))
  4426. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4427. (defun org-find-invisible-foreground ()
  4428. (let ((candidates (remove
  4429. "unspecified-bg"
  4430. (nconc
  4431. (list (face-background 'default)
  4432. (face-background 'org-default))
  4433. (mapcar
  4434. (lambda (alist)
  4435. (when (boundp alist)
  4436. (cdr (assoc 'background-color (symbol-value alist)))))
  4437. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4438. (list (face-foreground 'org-hide))))))
  4439. (car (remove nil candidates))))
  4440. (defun org-current-time ()
  4441. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4442. (if (> (car org-time-stamp-rounding-minutes) 1)
  4443. (let ((r (car org-time-stamp-rounding-minutes))
  4444. (time (decode-time)))
  4445. (apply 'encode-time
  4446. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4447. (nthcdr 2 time))))
  4448. (current-time)))
  4449. (defun org-today ()
  4450. "Return today date, considering `org-extend-today-until'."
  4451. (time-to-days
  4452. (time-subtract (current-time)
  4453. (list 0 (* 3600 org-extend-today-until) 0))))
  4454. ;;;; Font-Lock stuff, including the activators
  4455. (defvar org-mouse-map (make-sparse-keymap))
  4456. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4457. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4458. (when org-mouse-1-follows-link
  4459. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4460. (when org-tab-follows-link
  4461. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4462. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4463. (require 'font-lock)
  4464. (defconst org-non-link-chars "]\t\n\r<>")
  4465. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4466. "shell" "elisp" "doi" "message"))
  4467. (defvar org-link-types-re nil
  4468. "Matches a link that has a url-like prefix like \"http:\"")
  4469. (defvar org-link-re-with-space nil
  4470. "Matches a link with spaces, optional angular brackets around it.")
  4471. (defvar org-link-re-with-space2 nil
  4472. "Matches a link with spaces, optional angular brackets around it.")
  4473. (defvar org-link-re-with-space3 nil
  4474. "Matches a link with spaces, only for internal part in bracket links.")
  4475. (defvar org-angle-link-re nil
  4476. "Matches link with angular brackets, spaces are allowed.")
  4477. (defvar org-plain-link-re nil
  4478. "Matches plain link, without spaces.")
  4479. (defvar org-bracket-link-regexp nil
  4480. "Matches a link in double brackets.")
  4481. (defvar org-bracket-link-analytic-regexp nil
  4482. "Regular expression used to analyze links.
  4483. Here is what the match groups contain after a match:
  4484. 1: http:
  4485. 2: http
  4486. 3: path
  4487. 4: [desc]
  4488. 5: desc")
  4489. (defvar org-bracket-link-analytic-regexp++ nil
  4490. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4491. (defvar org-any-link-re nil
  4492. "Regular expression matching any link.")
  4493. (defcustom org-match-sexp-depth 3
  4494. "Number of stacked braces for sub/superscript matching.
  4495. This has to be set before loading org.el to be effective."
  4496. :group 'org-export-translation ; ??????????????????????????/
  4497. :type 'integer)
  4498. (defun org-create-multibrace-regexp (left right n)
  4499. "Create a regular expression which will match a balanced sexp.
  4500. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4501. as single character strings.
  4502. The regexp returned will match the entire expression including the
  4503. delimiters. It will also define a single group which contains the
  4504. match except for the outermost delimiters. The maximum depth of
  4505. stacked delimiters is N. Escaping delimiters is not possible."
  4506. (let* ((nothing (concat "[^" left right "]*?"))
  4507. (or "\\|")
  4508. (re nothing)
  4509. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4510. (while (> n 1)
  4511. (setq n (1- n)
  4512. re (concat re or next)
  4513. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4514. (concat left "\\(" re "\\)" right)))
  4515. (defvar org-match-substring-regexp
  4516. (concat
  4517. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4518. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4519. "\\|"
  4520. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4521. "\\|"
  4522. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4523. "The regular expression matching a sub- or superscript.")
  4524. (defvar org-match-substring-with-braces-regexp
  4525. (concat
  4526. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4527. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4528. "\\)")
  4529. "The regular expression matching a sub- or superscript, forcing braces.")
  4530. (defun org-make-link-regexps ()
  4531. "Update the link regular expressions.
  4532. This should be called after the variable `org-link-types' has changed."
  4533. (setq org-link-types-re
  4534. (concat
  4535. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4536. org-link-re-with-space
  4537. (concat
  4538. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4539. "\\([^" org-non-link-chars " ]"
  4540. "[^" org-non-link-chars "]*"
  4541. "[^" org-non-link-chars " ]\\)>?")
  4542. org-link-re-with-space2
  4543. (concat
  4544. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4545. "\\([^" org-non-link-chars " ]"
  4546. "[^\t\n\r]*"
  4547. "[^" org-non-link-chars " ]\\)>?")
  4548. org-link-re-with-space3
  4549. (concat
  4550. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4551. "\\([^" org-non-link-chars " ]"
  4552. "[^\t\n\r]*\\)")
  4553. org-angle-link-re
  4554. (concat
  4555. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4556. "\\([^" org-non-link-chars " ]"
  4557. "[^" org-non-link-chars "]*"
  4558. "\\)>")
  4559. org-plain-link-re
  4560. (concat
  4561. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4562. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4563. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4564. org-bracket-link-regexp
  4565. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4566. org-bracket-link-analytic-regexp
  4567. (concat
  4568. "\\[\\["
  4569. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4570. "\\([^]]+\\)"
  4571. "\\]"
  4572. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4573. "\\]")
  4574. org-bracket-link-analytic-regexp++
  4575. (concat
  4576. "\\[\\["
  4577. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4578. "\\([^]]+\\)"
  4579. "\\]"
  4580. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4581. "\\]")
  4582. org-any-link-re
  4583. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4584. org-angle-link-re "\\)\\|\\("
  4585. org-plain-link-re "\\)")))
  4586. (org-make-link-regexps)
  4587. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4588. "Regular expression for fast time stamp matching.")
  4589. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4590. "Regular expression for fast time stamp matching.")
  4591. (defconst org-ts-regexp0
  4592. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4593. "Regular expression matching time strings for analysis.
  4594. This one does not require the space after the date, so it can be used
  4595. on a string that terminates immediately after the date.")
  4596. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4597. "Regular expression matching time strings for analysis.")
  4598. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4599. "Regular expression matching time stamps, with groups.")
  4600. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4601. "Regular expression matching time stamps (also [..]), with groups.")
  4602. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4603. "Regular expression matching a time stamp range.")
  4604. (defconst org-tr-regexp-both
  4605. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4606. "Regular expression matching a time stamp range.")
  4607. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4608. org-ts-regexp "\\)?")
  4609. "Regular expression matching a time stamp or time stamp range.")
  4610. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4611. org-ts-regexp-both "\\)?")
  4612. "Regular expression matching a time stamp or time stamp range.
  4613. The time stamps may be either active or inactive.")
  4614. (defvar org-emph-face nil)
  4615. (defun org-do-emphasis-faces (limit)
  4616. "Run through the buffer and add overlays to emphasized strings."
  4617. (let (rtn a)
  4618. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4619. (if (not (= (char-after (match-beginning 3))
  4620. (char-after (match-beginning 4))))
  4621. (progn
  4622. (setq rtn t)
  4623. (setq a (assoc (match-string 3) org-emphasis-alist))
  4624. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4625. 'face
  4626. (nth 1 a))
  4627. (and (nth 4 a)
  4628. (org-remove-flyspell-overlays-in
  4629. (match-beginning 0) (match-end 0)))
  4630. (add-text-properties (match-beginning 2) (match-end 2)
  4631. '(font-lock-multiline t org-emphasis t))
  4632. (when org-hide-emphasis-markers
  4633. (add-text-properties (match-end 4) (match-beginning 5)
  4634. '(invisible org-link))
  4635. (add-text-properties (match-beginning 3) (match-end 3)
  4636. '(invisible org-link)))))
  4637. (backward-char 1))
  4638. rtn))
  4639. (defun org-emphasize (&optional char)
  4640. "Insert or change an emphasis, i.e. a font like bold or italic.
  4641. If there is an active region, change that region to a new emphasis.
  4642. If there is no region, just insert the marker characters and position
  4643. the cursor between them.
  4644. CHAR should be either the marker character, or the first character of the
  4645. HTML tag associated with that emphasis. If CHAR is a space, the means
  4646. to remove the emphasis of the selected region.
  4647. If char is not given (for example in an interactive call) it
  4648. will be prompted for."
  4649. (interactive)
  4650. (let ((eal org-emphasis-alist) e det
  4651. (erc org-emphasis-regexp-components)
  4652. (prompt "")
  4653. (string "") beg end move tag c s)
  4654. (if (org-region-active-p)
  4655. (setq beg (region-beginning) end (region-end)
  4656. string (buffer-substring beg end))
  4657. (setq move t))
  4658. (while (setq e (pop eal))
  4659. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4660. c (aref tag 0))
  4661. (push (cons c (string-to-char (car e))) det)
  4662. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4663. (substring tag 1)))))
  4664. (setq det (nreverse det))
  4665. (unless char
  4666. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4667. (setq char (read-char-exclusive)))
  4668. (setq char (or (cdr (assoc char det)) char))
  4669. (if (equal char ?\ )
  4670. (setq s "" move nil)
  4671. (unless (assoc (char-to-string char) org-emphasis-alist)
  4672. (error "No such emphasis marker: \"%c\"" char))
  4673. (setq s (char-to-string char)))
  4674. (while (and (> (length string) 1)
  4675. (equal (substring string 0 1) (substring string -1))
  4676. (assoc (substring string 0 1) org-emphasis-alist))
  4677. (setq string (substring string 1 -1)))
  4678. (setq string (concat s string s))
  4679. (if beg (delete-region beg end))
  4680. (unless (or (bolp)
  4681. (string-match (concat "[" (nth 0 erc) "\n]")
  4682. (char-to-string (char-before (point)))))
  4683. (insert " "))
  4684. (unless (or (eobp)
  4685. (string-match (concat "[" (nth 1 erc) "\n]")
  4686. (char-to-string (char-after (point)))))
  4687. (insert " ") (backward-char 1))
  4688. (insert string)
  4689. (and move (backward-char 1))))
  4690. (defconst org-nonsticky-props
  4691. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4692. (defsubst org-rear-nonsticky-at (pos)
  4693. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4694. (defun org-activate-plain-links (limit)
  4695. "Run through the buffer and add overlays to links."
  4696. (catch 'exit
  4697. (let (f)
  4698. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4699. (not (org-in-src-block-p)))
  4700. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4701. (setq f (get-text-property (match-beginning 0) 'face))
  4702. (unless (or (org-in-src-block-p)
  4703. (eq f 'org-tag)
  4704. (and (listp f) (memq 'org-tag f)))
  4705. (add-text-properties (match-beginning 0) (match-end 0)
  4706. (list 'mouse-face 'highlight
  4707. 'face 'org-link
  4708. 'keymap org-mouse-map))
  4709. (org-rear-nonsticky-at (match-end 0)))
  4710. t))))
  4711. (defun org-activate-code (limit)
  4712. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4713. (progn
  4714. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4715. (remove-text-properties (match-beginning 0) (match-end 0)
  4716. '(display t invisible t intangible t))
  4717. t)))
  4718. (defcustom org-src-fontify-natively nil
  4719. "When non-nil, fontify code in code blocks."
  4720. :type 'boolean
  4721. :version "24.1"
  4722. :group 'org-appearance
  4723. :group 'org-babel)
  4724. (defcustom org-allow-promoting-top-level-subtree nil
  4725. "When non-nil, allow promoting a top level subtree.
  4726. The leading star of the top level headline will be replaced
  4727. by a #."
  4728. :type 'boolean
  4729. :version "24.1"
  4730. :group 'org-appearance)
  4731. (defun org-fontify-meta-lines-and-blocks (limit)
  4732. (condition-case nil
  4733. (org-fontify-meta-lines-and-blocks-1 limit)
  4734. (error (message "org-mode fontification error"))))
  4735. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4736. "Fontify #+ lines and blocks, in the correct ways."
  4737. (let ((case-fold-search t))
  4738. (if (re-search-forward
  4739. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4740. limit t)
  4741. (let ((beg (match-beginning 0))
  4742. (block-start (match-end 0))
  4743. (block-end nil)
  4744. (lang (match-string 7))
  4745. (beg1 (line-beginning-position 2))
  4746. (dc1 (downcase (match-string 2)))
  4747. (dc3 (downcase (match-string 3)))
  4748. end end1 quoting block-type ovl)
  4749. (cond
  4750. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4751. ;; a single line of backend-specific content
  4752. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4753. (remove-text-properties (match-beginning 0) (match-end 0)
  4754. '(display t invisible t intangible t))
  4755. (add-text-properties (match-beginning 1) (match-end 3)
  4756. '(font-lock-fontified t face org-meta-line))
  4757. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4758. '(font-lock-fontified t face org-block))
  4759. ; for backend-specific code
  4760. t)
  4761. ((and (match-end 4) (equal dc3 "+begin"))
  4762. ;; Truly a block
  4763. (setq block-type (downcase (match-string 5))
  4764. quoting (member block-type org-protecting-blocks))
  4765. (when (re-search-forward
  4766. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4767. nil t) ;; on purpose, we look further than LIMIT
  4768. (setq end (min (point-max) (match-end 0))
  4769. end1 (min (point-max) (1- (match-beginning 0))))
  4770. (setq block-end (match-beginning 0))
  4771. (when quoting
  4772. (remove-text-properties beg end
  4773. '(display t invisible t intangible t)))
  4774. (add-text-properties
  4775. beg end
  4776. '(font-lock-fontified t font-lock-multiline t))
  4777. (add-text-properties beg beg1 '(face org-meta-line))
  4778. (add-text-properties end1 (min (point-max) (1+ end))
  4779. '(face org-meta-line)) ; for end_src
  4780. (cond
  4781. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4782. (org-src-font-lock-fontify-block lang block-start block-end)
  4783. ;; remove old background overlays
  4784. (mapc (lambda (ov)
  4785. (if (eq (overlay-get ov 'face) 'org-block-background)
  4786. (delete-overlay ov)))
  4787. (overlays-at (/ (+ beg1 block-end) 2)))
  4788. ;; add a background overlay
  4789. (setq ovl (make-overlay beg1 block-end))
  4790. (overlay-put ovl 'face 'org-block-background)
  4791. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4792. (quoting
  4793. (add-text-properties beg1 (min (point-max) (1+ end1))
  4794. '(face org-block))) ; end of source block
  4795. ((not org-fontify-quote-and-verse-blocks))
  4796. ((string= block-type "quote")
  4797. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4798. ((string= block-type "verse")
  4799. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4800. (add-text-properties beg beg1 '(face org-block-begin-line))
  4801. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4802. '(face org-block-end-line))
  4803. t))
  4804. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4805. (add-text-properties
  4806. beg (match-end 3)
  4807. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4808. '(font-lock-fontified t invisible t)
  4809. '(font-lock-fontified t face org-document-info-keyword)))
  4810. (add-text-properties
  4811. (match-beginning 6) (1+ (match-end 6))
  4812. (if (string-equal dc1 "+title:")
  4813. '(font-lock-fontified t face org-document-title)
  4814. '(font-lock-fontified t face org-document-info))))
  4815. ((or (equal dc1 "+results")
  4816. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4817. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4818. "+call:" "+header:" "+headers:" "+name:"))
  4819. (and (match-end 4) (equal dc3 "+attr")))
  4820. (add-text-properties
  4821. beg (match-end 0)
  4822. '(font-lock-fontified t face org-meta-line))
  4823. t)
  4824. ((member dc3 '(" " ""))
  4825. (add-text-properties
  4826. beg (match-end 0)
  4827. '(font-lock-fontified t face font-lock-comment-face)))
  4828. ((not (member (char-after beg) '(?\ ?\t)))
  4829. ;; just any other in-buffer setting, but not indented
  4830. (add-text-properties
  4831. beg (match-end 0)
  4832. '(font-lock-fontified t face org-meta-line))
  4833. t)
  4834. (t nil))))))
  4835. (defun org-activate-angle-links (limit)
  4836. "Run through the buffer and add overlays to links."
  4837. (if (and (re-search-forward org-angle-link-re limit t)
  4838. (not (org-in-src-block-p)))
  4839. (progn
  4840. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4841. (add-text-properties (match-beginning 0) (match-end 0)
  4842. (list 'mouse-face 'highlight
  4843. 'keymap org-mouse-map))
  4844. (org-rear-nonsticky-at (match-end 0))
  4845. t)))
  4846. (defun org-activate-footnote-links (limit)
  4847. "Run through the buffer and add overlays to footnotes."
  4848. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4849. (when fn
  4850. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4851. (org-remove-flyspell-overlays-in beg end)
  4852. (add-text-properties beg end
  4853. (list 'mouse-face 'highlight
  4854. 'keymap org-mouse-map
  4855. 'help-echo
  4856. (if (= (point-at-bol) beg)
  4857. "Footnote definition"
  4858. "Footnote reference")
  4859. 'font-lock-fontified t
  4860. 'font-lock-multiline t
  4861. 'face 'org-footnote))))))
  4862. (defun org-activate-bracket-links (limit)
  4863. "Run through the buffer and add overlays to bracketed links."
  4864. (if (and (re-search-forward org-bracket-link-regexp limit t)
  4865. (not (org-in-src-block-p)))
  4866. (let* ((help (concat "LINK: "
  4867. (org-match-string-no-properties 1)))
  4868. ;; FIXME: above we should remove the escapes.
  4869. ;; but that requires another match, protecting match data,
  4870. ;; a lot of overhead for font-lock.
  4871. (ip (org-maybe-intangible
  4872. (list 'invisible 'org-link
  4873. 'keymap org-mouse-map 'mouse-face 'highlight
  4874. 'font-lock-multiline t 'help-echo help)))
  4875. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4876. 'font-lock-multiline t 'help-echo help)))
  4877. ;; We need to remove the invisible property here. Table narrowing
  4878. ;; may have made some of this invisible.
  4879. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4880. (remove-text-properties (match-beginning 0) (match-end 0)
  4881. '(invisible nil))
  4882. (if (match-end 3)
  4883. (progn
  4884. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4885. (org-rear-nonsticky-at (match-beginning 3))
  4886. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4887. (org-rear-nonsticky-at (match-end 3))
  4888. (add-text-properties (match-end 3) (match-end 0) ip)
  4889. (org-rear-nonsticky-at (match-end 0)))
  4890. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4891. (org-rear-nonsticky-at (match-beginning 1))
  4892. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4893. (org-rear-nonsticky-at (match-end 1))
  4894. (add-text-properties (match-end 1) (match-end 0) ip)
  4895. (org-rear-nonsticky-at (match-end 0)))
  4896. t)))
  4897. (defun org-activate-dates (limit)
  4898. "Run through the buffer and add overlays to dates."
  4899. (if (re-search-forward org-tsr-regexp-both limit t)
  4900. (progn
  4901. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4902. (add-text-properties (match-beginning 0) (match-end 0)
  4903. (list 'mouse-face 'highlight
  4904. 'keymap org-mouse-map))
  4905. (org-rear-nonsticky-at (match-end 0))
  4906. (when org-display-custom-times
  4907. (if (match-end 3)
  4908. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4909. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4910. t)))
  4911. (defvar org-target-link-regexp nil
  4912. "Regular expression matching radio targets in plain text.")
  4913. (make-variable-buffer-local 'org-target-link-regexp)
  4914. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4915. "Regular expression matching a link target.")
  4916. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4917. "Regular expression matching a radio target.")
  4918. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4919. "Regular expression matching any target.")
  4920. (defun org-activate-target-links (limit)
  4921. "Run through the buffer and add overlays to target matches."
  4922. (when org-target-link-regexp
  4923. (let ((case-fold-search t))
  4924. (if (re-search-forward org-target-link-regexp limit t)
  4925. (progn
  4926. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4927. (add-text-properties (match-beginning 0) (match-end 0)
  4928. (list 'mouse-face 'highlight
  4929. 'keymap org-mouse-map
  4930. 'help-echo "Radio target link"
  4931. 'org-linked-text t))
  4932. (org-rear-nonsticky-at (match-end 0))
  4933. t)))))
  4934. (defun org-update-radio-target-regexp ()
  4935. "Find all radio targets in this file and update the regular expression."
  4936. (interactive)
  4937. (when (memq 'radio org-activate-links)
  4938. (setq org-target-link-regexp
  4939. (org-make-target-link-regexp (org-all-targets 'radio)))
  4940. (org-restart-font-lock)))
  4941. (defun org-hide-wide-columns (limit)
  4942. (let (s e)
  4943. (setq s (text-property-any (point) (or limit (point-max))
  4944. 'org-cwidth t))
  4945. (when s
  4946. (setq e (next-single-property-change s 'org-cwidth))
  4947. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4948. (goto-char e)
  4949. t)))
  4950. (defvar org-latex-and-specials-regexp nil
  4951. "Regular expression for highlighting export special stuff.")
  4952. (defvar org-match-substring-regexp)
  4953. (defvar org-match-substring-with-braces-regexp)
  4954. ;; This should be with the exporter code, but we also use if for font-locking
  4955. (defconst org-export-html-special-string-regexps
  4956. '(("\\\\-" . "&shy;")
  4957. ("---\\([^-]\\)" . "&mdash;\\1")
  4958. ("--\\([^-]\\)" . "&ndash;\\1")
  4959. ("\\.\\.\\." . "&hellip;"))
  4960. "Regular expressions for special string conversion.")
  4961. (defun org-compute-latex-and-specials-regexp ()
  4962. "Compute regular expression for stuff treated specially by exporters."
  4963. (if (not org-highlight-latex-fragments-and-specials)
  4964. (org-set-local 'org-latex-and-specials-regexp nil)
  4965. (require 'org-exp)
  4966. (let*
  4967. ((matchers (plist-get org-format-latex-options :matchers))
  4968. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4969. org-latex-regexps)))
  4970. (org-export-allow-BIND nil)
  4971. (options (org-combine-plists (org-default-export-plist)
  4972. (org-infile-export-plist)))
  4973. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4974. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4975. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4976. (org-export-html-expand (plist-get options :expand-quoted-html))
  4977. (org-export-with-special-strings (plist-get options :special-strings))
  4978. (re-sub
  4979. (cond
  4980. ((equal org-export-with-sub-superscripts '{})
  4981. (list org-match-substring-with-braces-regexp))
  4982. (org-export-with-sub-superscripts
  4983. (list org-match-substring-regexp))))
  4984. (re-latex
  4985. (if org-export-with-LaTeX-fragments
  4986. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4987. (re-macros
  4988. (if org-export-with-TeX-macros
  4989. (list (concat "\\\\"
  4990. (regexp-opt
  4991. (append
  4992. (delq nil
  4993. (mapcar 'car-safe
  4994. (append org-entities-user
  4995. org-entities)))
  4996. (if (boundp 'org-latex-entities)
  4997. (mapcar (lambda (x)
  4998. (or (car-safe x) x))
  4999. org-latex-entities)
  5000. nil))
  5001. 'words))) ; FIXME
  5002. ))
  5003. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5004. (re-special (if org-export-with-special-strings
  5005. (mapcar (lambda (x) (car x))
  5006. org-export-html-special-string-regexps)))
  5007. (re-rest
  5008. (delq nil
  5009. (list
  5010. (if org-export-html-expand "@<[^>\n]+>")
  5011. ))))
  5012. (org-set-local
  5013. 'org-latex-and-specials-regexp
  5014. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5015. re-rest) "\\|")))))
  5016. (defun org-do-latex-and-special-faces (limit)
  5017. "Run through the buffer and add overlays to links."
  5018. (when org-latex-and-specials-regexp
  5019. (let (rtn d)
  5020. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5021. limit t))
  5022. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5023. 'face))
  5024. '(org-code org-verbatim underline)))
  5025. (progn
  5026. (setq rtn t
  5027. d (cond ((member (char-after (1+ (match-beginning 0)))
  5028. '(?_ ?^)) 1)
  5029. (t 0)))
  5030. (font-lock-prepend-text-property
  5031. (+ d (match-beginning 0)) (match-end 0)
  5032. 'face 'org-latex-and-export-specials)
  5033. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5034. '(font-lock-multiline t)))))
  5035. rtn)))
  5036. (defun org-restart-font-lock ()
  5037. "Restart `font-lock-mode', to force refontification."
  5038. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5039. (font-lock-mode -1)
  5040. (font-lock-mode 1)))
  5041. (defun org-all-targets (&optional radio)
  5042. "Return a list of all targets in this file.
  5043. With optional argument RADIO, only find radio targets."
  5044. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5045. rtn)
  5046. (save-excursion
  5047. (goto-char (point-min))
  5048. (while (re-search-forward re nil t)
  5049. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5050. rtn)))
  5051. (defun org-make-target-link-regexp (targets)
  5052. "Make regular expression matching all strings in TARGETS.
  5053. The regular expression finds the targets also if there is a line break
  5054. between words."
  5055. (and targets
  5056. (concat
  5057. "\\<\\("
  5058. (mapconcat
  5059. (lambda (x)
  5060. (setq x (regexp-quote x))
  5061. (while (string-match " +" x)
  5062. (setq x (replace-match "\\s-+" t t x)))
  5063. x)
  5064. targets
  5065. "\\|")
  5066. "\\)\\>")))
  5067. (defun org-activate-tags (limit)
  5068. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5069. (progn
  5070. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5071. (add-text-properties (match-beginning 1) (match-end 1)
  5072. (list 'mouse-face 'highlight
  5073. 'keymap org-mouse-map))
  5074. (org-rear-nonsticky-at (match-end 1))
  5075. t)))
  5076. (defun org-outline-level ()
  5077. "Compute the outline level of the heading at point.
  5078. This function assumes that the cursor is at the beginning of a line matched
  5079. by `outline-regexp'. Otherwise it returns garbage.
  5080. If this is called at a normal headline, the level is the number of stars.
  5081. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5082. (save-excursion
  5083. (looking-at org-outline-regexp)
  5084. (1- (- (match-end 0) (match-beginning 0)))))
  5085. (defvar org-font-lock-keywords nil)
  5086. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5087. "Regular expression matching a property line.")
  5088. (defvar org-font-lock-hook nil
  5089. "Functions to be called for special font lock stuff.")
  5090. (defvar org-font-lock-set-keywords-hook nil
  5091. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5092. This is called after `org-font-lock-extra-keywords' is defined, but before
  5093. it is installed to be used by font lock. This can be useful if something
  5094. needs to be inserted at a specific position in the font-lock sequence.")
  5095. (defun org-font-lock-hook (limit)
  5096. "Run `org-font-lock-hook' within LIMIT."
  5097. (run-hook-with-args 'org-font-lock-hook limit))
  5098. (defun org-set-font-lock-defaults ()
  5099. "Set font lock defaults for the current buffer."
  5100. (let* ((em org-fontify-emphasized-text)
  5101. (lk org-activate-links)
  5102. (org-font-lock-extra-keywords
  5103. (list
  5104. ;; Call the hook
  5105. '(org-font-lock-hook)
  5106. ;; Headlines
  5107. `(,(if org-fontify-whole-heading-line
  5108. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5109. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5110. (1 (org-get-level-face 1))
  5111. (2 (org-get-level-face 2))
  5112. (3 (org-get-level-face 3)))
  5113. ;; Table lines
  5114. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5115. (1 'org-table t))
  5116. ;; Table internals
  5117. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5118. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5119. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5120. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5121. ;; Drawers
  5122. (list org-drawer-regexp '(0 'org-special-keyword t))
  5123. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5124. ;; Properties
  5125. (list org-property-re
  5126. '(1 'org-special-keyword t)
  5127. '(3 'org-property-value t))
  5128. ;; Links
  5129. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5130. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5131. (if (memq 'plain lk) '(org-activate-plain-links))
  5132. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5133. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5134. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5135. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5136. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5137. '(org-hide-wide-columns (0 nil append))
  5138. ;; TODO keyword
  5139. (list (format org-heading-keyword-regexp-format
  5140. org-todo-regexp)
  5141. '(2 (org-get-todo-face 2) t))
  5142. ;; DONE
  5143. (if org-fontify-done-headline
  5144. (list (format org-heading-keyword-regexp-format
  5145. (concat
  5146. "\\(?:"
  5147. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5148. "\\)"))
  5149. '(2 'org-headline-done t))
  5150. nil)
  5151. ;; Priorities
  5152. '(org-font-lock-add-priority-faces)
  5153. ;; Tags
  5154. '(org-font-lock-add-tag-faces)
  5155. ;; Special keywords
  5156. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5157. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5158. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5159. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5160. ;; Emphasis
  5161. (if em
  5162. (if (featurep 'xemacs)
  5163. '(org-do-emphasis-faces (0 nil append))
  5164. '(org-do-emphasis-faces)))
  5165. ;; Checkboxes
  5166. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5167. 1 'org-checkbox prepend)
  5168. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5169. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5170. (0 (org-get-checkbox-statistics-face) t)))
  5171. ;; Description list items
  5172. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5173. 1 'org-list-dt prepend)
  5174. ;; ARCHIVEd headings
  5175. (list (concat
  5176. org-outline-regexp-bol
  5177. "\\(.*:" org-archive-tag ":.*\\)")
  5178. '(1 'org-archived prepend))
  5179. ;; Specials
  5180. '(org-do-latex-and-special-faces)
  5181. '(org-fontify-entities)
  5182. '(org-raise-scripts)
  5183. ;; Code
  5184. '(org-activate-code (1 'org-code t))
  5185. ;; COMMENT
  5186. (list (format org-heading-keyword-regexp-format
  5187. (concat "\\("
  5188. org-comment-string "\\|" org-quote-string
  5189. "\\)"))
  5190. '(2 'org-special-keyword t))
  5191. ;; Blocks and meta lines
  5192. '(org-fontify-meta-lines-and-blocks)
  5193. )))
  5194. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5195. (run-hooks 'org-font-lock-set-keywords-hook)
  5196. ;; Now set the full font-lock-keywords
  5197. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5198. (org-set-local 'font-lock-defaults
  5199. '(org-font-lock-keywords t nil nil backward-paragraph))
  5200. (kill-local-variable 'font-lock-keywords) nil))
  5201. (defun org-toggle-pretty-entities ()
  5202. "Toggle the composition display of entities as UTF8 characters."
  5203. (interactive)
  5204. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5205. (org-restart-font-lock)
  5206. (if org-pretty-entities
  5207. (message "Entities are displayed as UTF8 characters")
  5208. (save-restriction
  5209. (widen)
  5210. (org-decompose-region (point-min) (point-max))
  5211. (message "Entities are displayed plain"))))
  5212. (defvar org-custom-properties-overlays nil
  5213. "List of overlays used for custom properties.")
  5214. (make-variable-buffer-local 'org-custom-properties-overlays)
  5215. (defun org-toggle-custom-properties-visibility ()
  5216. "Display or hide properties in `org-custom-properties'."
  5217. (interactive)
  5218. (if org-custom-properties-overlays
  5219. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5220. (setq org-custom-properties-overlays nil))
  5221. (unless (not org-custom-properties)
  5222. (save-excursion
  5223. (save-restriction
  5224. (widen)
  5225. (goto-char (point-min))
  5226. (while (re-search-forward org-property-re nil t)
  5227. (mapc (lambda(p)
  5228. (when (equal p (substring (match-string 1) 1 -1))
  5229. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5230. (overlay-put o 'invisible t)
  5231. (overlay-put o 'org-custom-property t)
  5232. (push o org-custom-properties-overlays))))
  5233. org-custom-properties)))))))
  5234. (defun org-fontify-entities (limit)
  5235. "Find an entity to fontify."
  5236. (let (ee)
  5237. (when org-pretty-entities
  5238. (catch 'match
  5239. (while (re-search-forward
  5240. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5241. limit t)
  5242. (if (and (not (org-in-indented-comment-line))
  5243. (setq ee (org-entity-get (match-string 1)))
  5244. (= (length (nth 6 ee)) 1))
  5245. (let*
  5246. ((end (if (equal (match-string 2) "{}")
  5247. (match-end 2)
  5248. (match-end 1))))
  5249. (add-text-properties
  5250. (match-beginning 0) end
  5251. (list 'font-lock-fontified t))
  5252. (compose-region (match-beginning 0) end
  5253. (nth 6 ee) nil)
  5254. (backward-char 1)
  5255. (throw 'match t))))
  5256. nil))))
  5257. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5258. "Fontify string S like in Org-mode."
  5259. (with-temp-buffer
  5260. (insert s)
  5261. (let ((org-odd-levels-only odd-levels))
  5262. (org-mode)
  5263. (font-lock-fontify-buffer)
  5264. (buffer-string))))
  5265. (defvar org-m nil)
  5266. (defvar org-l nil)
  5267. (defvar org-f nil)
  5268. (defun org-get-level-face (n)
  5269. "Get the right face for match N in font-lock matching of headlines."
  5270. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5271. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5272. (if org-cycle-level-faces
  5273. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5274. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5275. (cond
  5276. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5277. ((eq n 2) org-f)
  5278. (t (if org-level-color-stars-only nil org-f))))
  5279. (defun org-get-todo-face (kwd)
  5280. "Get the right face for a TODO keyword KWD.
  5281. If KWD is a number, get the corresponding match group."
  5282. (if (numberp kwd) (setq kwd (match-string kwd)))
  5283. (or (org-face-from-face-or-color
  5284. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5285. (and (member kwd org-done-keywords) 'org-done)
  5286. 'org-todo))
  5287. (defun org-face-from-face-or-color (context inherit face-or-color)
  5288. "Create a face list that inherits INHERIT, but sets the foreground color.
  5289. When FACE-OR-COLOR is not a string, just return it."
  5290. (if (stringp face-or-color)
  5291. (list :inherit inherit
  5292. (cdr (assoc context org-faces-easy-properties))
  5293. face-or-color)
  5294. face-or-color))
  5295. (defun org-font-lock-add-tag-faces (limit)
  5296. "Add the special tag faces."
  5297. (when (and org-tag-faces org-tags-special-faces-re)
  5298. (while (re-search-forward org-tags-special-faces-re limit t)
  5299. (add-text-properties (match-beginning 1) (match-end 1)
  5300. (list 'face (org-get-tag-face 1)
  5301. 'font-lock-fontified t))
  5302. (backward-char 1))))
  5303. (defun org-font-lock-add-priority-faces (limit)
  5304. "Add the special priority faces."
  5305. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5306. (when (save-match-data (org-at-heading-p))
  5307. (add-text-properties
  5308. (match-beginning 0) (match-end 0)
  5309. (list 'face (or (org-face-from-face-or-color
  5310. 'priority 'org-special-keyword
  5311. (cdr (assoc (char-after (match-beginning 1))
  5312. org-priority-faces)))
  5313. 'org-special-keyword)
  5314. 'font-lock-fontified t)))))
  5315. (defun org-get-tag-face (kwd)
  5316. "Get the right face for a TODO keyword KWD.
  5317. If KWD is a number, get the corresponding match group."
  5318. (if (numberp kwd) (setq kwd (match-string kwd)))
  5319. (or (org-face-from-face-or-color
  5320. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5321. 'org-tag))
  5322. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5323. "Remove fontification and activation overlays from links."
  5324. (font-lock-default-unfontify-region beg end)
  5325. (let* ((buffer-undo-list t)
  5326. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5327. (inhibit-modification-hooks t)
  5328. deactivate-mark buffer-file-name buffer-file-truename)
  5329. (org-decompose-region beg end)
  5330. (remove-text-properties beg end
  5331. '(mouse-face t keymap t org-linked-text t
  5332. invisible t intangible t
  5333. org-no-flyspell t org-emphasis t))
  5334. (org-remove-font-lock-display-properties beg end)))
  5335. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5336. ((raise 0.3) (height 0.7))
  5337. ((raise -0.5))
  5338. ((raise 0.5)))
  5339. "Display properties for showing superscripts and subscripts.")
  5340. (defun org-remove-font-lock-display-properties (beg end)
  5341. "Remove specific display properties that have been added by font lock.
  5342. The will remove the raise properties that are used to show superscripts
  5343. and subscripts."
  5344. (let (next prop)
  5345. (while (< beg end)
  5346. (setq next (next-single-property-change beg 'display nil end)
  5347. prop (get-text-property beg 'display))
  5348. (if (member prop org-script-display)
  5349. (put-text-property beg next 'display nil))
  5350. (setq beg next))))
  5351. (defun org-raise-scripts (limit)
  5352. "Add raise properties to sub/superscripts."
  5353. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5354. (if (re-search-forward
  5355. (if (eq org-use-sub-superscripts t)
  5356. org-match-substring-regexp
  5357. org-match-substring-with-braces-regexp)
  5358. limit t)
  5359. (let* ((pos (point)) table-p comment-p
  5360. (mpos (match-beginning 3))
  5361. (emph-p (get-text-property mpos 'org-emphasis))
  5362. (link-p (get-text-property mpos 'mouse-face))
  5363. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5364. (goto-char (point-at-bol))
  5365. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5366. comment-p (org-looking-at-p "[ \t]*#"))
  5367. (goto-char pos)
  5368. ;; FIXME: Should we go back one character here, for a_b^c
  5369. ;; (goto-char (1- pos)) ;????????????????????
  5370. (if (or comment-p emph-p link-p keyw-p)
  5371. t
  5372. (put-text-property (match-beginning 3) (match-end 0)
  5373. 'display
  5374. (if (equal (char-after (match-beginning 2)) ?^)
  5375. (nth (if table-p 3 1) org-script-display)
  5376. (nth (if table-p 2 0) org-script-display)))
  5377. (add-text-properties (match-beginning 2) (match-end 2)
  5378. (list 'invisible t
  5379. 'org-dwidth t 'org-dwidth-n 1))
  5380. (if (and (eq (char-after (match-beginning 3)) ?{)
  5381. (eq (char-before (match-end 3)) ?}))
  5382. (progn
  5383. (add-text-properties
  5384. (match-beginning 3) (1+ (match-beginning 3))
  5385. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5386. (add-text-properties
  5387. (1- (match-end 3)) (match-end 3)
  5388. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5389. t)))))
  5390. ;;;; Visibility cycling, including org-goto and indirect buffer
  5391. ;;; Cycling
  5392. (defvar org-cycle-global-status nil)
  5393. (make-variable-buffer-local 'org-cycle-global-status)
  5394. (defvar org-cycle-subtree-status nil)
  5395. (make-variable-buffer-local 'org-cycle-subtree-status)
  5396. (defvar org-inlinetask-min-level)
  5397. ;;;###autoload
  5398. (defun org-cycle (&optional arg)
  5399. "TAB-action and visibility cycling for Org-mode.
  5400. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5401. is outline visibility cycling, but it also invokes other actions
  5402. in special contexts.
  5403. - When this function is called with a prefix argument, rotate the entire
  5404. buffer through 3 states (global cycling)
  5405. 1. OVERVIEW: Show only top-level headlines.
  5406. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5407. 3. SHOW ALL: Show everything.
  5408. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5409. determined by the variable `org-startup-folded', and by any VISIBILITY
  5410. properties in the buffer.
  5411. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5412. including any drawers.
  5413. - When inside a table, re-align the table and move to the next field.
  5414. - When point is at the beginning of a headline, rotate the subtree started
  5415. by this line through 3 different states (local cycling)
  5416. 1. FOLDED: Only the main headline is shown.
  5417. 2. CHILDREN: The main headline and the direct children are shown.
  5418. From this state, you can move to one of the children
  5419. and zoom in further.
  5420. 3. SUBTREE: Show the entire subtree, including body text.
  5421. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5422. - When point is at the beginning of an empty headline and the variable
  5423. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5424. of the headline by demoting and promoting it to likely levels. This
  5425. speeds up creation document structure by pressing TAB once or several
  5426. times right after creating a new headline.
  5427. - When there is a numeric prefix, go up to a heading with level ARG, do
  5428. a `show-subtree' and return to the previous cursor position. If ARG
  5429. is negative, go up that many levels.
  5430. - When point is not at the beginning of a headline, execute the global
  5431. binding for TAB, which is re-indenting the line. See the option
  5432. `org-cycle-emulate-tab' for details.
  5433. - Special case: if point is at the beginning of the buffer and there is
  5434. no headline in line 1, this function will act as if called with prefix arg
  5435. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5436. But only if also the variable `org-cycle-global-at-bob' is t."
  5437. (interactive "P")
  5438. (org-load-modules-maybe)
  5439. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5440. (and org-cycle-level-after-item/entry-creation
  5441. (or (org-cycle-level)
  5442. (org-cycle-item-indentation))))
  5443. (let* ((limit-level
  5444. (or org-cycle-max-level
  5445. (and (boundp 'org-inlinetask-min-level)
  5446. org-inlinetask-min-level
  5447. (1- org-inlinetask-min-level))))
  5448. (nstars (and limit-level
  5449. (if org-odd-levels-only
  5450. (and limit-level (1- (* limit-level 2)))
  5451. limit-level)))
  5452. (org-outline-regexp
  5453. (if (not (derived-mode-p 'org-mode))
  5454. outline-regexp
  5455. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5456. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5457. (not (looking-at org-outline-regexp))))
  5458. (org-cycle-hook
  5459. (if bob-special
  5460. (delq 'org-optimize-window-after-visibility-change
  5461. (copy-sequence org-cycle-hook))
  5462. org-cycle-hook))
  5463. (pos (point)))
  5464. (if (or bob-special (equal arg '(4)))
  5465. ;; special case: use global cycling
  5466. (setq arg t))
  5467. (cond
  5468. ((equal arg '(16))
  5469. (setq last-command 'dummy)
  5470. (org-set-startup-visibility)
  5471. (message "Startup visibility, plus VISIBILITY properties"))
  5472. ((equal arg '(64))
  5473. (show-all)
  5474. (message "Entire buffer visible, including drawers"))
  5475. ;; Table: enter it or move to the next field.
  5476. ((org-at-table-p 'any)
  5477. (if (org-at-table.el-p)
  5478. (message "Use C-c ' to edit table.el tables")
  5479. (if arg (org-table-edit-field t)
  5480. (org-table-justify-field-maybe)
  5481. (call-interactively 'org-table-next-field))))
  5482. ((run-hook-with-args-until-success
  5483. 'org-tab-after-check-for-table-hook))
  5484. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5485. ((eq arg t) (org-cycle-internal-global))
  5486. ;; Drawers: delegate to `org-flag-drawer'.
  5487. ((and org-drawers org-drawer-regexp
  5488. (save-excursion
  5489. (beginning-of-line 1)
  5490. (looking-at org-drawer-regexp)))
  5491. (org-flag-drawer ; toggle block visibility
  5492. (not (get-char-property (match-end 0) 'invisible))))
  5493. ;; Show-subtree, ARG levels up from here.
  5494. ((integerp arg)
  5495. (save-excursion
  5496. (org-back-to-heading)
  5497. (outline-up-heading (if (< arg 0) (- arg)
  5498. (- (funcall outline-level) arg)))
  5499. (org-show-subtree)))
  5500. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5501. ((and (featurep 'org-inlinetask)
  5502. (org-inlinetask-at-task-p)
  5503. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5504. (org-inlinetask-toggle-visibility))
  5505. ((org-try-cdlatex-tab))
  5506. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5507. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5508. (save-excursion (beginning-of-line 1)
  5509. (looking-at org-outline-regexp)))
  5510. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5511. (org-cycle-internal-local))
  5512. ;; From there: TAB emulation and template completion.
  5513. (buffer-read-only (org-back-to-heading))
  5514. ((run-hook-with-args-until-success
  5515. 'org-tab-after-check-for-cycling-hook))
  5516. ((org-try-structure-completion))
  5517. ((run-hook-with-args-until-success
  5518. 'org-tab-before-tab-emulation-hook))
  5519. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5520. (or (not (bolp))
  5521. (not (looking-at org-outline-regexp))))
  5522. (call-interactively (global-key-binding "\t")))
  5523. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5524. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5525. (or (and (eq org-cycle-emulate-tab 'white)
  5526. (= (match-end 0) (point-at-eol)))
  5527. (and (eq org-cycle-emulate-tab 'whitestart)
  5528. (>= (match-end 0) pos))))
  5529. t
  5530. (eq org-cycle-emulate-tab t))
  5531. (call-interactively (global-key-binding "\t")))
  5532. (t (save-excursion
  5533. (org-back-to-heading)
  5534. (org-cycle)))))))
  5535. (defun org-cycle-internal-global ()
  5536. "Do the global cycling action."
  5537. ;; Hack to avoid display of messages for .org attachments in Gnus
  5538. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5539. (cond
  5540. ((and (eq last-command this-command)
  5541. (eq org-cycle-global-status 'overview))
  5542. ;; We just created the overview - now do table of contents
  5543. ;; This can be slow in very large buffers, so indicate action
  5544. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5545. (unless ga (message "CONTENTS..."))
  5546. (org-content)
  5547. (unless ga (message "CONTENTS...done"))
  5548. (setq org-cycle-global-status 'contents)
  5549. (run-hook-with-args 'org-cycle-hook 'contents))
  5550. ((and (eq last-command this-command)
  5551. (eq org-cycle-global-status 'contents))
  5552. ;; We just showed the table of contents - now show everything
  5553. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5554. (show-all)
  5555. (unless ga (message "SHOW ALL"))
  5556. (setq org-cycle-global-status 'all)
  5557. (run-hook-with-args 'org-cycle-hook 'all))
  5558. (t
  5559. ;; Default action: go to overview
  5560. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5561. (org-overview)
  5562. (unless ga (message "OVERVIEW"))
  5563. (setq org-cycle-global-status 'overview)
  5564. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5565. (defun org-cycle-internal-local ()
  5566. "Do the local cycling action."
  5567. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5568. ;; First, determine end of headline (EOH), end of subtree or item
  5569. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5570. (save-excursion
  5571. (if (org-at-item-p)
  5572. (progn
  5573. (beginning-of-line)
  5574. (setq struct (org-list-struct))
  5575. (setq eoh (point-at-eol))
  5576. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5577. (setq has-children (org-list-has-child-p (point) struct)))
  5578. (org-back-to-heading)
  5579. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5580. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5581. (setq has-children
  5582. (or (save-excursion
  5583. (let ((level (funcall outline-level)))
  5584. (outline-next-heading)
  5585. (and (org-at-heading-p t)
  5586. (> (funcall outline-level) level))))
  5587. (save-excursion
  5588. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5589. ;; Determine end invisible part of buffer (EOL)
  5590. (beginning-of-line 2)
  5591. ;; XEmacs doesn't have `next-single-char-property-change'
  5592. (if (featurep 'xemacs)
  5593. (while (and (not (eobp)) ;; this is like `next-line'
  5594. (get-char-property (1- (point)) 'invisible))
  5595. (beginning-of-line 2))
  5596. (while (and (not (eobp)) ;; this is like `next-line'
  5597. (get-char-property (1- (point)) 'invisible))
  5598. (goto-char (next-single-char-property-change (point) 'invisible))
  5599. (and (eolp) (beginning-of-line 2))))
  5600. (setq eol (point)))
  5601. ;; Find out what to do next and set `this-command'
  5602. (cond
  5603. ((= eos eoh)
  5604. ;; Nothing is hidden behind this heading
  5605. (unless (org-before-first-heading-p)
  5606. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5607. (message "EMPTY ENTRY")
  5608. (setq org-cycle-subtree-status nil)
  5609. (save-excursion
  5610. (goto-char eos)
  5611. (outline-next-heading)
  5612. (if (outline-invisible-p) (org-flag-heading nil))))
  5613. ((and (or (>= eol eos)
  5614. (not (string-match "\\S-" (buffer-substring eol eos))))
  5615. (or has-children
  5616. (not (setq children-skipped
  5617. org-cycle-skip-children-state-if-no-children))))
  5618. ;; Entire subtree is hidden in one line: children view
  5619. (unless (org-before-first-heading-p)
  5620. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5621. (if (org-at-item-p)
  5622. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5623. (org-show-entry)
  5624. (org-with-limited-levels (show-children))
  5625. ;; FIXME: This slows down the func way too much.
  5626. ;; How keep drawers hidden in subtree anyway?
  5627. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5628. ;; (org-cycle-hide-drawers 'subtree))
  5629. ;; Fold every list in subtree to top-level items.
  5630. (when (eq org-cycle-include-plain-lists 'integrate)
  5631. (save-excursion
  5632. (org-back-to-heading)
  5633. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5634. (beginning-of-line 1)
  5635. (let* ((struct (org-list-struct))
  5636. (prevs (org-list-prevs-alist struct))
  5637. (end (org-list-get-bottom-point struct)))
  5638. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5639. (org-list-get-all-items (point) struct prevs))
  5640. (goto-char end))))))
  5641. (message "CHILDREN")
  5642. (save-excursion
  5643. (goto-char eos)
  5644. (outline-next-heading)
  5645. (if (outline-invisible-p) (org-flag-heading nil)))
  5646. (setq org-cycle-subtree-status 'children)
  5647. (unless (org-before-first-heading-p)
  5648. (run-hook-with-args 'org-cycle-hook 'children)))
  5649. ((or children-skipped
  5650. (and (eq last-command this-command)
  5651. (eq org-cycle-subtree-status 'children)))
  5652. ;; We just showed the children, or no children are there,
  5653. ;; now show everything.
  5654. (unless (org-before-first-heading-p)
  5655. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5656. (outline-flag-region eoh eos nil)
  5657. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5658. (setq org-cycle-subtree-status 'subtree)
  5659. (unless (org-before-first-heading-p)
  5660. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5661. (t
  5662. ;; Default action: hide the subtree.
  5663. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5664. (outline-flag-region eoh eos t)
  5665. (message "FOLDED")
  5666. (setq org-cycle-subtree-status 'folded)
  5667. (unless (org-before-first-heading-p)
  5668. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5669. ;;;###autoload
  5670. (defun org-global-cycle (&optional arg)
  5671. "Cycle the global visibility. For details see `org-cycle'.
  5672. With \\[universal-argument] prefix arg, switch to startup visibility.
  5673. With a numeric prefix, show all headlines up to that level."
  5674. (interactive "P")
  5675. (let ((org-cycle-include-plain-lists
  5676. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5677. (cond
  5678. ((integerp arg)
  5679. (show-all)
  5680. (hide-sublevels arg)
  5681. (setq org-cycle-global-status 'contents))
  5682. ((equal arg '(4))
  5683. (org-set-startup-visibility)
  5684. (message "Startup visibility, plus VISIBILITY properties."))
  5685. (t
  5686. (org-cycle '(4))))))
  5687. (defun org-set-startup-visibility ()
  5688. "Set the visibility required by startup options and properties."
  5689. (cond
  5690. ((eq org-startup-folded t)
  5691. (org-cycle '(4)))
  5692. ((eq org-startup-folded 'content)
  5693. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5694. (org-cycle '(4)) (org-cycle '(4)))))
  5695. (unless (eq org-startup-folded 'showeverything)
  5696. (if org-hide-block-startup (org-hide-block-all))
  5697. (org-set-visibility-according-to-property 'no-cleanup)
  5698. (org-cycle-hide-archived-subtrees 'all)
  5699. (org-cycle-hide-drawers 'all)
  5700. (org-cycle-show-empty-lines t)))
  5701. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5702. "Switch subtree visibilities according to :VISIBILITY: property."
  5703. (interactive)
  5704. (let (org-show-entry-below state)
  5705. (save-excursion
  5706. (goto-char (point-min))
  5707. (while (re-search-forward
  5708. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5709. nil t)
  5710. (setq state (match-string 1))
  5711. (save-excursion
  5712. (org-back-to-heading t)
  5713. (hide-subtree)
  5714. (org-reveal)
  5715. (cond
  5716. ((equal state '("fold" "folded"))
  5717. (hide-subtree))
  5718. ((equal state "children")
  5719. (org-show-hidden-entry)
  5720. (show-children))
  5721. ((equal state "content")
  5722. (save-excursion
  5723. (save-restriction
  5724. (org-narrow-to-subtree)
  5725. (org-content))))
  5726. ((member state '("all" "showall"))
  5727. (show-subtree)))))
  5728. (unless no-cleanup
  5729. (org-cycle-hide-archived-subtrees 'all)
  5730. (org-cycle-hide-drawers 'all)
  5731. (org-cycle-show-empty-lines 'all)))))
  5732. ;; This function uses outline-regexp instead of the more fundamental
  5733. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5734. ;; buffers, where outline-regexp is needed.
  5735. (defun org-overview ()
  5736. "Switch to overview mode, showing only top-level headlines.
  5737. Really, this shows all headlines with level equal or greater than the level
  5738. of the first headline in the buffer. This is important, because if the
  5739. first headline is not level one, then (hide-sublevels 1) gives confusing
  5740. results."
  5741. (interactive)
  5742. (let ((level (save-excursion
  5743. (goto-char (point-min))
  5744. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5745. (progn
  5746. (goto-char (match-beginning 0))
  5747. (funcall outline-level))))))
  5748. (and level (hide-sublevels level))))
  5749. (defun org-content (&optional arg)
  5750. "Show all headlines in the buffer, like a table of contents.
  5751. With numerical argument N, show content up to level N."
  5752. (interactive "P")
  5753. (save-excursion
  5754. ;; Visit all headings and show their offspring
  5755. (and (integerp arg) (org-overview))
  5756. (goto-char (point-max))
  5757. (catch 'exit
  5758. (while (and (progn (condition-case nil
  5759. (outline-previous-visible-heading 1)
  5760. (error (goto-char (point-min))))
  5761. t)
  5762. (looking-at org-outline-regexp))
  5763. (if (integerp arg)
  5764. (show-children (1- arg))
  5765. (show-branches))
  5766. (if (bobp) (throw 'exit nil))))))
  5767. (defun org-optimize-window-after-visibility-change (state)
  5768. "Adjust the window after a change in outline visibility.
  5769. This function is the default value of the hook `org-cycle-hook'."
  5770. (when (get-buffer-window (current-buffer))
  5771. (cond
  5772. ((eq state 'content) nil)
  5773. ((eq state 'all) nil)
  5774. ((eq state 'folded) nil)
  5775. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5776. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5777. (defun org-remove-empty-overlays-at (pos)
  5778. "Remove outline overlays that do not contain non-white stuff."
  5779. (mapc
  5780. (lambda (o)
  5781. (and (eq 'outline (overlay-get o 'invisible))
  5782. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5783. (overlay-end o))))
  5784. (delete-overlay o)))
  5785. (overlays-at pos)))
  5786. (defun org-clean-visibility-after-subtree-move ()
  5787. "Fix visibility issues after moving a subtree."
  5788. ;; First, find a reasonable region to look at:
  5789. ;; Start two siblings above, end three below
  5790. (let* ((beg (save-excursion
  5791. (and (org-get-last-sibling)
  5792. (org-get-last-sibling))
  5793. (point)))
  5794. (end (save-excursion
  5795. (and (org-get-next-sibling)
  5796. (org-get-next-sibling)
  5797. (org-get-next-sibling))
  5798. (if (org-at-heading-p)
  5799. (point-at-eol)
  5800. (point))))
  5801. (level (looking-at "\\*+"))
  5802. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5803. (save-excursion
  5804. (save-restriction
  5805. (narrow-to-region beg end)
  5806. (when re
  5807. ;; Properly fold already folded siblings
  5808. (goto-char (point-min))
  5809. (while (re-search-forward re nil t)
  5810. (if (and (not (outline-invisible-p))
  5811. (save-excursion
  5812. (goto-char (point-at-eol)) (outline-invisible-p)))
  5813. (hide-entry))))
  5814. (org-cycle-show-empty-lines 'overview)
  5815. (org-cycle-hide-drawers 'overview)))))
  5816. (defun org-cycle-show-empty-lines (state)
  5817. "Show empty lines above all visible headlines.
  5818. The region to be covered depends on STATE when called through
  5819. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5820. entire buffer covered. Note that an empty line is only shown if there
  5821. are at least `org-cycle-separator-lines' empty lines before the headline."
  5822. (when (not (= org-cycle-separator-lines 0))
  5823. (save-excursion
  5824. (let* ((n (abs org-cycle-separator-lines))
  5825. (re (cond
  5826. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5827. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5828. (t (let ((ns (number-to-string (- n 2))))
  5829. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5830. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5831. beg end b e)
  5832. (cond
  5833. ((memq state '(overview contents t))
  5834. (setq beg (point-min) end (point-max)))
  5835. ((memq state '(children folded))
  5836. (setq beg (point) end (progn (org-end-of-subtree t t)
  5837. (beginning-of-line 2)
  5838. (point)))))
  5839. (when beg
  5840. (goto-char beg)
  5841. (while (re-search-forward re end t)
  5842. (unless (get-char-property (match-end 1) 'invisible)
  5843. (setq e (match-end 1))
  5844. (if (< org-cycle-separator-lines 0)
  5845. (setq b (save-excursion
  5846. (goto-char (match-beginning 0))
  5847. (org-back-over-empty-lines)
  5848. (if (save-excursion
  5849. (goto-char (max (point-min) (1- (point))))
  5850. (org-at-heading-p))
  5851. (1- (point))
  5852. (point))))
  5853. (setq b (match-beginning 1)))
  5854. (outline-flag-region b e nil)))))))
  5855. ;; Never hide empty lines at the end of the file.
  5856. (save-excursion
  5857. (goto-char (point-max))
  5858. (outline-previous-heading)
  5859. (outline-end-of-heading)
  5860. (if (and (looking-at "[ \t\n]+")
  5861. (= (match-end 0) (point-max)))
  5862. (outline-flag-region (point) (match-end 0) nil))))
  5863. (defun org-show-empty-lines-in-parent ()
  5864. "Move to the parent and re-show empty lines before visible headlines."
  5865. (save-excursion
  5866. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5867. (org-cycle-show-empty-lines context))))
  5868. (defun org-files-list ()
  5869. "Return `org-agenda-files' list, plus all open org-mode files.
  5870. This is useful for operations that need to scan all of a user's
  5871. open and agenda-wise Org files."
  5872. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5873. (dolist (buf (buffer-list))
  5874. (with-current-buffer buf
  5875. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5876. (let ((file (expand-file-name (buffer-file-name))))
  5877. (unless (member file files)
  5878. (push file files))))))
  5879. files))
  5880. (defsubst org-entry-beginning-position ()
  5881. "Return the beginning position of the current entry."
  5882. (save-excursion (outline-back-to-heading t) (point)))
  5883. (defsubst org-entry-end-position ()
  5884. "Return the end position of the current entry."
  5885. (save-excursion (outline-next-heading) (point)))
  5886. (defun org-cycle-hide-drawers (state)
  5887. "Re-hide all drawers after a visibility state change."
  5888. (when (and (derived-mode-p 'org-mode)
  5889. (not (memq state '(overview folded contents))))
  5890. (save-excursion
  5891. (let* ((globalp (memq state '(contents all)))
  5892. (beg (if globalp (point-min) (point)))
  5893. (end (if globalp (point-max)
  5894. (if (eq state 'children)
  5895. (save-excursion (outline-next-heading) (point))
  5896. (org-end-of-subtree t)))))
  5897. (goto-char beg)
  5898. (while (re-search-forward org-drawer-regexp end t)
  5899. (org-flag-drawer t))))))
  5900. (defun org-flag-drawer (flag)
  5901. "When FLAG is non-nil, hide the drawer we are within.
  5902. Otherwise make it visible."
  5903. (save-excursion
  5904. (beginning-of-line 1)
  5905. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5906. (let ((b (match-end 0)))
  5907. (if (re-search-forward
  5908. "^[ \t]*:END:"
  5909. (save-excursion (outline-next-heading) (point)) t)
  5910. (outline-flag-region b (point-at-eol) flag)
  5911. (error ":END: line missing at position %s" b))))))
  5912. (defun org-subtree-end-visible-p ()
  5913. "Is the end of the current subtree visible?"
  5914. (pos-visible-in-window-p
  5915. (save-excursion (org-end-of-subtree t) (point))))
  5916. (defun org-first-headline-recenter (&optional N)
  5917. "Move cursor to the first headline and recenter the headline.
  5918. Optional argument N means put the headline into the Nth line of the window."
  5919. (goto-char (point-min))
  5920. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5921. (beginning-of-line)
  5922. (recenter (prefix-numeric-value N))))
  5923. ;;; Saving and restoring visibility
  5924. (defun org-outline-overlay-data (&optional use-markers)
  5925. "Return a list of the locations of all outline overlays.
  5926. These are overlays with the `invisible' property value `outline'.
  5927. The return value is a list of cons cells, with start and stop
  5928. positions for each overlay.
  5929. If USE-MARKERS is set, return the positions as markers."
  5930. (let (beg end)
  5931. (save-excursion
  5932. (save-restriction
  5933. (widen)
  5934. (delq nil
  5935. (mapcar (lambda (o)
  5936. (when (eq (overlay-get o 'invisible) 'outline)
  5937. (setq beg (overlay-start o)
  5938. end (overlay-end o))
  5939. (and beg end (> end beg)
  5940. (if use-markers
  5941. (cons (move-marker (make-marker) beg)
  5942. (move-marker (make-marker) end))
  5943. (cons beg end)))))
  5944. (overlays-in (point-min) (point-max))))))))
  5945. (defun org-set-outline-overlay-data (data)
  5946. "Create visibility overlays for all positions in DATA.
  5947. DATA should have been made by `org-outline-overlay-data'."
  5948. (let (o)
  5949. (save-excursion
  5950. (save-restriction
  5951. (widen)
  5952. (show-all)
  5953. (mapc (lambda (c)
  5954. (outline-flag-region (car c) (cdr c) t))
  5955. data)))))
  5956. ;;; Folding of blocks
  5957. (defvar org-hide-block-overlays nil
  5958. "Overlays hiding blocks.")
  5959. (make-variable-buffer-local 'org-hide-block-overlays)
  5960. (defun org-block-map (function &optional start end)
  5961. "Call FUNCTION at the head of all source blocks in the current buffer.
  5962. Optional arguments START and END can be used to limit the range."
  5963. (let ((start (or start (point-min)))
  5964. (end (or end (point-max))))
  5965. (save-excursion
  5966. (goto-char start)
  5967. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5968. (save-excursion
  5969. (save-match-data
  5970. (goto-char (match-beginning 0))
  5971. (funcall function)))))))
  5972. (defun org-hide-block-toggle-all ()
  5973. "Toggle the visibility of all blocks in the current buffer."
  5974. (org-block-map #'org-hide-block-toggle))
  5975. (defun org-hide-block-all ()
  5976. "Fold all blocks in the current buffer."
  5977. (interactive)
  5978. (org-show-block-all)
  5979. (org-block-map #'org-hide-block-toggle-maybe))
  5980. (defun org-show-block-all ()
  5981. "Unfold all blocks in the current buffer."
  5982. (interactive)
  5983. (mapc 'delete-overlay org-hide-block-overlays)
  5984. (setq org-hide-block-overlays nil))
  5985. (defun org-hide-block-toggle-maybe ()
  5986. "Toggle visibility of block at point."
  5987. (interactive)
  5988. (let ((case-fold-search t))
  5989. (if (save-excursion
  5990. (beginning-of-line 1)
  5991. (looking-at org-block-regexp))
  5992. (progn (org-hide-block-toggle)
  5993. t) ;; to signal that we took action
  5994. nil))) ;; to signal that we did not
  5995. (defun org-hide-block-toggle (&optional force)
  5996. "Toggle the visibility of the current block."
  5997. (interactive)
  5998. (save-excursion
  5999. (beginning-of-line)
  6000. (if (re-search-forward org-block-regexp nil t)
  6001. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6002. (end (match-end 0)) ;; end of entire body
  6003. ov)
  6004. (if (memq t (mapcar (lambda (overlay)
  6005. (eq (overlay-get overlay 'invisible)
  6006. 'org-hide-block))
  6007. (overlays-at start)))
  6008. (if (or (not force) (eq force 'off))
  6009. (mapc (lambda (ov)
  6010. (when (member ov org-hide-block-overlays)
  6011. (setq org-hide-block-overlays
  6012. (delq ov org-hide-block-overlays)))
  6013. (when (eq (overlay-get ov 'invisible)
  6014. 'org-hide-block)
  6015. (delete-overlay ov)))
  6016. (overlays-at start)))
  6017. (setq ov (make-overlay start end))
  6018. (overlay-put ov 'invisible 'org-hide-block)
  6019. ;; make the block accessible to isearch
  6020. (overlay-put
  6021. ov 'isearch-open-invisible
  6022. (lambda (ov)
  6023. (when (member ov org-hide-block-overlays)
  6024. (setq org-hide-block-overlays
  6025. (delq ov org-hide-block-overlays)))
  6026. (when (eq (overlay-get ov 'invisible)
  6027. 'org-hide-block)
  6028. (delete-overlay ov))))
  6029. (push ov org-hide-block-overlays)))
  6030. (error "Not looking at a source block"))))
  6031. ;; org-tab-after-check-for-cycling-hook
  6032. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6033. ;; Remove overlays when changing major mode
  6034. (add-hook 'org-mode-hook
  6035. (lambda () (org-add-hook 'change-major-mode-hook
  6036. 'org-show-block-all 'append 'local)))
  6037. ;;; Org-goto
  6038. (defvar org-goto-window-configuration nil)
  6039. (defvar org-goto-marker nil)
  6040. (defvar org-goto-map)
  6041. (defun org-goto-map ()
  6042. "Set the keymap `org-goto'."
  6043. (setq org-goto-map
  6044. (let ((map (make-sparse-keymap)))
  6045. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6046. mouse-drag-region universal-argument org-occur))
  6047. cmd)
  6048. (while (setq cmd (pop cmds))
  6049. (substitute-key-definition cmd cmd map global-map)))
  6050. (suppress-keymap map)
  6051. (org-defkey map "\C-m" 'org-goto-ret)
  6052. (org-defkey map [(return)] 'org-goto-ret)
  6053. (org-defkey map [(left)] 'org-goto-left)
  6054. (org-defkey map [(right)] 'org-goto-right)
  6055. (org-defkey map [(control ?g)] 'org-goto-quit)
  6056. (org-defkey map "\C-i" 'org-cycle)
  6057. (org-defkey map [(tab)] 'org-cycle)
  6058. (org-defkey map [(down)] 'outline-next-visible-heading)
  6059. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6060. (if org-goto-auto-isearch
  6061. (if (fboundp 'define-key-after)
  6062. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6063. nil)
  6064. (org-defkey map "q" 'org-goto-quit)
  6065. (org-defkey map "n" 'outline-next-visible-heading)
  6066. (org-defkey map "p" 'outline-previous-visible-heading)
  6067. (org-defkey map "f" 'outline-forward-same-level)
  6068. (org-defkey map "b" 'outline-backward-same-level)
  6069. (org-defkey map "u" 'outline-up-heading))
  6070. (org-defkey map "/" 'org-occur)
  6071. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6072. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6073. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6074. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6075. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6076. map)))
  6077. (defconst org-goto-help
  6078. "Browse buffer copy, to find location or copy text.%s
  6079. RET=jump to location C-g=quit and return to previous location
  6080. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6081. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6082. ;; FIXME: Docstring does not mention both interfaces
  6083. (defun org-goto (&optional alternative-interface)
  6084. "Look up a different location in the current file, keeping current visibility.
  6085. When you want look-up or go to a different location in a
  6086. document, the fastest way is often to fold the entire buffer and
  6087. then dive into the tree. This method has the disadvantage, that
  6088. the previous location will be folded, which may not be what you
  6089. want.
  6090. This command works around this by showing a copy of the current
  6091. buffer in an indirect buffer, in overview mode. You can dive
  6092. into the tree in that copy, use org-occur and incremental search
  6093. to find a location. When pressing RET or `Q', the command
  6094. returns to the original buffer in which the visibility is still
  6095. unchanged. After RET it will also jump to the location selected
  6096. in the indirect buffer and expose the headline hierarchy above.
  6097. With a prefix argument, use the alternative interface: e.g. if
  6098. `org-goto-interface' is 'outline use 'outline-path-completion."
  6099. (interactive "P")
  6100. (org-goto-map)
  6101. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6102. (org-refile-use-outline-path t)
  6103. (org-refile-target-verify-function nil)
  6104. (interface
  6105. (if (not alternative-interface)
  6106. org-goto-interface
  6107. (if (eq org-goto-interface 'outline)
  6108. 'outline-path-completion
  6109. 'outline)))
  6110. (org-goto-start-pos (point))
  6111. (selected-point
  6112. (if (eq interface 'outline)
  6113. (car (org-get-location (current-buffer) org-goto-help))
  6114. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6115. (org-refile-check-position pa)
  6116. (nth 3 pa)))))
  6117. (if selected-point
  6118. (progn
  6119. (org-mark-ring-push org-goto-start-pos)
  6120. (goto-char selected-point)
  6121. (if (or (outline-invisible-p) (org-invisible-p2))
  6122. (org-show-context 'org-goto)))
  6123. (message "Quit"))))
  6124. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6125. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6126. (defvar org-goto-local-auto-isearch-map) ; defined below
  6127. (defun org-get-location (buf help)
  6128. "Let the user select a location in the Org-mode buffer BUF.
  6129. This function uses a recursive edit. It returns the selected position
  6130. or nil."
  6131. (org-no-popups
  6132. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6133. (isearch-hide-immediately nil)
  6134. (isearch-search-fun-function
  6135. (lambda () 'org-goto-local-search-headings))
  6136. (org-goto-selected-point org-goto-exit-command))
  6137. (save-excursion
  6138. (save-window-excursion
  6139. (delete-other-windows)
  6140. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6141. (org-pop-to-buffer-same-window
  6142. (condition-case nil
  6143. (make-indirect-buffer (current-buffer) "*org-goto*")
  6144. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6145. (with-output-to-temp-buffer "*Org Help*"
  6146. (princ (format help (if org-goto-auto-isearch
  6147. " Just type for auto-isearch."
  6148. " n/p/f/b/u to navigate, q to quit."))))
  6149. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6150. (setq buffer-read-only nil)
  6151. (let ((org-startup-truncated t)
  6152. (org-startup-folded nil)
  6153. (org-startup-align-all-tables nil))
  6154. (org-mode)
  6155. (org-overview))
  6156. (setq buffer-read-only t)
  6157. (if (and (boundp 'org-goto-start-pos)
  6158. (integer-or-marker-p org-goto-start-pos))
  6159. (let ((org-show-hierarchy-above t)
  6160. (org-show-siblings t)
  6161. (org-show-following-heading t))
  6162. (goto-char org-goto-start-pos)
  6163. (and (outline-invisible-p) (org-show-context)))
  6164. (goto-char (point-min)))
  6165. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6166. (message "Select location and press RET")
  6167. (use-local-map org-goto-map)
  6168. (recursive-edit)))
  6169. (kill-buffer "*org-goto*")
  6170. (cons org-goto-selected-point org-goto-exit-command))))
  6171. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6172. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6173. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6174. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6175. (defun org-goto-local-search-headings (string bound noerror)
  6176. "Search and make sure that any matches are in headlines."
  6177. (catch 'return
  6178. (while (if isearch-forward
  6179. (search-forward string bound noerror)
  6180. (search-backward string bound noerror))
  6181. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6182. (and (member :headline context)
  6183. (not (member :tags context))))
  6184. (throw 'return (point))))))
  6185. (defun org-goto-local-auto-isearch ()
  6186. "Start isearch."
  6187. (interactive)
  6188. (goto-char (point-min))
  6189. (let ((keys (this-command-keys)))
  6190. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6191. (isearch-mode t)
  6192. (isearch-process-search-char (string-to-char keys)))))
  6193. (defun org-goto-ret (&optional arg)
  6194. "Finish `org-goto' by going to the new location."
  6195. (interactive "P")
  6196. (setq org-goto-selected-point (point)
  6197. org-goto-exit-command 'return)
  6198. (throw 'exit nil))
  6199. (defun org-goto-left ()
  6200. "Finish `org-goto' by going to the new location."
  6201. (interactive)
  6202. (if (org-at-heading-p)
  6203. (progn
  6204. (beginning-of-line 1)
  6205. (setq org-goto-selected-point (point)
  6206. org-goto-exit-command 'left)
  6207. (throw 'exit nil))
  6208. (error "Not on a heading")))
  6209. (defun org-goto-right ()
  6210. "Finish `org-goto' by going to the new location."
  6211. (interactive)
  6212. (if (org-at-heading-p)
  6213. (progn
  6214. (setq org-goto-selected-point (point)
  6215. org-goto-exit-command 'right)
  6216. (throw 'exit nil))
  6217. (error "Not on a heading")))
  6218. (defun org-goto-quit ()
  6219. "Finish `org-goto' without cursor motion."
  6220. (interactive)
  6221. (setq org-goto-selected-point nil)
  6222. (setq org-goto-exit-command 'quit)
  6223. (throw 'exit nil))
  6224. ;;; Indirect buffer display of subtrees
  6225. (defvar org-indirect-dedicated-frame nil
  6226. "This is the frame being used for indirect tree display.")
  6227. (defvar org-last-indirect-buffer nil)
  6228. (defun org-tree-to-indirect-buffer (&optional arg)
  6229. "Create indirect buffer and narrow it to current subtree.
  6230. With a numerical prefix ARG, go up to this level and then take that tree.
  6231. If ARG is negative, go up that many levels.
  6232. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6233. indirect buffer previously made with this command, to avoid proliferation of
  6234. indirect buffers. However, when you call the command with a \
  6235. \\[universal-argument] prefix, or
  6236. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6237. is kept so that you can work with several indirect buffers at the same time.
  6238. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6239. \\[universal-argument] prefix also
  6240. requests that a new frame be made for the new buffer, so that the dedicated
  6241. frame is not changed."
  6242. (interactive "P")
  6243. (let ((cbuf (current-buffer))
  6244. (cwin (selected-window))
  6245. (pos (point))
  6246. beg end level heading ibuf)
  6247. (save-excursion
  6248. (org-back-to-heading t)
  6249. (when (numberp arg)
  6250. (setq level (org-outline-level))
  6251. (if (< arg 0) (setq arg (+ level arg)))
  6252. (while (> (setq level (org-outline-level)) arg)
  6253. (org-up-heading-safe)))
  6254. (setq beg (point)
  6255. heading (org-get-heading))
  6256. (org-end-of-subtree t t)
  6257. (if (org-at-heading-p) (backward-char 1))
  6258. (setq end (point)))
  6259. (if (and (buffer-live-p org-last-indirect-buffer)
  6260. (not (eq org-indirect-buffer-display 'new-frame))
  6261. (not arg))
  6262. (kill-buffer org-last-indirect-buffer))
  6263. (setq ibuf (org-get-indirect-buffer cbuf)
  6264. org-last-indirect-buffer ibuf)
  6265. (cond
  6266. ((or (eq org-indirect-buffer-display 'new-frame)
  6267. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6268. (select-frame (make-frame))
  6269. (delete-other-windows)
  6270. (org-pop-to-buffer-same-window ibuf)
  6271. (org-set-frame-title heading))
  6272. ((eq org-indirect-buffer-display 'dedicated-frame)
  6273. (raise-frame
  6274. (select-frame (or (and org-indirect-dedicated-frame
  6275. (frame-live-p org-indirect-dedicated-frame)
  6276. org-indirect-dedicated-frame)
  6277. (setq org-indirect-dedicated-frame (make-frame)))))
  6278. (delete-other-windows)
  6279. (org-pop-to-buffer-same-window ibuf)
  6280. (org-set-frame-title (concat "Indirect: " heading)))
  6281. ((eq org-indirect-buffer-display 'current-window)
  6282. (org-pop-to-buffer-same-window ibuf))
  6283. ((eq org-indirect-buffer-display 'other-window)
  6284. (pop-to-buffer ibuf))
  6285. (t (error "Invalid value")))
  6286. (if (featurep 'xemacs)
  6287. (save-excursion (org-mode) (turn-on-font-lock)))
  6288. (narrow-to-region beg end)
  6289. (show-all)
  6290. (goto-char pos)
  6291. (run-hook-with-args 'org-cycle-hook 'all)
  6292. (and (window-live-p cwin) (select-window cwin))))
  6293. (defun org-get-indirect-buffer (&optional buffer)
  6294. (setq buffer (or buffer (current-buffer)))
  6295. (let ((n 1) (base (buffer-name buffer)) bname)
  6296. (while (buffer-live-p
  6297. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6298. (setq n (1+ n)))
  6299. (condition-case nil
  6300. (make-indirect-buffer buffer bname 'clone)
  6301. (error (make-indirect-buffer buffer bname)))))
  6302. (defun org-set-frame-title (title)
  6303. "Set the title of the current frame to the string TITLE."
  6304. ;; FIXME: how to name a single frame in XEmacs???
  6305. (unless (featurep 'xemacs)
  6306. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6307. ;;;; Structure editing
  6308. ;;; Inserting headlines
  6309. (defun org-previous-line-empty-p ()
  6310. (save-excursion
  6311. (and (not (bobp))
  6312. (or (beginning-of-line 0) t)
  6313. (save-match-data
  6314. (looking-at "[ \t]*$")))))
  6315. (defun org-insert-heading (&optional force-heading invisible-ok)
  6316. "Insert a new heading or item with same depth at point.
  6317. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6318. If point is at the beginning of a headline, insert a sibling before the
  6319. current headline. If point is not at the beginning, split the line,
  6320. create the new headline with the text in the current line after point
  6321. \(but see also the variable `org-M-RET-may-split-line').
  6322. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6323. This is important for non-interactive uses of the command."
  6324. (interactive "P")
  6325. (if (or (= (buffer-size) 0)
  6326. (and (not (save-excursion
  6327. (and (ignore-errors (org-back-to-heading invisible-ok))
  6328. (org-at-heading-p))))
  6329. (or force-heading (not (org-in-item-p)))))
  6330. (progn
  6331. (insert "\n* ")
  6332. (run-hooks 'org-insert-heading-hook))
  6333. (when (or force-heading (not (org-insert-item)))
  6334. (let* ((empty-line-p nil)
  6335. (level nil)
  6336. (on-heading (org-at-heading-p))
  6337. (head (save-excursion
  6338. (condition-case nil
  6339. (progn
  6340. (org-back-to-heading invisible-ok)
  6341. (when (and (not on-heading)
  6342. (featurep 'org-inlinetask)
  6343. (integerp org-inlinetask-min-level)
  6344. (>= (length (match-string 0))
  6345. org-inlinetask-min-level))
  6346. ;; Find a heading level before the inline task
  6347. (while (and (setq level (org-up-heading-safe))
  6348. (>= level org-inlinetask-min-level)))
  6349. (if (org-at-heading-p)
  6350. (org-back-to-heading invisible-ok)
  6351. (error "This should not happen")))
  6352. (setq empty-line-p (org-previous-line-empty-p))
  6353. (match-string 0))
  6354. (error "*"))))
  6355. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6356. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6357. pos hide-previous previous-pos)
  6358. (cond
  6359. ((and (org-at-heading-p) (bolp)
  6360. (or (bobp)
  6361. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6362. ;; insert before the current line
  6363. (open-line (if blank 2 1)))
  6364. ((and (bolp)
  6365. (not org-insert-heading-respect-content)
  6366. (or (bobp)
  6367. (save-excursion
  6368. (backward-char 1) (not (outline-invisible-p)))))
  6369. ;; insert right here
  6370. nil)
  6371. (t
  6372. ;; somewhere in the line
  6373. (save-excursion
  6374. (setq previous-pos (point-at-bol))
  6375. (end-of-line)
  6376. (setq hide-previous (outline-invisible-p)))
  6377. (and org-insert-heading-respect-content (org-show-subtree))
  6378. (let ((split
  6379. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6380. (save-excursion
  6381. (let ((p (point)))
  6382. (goto-char (point-at-bol))
  6383. (and (looking-at org-complex-heading-regexp)
  6384. (match-beginning 4)
  6385. (> p (match-beginning 4)))))))
  6386. tags pos)
  6387. (cond
  6388. (org-insert-heading-respect-content
  6389. (org-end-of-subtree nil t)
  6390. (when (featurep 'org-inlinetask)
  6391. (while (and (not (eobp))
  6392. (looking-at "\\(\\*+\\)[ \t]+")
  6393. (>= (length (match-string 1))
  6394. org-inlinetask-min-level))
  6395. (org-end-of-subtree nil t)))
  6396. (or (bolp) (newline))
  6397. (or (org-previous-line-empty-p)
  6398. (and blank (newline)))
  6399. (open-line 1))
  6400. ((org-at-heading-p)
  6401. (when hide-previous
  6402. (show-children)
  6403. (org-show-entry))
  6404. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6405. (setq tags (and (match-end 2) (match-string 2)))
  6406. (and (match-end 1)
  6407. (delete-region (match-beginning 1) (match-end 1)))
  6408. (setq pos (point-at-bol))
  6409. (or split (end-of-line 1))
  6410. (delete-horizontal-space)
  6411. (if (string-match "\\`\\*+\\'"
  6412. (buffer-substring (point-at-bol) (point)))
  6413. (insert " "))
  6414. (newline (if blank 2 1))
  6415. (when tags
  6416. (save-excursion
  6417. (goto-char pos)
  6418. (end-of-line 1)
  6419. (insert " " tags)
  6420. (org-set-tags nil 'align))))
  6421. (t
  6422. (or split (end-of-line 1))
  6423. (newline (if blank 2 1)))))))
  6424. (insert head) (just-one-space)
  6425. (setq pos (point))
  6426. (end-of-line 1)
  6427. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6428. (when (and org-insert-heading-respect-content hide-previous)
  6429. (save-excursion
  6430. (goto-char previous-pos)
  6431. (hide-subtree)))
  6432. (run-hooks 'org-insert-heading-hook)))))
  6433. (defun org-get-heading (&optional no-tags no-todo)
  6434. "Return the heading of the current entry, without the stars.
  6435. When NO-TAGS is non-nil, don't include tags.
  6436. When NO-TODO is non-nil, don't include TODO keywords."
  6437. (save-excursion
  6438. (org-back-to-heading t)
  6439. (cond
  6440. ((and no-tags no-todo)
  6441. (looking-at org-complex-heading-regexp)
  6442. (match-string 4))
  6443. (no-tags
  6444. (looking-at (concat org-outline-regexp
  6445. "\\(.*?\\)"
  6446. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6447. (match-string 1))
  6448. (no-todo
  6449. (looking-at org-todo-line-regexp)
  6450. (match-string 3))
  6451. (t (looking-at org-heading-regexp)
  6452. (match-string 2)))))
  6453. (defun org-heading-components ()
  6454. "Return the components of the current heading.
  6455. This is a list with the following elements:
  6456. - the level as an integer
  6457. - the reduced level, different if `org-odd-levels-only' is set.
  6458. - the TODO keyword, or nil
  6459. - the priority character, like ?A, or nil if no priority is given
  6460. - the headline text itself, or the tags string if no headline text
  6461. - the tags string, or nil."
  6462. (save-excursion
  6463. (org-back-to-heading t)
  6464. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6465. (list (length (match-string 1))
  6466. (org-reduced-level (length (match-string 1)))
  6467. (org-match-string-no-properties 2)
  6468. (and (match-end 3) (aref (match-string 3) 2))
  6469. (org-match-string-no-properties 4)
  6470. (org-match-string-no-properties 5)))))
  6471. (defun org-get-entry ()
  6472. "Get the entry text, after heading, entire subtree."
  6473. (save-excursion
  6474. (org-back-to-heading t)
  6475. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6476. (defun org-insert-heading-after-current ()
  6477. "Insert a new heading with same level as current, after current subtree."
  6478. (interactive)
  6479. (org-back-to-heading)
  6480. (org-insert-heading)
  6481. (org-move-subtree-down)
  6482. (end-of-line 1))
  6483. (defun org-insert-heading-respect-content ()
  6484. (interactive)
  6485. (let ((org-insert-heading-respect-content t))
  6486. (org-insert-heading t)))
  6487. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6488. (interactive "P")
  6489. (let ((org-insert-heading-respect-content t))
  6490. (org-insert-todo-heading force-state t)))
  6491. (defun org-insert-todo-heading (arg &optional force-heading)
  6492. "Insert a new heading with the same level and TODO state as current heading.
  6493. If the heading has no TODO state, or if the state is DONE, use the first
  6494. state (TODO by default). Also with prefix arg, force first state."
  6495. (interactive "P")
  6496. (when (or force-heading (not (org-insert-item 'checkbox)))
  6497. (org-insert-heading force-heading)
  6498. (save-excursion
  6499. (org-back-to-heading)
  6500. (outline-previous-heading)
  6501. (looking-at org-todo-line-regexp))
  6502. (let*
  6503. ((new-mark-x
  6504. (if (or arg
  6505. (not (match-beginning 2))
  6506. (member (match-string 2) org-done-keywords))
  6507. (car org-todo-keywords-1)
  6508. (match-string 2)))
  6509. (new-mark
  6510. (or
  6511. (run-hook-with-args-until-success
  6512. 'org-todo-get-default-hook new-mark-x nil)
  6513. new-mark-x)))
  6514. (beginning-of-line 1)
  6515. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6516. (if org-treat-insert-todo-heading-as-state-change
  6517. (org-todo new-mark)
  6518. (insert new-mark " "))))
  6519. (when org-provide-todo-statistics
  6520. (org-update-parent-todo-statistics))))
  6521. (defun org-insert-subheading (arg)
  6522. "Insert a new subheading and demote it.
  6523. Works for outline headings and for plain lists alike."
  6524. (interactive "P")
  6525. (org-insert-heading arg)
  6526. (cond
  6527. ((org-at-heading-p) (org-do-demote))
  6528. ((org-at-item-p) (org-indent-item))))
  6529. (defun org-insert-todo-subheading (arg)
  6530. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6531. Works for outline headings and for plain lists alike."
  6532. (interactive "P")
  6533. (org-insert-todo-heading arg)
  6534. (cond
  6535. ((org-at-heading-p) (org-do-demote))
  6536. ((org-at-item-p) (org-indent-item))))
  6537. ;;; Promotion and Demotion
  6538. (defvar org-after-demote-entry-hook nil
  6539. "Hook run after an entry has been demoted.
  6540. The cursor will be at the beginning of the entry.
  6541. When a subtree is being demoted, the hook will be called for each node.")
  6542. (defvar org-after-promote-entry-hook nil
  6543. "Hook run after an entry has been promoted.
  6544. The cursor will be at the beginning of the entry.
  6545. When a subtree is being promoted, the hook will be called for each node.")
  6546. (defun org-promote-subtree ()
  6547. "Promote the entire subtree.
  6548. See also `org-promote'."
  6549. (interactive)
  6550. (save-excursion
  6551. (org-with-limited-levels (org-map-tree 'org-promote)))
  6552. (org-fix-position-after-promote))
  6553. (defun org-demote-subtree ()
  6554. "Demote the entire subtree. See `org-demote'.
  6555. See also `org-promote'."
  6556. (interactive)
  6557. (save-excursion
  6558. (org-with-limited-levels (org-map-tree 'org-demote)))
  6559. (org-fix-position-after-promote))
  6560. (defun org-do-promote ()
  6561. "Promote the current heading higher up the tree.
  6562. If the region is active in `transient-mark-mode', promote all headings
  6563. in the region."
  6564. (interactive)
  6565. (save-excursion
  6566. (if (org-region-active-p)
  6567. (org-map-region 'org-promote (region-beginning) (region-end))
  6568. (org-promote)))
  6569. (org-fix-position-after-promote))
  6570. (defun org-do-demote ()
  6571. "Demote the current heading lower down the tree.
  6572. If the region is active in `transient-mark-mode', demote all headings
  6573. in the region."
  6574. (interactive)
  6575. (save-excursion
  6576. (if (org-region-active-p)
  6577. (org-map-region 'org-demote (region-beginning) (region-end))
  6578. (org-demote)))
  6579. (org-fix-position-after-promote))
  6580. (defun org-fix-position-after-promote ()
  6581. "Make sure that after pro/demotion cursor position is right."
  6582. (let ((pos (point)))
  6583. (when (save-excursion
  6584. (beginning-of-line 1)
  6585. (looking-at org-todo-line-regexp)
  6586. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6587. (cond ((eobp) (insert " "))
  6588. ((eolp) (insert " "))
  6589. ((equal (char-after) ?\ ) (forward-char 1))))))
  6590. (defun org-current-level ()
  6591. "Return the level of the current entry, or nil if before the first headline.
  6592. The level is the number of stars at the beginning of the headline."
  6593. (save-excursion
  6594. (org-with-limited-levels
  6595. (if (ignore-errors (org-back-to-heading t))
  6596. (funcall outline-level)))))
  6597. (defun org-get-previous-line-level ()
  6598. "Return the outline depth of the last headline before the current line.
  6599. Returns 0 for the first headline in the buffer, and nil if before the
  6600. first headline."
  6601. (let ((current-level (org-current-level))
  6602. (prev-level (when (> (line-number-at-pos) 1)
  6603. (save-excursion
  6604. (beginning-of-line 0)
  6605. (org-current-level)))))
  6606. (cond ((null current-level) nil) ; Before first headline
  6607. ((null prev-level) 0) ; At first headline
  6608. (prev-level))))
  6609. (defun org-reduced-level (l)
  6610. "Compute the effective level of a heading.
  6611. This takes into account the setting of `org-odd-levels-only'."
  6612. (cond
  6613. ((zerop l) 0)
  6614. (org-odd-levels-only (1+ (floor (/ l 2))))
  6615. (t l)))
  6616. (defun org-level-increment ()
  6617. "Return the number of stars that will be added or removed at a
  6618. time to headlines when structure editing, based on the value of
  6619. `org-odd-levels-only'."
  6620. (if org-odd-levels-only 2 1))
  6621. (defun org-get-valid-level (level &optional change)
  6622. "Rectify a level change under the influence of `org-odd-levels-only'
  6623. LEVEL is a current level, CHANGE is by how much the level should be
  6624. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6625. even level numbers will become the next higher odd number."
  6626. (if org-odd-levels-only
  6627. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6628. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6629. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6630. (max 1 (+ level (or change 0)))))
  6631. (if (boundp 'define-obsolete-function-alias)
  6632. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6633. (define-obsolete-function-alias 'org-get-legal-level
  6634. 'org-get-valid-level)
  6635. (define-obsolete-function-alias 'org-get-legal-level
  6636. 'org-get-valid-level "23.1")))
  6637. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6638. ;; ̀org-with-limited-levels'
  6639. (defun org-promote ()
  6640. "Promote the current heading higher up the tree.
  6641. If the region is active in `transient-mark-mode', promote all headings
  6642. in the region."
  6643. (org-back-to-heading t)
  6644. (let* ((level (save-match-data (funcall outline-level)))
  6645. (after-change-functions (remove 'flyspell-after-change-function
  6646. after-change-functions))
  6647. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6648. (diff (abs (- level (length up-head) -1))))
  6649. (cond ((and (= level 1) org-called-with-limited-levels
  6650. org-allow-promoting-top-level-subtree)
  6651. (replace-match "# " nil t))
  6652. ((= level 1)
  6653. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6654. (t (replace-match up-head nil t)))
  6655. ;; Fixup tag positioning
  6656. (unless (= level 1)
  6657. (and org-auto-align-tags (org-set-tags nil t))
  6658. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6659. (run-hooks 'org-after-promote-entry-hook)))
  6660. (defun org-demote ()
  6661. "Demote the current heading lower down the tree.
  6662. If the region is active in `transient-mark-mode', demote all headings
  6663. in the region."
  6664. (org-back-to-heading t)
  6665. (let* ((level (save-match-data (funcall outline-level)))
  6666. (after-change-functions (remove 'flyspell-after-change-function
  6667. after-change-functions))
  6668. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6669. (diff (abs (- level (length down-head) -1))))
  6670. (replace-match down-head nil t)
  6671. ;; Fixup tag positioning
  6672. (and org-auto-align-tags (org-set-tags nil t))
  6673. (if org-adapt-indentation (org-fixup-indentation diff))
  6674. (run-hooks 'org-after-demote-entry-hook)))
  6675. (defun org-cycle-level ()
  6676. "Cycle the level of an empty headline through possible states.
  6677. This goes first to child, then to parent, level, then up the hierarchy.
  6678. After top level, it switches back to sibling level."
  6679. (interactive)
  6680. (let ((org-adapt-indentation nil))
  6681. (when (org-point-at-end-of-empty-headline)
  6682. (setq this-command 'org-cycle-level) ; Only needed for caching
  6683. (let ((cur-level (org-current-level))
  6684. (prev-level (org-get-previous-line-level)))
  6685. (cond
  6686. ;; If first headline in file, promote to top-level.
  6687. ((= prev-level 0)
  6688. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6689. do (org-do-promote)))
  6690. ;; If same level as prev, demote one.
  6691. ((= prev-level cur-level)
  6692. (org-do-demote))
  6693. ;; If parent is top-level, promote to top level if not already.
  6694. ((= prev-level 1)
  6695. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6696. do (org-do-promote)))
  6697. ;; If top-level, return to prev-level.
  6698. ((= cur-level 1)
  6699. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6700. do (org-do-demote)))
  6701. ;; If less than prev-level, promote one.
  6702. ((< cur-level prev-level)
  6703. (org-do-promote))
  6704. ;; If deeper than prev-level, promote until higher than
  6705. ;; prev-level.
  6706. ((> cur-level prev-level)
  6707. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6708. do (org-do-promote))))
  6709. t))))
  6710. (defun org-map-tree (fun)
  6711. "Call FUN for every heading underneath the current one."
  6712. (org-back-to-heading)
  6713. (let ((level (funcall outline-level)))
  6714. (save-excursion
  6715. (funcall fun)
  6716. (while (and (progn
  6717. (outline-next-heading)
  6718. (> (funcall outline-level) level))
  6719. (not (eobp)))
  6720. (funcall fun)))))
  6721. (defun org-map-region (fun beg end)
  6722. "Call FUN for every heading between BEG and END."
  6723. (let ((org-ignore-region t))
  6724. (save-excursion
  6725. (setq end (copy-marker end))
  6726. (goto-char beg)
  6727. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6728. (< (point) end))
  6729. (funcall fun))
  6730. (while (and (progn
  6731. (outline-next-heading)
  6732. (< (point) end))
  6733. (not (eobp)))
  6734. (funcall fun)))))
  6735. (defvar org-property-end-re) ; silence byte-compiler
  6736. (defun org-fixup-indentation (diff)
  6737. "Change the indentation in the current entry by DIFF.
  6738. However, if any line in the current entry has no indentation, or if it
  6739. would end up with no indentation after the change, nothing at all is done."
  6740. (save-excursion
  6741. (let ((end (save-excursion (outline-next-heading)
  6742. (point-marker)))
  6743. (prohibit (if (> diff 0)
  6744. "^\\S-"
  6745. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6746. col)
  6747. (unless (save-excursion (end-of-line 1)
  6748. (re-search-forward prohibit end t))
  6749. (while (and (< (point) end)
  6750. (re-search-forward "^[ \t]+" end t))
  6751. (goto-char (match-end 0))
  6752. (setq col (current-column))
  6753. (if (< diff 0) (replace-match ""))
  6754. (org-indent-to-column (+ diff col))))
  6755. (move-marker end nil))))
  6756. (defun org-convert-to-odd-levels ()
  6757. "Convert an org-mode file with all levels allowed to one with odd levels.
  6758. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6759. level 5 etc."
  6760. (interactive)
  6761. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6762. (let ((outline-level 'org-outline-level)
  6763. (org-odd-levels-only nil) n)
  6764. (save-excursion
  6765. (goto-char (point-min))
  6766. (while (re-search-forward "^\\*\\*+ " nil t)
  6767. (setq n (- (length (match-string 0)) 2))
  6768. (while (>= (setq n (1- n)) 0)
  6769. (org-demote))
  6770. (end-of-line 1))))))
  6771. (defun org-convert-to-oddeven-levels ()
  6772. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6773. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6774. file contains a section with an even level, conversion would
  6775. destroy the structure of the file. An error is signaled in this
  6776. case."
  6777. (interactive)
  6778. (goto-char (point-min))
  6779. ;; First check if there are no even levels
  6780. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6781. (org-show-context t)
  6782. (error "Not all levels are odd in this file. Conversion not possible"))
  6783. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6784. (let ((outline-regexp org-outline-regexp)
  6785. (outline-level 'org-outline-level)
  6786. (org-odd-levels-only nil) n)
  6787. (save-excursion
  6788. (goto-char (point-min))
  6789. (while (re-search-forward "^\\*\\*+ " nil t)
  6790. (setq n (/ (1- (length (match-string 0))) 2))
  6791. (while (>= (setq n (1- n)) 0)
  6792. (org-promote))
  6793. (end-of-line 1))))))
  6794. (defun org-tr-level (n)
  6795. "Make N odd if required."
  6796. (if org-odd-levels-only (1+ (/ n 2)) n))
  6797. ;;; Vertical tree motion, cutting and pasting of subtrees
  6798. (defun org-move-subtree-up (&optional arg)
  6799. "Move the current subtree up past ARG headlines of the same level."
  6800. (interactive "p")
  6801. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6802. (defun org-move-subtree-down (&optional arg)
  6803. "Move the current subtree down past ARG headlines of the same level."
  6804. (interactive "p")
  6805. (setq arg (prefix-numeric-value arg))
  6806. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6807. 'org-get-last-sibling))
  6808. (ins-point (make-marker))
  6809. (cnt (abs arg))
  6810. (col (current-column))
  6811. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6812. ;; Select the tree
  6813. (org-back-to-heading)
  6814. (setq beg0 (point))
  6815. (save-excursion
  6816. (setq ne-beg (org-back-over-empty-lines))
  6817. (setq beg (point)))
  6818. (save-match-data
  6819. (save-excursion (outline-end-of-heading)
  6820. (setq folded (outline-invisible-p)))
  6821. (outline-end-of-subtree))
  6822. (outline-next-heading)
  6823. (setq ne-end (org-back-over-empty-lines))
  6824. (setq end (point))
  6825. (goto-char beg0)
  6826. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6827. ;; include less whitespace
  6828. (save-excursion
  6829. (goto-char beg)
  6830. (forward-line (- ne-beg ne-end))
  6831. (setq beg (point))))
  6832. ;; Find insertion point, with error handling
  6833. (while (> cnt 0)
  6834. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6835. (progn (goto-char beg0)
  6836. (error "Cannot move past superior level or buffer limit")))
  6837. (setq cnt (1- cnt)))
  6838. (if (> arg 0)
  6839. ;; Moving forward - still need to move over subtree
  6840. (progn (org-end-of-subtree t t)
  6841. (save-excursion
  6842. (org-back-over-empty-lines)
  6843. (or (bolp) (newline)))))
  6844. (setq ne-ins (org-back-over-empty-lines))
  6845. (move-marker ins-point (point))
  6846. (setq txt (buffer-substring beg end))
  6847. (org-save-markers-in-region beg end)
  6848. (delete-region beg end)
  6849. (org-remove-empty-overlays-at beg)
  6850. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6851. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6852. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6853. (let ((bbb (point)))
  6854. (insert-before-markers txt)
  6855. (org-reinstall-markers-in-region bbb)
  6856. (move-marker ins-point bbb))
  6857. (or (bolp) (insert "\n"))
  6858. (setq ins-end (point))
  6859. (goto-char ins-point)
  6860. (org-skip-whitespace)
  6861. (when (and (< arg 0)
  6862. (org-first-sibling-p)
  6863. (> ne-ins ne-beg))
  6864. ;; Move whitespace back to beginning
  6865. (save-excursion
  6866. (goto-char ins-end)
  6867. (let ((kill-whole-line t))
  6868. (kill-line (- ne-ins ne-beg)) (point)))
  6869. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6870. (move-marker ins-point nil)
  6871. (if folded
  6872. (hide-subtree)
  6873. (org-show-entry)
  6874. (show-children)
  6875. (org-cycle-hide-drawers 'children))
  6876. (org-clean-visibility-after-subtree-move)
  6877. ;; move back to the initial column we were at
  6878. (move-to-column col)))
  6879. (defvar org-subtree-clip ""
  6880. "Clipboard for cut and paste of subtrees.
  6881. This is actually only a copy of the kill, because we use the normal kill
  6882. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6883. (defvar org-subtree-clip-folded nil
  6884. "Was the last copied subtree folded?
  6885. This is used to fold the tree back after pasting.")
  6886. (defun org-cut-subtree (&optional n)
  6887. "Cut the current subtree into the clipboard.
  6888. With prefix arg N, cut this many sequential subtrees.
  6889. This is a short-hand for marking the subtree and then cutting it."
  6890. (interactive "p")
  6891. (org-copy-subtree n 'cut))
  6892. (defun org-copy-subtree (&optional n cut force-store-markers)
  6893. "Cut the current subtree into the clipboard.
  6894. With prefix arg N, cut this many sequential subtrees.
  6895. This is a short-hand for marking the subtree and then copying it.
  6896. If CUT is non-nil, actually cut the subtree.
  6897. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6898. of some markers in the region, even if CUT is non-nil. This is
  6899. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6900. (interactive "p")
  6901. (let (beg end folded (beg0 (point)))
  6902. (if (org-called-interactively-p 'any)
  6903. (org-back-to-heading nil) ; take what looks like a subtree
  6904. (org-back-to-heading t)) ; take what is really there
  6905. (setq beg (point))
  6906. (skip-chars-forward " \t\r\n")
  6907. (save-match-data
  6908. (save-excursion (outline-end-of-heading)
  6909. (setq folded (outline-invisible-p)))
  6910. (condition-case nil
  6911. (org-forward-heading-same-level (1- n) t)
  6912. (error nil))
  6913. (org-end-of-subtree t t))
  6914. (setq end (point))
  6915. (goto-char beg0)
  6916. (when (> end beg)
  6917. (setq org-subtree-clip-folded folded)
  6918. (when (or cut force-store-markers)
  6919. (org-save-markers-in-region beg end))
  6920. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6921. (setq org-subtree-clip (current-kill 0))
  6922. (message "%s: Subtree(s) with %d characters"
  6923. (if cut "Cut" "Copied")
  6924. (length org-subtree-clip)))))
  6925. (defun org-paste-subtree (&optional level tree for-yank)
  6926. "Paste the clipboard as a subtree, with modification of headline level.
  6927. The entire subtree is promoted or demoted in order to match a new headline
  6928. level.
  6929. If the cursor is at the beginning of a headline, the same level as
  6930. that headline is used to paste the tree
  6931. If not, the new level is derived from the *visible* headings
  6932. before and after the insertion point, and taken to be the inferior headline
  6933. level of the two. So if the previous visible heading is level 3 and the
  6934. next is level 4 (or vice versa), level 4 will be used for insertion.
  6935. This makes sure that the subtree remains an independent subtree and does
  6936. not swallow low level entries.
  6937. You can also force a different level, either by using a numeric prefix
  6938. argument, or by inserting the heading marker by hand. For example, if the
  6939. cursor is after \"*****\", then the tree will be shifted to level 5.
  6940. If optional TREE is given, use this text instead of the kill ring.
  6941. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6942. move back over whitespace before inserting, and move point to the end of
  6943. the inserted text when done."
  6944. (interactive "P")
  6945. (setq tree (or tree (and kill-ring (current-kill 0))))
  6946. (unless (org-kill-is-subtree-p tree)
  6947. (error "%s"
  6948. (substitute-command-keys
  6949. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6950. (org-with-limited-levels
  6951. (let* ((visp (not (outline-invisible-p)))
  6952. (txt tree)
  6953. (^re_ "\\(\\*+\\)[ \t]*")
  6954. (old-level (if (string-match org-outline-regexp-bol txt)
  6955. (- (match-end 0) (match-beginning 0) 1)
  6956. -1))
  6957. (force-level (cond (level (prefix-numeric-value level))
  6958. ((and (looking-at "[ \t]*$")
  6959. (string-match
  6960. "^\\*+$" (buffer-substring
  6961. (point-at-bol) (point))))
  6962. (- (match-end 1) (match-beginning 1)))
  6963. ((and (bolp)
  6964. (looking-at org-outline-regexp))
  6965. (- (match-end 0) (point) 1))))
  6966. (previous-level (save-excursion
  6967. (condition-case nil
  6968. (progn
  6969. (outline-previous-visible-heading 1)
  6970. (if (looking-at ^re_)
  6971. (- (match-end 0) (match-beginning 0) 1)
  6972. 1))
  6973. (error 1))))
  6974. (next-level (save-excursion
  6975. (condition-case nil
  6976. (progn
  6977. (or (looking-at org-outline-regexp)
  6978. (outline-next-visible-heading 1))
  6979. (if (looking-at ^re_)
  6980. (- (match-end 0) (match-beginning 0) 1)
  6981. 1))
  6982. (error 1))))
  6983. (new-level (or force-level (max previous-level next-level)))
  6984. (shift (if (or (= old-level -1)
  6985. (= new-level -1)
  6986. (= old-level new-level))
  6987. 0
  6988. (- new-level old-level)))
  6989. (delta (if (> shift 0) -1 1))
  6990. (func (if (> shift 0) 'org-demote 'org-promote))
  6991. (org-odd-levels-only nil)
  6992. beg end newend)
  6993. ;; Remove the forced level indicator
  6994. (if force-level
  6995. (delete-region (point-at-bol) (point)))
  6996. ;; Paste
  6997. (beginning-of-line (if (bolp) 1 2))
  6998. (setq beg (point))
  6999. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7000. (insert-before-markers txt)
  7001. (unless (string-match "\n\\'" txt) (insert "\n"))
  7002. (setq newend (point))
  7003. (org-reinstall-markers-in-region beg)
  7004. (setq end (point))
  7005. (goto-char beg)
  7006. (skip-chars-forward " \t\n\r")
  7007. (setq beg (point))
  7008. (if (and (outline-invisible-p) visp)
  7009. (save-excursion (outline-show-heading)))
  7010. ;; Shift if necessary
  7011. (unless (= shift 0)
  7012. (save-restriction
  7013. (narrow-to-region beg end)
  7014. (while (not (= shift 0))
  7015. (org-map-region func (point-min) (point-max))
  7016. (setq shift (+ delta shift)))
  7017. (goto-char (point-min))
  7018. (setq newend (point-max))))
  7019. (when (or (org-called-interactively-p 'interactive) for-yank)
  7020. (message "Clipboard pasted as level %d subtree" new-level))
  7021. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7022. kill-ring
  7023. (eq org-subtree-clip (current-kill 0))
  7024. org-subtree-clip-folded)
  7025. ;; The tree was folded before it was killed/copied
  7026. (hide-subtree))
  7027. (and for-yank (goto-char newend)))))
  7028. (defun org-kill-is-subtree-p (&optional txt)
  7029. "Check if the current kill is an outline subtree, or a set of trees.
  7030. Returns nil if kill does not start with a headline, or if the first
  7031. headline level is not the largest headline level in the tree.
  7032. So this will actually accept several entries of equal levels as well,
  7033. which is OK for `org-paste-subtree'.
  7034. If optional TXT is given, check this string instead of the current kill."
  7035. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7036. (re (org-get-limited-outline-regexp))
  7037. (^re (concat "^" re))
  7038. (start-level (and kill
  7039. (string-match
  7040. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7041. kill)
  7042. (- (match-end 2) (match-beginning 2) 1)))
  7043. (start (1+ (or (match-beginning 2) -1))))
  7044. (if (not start-level)
  7045. (progn
  7046. nil) ;; does not even start with a heading
  7047. (catch 'exit
  7048. (while (setq start (string-match ^re kill (1+ start)))
  7049. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7050. (throw 'exit nil)))
  7051. t))))
  7052. (defvar org-markers-to-move nil
  7053. "Markers that should be moved with a cut-and-paste operation.
  7054. Those markers are stored together with their positions relative to
  7055. the start of the region.")
  7056. (defun org-save-markers-in-region (beg end)
  7057. "Check markers in region.
  7058. If these markers are between BEG and END, record their position relative
  7059. to BEG, so that after moving the block of text, we can put the markers back
  7060. into place.
  7061. This function gets called just before an entry or tree gets cut from the
  7062. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7063. called immediately, to move the markers with the entries."
  7064. (setq org-markers-to-move nil)
  7065. (when (featurep 'org-clock)
  7066. (org-clock-save-markers-for-cut-and-paste beg end))
  7067. (when (featurep 'org-agenda)
  7068. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7069. (defun org-check-and-save-marker (marker beg end)
  7070. "Check if MARKER is between BEG and END.
  7071. If yes, remember the marker and the distance to BEG."
  7072. (when (and (marker-buffer marker)
  7073. (equal (marker-buffer marker) (current-buffer)))
  7074. (if (and (>= marker beg) (< marker end))
  7075. (push (cons marker (- marker beg)) org-markers-to-move))))
  7076. (defun org-reinstall-markers-in-region (beg)
  7077. "Move all remembered markers to their position relative to BEG."
  7078. (mapc (lambda (x)
  7079. (move-marker (car x) (+ beg (cdr x))))
  7080. org-markers-to-move)
  7081. (setq org-markers-to-move nil))
  7082. (defun org-narrow-to-subtree ()
  7083. "Narrow buffer to the current subtree."
  7084. (interactive)
  7085. (save-excursion
  7086. (save-match-data
  7087. (org-with-limited-levels
  7088. (narrow-to-region
  7089. (progn (org-back-to-heading t) (point))
  7090. (progn (org-end-of-subtree t t)
  7091. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7092. (point)))))))
  7093. (defun org-narrow-to-block ()
  7094. "Narrow buffer to the current block."
  7095. (interactive)
  7096. (let* ((case-fold-search t)
  7097. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7098. "^[ \t]*#\\+end_.*")))
  7099. (if blockp
  7100. (narrow-to-region (car blockp) (cdr blockp))
  7101. (error "Not in a block"))))
  7102. (eval-when-compile
  7103. (defvar org-property-drawer-re))
  7104. (defvar org-property-start-re) ;; defined below
  7105. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7106. "Clone the task (subtree) at point N times.
  7107. The clones will be inserted as siblings.
  7108. In interactive use, the user will be prompted for the number of
  7109. clones to be produced, and for a time SHIFT, which may be a
  7110. repeater as used in time stamps, for example `+3d'.
  7111. When a valid repeater is given and the entry contains any time
  7112. stamps, the clones will become a sequence in time, with time
  7113. stamps in the subtree shifted for each clone produced. If SHIFT
  7114. is nil or the empty string, time stamps will be left alone. The
  7115. ID property of the original subtree is removed.
  7116. If the original subtree did contain time stamps with a repeater,
  7117. the following will happen:
  7118. - the repeater will be removed in each clone
  7119. - an additional clone will be produced, with the current, unshifted
  7120. date(s) in the entry.
  7121. - the original entry will be placed *after* all the clones, with
  7122. repeater intact.
  7123. - the start days in the repeater in the original entry will be shifted
  7124. to past the last clone.
  7125. In this way you can spell out a number of instances of a repeating task,
  7126. and still retain the repeater to cover future instances of the task."
  7127. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7128. (let (beg end template task idprop
  7129. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7130. (drawer-re org-drawer-regexp))
  7131. (if (not (and (integerp n) (> n 0)))
  7132. (error "Invalid number of replications %s" n))
  7133. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7134. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7135. shift)))
  7136. (error "Invalid shift specification %s" shift))
  7137. (when doshift
  7138. (setq shift-n (string-to-number (match-string 1 shift))
  7139. shift-what (cdr (assoc (match-string 2 shift)
  7140. '(("d" . day) ("w" . week)
  7141. ("m" . month) ("y" . year))))))
  7142. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7143. (setq nmin 1 nmax n)
  7144. (org-back-to-heading t)
  7145. (setq beg (point))
  7146. (setq idprop (org-entry-get nil "ID"))
  7147. (org-end-of-subtree t t)
  7148. (or (bolp) (insert "\n"))
  7149. (setq end (point))
  7150. (setq template (buffer-substring beg end))
  7151. (when (and doshift
  7152. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7153. (delete-region beg end)
  7154. (setq end beg)
  7155. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7156. (goto-char end)
  7157. (loop for n from nmin to nmax do
  7158. ;; prepare clone
  7159. (with-temp-buffer
  7160. (insert template)
  7161. (org-mode)
  7162. (goto-char (point-min))
  7163. (org-show-subtree)
  7164. (and idprop (if org-clone-delete-id
  7165. (org-entry-delete nil "ID")
  7166. (org-id-get-create t)))
  7167. (unless (= n 0)
  7168. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7169. (kill-whole-line))
  7170. (goto-char (point-min))
  7171. (while (re-search-forward drawer-re nil t)
  7172. (mapc (lambda (d)
  7173. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7174. (goto-char (point-min))
  7175. (when doshift
  7176. (while (re-search-forward org-ts-regexp-both nil t)
  7177. (org-timestamp-change (* n shift-n) shift-what))
  7178. (unless (= n n-no-remove)
  7179. (goto-char (point-min))
  7180. (while (re-search-forward org-ts-regexp nil t)
  7181. (save-excursion
  7182. (goto-char (match-beginning 0))
  7183. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7184. (delete-region (match-beginning 1) (match-end 1)))))))
  7185. (setq task (buffer-string)))
  7186. (insert task))
  7187. (goto-char beg)))
  7188. ;;; Outline Sorting
  7189. (defun org-sort (with-case)
  7190. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7191. Optional argument WITH-CASE means sort case-sensitively."
  7192. (interactive "P")
  7193. (cond
  7194. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7195. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7196. (t
  7197. (org-call-with-arg 'org-sort-entries with-case))))
  7198. (defun org-sort-remove-invisible (s)
  7199. (remove-text-properties 0 (length s) org-rm-props s)
  7200. (while (string-match org-bracket-link-regexp s)
  7201. (setq s (replace-match (if (match-end 2)
  7202. (match-string 3 s)
  7203. (match-string 1 s)) t t s)))
  7204. s)
  7205. (defvar org-priority-regexp) ; defined later in the file
  7206. (defvar org-after-sorting-entries-or-items-hook nil
  7207. "Hook that is run after a bunch of entries or items have been sorted.
  7208. When children are sorted, the cursor is in the parent line when this
  7209. hook gets called. When a region or a plain list is sorted, the cursor
  7210. will be in the first entry of the sorted region/list.")
  7211. (defun org-sort-entries
  7212. (&optional with-case sorting-type getkey-func compare-func property)
  7213. "Sort entries on a certain level of an outline tree.
  7214. If there is an active region, the entries in the region are sorted.
  7215. Else, if the cursor is before the first entry, sort the top-level items.
  7216. Else, the children of the entry at point are sorted.
  7217. Sorting can be alphabetically, numerically, by date/time as given by
  7218. a time stamp, by a property or by priority.
  7219. The command prompts for the sorting type unless it has been given to the
  7220. function through the SORTING-TYPE argument, which needs to be a character,
  7221. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7222. precise meaning of each character:
  7223. n Numerically, by converting the beginning of the entry/item to a number.
  7224. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7225. o By order of TODO keywords.
  7226. t By date/time, either the first active time stamp in the entry, or, if
  7227. none exist, by the first inactive one.
  7228. s By the scheduled date/time.
  7229. d By deadline date/time.
  7230. c By creation time, which is assumed to be the first inactive time stamp
  7231. at the beginning of a line.
  7232. p By priority according to the cookie.
  7233. r By the value of a property.
  7234. Capital letters will reverse the sort order.
  7235. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7236. called with point at the beginning of the record. It must return either
  7237. a string or a number that should serve as the sorting key for that record.
  7238. Comparing entries ignores case by default. However, with an optional argument
  7239. WITH-CASE, the sorting considers case as well."
  7240. (interactive "P")
  7241. (let ((case-func (if with-case 'identity 'downcase))
  7242. (cmstr
  7243. ;; The clock marker is lost when using `sort-subr', let's
  7244. ;; store the clocking string.
  7245. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7246. (save-excursion
  7247. (goto-char org-clock-marker)
  7248. (looking-back "^.*") (match-string-no-properties 0))))
  7249. start beg end stars re re2
  7250. txt what tmp)
  7251. ;; Find beginning and end of region to sort
  7252. (cond
  7253. ((org-region-active-p)
  7254. ;; we will sort the region
  7255. (setq end (region-end)
  7256. what "region")
  7257. (goto-char (region-beginning))
  7258. (if (not (org-at-heading-p)) (outline-next-heading))
  7259. (setq start (point)))
  7260. ((or (org-at-heading-p)
  7261. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7262. ;; we will sort the children of the current headline
  7263. (org-back-to-heading)
  7264. (setq start (point)
  7265. end (progn (org-end-of-subtree t t)
  7266. (or (bolp) (insert "\n"))
  7267. (org-back-over-empty-lines)
  7268. (point))
  7269. what "children")
  7270. (goto-char start)
  7271. (show-subtree)
  7272. (outline-next-heading))
  7273. (t
  7274. ;; we will sort the top-level entries in this file
  7275. (goto-char (point-min))
  7276. (or (org-at-heading-p) (outline-next-heading))
  7277. (setq start (point))
  7278. (goto-char (point-max))
  7279. (beginning-of-line 1)
  7280. (when (looking-at ".*?\\S-")
  7281. ;; File ends in a non-white line
  7282. (end-of-line 1)
  7283. (insert "\n"))
  7284. (setq end (point-max))
  7285. (setq what "top-level")
  7286. (goto-char start)
  7287. (show-all)))
  7288. (setq beg (point))
  7289. (if (>= beg end) (error "Nothing to sort"))
  7290. (looking-at "\\(\\*+\\)")
  7291. (setq stars (match-string 1)
  7292. re (concat "^" (regexp-quote stars) " +")
  7293. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7294. txt (buffer-substring beg end))
  7295. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7296. (if (and (not (equal stars "*")) (string-match re2 txt))
  7297. (error "Region to sort contains a level above the first entry"))
  7298. (unless sorting-type
  7299. (message
  7300. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7301. [t]ime [s]cheduled [d]eadline [c]reated
  7302. A/N/P/R/O/F/T/S/D/C means reversed:"
  7303. what)
  7304. (setq sorting-type (read-char-exclusive))
  7305. (and (= (downcase sorting-type) ?f)
  7306. (setq getkey-func
  7307. (org-icompleting-read "Sort using function: "
  7308. obarray 'fboundp t nil nil))
  7309. (setq getkey-func (intern getkey-func)))
  7310. (and (= (downcase sorting-type) ?r)
  7311. (setq property
  7312. (org-icompleting-read "Property: "
  7313. (mapcar 'list (org-buffer-property-keys t))
  7314. nil t))))
  7315. (message "Sorting entries...")
  7316. (save-restriction
  7317. (narrow-to-region start end)
  7318. (let ((dcst (downcase sorting-type))
  7319. (case-fold-search nil)
  7320. (now (current-time)))
  7321. (sort-subr
  7322. (/= dcst sorting-type)
  7323. ;; This function moves to the beginning character of the "record" to
  7324. ;; be sorted.
  7325. (lambda nil
  7326. (if (re-search-forward re nil t)
  7327. (goto-char (match-beginning 0))
  7328. (goto-char (point-max))))
  7329. ;; This function moves to the last character of the "record" being
  7330. ;; sorted.
  7331. (lambda nil
  7332. (save-match-data
  7333. (condition-case nil
  7334. (outline-forward-same-level 1)
  7335. (error
  7336. (goto-char (point-max))))))
  7337. ;; This function returns the value that gets sorted against.
  7338. (lambda nil
  7339. (cond
  7340. ((= dcst ?n)
  7341. (if (looking-at org-complex-heading-regexp)
  7342. (string-to-number (match-string 4))
  7343. nil))
  7344. ((= dcst ?a)
  7345. (if (looking-at org-complex-heading-regexp)
  7346. (funcall case-func (match-string 4))
  7347. nil))
  7348. ((= dcst ?t)
  7349. (let ((end (save-excursion (outline-next-heading) (point))))
  7350. (if (or (re-search-forward org-ts-regexp end t)
  7351. (re-search-forward org-ts-regexp-both end t))
  7352. (org-time-string-to-seconds (match-string 0))
  7353. (org-float-time now))))
  7354. ((= dcst ?c)
  7355. (let ((end (save-excursion (outline-next-heading) (point))))
  7356. (if (re-search-forward
  7357. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7358. end t)
  7359. (org-time-string-to-seconds (match-string 0))
  7360. (org-float-time now))))
  7361. ((= dcst ?s)
  7362. (let ((end (save-excursion (outline-next-heading) (point))))
  7363. (if (re-search-forward org-scheduled-time-regexp end t)
  7364. (org-time-string-to-seconds (match-string 1))
  7365. (org-float-time now))))
  7366. ((= dcst ?d)
  7367. (let ((end (save-excursion (outline-next-heading) (point))))
  7368. (if (re-search-forward org-deadline-time-regexp end t)
  7369. (org-time-string-to-seconds (match-string 1))
  7370. (org-float-time now))))
  7371. ((= dcst ?p)
  7372. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7373. (string-to-char (match-string 2))
  7374. org-default-priority))
  7375. ((= dcst ?r)
  7376. (or (org-entry-get nil property) ""))
  7377. ((= dcst ?o)
  7378. (if (looking-at org-complex-heading-regexp)
  7379. (- 9999 (length (member (match-string 2)
  7380. org-todo-keywords-1)))))
  7381. ((= dcst ?f)
  7382. (if getkey-func
  7383. (progn
  7384. (setq tmp (funcall getkey-func))
  7385. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7386. tmp)
  7387. (error "Invalid key function `%s'" getkey-func)))
  7388. (t (error "Invalid sorting type `%c'" sorting-type))))
  7389. nil
  7390. (cond
  7391. ((= dcst ?a) 'string<)
  7392. ((= dcst ?f) compare-func)
  7393. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7394. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7395. ;; Reset the clock marker if needed
  7396. (when cmstr
  7397. (save-excursion
  7398. (goto-char start)
  7399. (search-forward cmstr nil t)
  7400. (move-marker org-clock-marker (point))))
  7401. (message "Sorting entries...done")))
  7402. (defun org-do-sort (table what &optional with-case sorting-type)
  7403. "Sort TABLE of WHAT according to SORTING-TYPE.
  7404. The user will be prompted for the SORTING-TYPE if the call to this
  7405. function does not specify it. WHAT is only for the prompt, to indicate
  7406. what is being sorted. The sorting key will be extracted from
  7407. the car of the elements of the table.
  7408. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7409. (unless sorting-type
  7410. (message
  7411. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7412. what)
  7413. (setq sorting-type (read-char-exclusive)))
  7414. (let ((dcst (downcase sorting-type))
  7415. extractfun comparefun)
  7416. ;; Define the appropriate functions
  7417. (cond
  7418. ((= dcst ?n)
  7419. (setq extractfun 'string-to-number
  7420. comparefun (if (= dcst sorting-type) '< '>)))
  7421. ((= dcst ?a)
  7422. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7423. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7424. comparefun (if (= dcst sorting-type)
  7425. 'string<
  7426. (lambda (a b) (and (not (string< a b))
  7427. (not (string= a b)))))))
  7428. ((= dcst ?t)
  7429. (setq extractfun
  7430. (lambda (x)
  7431. (if (or (string-match org-ts-regexp x)
  7432. (string-match org-ts-regexp-both x))
  7433. (org-float-time
  7434. (org-time-string-to-time (match-string 0 x)))
  7435. 0))
  7436. comparefun (if (= dcst sorting-type) '< '>)))
  7437. (t (error "Invalid sorting type `%c'" sorting-type)))
  7438. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7439. table)
  7440. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7441. ;;; The orgstruct minor mode
  7442. ;; Define a minor mode which can be used in other modes in order to
  7443. ;; integrate the org-mode structure editing commands.
  7444. ;; This is really a hack, because the org-mode structure commands use
  7445. ;; keys which normally belong to the major mode. Here is how it
  7446. ;; works: The minor mode defines all the keys necessary to operate the
  7447. ;; structure commands, but wraps the commands into a function which
  7448. ;; tests if the cursor is currently at a headline or a plain list
  7449. ;; item. If that is the case, the structure command is used,
  7450. ;; temporarily setting many Org-mode variables like regular
  7451. ;; expressions for filling etc. However, when any of those keys is
  7452. ;; used at a different location, function uses `key-binding' to look
  7453. ;; up if the key has an associated command in another currently active
  7454. ;; keymap (minor modes, major mode, global), and executes that
  7455. ;; command. There might be problems if any of the keys is otherwise
  7456. ;; used as a prefix key.
  7457. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7458. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7459. ;; addresses this by checking explicitly for both bindings.
  7460. (defvar orgstruct-mode-map (make-sparse-keymap)
  7461. "Keymap for the minor `orgstruct-mode'.")
  7462. (defvar org-local-vars nil
  7463. "List of local variables, for use by `orgstruct-mode'.")
  7464. ;;;###autoload
  7465. (define-minor-mode orgstruct-mode
  7466. "Toggle the minor mode `orgstruct-mode'.
  7467. This mode is for using Org-mode structure commands in other
  7468. modes. The following keys behave as if Org-mode were active, if
  7469. the cursor is on a headline, or on a plain list item (both as
  7470. defined by Org-mode).
  7471. M-up Move entry/item up
  7472. M-down Move entry/item down
  7473. M-left Promote
  7474. M-right Demote
  7475. M-S-up Move entry/item up
  7476. M-S-down Move entry/item down
  7477. M-S-left Promote subtree
  7478. M-S-right Demote subtree
  7479. M-q Fill paragraph and items like in Org-mode
  7480. C-c ^ Sort entries
  7481. C-c - Cycle list bullet
  7482. TAB Cycle item visibility
  7483. M-RET Insert new heading/item
  7484. S-M-RET Insert new TODO heading / Checkbox item
  7485. C-c C-c Set tags / toggle checkbox"
  7486. nil " OrgStruct" nil
  7487. (org-load-modules-maybe)
  7488. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7489. ;;;###autoload
  7490. (defun turn-on-orgstruct ()
  7491. "Unconditionally turn on `orgstruct-mode'."
  7492. (orgstruct-mode 1))
  7493. (defvar org-fb-vars nil)
  7494. (make-variable-buffer-local 'org-fb-vars)
  7495. (defun orgstruct++-mode (&optional arg)
  7496. "Toggle `orgstruct-mode', the enhanced version of it.
  7497. In addition to setting orgstruct-mode, this also exports all
  7498. indentation and autofilling variables from org-mode into the
  7499. buffer. It will also recognize item context in multiline items."
  7500. (interactive "P")
  7501. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7502. (if (< arg 1)
  7503. (progn (orgstruct-mode -1)
  7504. (mapc (lambda(v)
  7505. (org-set-local (car v)
  7506. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7507. org-fb-vars))
  7508. (orgstruct-mode 1)
  7509. (setq org-fb-vars nil)
  7510. (let (var val)
  7511. (mapc
  7512. (lambda (x)
  7513. (when (string-match
  7514. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7515. (symbol-name (car x)))
  7516. (setq var (car x) val (nth 1 x))
  7517. (push (list var `(quote ,(eval var))) org-fb-vars)
  7518. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7519. org-local-vars)
  7520. (org-set-local 'orgstruct-is-++ t))))
  7521. (defvar orgstruct-is-++ nil
  7522. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7523. (make-variable-buffer-local 'orgstruct-is-++)
  7524. ;;;###autoload
  7525. (defun turn-on-orgstruct++ ()
  7526. "Unconditionally turn on `orgstruct++-mode'."
  7527. (orgstruct++-mode 1))
  7528. (defun orgstruct-error ()
  7529. "Error when there is no default binding for a structure key."
  7530. (interactive)
  7531. (error "This key has no function outside structure elements"))
  7532. (defun orgstruct-setup ()
  7533. "Setup orgstruct keymaps."
  7534. (let ((nfunc 0)
  7535. (bindings
  7536. (list
  7537. '([(meta up)] org-metaup)
  7538. '([(meta down)] org-metadown)
  7539. '([(meta left)] org-metaleft)
  7540. '([(meta right)] org-metaright)
  7541. '([(meta shift up)] org-shiftmetaup)
  7542. '([(meta shift down)] org-shiftmetadown)
  7543. '([(meta shift left)] org-shiftmetaleft)
  7544. '([(meta shift right)] org-shiftmetaright)
  7545. '([?\e (up)] org-metaup)
  7546. '([?\e (down)] org-metadown)
  7547. '([?\e (left)] org-metaleft)
  7548. '([?\e (right)] org-metaright)
  7549. '([?\e (shift up)] org-shiftmetaup)
  7550. '([?\e (shift down)] org-shiftmetadown)
  7551. '([?\e (shift left)] org-shiftmetaleft)
  7552. '([?\e (shift right)] org-shiftmetaright)
  7553. '([(shift up)] org-shiftup)
  7554. '([(shift down)] org-shiftdown)
  7555. '([(shift left)] org-shiftleft)
  7556. '([(shift right)] org-shiftright)
  7557. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7558. '("\M-q" fill-paragraph)
  7559. '("\C-c^" org-sort)
  7560. '("\C-c-" org-cycle-list-bullet)))
  7561. elt key fun cmd)
  7562. (while (setq elt (pop bindings))
  7563. (setq nfunc (1+ nfunc))
  7564. (setq key (org-key (car elt))
  7565. fun (nth 1 elt)
  7566. cmd (orgstruct-make-binding fun nfunc key))
  7567. (org-defkey orgstruct-mode-map key cmd))
  7568. ;; Prevent an error for users who forgot to make autoloads
  7569. (require 'org-element)
  7570. ;; Special treatment needed for TAB and RET
  7571. (org-defkey orgstruct-mode-map [(tab)]
  7572. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7573. (org-defkey orgstruct-mode-map "\C-i"
  7574. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7575. (org-defkey orgstruct-mode-map "\M-\C-m"
  7576. (orgstruct-make-binding 'org-insert-heading 105
  7577. "\M-\C-m" [(meta return)]))
  7578. (org-defkey orgstruct-mode-map [(meta return)]
  7579. (orgstruct-make-binding 'org-insert-heading 106
  7580. [(meta return)] "\M-\C-m"))
  7581. (org-defkey orgstruct-mode-map [(shift meta return)]
  7582. (orgstruct-make-binding 'org-insert-todo-heading 107
  7583. [(meta return)] "\M-\C-m"))
  7584. (org-defkey orgstruct-mode-map "\e\C-m"
  7585. (orgstruct-make-binding 'org-insert-heading 108
  7586. "\e\C-m" [?\e (return)]))
  7587. (org-defkey orgstruct-mode-map [?\e (return)]
  7588. (orgstruct-make-binding 'org-insert-heading 109
  7589. [?\e (return)] "\e\C-m"))
  7590. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7591. (orgstruct-make-binding 'org-insert-todo-heading 110
  7592. [?\e (return)] "\e\C-m"))
  7593. (unless org-local-vars
  7594. (setq org-local-vars (org-get-local-variables)))
  7595. t))
  7596. (defun orgstruct-make-binding (fun n &rest keys)
  7597. "Create a function for binding in the structure minor mode.
  7598. FUN is the command to call inside a table. N is used to create a unique
  7599. command name. KEYS are keys that should be checked in for a command
  7600. to execute outside of tables."
  7601. (eval
  7602. (list 'defun
  7603. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7604. '(arg)
  7605. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7606. "Outside of structure, run the binding of `"
  7607. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7608. "'.")
  7609. '(interactive "p")
  7610. (list 'if
  7611. `(org-context-p 'headline 'item
  7612. (and orgstruct-is-++
  7613. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7614. 'item-body))
  7615. (list 'org-run-like-in-org-mode (list 'quote fun))
  7616. (list 'let '(orgstruct-mode)
  7617. (list 'call-interactively
  7618. (append '(or)
  7619. (mapcar (lambda (k)
  7620. (list 'key-binding k))
  7621. keys)
  7622. '('orgstruct-error))))))))
  7623. (defun org-contextualize-keys (alist contexts)
  7624. "Return valid elements in ALIST depending on CONTEXTS.
  7625. `org-agenda-custom-commands' or `org-capture-templates' are the
  7626. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7627. or `org-capture-templates-contexts' are the associated contexts
  7628. definitions."
  7629. (let ((contexts
  7630. ;; normalize contexts
  7631. (mapcar
  7632. (lambda(c) (cond ((listp (cadr c))
  7633. (list (car c) (car c) (cadr c)))
  7634. ((string= "" (cadr c))
  7635. (list (car c) (car c) (caddr c)))
  7636. (t c))) contexts))
  7637. (a alist) c r s)
  7638. ;; loop over all commands or templates
  7639. (while (setq c (pop a))
  7640. (let (vrules repl)
  7641. (cond
  7642. ((not (assoc (car c) contexts))
  7643. (push c r))
  7644. ((and (assoc (car c) contexts)
  7645. (setq vrules (org-contextualize-validate-key
  7646. (car c) contexts)))
  7647. (mapc (lambda (vr)
  7648. (when (not (equal (car vr) (cadr vr)))
  7649. (setq repl vr))) vrules)
  7650. (if (not repl) (push c r)
  7651. (push (cadr repl) s)
  7652. (push
  7653. (cons (car c)
  7654. (cdr (or (assoc (cadr repl) alist)
  7655. (error "Undefined key `%s' as contextual replacement for `%s'"
  7656. (cadr repl) (car c)))))
  7657. r))))))
  7658. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7659. (delq
  7660. nil
  7661. (delete-dups
  7662. (mapcar (lambda (x)
  7663. (let ((tpl (car x)))
  7664. (when (not (delq
  7665. nil
  7666. (mapcar (lambda(y)
  7667. (equal y tpl)) s))) x)))
  7668. (reverse r))))))
  7669. (defun org-contextualize-validate-key (key contexts)
  7670. "Check CONTEXTS for agenda or capture KEY."
  7671. (let (r rr res)
  7672. (while (setq r (pop contexts))
  7673. (mapc
  7674. (lambda (rr)
  7675. (when
  7676. (and (equal key (car r))
  7677. (if (functionp rr) (funcall rr)
  7678. (or (and (eq (car rr) 'in-file)
  7679. (buffer-file-name)
  7680. (string-match (cdr rr) (buffer-file-name)))
  7681. (and (eq (car rr) 'in-mode)
  7682. (string-match (cdr rr) (symbol-name major-mode)))
  7683. (when (and (eq (car rr) 'not-in-file)
  7684. (buffer-file-name))
  7685. (not (string-match (cdr rr) (buffer-file-name))))
  7686. (when (eq (car rr) 'not-in-mode)
  7687. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7688. (push r res)))
  7689. (car (last r))))
  7690. (delete-dups (delq nil res))))
  7691. (defun org-context-p (&rest contexts)
  7692. "Check if local context is any of CONTEXTS.
  7693. Possible values in the list of contexts are `table', `headline', and `item'."
  7694. (let ((pos (point)))
  7695. (goto-char (point-at-bol))
  7696. (prog1 (or (and (memq 'table contexts)
  7697. (looking-at "[ \t]*|"))
  7698. (and (memq 'headline contexts)
  7699. (looking-at org-outline-regexp))
  7700. (and (memq 'item contexts)
  7701. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7702. (and (memq 'item-body contexts)
  7703. (org-in-item-p)))
  7704. (goto-char pos))))
  7705. (defun org-get-local-variables ()
  7706. "Return a list of all local variables in an Org mode buffer."
  7707. (let (varlist)
  7708. (with-current-buffer (get-buffer-create "*Org tmp*")
  7709. (erase-buffer)
  7710. (org-mode)
  7711. (setq varlist (buffer-local-variables)))
  7712. (kill-buffer "*Org tmp*")
  7713. (delq nil
  7714. (mapcar
  7715. (lambda (x)
  7716. (setq x
  7717. (if (symbolp x)
  7718. (list x)
  7719. (list (car x) (list 'quote (cdr x)))))
  7720. (if (string-match
  7721. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7722. (symbol-name (car x)))
  7723. x nil))
  7724. varlist))))
  7725. (defun org-clone-local-variables (from-buffer &optional regexp)
  7726. "Clone local variables from FROM-BUFFER.
  7727. Optional argument REGEXP selects variables to clone."
  7728. (mapc
  7729. (lambda (pair)
  7730. (and (symbolp (car pair))
  7731. (or (null regexp)
  7732. (string-match regexp (symbol-name (car pair))))
  7733. (set (make-local-variable (car pair))
  7734. (cdr pair))))
  7735. (buffer-local-variables from-buffer)))
  7736. ;;;###autoload
  7737. (defun org-run-like-in-org-mode (cmd)
  7738. "Run a command, pretending that the current buffer is in Org-mode.
  7739. This will temporarily bind local variables that are typically bound in
  7740. Org-mode to the values they have in Org-mode, and then interactively
  7741. call CMD."
  7742. (org-load-modules-maybe)
  7743. (unless org-local-vars
  7744. (setq org-local-vars (org-get-local-variables)))
  7745. (eval (list 'let org-local-vars
  7746. (list 'call-interactively (list 'quote cmd)))))
  7747. ;;;; Archiving
  7748. (defun org-get-category (&optional pos force-refresh)
  7749. "Get the category applying to position POS."
  7750. (save-match-data
  7751. (if force-refresh (org-refresh-category-properties))
  7752. (let ((pos (or pos (point))))
  7753. (or (get-text-property pos 'org-category)
  7754. (progn (org-refresh-category-properties)
  7755. (get-text-property pos 'org-category))))))
  7756. (defun org-refresh-category-properties ()
  7757. "Refresh category text properties in the buffer."
  7758. (let ((case-fold-search t)
  7759. (inhibit-read-only t)
  7760. (def-cat (cond
  7761. ((null org-category)
  7762. (if buffer-file-name
  7763. (file-name-sans-extension
  7764. (file-name-nondirectory buffer-file-name))
  7765. "???"))
  7766. ((symbolp org-category) (symbol-name org-category))
  7767. (t org-category)))
  7768. beg end cat pos optionp)
  7769. (org-unmodified
  7770. (save-excursion
  7771. (save-restriction
  7772. (widen)
  7773. (goto-char (point-min))
  7774. (put-text-property (point) (point-max) 'org-category def-cat)
  7775. (while (re-search-forward
  7776. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7777. (setq pos (match-end 0)
  7778. optionp (equal (char-after (match-beginning 0)) ?#)
  7779. cat (org-trim (match-string 2)))
  7780. (if optionp
  7781. (setq beg (point-at-bol) end (point-max))
  7782. (org-back-to-heading t)
  7783. (setq beg (point) end (org-end-of-subtree t t)))
  7784. (put-text-property beg end 'org-category cat)
  7785. (put-text-property beg end 'org-category-position beg)
  7786. (goto-char pos)))))))
  7787. ;;;; Link Stuff
  7788. ;;; Link abbreviations
  7789. (defun org-link-expand-abbrev (link)
  7790. "Apply replacements as defined in `org-link-abbrev-alist'."
  7791. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7792. (let* ((key (match-string 1 link))
  7793. (as (or (assoc key org-link-abbrev-alist-local)
  7794. (assoc key org-link-abbrev-alist)))
  7795. (tag (and (match-end 2) (match-string 3 link)))
  7796. rpl)
  7797. (if (not as)
  7798. link
  7799. (setq rpl (cdr as))
  7800. (cond
  7801. ((symbolp rpl) (funcall rpl tag))
  7802. ((string-match "%(\\([^)]+\\))" rpl)
  7803. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7804. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7805. ((string-match "%h" rpl)
  7806. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7807. (t (concat rpl tag)))))
  7808. link))
  7809. ;;; Storing and inserting links
  7810. (defvar org-insert-link-history nil
  7811. "Minibuffer history for links inserted with `org-insert-link'.")
  7812. (defvar org-stored-links nil
  7813. "Contains the links stored with `org-store-link'.")
  7814. (defvar org-store-link-plist nil
  7815. "Plist with info about the most recently link created with `org-store-link'.")
  7816. (defvar org-link-protocols nil
  7817. "Link protocols added to Org-mode using `org-add-link-type'.")
  7818. (defvar org-store-link-functions nil
  7819. "List of functions that are called to create and store a link.
  7820. Each function will be called in turn until one returns a non-nil
  7821. value. Each function should check if it is responsible for creating
  7822. this link (for example by looking at the major mode).
  7823. If not, it must exit and return nil.
  7824. If yes, it should return a non-nil value after a calling
  7825. `org-store-link-props' with a list of properties and values.
  7826. Special properties are:
  7827. :type The link prefix, like \"http\". This must be given.
  7828. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7829. This is obligatory as well.
  7830. :description Optional default description for the second pair
  7831. of brackets in an Org-mode link. The user can still change
  7832. this when inserting this link into an Org-mode buffer.
  7833. In addition to these, any additional properties can be specified
  7834. and then used in capture templates.")
  7835. (defun org-add-link-type (type &optional follow export)
  7836. "Add TYPE to the list of `org-link-types'.
  7837. Re-compute all regular expressions depending on `org-link-types'
  7838. FOLLOW and EXPORT are two functions.
  7839. FOLLOW should take the link path as the single argument and do whatever
  7840. is necessary to follow the link, for example find a file or display
  7841. a mail message.
  7842. EXPORT should format the link path for export to one of the export formats.
  7843. It should be a function accepting three arguments:
  7844. path the path of the link, the text after the prefix (like \"http:\")
  7845. desc the description of the link, if any, or a description added by
  7846. org-export-normalize-links if there is none
  7847. format the export format, a symbol like `html' or `latex' or `ascii'..
  7848. The function may use the FORMAT information to return different values
  7849. depending on the format. The return value will be put literally into
  7850. the exported file. If the return value is nil, this means Org should
  7851. do what it normally does with links which do not have EXPORT defined.
  7852. Org-mode has a built-in default for exporting links. If you are happy with
  7853. this default, there is no need to define an export function for the link
  7854. type. For a simple example of an export function, see `org-bbdb.el'."
  7855. (add-to-list 'org-link-types type t)
  7856. (org-make-link-regexps)
  7857. (if (assoc type org-link-protocols)
  7858. (setcdr (assoc type org-link-protocols) (list follow export))
  7859. (push (list type follow export) org-link-protocols)))
  7860. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7861. (defvar org-link-to-org-use-id) ; Defined in org-id.el
  7862. ;;;###autoload
  7863. (defun org-store-link (arg)
  7864. "\\<org-mode-map>Store an org-link to the current location.
  7865. This link is added to `org-stored-links' and can later be inserted
  7866. into an org-buffer with \\[org-insert-link].
  7867. For some link types, a prefix arg is interpreted:
  7868. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7869. For file links, arg negates `org-context-in-file-links'."
  7870. (interactive "P")
  7871. (org-load-modules-maybe)
  7872. (setq org-store-link-plist nil) ; reset
  7873. (org-with-limited-levels
  7874. (let (link cpltxt desc description search txt custom-id agenda-link)
  7875. (cond
  7876. ((run-hook-with-args-until-success 'org-store-link-functions)
  7877. (setq link (plist-get org-store-link-plist :link)
  7878. desc (or (plist-get org-store-link-plist :description) link)))
  7879. ((org-src-edit-buffer-p)
  7880. (let (label gc)
  7881. (while (or (not label)
  7882. (save-excursion
  7883. (save-restriction
  7884. (widen)
  7885. (goto-char (point-min))
  7886. (re-search-forward
  7887. (regexp-quote (format org-coderef-label-format label))
  7888. nil t))))
  7889. (when label (message "Label exists already") (sit-for 2))
  7890. (setq label (read-string "Code line label: " label)))
  7891. (end-of-line 1)
  7892. (setq link (format org-coderef-label-format label))
  7893. (setq gc (- 79 (length link)))
  7894. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7895. (insert link)
  7896. (setq link (concat "(" label ")") desc nil)))
  7897. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7898. ;; We are in the agenda, link to referenced location
  7899. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7900. (get-text-property (point) 'org-marker))))
  7901. (when m
  7902. (org-with-point-at m
  7903. (setq agenda-link
  7904. (if (org-called-interactively-p 'any)
  7905. (call-interactively 'org-store-link)
  7906. (org-store-link nil)))))))
  7907. ((eq major-mode 'calendar-mode)
  7908. (let ((cd (calendar-cursor-to-date)))
  7909. (setq link
  7910. (format-time-string
  7911. (car org-time-stamp-formats)
  7912. (apply 'encode-time
  7913. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7914. nil nil nil))))
  7915. (org-store-link-props :type "calendar" :date cd)))
  7916. ((eq major-mode 'help-mode)
  7917. (setq link (concat "help:" (save-excursion
  7918. (goto-char (point-min))
  7919. (looking-at "^[^ ]+")
  7920. (match-string 0))))
  7921. (org-store-link-props :type "help"))
  7922. ((eq major-mode 'w3-mode)
  7923. (setq cpltxt (if (and (buffer-name)
  7924. (not (string-match "Untitled" (buffer-name))))
  7925. (buffer-name)
  7926. (url-view-url t))
  7927. link (url-view-url t))
  7928. (org-store-link-props :type "w3" :url (url-view-url t)))
  7929. ((eq major-mode 'w3m-mode)
  7930. (setq cpltxt (or w3m-current-title w3m-current-url)
  7931. link w3m-current-url)
  7932. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7933. ((setq search (run-hook-with-args-until-success
  7934. 'org-create-file-search-functions))
  7935. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7936. "::" search))
  7937. (setq cpltxt (or description link)))
  7938. ((eq major-mode 'image-mode)
  7939. (setq cpltxt (concat "file:"
  7940. (abbreviate-file-name buffer-file-name))
  7941. link cpltxt)
  7942. (org-store-link-props :type "image" :file buffer-file-name))
  7943. ((eq major-mode 'dired-mode)
  7944. ;; link to the file in the current line
  7945. (let ((file (dired-get-filename nil t)))
  7946. (setq file (if file
  7947. (abbreviate-file-name
  7948. (expand-file-name (dired-get-filename nil t)))
  7949. ;; otherwise, no file so use current directory.
  7950. default-directory))
  7951. (setq cpltxt (concat "file:" file)
  7952. link cpltxt)))
  7953. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7954. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7955. (cond
  7956. ((org-in-regexp "<<\\(.*?\\)>>")
  7957. (setq cpltxt
  7958. (concat "file:"
  7959. (abbreviate-file-name
  7960. (buffer-file-name (buffer-base-buffer)))
  7961. "::" (match-string 1))
  7962. link cpltxt))
  7963. ((and (featurep 'org-id)
  7964. (or (eq org-id-link-to-org-use-id t)
  7965. (and (org-called-interactively-p 'any)
  7966. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  7967. (and (eq org-id-link-to-org-use-id
  7968. 'create-if-interactive-and-no-custom-id)
  7969. (not custom-id))))
  7970. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  7971. ;; We can make a link using the ID.
  7972. (setq link (condition-case nil
  7973. (prog1 (org-id-store-link)
  7974. (setq desc (plist-get org-store-link-plist :description)))
  7975. (error
  7976. ;; probably before first headline, link to file only
  7977. (concat "file:"
  7978. (abbreviate-file-name
  7979. (buffer-file-name (buffer-base-buffer))))))))
  7980. (t
  7981. ;; Just link to current headline
  7982. (setq cpltxt (concat "file:"
  7983. (abbreviate-file-name
  7984. (buffer-file-name (buffer-base-buffer)))))
  7985. ;; Add a context search string
  7986. (when (org-xor org-context-in-file-links arg)
  7987. (setq txt (cond
  7988. ((org-at-heading-p) nil)
  7989. ((org-region-active-p)
  7990. (buffer-substring (region-beginning) (region-end)))))
  7991. (when (or (null txt) (string-match "\\S-" txt))
  7992. (setq cpltxt
  7993. (concat cpltxt "::"
  7994. (condition-case nil
  7995. (org-make-org-heading-search-string txt)
  7996. (error "")))
  7997. desc (or (nth 4 (ignore-errors
  7998. (org-heading-components))) "NONE"))))
  7999. (if (string-match "::\\'" cpltxt)
  8000. (setq cpltxt (substring cpltxt 0 -2)))
  8001. (setq link cpltxt))))
  8002. ((buffer-file-name (buffer-base-buffer))
  8003. ;; Just link to this file here.
  8004. (setq cpltxt (concat "file:"
  8005. (abbreviate-file-name
  8006. (buffer-file-name (buffer-base-buffer)))))
  8007. ;; Add a context string
  8008. (when (org-xor org-context-in-file-links arg)
  8009. (setq txt (if (org-region-active-p)
  8010. (buffer-substring (region-beginning) (region-end))
  8011. (buffer-substring (point-at-bol) (point-at-eol))))
  8012. ;; Only use search option if there is some text.
  8013. (when (string-match "\\S-" txt)
  8014. (setq cpltxt
  8015. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8016. desc "NONE")))
  8017. (setq link cpltxt))
  8018. ((org-called-interactively-p 'interactive)
  8019. (error "Cannot link to a buffer which is not visiting a file"))
  8020. (t (setq link nil)))
  8021. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8022. (setq link (or link cpltxt)
  8023. desc (or desc cpltxt))
  8024. (if (equal desc "NONE") (setq desc nil))
  8025. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8026. (progn
  8027. (setq org-stored-links
  8028. (cons (list link desc) org-stored-links))
  8029. (message "Stored: %s" (or desc link))
  8030. (when custom-id
  8031. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8032. "::#" custom-id))
  8033. (setq org-stored-links
  8034. (cons (list link desc) org-stored-links))))
  8035. (or agenda-link (and link (org-make-link-string link desc)))))))
  8036. (defun org-store-link-props (&rest plist)
  8037. "Store link properties, extract names and addresses."
  8038. (let (x adr)
  8039. (when (setq x (plist-get plist :from))
  8040. (setq adr (mail-extract-address-components x))
  8041. (setq plist (plist-put plist :fromname (car adr)))
  8042. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8043. (when (setq x (plist-get plist :to))
  8044. (setq adr (mail-extract-address-components x))
  8045. (setq plist (plist-put plist :toname (car adr)))
  8046. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8047. (let ((from (plist-get plist :from))
  8048. (to (plist-get plist :to)))
  8049. (when (and from to org-from-is-user-regexp)
  8050. (setq plist
  8051. (plist-put plist :fromto
  8052. (if (string-match org-from-is-user-regexp from)
  8053. (concat "to %t")
  8054. (concat "from %f"))))))
  8055. (setq org-store-link-plist plist))
  8056. (defun org-add-link-props (&rest plist)
  8057. "Add these properties to the link property list."
  8058. (let (key value)
  8059. (while plist
  8060. (setq key (pop plist) value (pop plist))
  8061. (setq org-store-link-plist
  8062. (plist-put org-store-link-plist key value)))))
  8063. (defun org-email-link-description (&optional fmt)
  8064. "Return the description part of an email link.
  8065. This takes information from `org-store-link-plist' and formats it
  8066. according to FMT (default from `org-email-link-description-format')."
  8067. (setq fmt (or fmt org-email-link-description-format))
  8068. (let* ((p org-store-link-plist)
  8069. (to (plist-get p :toaddress))
  8070. (from (plist-get p :fromaddress))
  8071. (table
  8072. (list
  8073. (cons "%c" (plist-get p :fromto))
  8074. (cons "%F" (plist-get p :from))
  8075. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8076. (cons "%T" (plist-get p :to))
  8077. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8078. (cons "%s" (plist-get p :subject))
  8079. (cons "%d" (plist-get p :date))
  8080. (cons "%m" (plist-get p :message-id)))))
  8081. (when (string-match "%c" fmt)
  8082. ;; Check if the user wrote this message
  8083. (if (and org-from-is-user-regexp from to
  8084. (save-match-data (string-match org-from-is-user-regexp from)))
  8085. (setq fmt (replace-match "to %t" t t fmt))
  8086. (setq fmt (replace-match "from %f" t t fmt))))
  8087. (org-replace-escapes fmt table)))
  8088. (defun org-make-org-heading-search-string (&optional string heading)
  8089. "Make search string for STRING or current headline."
  8090. (interactive)
  8091. (let ((s (or string (org-get-heading)))
  8092. (lines org-context-in-file-links))
  8093. (unless (and string (not heading))
  8094. ;; We are using a headline, clean up garbage in there.
  8095. (if (string-match org-todo-regexp s)
  8096. (setq s (replace-match "" t t s)))
  8097. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8098. (setq s (replace-match "" t t s)))
  8099. (setq s (org-trim s))
  8100. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8101. org-comment-string "\\)") s)
  8102. (setq s (replace-match "" t t s)))
  8103. (while (string-match org-ts-regexp s)
  8104. (setq s (replace-match "" t t s))))
  8105. (or string (setq s (concat "*" s))) ; Add * for headlines
  8106. (when (and string (integerp lines) (> lines 0))
  8107. (let ((slines (org-split-string s "\n")))
  8108. (when (< lines (length slines))
  8109. (setq s (mapconcat
  8110. 'identity
  8111. (reverse (nthcdr (- (length slines) lines)
  8112. (reverse slines))) "\n")))))
  8113. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8114. (defun org-make-link-string (link &optional description)
  8115. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8116. (unless (string-match "\\S-" link)
  8117. (error "Empty link"))
  8118. (when (and description
  8119. (stringp description)
  8120. (not (string-match "\\S-" description)))
  8121. (setq description nil))
  8122. (when (stringp description)
  8123. ;; Remove brackets from the description, they are fatal.
  8124. (while (string-match "\\[" description)
  8125. (setq description (replace-match "{" t t description)))
  8126. (while (string-match "\\]" description)
  8127. (setq description (replace-match "}" t t description))))
  8128. (when (equal link description)
  8129. ;; No description needed, it is identical
  8130. (setq description nil))
  8131. (when (and (not description)
  8132. (not (string-match (org-image-file-name-regexp) link))
  8133. (not (equal link (org-link-escape link))))
  8134. (setq description (org-extract-attributes link)))
  8135. (setq link
  8136. (cond ((string-match (org-image-file-name-regexp) link) link)
  8137. ((string-match org-link-types-re link)
  8138. (concat (match-string 1 link)
  8139. (org-link-escape (substring link (match-end 1)))))
  8140. (t (org-link-escape link))))
  8141. (concat "[[" link "]"
  8142. (if description (concat "[" description "]") "")
  8143. "]"))
  8144. (defconst org-link-escape-chars
  8145. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8146. "List of characters that should be escaped in link.
  8147. This is the list that is used for internal purposes.")
  8148. (defconst org-link-escape-chars-browser
  8149. '(?\ )
  8150. "List of escapes for characters that are problematic in links.
  8151. This is the list that is used before handing over to the browser.")
  8152. (defun org-link-escape (text &optional table merge)
  8153. "Return percent escaped representation of TEXT.
  8154. TEXT is a string with the text to escape.
  8155. Optional argument TABLE is a list with characters that should be
  8156. escaped. When nil, `org-link-escape-chars' is used.
  8157. If optional argument MERGE is set, merge TABLE into
  8158. `org-link-escape-chars'."
  8159. (cond
  8160. ((and table merge)
  8161. (mapc (lambda (defchr)
  8162. (unless (member defchr table)
  8163. (setq table (cons defchr table)))) org-link-escape-chars))
  8164. ((null table)
  8165. (setq table org-link-escape-chars)))
  8166. (mapconcat
  8167. (lambda (char)
  8168. (if (or (member char table)
  8169. (and (or (< char 32) (= char 37) (> char 126))
  8170. org-url-hexify-p))
  8171. (mapconcat (lambda (sequence-element)
  8172. (format "%%%.2X" sequence-element))
  8173. (or (encode-coding-char char 'utf-8)
  8174. (error "Unable to percent escape character: %s"
  8175. (char-to-string char))) "")
  8176. (char-to-string char))) text ""))
  8177. (defun org-link-unescape (str)
  8178. "Unhex hexified Unicode strings as returned from the JavaScript function
  8179. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8180. (unless (and (null str) (string= "" str))
  8181. (let ((pos 0) (case-fold-search t) unhexed)
  8182. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8183. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8184. (setq str (replace-match unhexed t t str))
  8185. (setq pos (+ pos (length unhexed))))))
  8186. str)
  8187. (defun org-link-unescape-compound (hex)
  8188. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8189. Note: this function also decodes single byte encodings like
  8190. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8191. (save-match-data
  8192. (let* ((bytes (cdr (split-string hex "%")))
  8193. (ret "")
  8194. (eat 0)
  8195. (sum 0))
  8196. (while bytes
  8197. (let* ((val (string-to-number (pop bytes) 16))
  8198. (shift-xor
  8199. (if (= 0 eat)
  8200. (cond
  8201. ((>= val 252) (cons 6 252))
  8202. ((>= val 248) (cons 5 248))
  8203. ((>= val 240) (cons 4 240))
  8204. ((>= val 224) (cons 3 224))
  8205. ((>= val 192) (cons 2 192))
  8206. (t (cons 0 0)))
  8207. (cons 6 128))))
  8208. (if (>= val 192) (setq eat (car shift-xor)))
  8209. (setq val (logxor val (cdr shift-xor)))
  8210. (setq sum (+ (lsh sum (car shift-xor)) val))
  8211. (if (> eat 0) (setq eat (- eat 1)))
  8212. (cond
  8213. ((= 0 eat) ;multi byte
  8214. (setq ret (concat ret (org-char-to-string sum)))
  8215. (setq sum 0))
  8216. ((not bytes) ; single byte(s)
  8217. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8218. )) ;; end (while bytes
  8219. ret )))
  8220. (defun org-link-unescape-single-byte-sequence (hex)
  8221. "Unhexify hex-encoded single byte character sequences."
  8222. (mapconcat (lambda (byte)
  8223. (char-to-string (string-to-number byte 16)))
  8224. (cdr (split-string hex "%")) ""))
  8225. (defun org-xor (a b)
  8226. "Exclusive or."
  8227. (if a (not b) b))
  8228. (defun org-fixup-message-id-for-http (s)
  8229. "Replace special characters in a message id, so it can be used in an http query."
  8230. (when (string-match "%" s)
  8231. (setq s (mapconcat (lambda (c)
  8232. (if (eq c ?%)
  8233. "%25"
  8234. (char-to-string c)))
  8235. s "")))
  8236. (while (string-match "<" s)
  8237. (setq s (replace-match "%3C" t t s)))
  8238. (while (string-match ">" s)
  8239. (setq s (replace-match "%3E" t t s)))
  8240. (while (string-match "@" s)
  8241. (setq s (replace-match "%40" t t s)))
  8242. s)
  8243. (defun org-link-prettify (link)
  8244. "Return a human-readable representation of LINK.
  8245. The car of LINK must be a raw link the cdr of LINK must be either
  8246. a link description or nil."
  8247. (let ((desc (or (cadr link) "<no description>")))
  8248. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8249. "<" (car link) ">")))
  8250. ;;;###autoload
  8251. (defun org-insert-link-global ()
  8252. "Insert a link like Org-mode does.
  8253. This command can be called in any mode to insert a link in Org-mode syntax."
  8254. (interactive)
  8255. (org-load-modules-maybe)
  8256. (org-run-like-in-org-mode 'org-insert-link))
  8257. (defun org-insert-all-links (&optional keep)
  8258. "Insert all links in `org-stored-links'."
  8259. (interactive "P")
  8260. (let ((links (copy-sequence org-stored-links)) l)
  8261. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8262. (insert "- ")
  8263. (org-insert-link nil (car l) (cadr l))
  8264. (insert "\n"))))
  8265. (defun org-link-fontify-links-to-this-file ()
  8266. "Fontify links to the current file in `org-stored-links'."
  8267. (let ((f (buffer-file-name)) a b)
  8268. (setq a (mapcar (lambda(l)
  8269. (let ((ll (car l)))
  8270. (when (and (string-match "^file:\\(.+\\)::" ll)
  8271. (equal f (expand-file-name (match-string 1 ll))))
  8272. ll)))
  8273. org-stored-links))
  8274. (when (featurep 'org-id)
  8275. (setq b (mapcar (lambda(l)
  8276. (let ((ll (car l)))
  8277. (when (and (string-match "^id:\\(.+\\)$" ll)
  8278. (equal f (expand-file-name
  8279. (or (org-id-find-id-file
  8280. (match-string 1 ll)) ""))))
  8281. ll)))
  8282. org-stored-links)))
  8283. (mapcar (lambda(l)
  8284. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8285. (delq nil (append a b)))))
  8286. (defvar org-link-links-in-this-file nil)
  8287. (defun org-insert-link (&optional complete-file link-location default-description)
  8288. "Insert a link. At the prompt, enter the link.
  8289. Completion can be used to insert any of the link protocol prefixes like
  8290. http or ftp in use.
  8291. The history can be used to select a link previously stored with
  8292. `org-store-link'. When the empty string is entered (i.e. if you just
  8293. press RET at the prompt), the link defaults to the most recently
  8294. stored link. As SPC triggers completion in the minibuffer, you need to
  8295. use M-SPC or C-q SPC to force the insertion of a space character.
  8296. You will also be prompted for a description, and if one is given, it will
  8297. be displayed in the buffer instead of the link.
  8298. If there is already a link at point, this command will allow you to edit link
  8299. and description parts.
  8300. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8301. be selected using completion. The path to the file will be relative to the
  8302. current directory if the file is in the current directory or a subdirectory.
  8303. Otherwise, the link will be the absolute path as completed in the minibuffer
  8304. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8305. option `org-link-file-path-type'.
  8306. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8307. the current directory or below.
  8308. With three \\[universal-argument] prefixes, negate the meaning of
  8309. `org-keep-stored-link-after-insertion'.
  8310. If `org-make-link-description-function' is non-nil, this function will be
  8311. called with the link target, and the result will be the default
  8312. link description.
  8313. If the LINK-LOCATION parameter is non-nil, this value will be
  8314. used as the link location instead of reading one interactively.
  8315. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8316. be used as the default description."
  8317. (interactive "P")
  8318. (let* ((wcf (current-window-configuration))
  8319. (region (if (org-region-active-p)
  8320. (buffer-substring (region-beginning) (region-end))))
  8321. (remove (and region (list (region-beginning) (region-end))))
  8322. (desc region)
  8323. tmphist ; byte-compile incorrectly complains about this
  8324. (link link-location)
  8325. (abbrevs org-link-abbrev-alist-local)
  8326. entry file all-prefixes auto-desc)
  8327. (cond
  8328. (link-location) ; specified by arg, just use it.
  8329. ((org-in-regexp org-bracket-link-regexp 1)
  8330. ;; We do have a link at point, and we are going to edit it.
  8331. (setq remove (list (match-beginning 0) (match-end 0)))
  8332. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8333. (setq link (read-string "Link: "
  8334. (org-link-unescape
  8335. (org-match-string-no-properties 1)))))
  8336. ((or (org-in-regexp org-angle-link-re)
  8337. (org-in-regexp org-plain-link-re))
  8338. ;; Convert to bracket link
  8339. (setq remove (list (match-beginning 0) (match-end 0))
  8340. link (read-string "Link: "
  8341. (org-remove-angle-brackets (match-string 0)))))
  8342. ((member complete-file '((4) (16)))
  8343. ;; Completing read for file names.
  8344. (setq link (org-file-complete-link complete-file)))
  8345. (t
  8346. ;; Read link, with completion for stored links.
  8347. (org-link-fontify-links-to-this-file)
  8348. (org-switch-to-buffer-other-window "*Org Links*")
  8349. (with-current-buffer "*Org Links*"
  8350. (erase-buffer)
  8351. (insert "Insert a link.
  8352. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8353. (when org-stored-links
  8354. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8355. (insert (mapconcat 'org-link-prettify
  8356. (reverse org-stored-links) "\n")))
  8357. (goto-char (point-min)))
  8358. (let ((cw (selected-window)))
  8359. (select-window (get-buffer-window "*Org Links*" 'visible))
  8360. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8361. (unless (pos-visible-in-window-p (point-max))
  8362. (org-fit-window-to-buffer))
  8363. (and (window-live-p cw) (select-window cw)))
  8364. ;; Fake a link history, containing the stored links.
  8365. (setq tmphist (append (mapcar 'car org-stored-links)
  8366. org-insert-link-history))
  8367. (setq all-prefixes (append (mapcar 'car abbrevs)
  8368. (mapcar 'car org-link-abbrev-alist)
  8369. org-link-types))
  8370. (unwind-protect
  8371. (progn
  8372. (setq link
  8373. (let ((org-completion-use-ido nil)
  8374. (org-completion-use-iswitchb nil))
  8375. (org-completing-read
  8376. "Link: "
  8377. (append
  8378. (mapcar (lambda (x) (list (concat x ":")))
  8379. all-prefixes)
  8380. (mapcar 'car org-stored-links)
  8381. (mapcar 'cadr org-stored-links))
  8382. nil nil nil
  8383. 'tmphist
  8384. (caar org-stored-links))))
  8385. (if (not (string-match "\\S-" link))
  8386. (error "No link selected"))
  8387. (mapc (lambda(l)
  8388. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8389. org-stored-links)
  8390. (if (or (member link all-prefixes)
  8391. (and (equal ":" (substring link -1))
  8392. (member (substring link 0 -1) all-prefixes)
  8393. (setq link (substring link 0 -1))))
  8394. (setq link (org-link-try-special-completion link))))
  8395. (set-window-configuration wcf)
  8396. (kill-buffer "*Org Links*"))
  8397. (setq entry (assoc link org-stored-links))
  8398. (or entry (push link org-insert-link-history))
  8399. (setq desc (or desc (nth 1 entry)))))
  8400. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8401. (not org-keep-stored-link-after-insertion))
  8402. (setq org-stored-links (delq (assoc link org-stored-links)
  8403. org-stored-links)))
  8404. (if (string-match org-plain-link-re link)
  8405. ;; URL-like link, normalize the use of angular brackets.
  8406. (setq link (org-remove-angle-brackets link)))
  8407. ;; Check if we are linking to the current file with a search
  8408. ;; option If yes, simplify the link by using only the search
  8409. ;; option.
  8410. (when (and buffer-file-name
  8411. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8412. (let* ((path (match-string 1 link))
  8413. (case-fold-search nil)
  8414. (search (match-string 2 link)))
  8415. (save-match-data
  8416. (if (equal (file-truename buffer-file-name) (file-truename path))
  8417. ;; We are linking to this same file, with a search option
  8418. (setq link search)))))
  8419. ;; Check if we can/should use a relative path. If yes, simplify the link
  8420. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8421. (let* ((type (match-string 1 link))
  8422. (path (match-string 2 link))
  8423. (origpath path)
  8424. (case-fold-search nil))
  8425. (cond
  8426. ((or (eq org-link-file-path-type 'absolute)
  8427. (equal complete-file '(16)))
  8428. (setq path (abbreviate-file-name (expand-file-name path))))
  8429. ((eq org-link-file-path-type 'noabbrev)
  8430. (setq path (expand-file-name path)))
  8431. ((eq org-link-file-path-type 'relative)
  8432. (setq path (file-relative-name path)))
  8433. (t
  8434. (save-match-data
  8435. (if (string-match (concat "^" (regexp-quote
  8436. (expand-file-name
  8437. (file-name-as-directory
  8438. default-directory))))
  8439. (expand-file-name path))
  8440. ;; We are linking a file with relative path name.
  8441. (setq path (substring (expand-file-name path)
  8442. (match-end 0)))
  8443. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8444. (setq link (concat type path))
  8445. (if (equal desc origpath)
  8446. (setq desc path))))
  8447. (if org-make-link-description-function
  8448. (setq desc
  8449. (or (condition-case nil
  8450. (funcall org-make-link-description-function link desc)
  8451. (error (progn (message "Can't get link description from `%s'"
  8452. (symbol-name org-make-link-description-function))
  8453. (sit-for 2) nil)))
  8454. (read-string "Description: " default-description)))
  8455. (if default-description (setq desc default-description)
  8456. (setq desc (or (and auto-desc desc)
  8457. (read-string "Description: " desc)))))
  8458. (unless (string-match "\\S-" desc) (setq desc nil))
  8459. (if remove (apply 'delete-region remove))
  8460. (insert (org-make-link-string link desc))))
  8461. (defun org-link-try-special-completion (type)
  8462. "If there is completion support for link type TYPE, offer it."
  8463. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8464. (if (functionp fun)
  8465. (funcall fun)
  8466. (read-string "Link (no completion support): " (concat type ":")))))
  8467. (defun org-file-complete-link (&optional arg)
  8468. "Create a file link using completion."
  8469. (let (file link)
  8470. (setq file (read-file-name "File: "))
  8471. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8472. (pwd1 (file-name-as-directory (abbreviate-file-name
  8473. (expand-file-name ".")))))
  8474. (cond
  8475. ((equal arg '(16))
  8476. (setq link (concat
  8477. "file:"
  8478. (abbreviate-file-name (expand-file-name file)))))
  8479. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8480. (setq link (concat "file:" (match-string 1 file))))
  8481. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8482. (expand-file-name file))
  8483. (setq link (concat
  8484. "file:" (match-string 1 (expand-file-name file)))))
  8485. (t (setq link (concat "file:" file)))))
  8486. link))
  8487. (defun org-completing-read (&rest args)
  8488. "Completing-read with SPACE being a normal character."
  8489. (let ((enable-recursive-minibuffers t)
  8490. (minibuffer-local-completion-map
  8491. (copy-keymap minibuffer-local-completion-map)))
  8492. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8493. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8494. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8495. (apply 'org-icompleting-read args)))
  8496. (defun org-completing-read-no-i (&rest args)
  8497. (let (org-completion-use-ido org-completion-use-iswitchb)
  8498. (apply 'org-completing-read args)))
  8499. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8500. "Use iswitch as a completing-read replacement to choose from choices.
  8501. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8502. from."
  8503. (let* ((iswitchb-use-virtual-buffers nil)
  8504. (iswitchb-make-buflist-hook
  8505. (lambda ()
  8506. (setq iswitchb-temp-buflist choices))))
  8507. (iswitchb-read-buffer prompt)))
  8508. (defun org-icompleting-read (&rest args)
  8509. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8510. (org-without-partial-completion
  8511. (if (and org-completion-use-ido
  8512. (fboundp 'ido-completing-read)
  8513. (boundp 'ido-mode) ido-mode
  8514. (listp (second args)))
  8515. (let ((ido-enter-matching-directory nil))
  8516. (apply 'ido-completing-read (concat (car args))
  8517. (if (consp (car (nth 1 args)))
  8518. (mapcar 'car (nth 1 args))
  8519. (nth 1 args))
  8520. (cddr args)))
  8521. (if (and org-completion-use-iswitchb
  8522. (boundp 'iswitchb-mode) iswitchb-mode
  8523. (listp (second args)))
  8524. (apply 'org-iswitchb-completing-read (concat (car args))
  8525. (if (consp (car (nth 1 args)))
  8526. (mapcar 'car (nth 1 args))
  8527. (nth 1 args))
  8528. (cddr args))
  8529. (apply 'completing-read args)))))
  8530. (defun org-extract-attributes (s)
  8531. "Extract the attributes cookie from a string and set as text property."
  8532. (let (a attr (start 0) key value)
  8533. (save-match-data
  8534. (when (string-match "{{\\([^}]+\\)}}$" s)
  8535. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8536. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8537. (setq key (match-string 1 a) value (match-string 2 a)
  8538. start (match-end 0)
  8539. attr (plist-put attr (intern key) value))))
  8540. (org-add-props s nil 'org-attr attr))
  8541. s))
  8542. (defun org-extract-attributes-from-string (tag)
  8543. (let (key value attr)
  8544. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8545. (setq key (match-string 1 tag) value (match-string 2 tag)
  8546. tag (replace-match "" t t tag)
  8547. attr (plist-put attr (intern key) value)))
  8548. (cons tag attr)))
  8549. (defun org-attributes-to-string (plist)
  8550. "Format a property list into an HTML attribute list."
  8551. (let ((s "") key value)
  8552. (while plist
  8553. (setq key (pop plist) value (pop plist))
  8554. (and value
  8555. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8556. s))
  8557. ;;; Opening/following a link
  8558. (defvar org-link-search-failed nil)
  8559. (defvar org-open-link-functions nil
  8560. "Hook for functions finding a plain text link.
  8561. These functions must take a single argument, the link content.
  8562. They will be called for links that look like [[link text][description]]
  8563. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8564. like a filename (e.g. \"./blue.png\").
  8565. These functions will be called *before* Org attempts to resolve the
  8566. link by doing text searches in the current buffer - so if you want a
  8567. link \"[[target]]\" to still find \"<<target>>\", your function should
  8568. handle this as a special case.
  8569. When the function does handle the link, it must return a non-nil value.
  8570. If it decides that it is not responsible for this link, it must return
  8571. nil to indicate that that Org-mode can continue with other options
  8572. like exact and fuzzy text search.")
  8573. (defun org-next-link ()
  8574. "Move forward to the next link.
  8575. If the link is in hidden text, expose it."
  8576. (interactive)
  8577. (when (and org-link-search-failed (eq this-command last-command))
  8578. (goto-char (point-min))
  8579. (message "Link search wrapped back to beginning of buffer"))
  8580. (setq org-link-search-failed nil)
  8581. (let* ((pos (point))
  8582. (ct (org-context))
  8583. (a (assoc :link ct)))
  8584. (if a (goto-char (nth 2 a)))
  8585. (if (re-search-forward org-any-link-re nil t)
  8586. (progn
  8587. (goto-char (match-beginning 0))
  8588. (if (outline-invisible-p) (org-show-context)))
  8589. (goto-char pos)
  8590. (setq org-link-search-failed t)
  8591. (error "No further link found"))))
  8592. (defun org-previous-link ()
  8593. "Move backward to the previous link.
  8594. If the link is in hidden text, expose it."
  8595. (interactive)
  8596. (when (and org-link-search-failed (eq this-command last-command))
  8597. (goto-char (point-max))
  8598. (message "Link search wrapped back to end of buffer"))
  8599. (setq org-link-search-failed nil)
  8600. (let* ((pos (point))
  8601. (ct (org-context))
  8602. (a (assoc :link ct)))
  8603. (if a (goto-char (nth 1 a)))
  8604. (if (re-search-backward org-any-link-re nil t)
  8605. (progn
  8606. (goto-char (match-beginning 0))
  8607. (if (outline-invisible-p) (org-show-context)))
  8608. (goto-char pos)
  8609. (setq org-link-search-failed t)
  8610. (error "No further link found"))))
  8611. (defun org-translate-link (s)
  8612. "Translate a link string if a translation function has been defined."
  8613. (if (and org-link-translation-function
  8614. (fboundp org-link-translation-function)
  8615. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8616. (progn
  8617. (setq s (funcall org-link-translation-function
  8618. (match-string 1 s) (match-string 2 s)))
  8619. (concat (car s) ":" (cdr s)))
  8620. s))
  8621. (defun org-translate-link-from-planner (type path)
  8622. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8623. This is still an experimental function, your mileage may vary."
  8624. (cond
  8625. ((member type '("http" "https" "news" "ftp"))
  8626. ;; standard Internet links are the same.
  8627. nil)
  8628. ((and (equal type "irc") (string-match "^//" path))
  8629. ;; Planner has two / at the beginning of an irc link, we have 1.
  8630. ;; We should have zero, actually....
  8631. (setq path (substring path 1)))
  8632. ((and (equal type "lisp") (string-match "^/" path))
  8633. ;; Planner has a slash, we do not.
  8634. (setq type "elisp" path (substring path 1)))
  8635. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8636. ;; A typical message link. Planner has the id after the final slash,
  8637. ;; we separate it with a hash mark
  8638. (setq path (concat (match-string 1 path) "#"
  8639. (org-remove-angle-brackets (match-string 2 path)))))
  8640. )
  8641. (cons type path))
  8642. (defun org-find-file-at-mouse (ev)
  8643. "Open file link or URL at mouse."
  8644. (interactive "e")
  8645. (mouse-set-point ev)
  8646. (org-open-at-point 'in-emacs))
  8647. (defun org-open-at-mouse (ev)
  8648. "Open file link or URL at mouse.
  8649. See the docstring of `org-open-file' for details."
  8650. (interactive "e")
  8651. (mouse-set-point ev)
  8652. (if (eq major-mode 'org-agenda-mode)
  8653. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8654. (org-open-at-point))
  8655. (defvar org-window-config-before-follow-link nil
  8656. "The window configuration before following a link.
  8657. This is saved in case the need arises to restore it.")
  8658. (defvar org-open-link-marker (make-marker)
  8659. "Marker pointing to the location where `org-open-at-point; was called.")
  8660. ;;;###autoload
  8661. (defun org-open-at-point-global ()
  8662. "Follow a link like Org-mode does.
  8663. This command can be called in any mode to follow a link that has
  8664. Org-mode syntax."
  8665. (interactive)
  8666. (org-run-like-in-org-mode 'org-open-at-point))
  8667. ;;;###autoload
  8668. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8669. "Open a link in the string S, as if it was in Org-mode."
  8670. (interactive "sLink: \nP")
  8671. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8672. (with-temp-buffer
  8673. (let ((org-inhibit-startup (not reference-buffer)))
  8674. (org-mode)
  8675. (insert s)
  8676. (goto-char (point-min))
  8677. (when reference-buffer
  8678. (setq org-link-abbrev-alist-local
  8679. (with-current-buffer reference-buffer
  8680. org-link-abbrev-alist-local)))
  8681. (org-open-at-point arg reference-buffer)))))
  8682. (defvar org-open-at-point-functions nil
  8683. "Hook that is run when following a link at point.
  8684. Functions in this hook must return t if they identify and follow
  8685. a link at point. If they don't find anything interesting at point,
  8686. they must return nil.")
  8687. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8688. (defun org-open-at-point (&optional arg reference-buffer)
  8689. "Open link at or after point.
  8690. If there is no link at point, this function will search forward up to
  8691. the end of the current line.
  8692. Normally, files will be opened by an appropriate application. If the
  8693. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8694. With a double prefix argument, try to open outside of Emacs, in the
  8695. application the system uses for this file type."
  8696. (interactive "P")
  8697. ;; if in a code block, then open the block's results
  8698. (unless (call-interactively #'org-babel-open-src-block-result)
  8699. (org-load-modules-maybe)
  8700. (move-marker org-open-link-marker (point))
  8701. (setq org-window-config-before-follow-link (current-window-configuration))
  8702. (org-remove-occur-highlights nil nil t)
  8703. (cond
  8704. ((and (org-at-heading-p)
  8705. (not (org-at-timestamp-p t))
  8706. (not (org-in-regexp
  8707. (concat org-plain-link-re "\\|"
  8708. org-bracket-link-regexp "\\|"
  8709. org-angle-link-re "\\|"
  8710. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8711. (not (get-text-property (point) 'org-linked-text)))
  8712. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8713. (lk (car lkall))
  8714. (lkend (cdr lkall)))
  8715. (when lk
  8716. (prog1 (search-forward lk nil lkend)
  8717. (goto-char (match-beginning 0))
  8718. (org-open-at-point))))
  8719. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8720. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8721. ((and (org-at-timestamp-p t)
  8722. (not (org-in-regexp org-bracket-link-regexp)))
  8723. (org-follow-timestamp-link))
  8724. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8725. (not (org-in-regexp org-any-link-re)))
  8726. (org-footnote-action))
  8727. (t
  8728. (let (type path link line search (pos (point)))
  8729. (catch 'match
  8730. (save-excursion
  8731. (skip-chars-forward "^]\n\r")
  8732. (when (org-in-regexp org-bracket-link-regexp 1)
  8733. (setq link (org-extract-attributes
  8734. (org-link-unescape (org-match-string-no-properties 1))))
  8735. (while (string-match " *\n *" link)
  8736. (setq link (replace-match " " t t link)))
  8737. (setq link (org-link-expand-abbrev link))
  8738. (cond
  8739. ((or (file-name-absolute-p link)
  8740. (string-match "^\\.\\.?/" link))
  8741. (setq type "file" path link))
  8742. ((string-match org-link-re-with-space3 link)
  8743. (setq type (match-string 1 link) path (match-string 2 link)))
  8744. ((string-match "^help:+\\(.+\\)" link)
  8745. (setq type "help" path (match-string 1 link)))
  8746. (t (setq type "thisfile" path link)))
  8747. (throw 'match t)))
  8748. (when (get-text-property (point) 'org-linked-text)
  8749. (setq type "thisfile"
  8750. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8751. (1+ (point)) (point))
  8752. path (buffer-substring
  8753. (or (previous-single-property-change pos 'org-linked-text)
  8754. (point-min))
  8755. (or (next-single-property-change pos 'org-linked-text)
  8756. (point-max))))
  8757. (throw 'match t))
  8758. (save-excursion
  8759. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8760. (when (or (org-in-regexp org-angle-link-re)
  8761. (and plinkpos (goto-char (car plinkpos))
  8762. (save-match-data (not (looking-back "\\[\\[")))))
  8763. (setq type (match-string 1)
  8764. path (org-link-unescape (match-string 2)))
  8765. (throw 'match t))))
  8766. (save-excursion
  8767. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8768. (setq type "tags"
  8769. path (match-string 1))
  8770. (while (string-match ":" path)
  8771. (setq path (replace-match "+" t t path)))
  8772. (throw 'match t)))
  8773. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8774. (setq type "tree-match"
  8775. path (match-string 1))
  8776. (throw 'match t)))
  8777. (unless path
  8778. (user-error "No link found"))
  8779. ;; switch back to reference buffer
  8780. ;; needed when if called in a temporary buffer through
  8781. ;; org-open-link-from-string
  8782. (with-current-buffer (or reference-buffer (current-buffer))
  8783. ;; Remove any trailing spaces in path
  8784. (if (string-match " +\\'" path)
  8785. (setq path (replace-match "" t t path)))
  8786. (if (and org-link-translation-function
  8787. (fboundp org-link-translation-function))
  8788. ;; Check if we need to translate the link
  8789. (let ((tmp (funcall org-link-translation-function type path)))
  8790. (setq type (car tmp) path (cdr tmp))))
  8791. (cond
  8792. ((assoc type org-link-protocols)
  8793. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8794. ((equal type "help")
  8795. (let ((f-or-v (intern path)))
  8796. (cond ((fboundp f-or-v)
  8797. (describe-function f-or-v))
  8798. ((boundp f-or-v)
  8799. (describe-variable f-or-v))
  8800. (t (error "Not a known function or variable")))))
  8801. ((equal type "mailto")
  8802. (let ((cmd (car org-link-mailto-program))
  8803. (args (cdr org-link-mailto-program)) args1
  8804. (address path) (subject "") a)
  8805. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8806. (setq address (match-string 1 path)
  8807. subject (org-link-escape (match-string 2 path))))
  8808. (while args
  8809. (cond
  8810. ((not (stringp (car args))) (push (pop args) args1))
  8811. (t (setq a (pop args))
  8812. (if (string-match "%a" a)
  8813. (setq a (replace-match address t t a)))
  8814. (if (string-match "%s" a)
  8815. (setq a (replace-match subject t t a)))
  8816. (push a args1))))
  8817. (apply cmd (nreverse args1))))
  8818. ((member type '("http" "https" "ftp" "news"))
  8819. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8820. (org-link-escape
  8821. path org-link-escape-chars-browser)
  8822. path))))
  8823. ((string= type "doi")
  8824. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8825. (org-link-escape
  8826. path org-link-escape-chars-browser)
  8827. path))))
  8828. ((member type '("message"))
  8829. (browse-url (concat type ":" path)))
  8830. ((string= type "tags")
  8831. (org-tags-view arg path))
  8832. ((string= type "tree-match")
  8833. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8834. ((string= type "file")
  8835. (if (string-match "::\\([0-9]+\\)\\'" path)
  8836. (setq line (string-to-number (match-string 1 path))
  8837. path (substring path 0 (match-beginning 0)))
  8838. (if (string-match "::\\(.+\\)\\'" path)
  8839. (setq search (match-string 1 path)
  8840. path (substring path 0 (match-beginning 0)))))
  8841. (if (string-match "[*?{]" (file-name-nondirectory path))
  8842. (dired path)
  8843. (org-open-file path arg line search)))
  8844. ((string= type "shell")
  8845. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8846. (cmd path))
  8847. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8848. (string-match org-confirm-shell-link-not-regexp cmd))
  8849. (not org-confirm-shell-link-function)
  8850. (funcall org-confirm-shell-link-function
  8851. (format "Execute \"%s\" in shell? "
  8852. (org-add-props cmd nil
  8853. 'face 'org-warning))))
  8854. (progn
  8855. (message "Executing %s" cmd)
  8856. (shell-command cmd buf)
  8857. (if (featurep 'midnight)
  8858. (setq clean-buffer-list-kill-buffer-names
  8859. (cons buf clean-buffer-list-kill-buffer-names))))
  8860. (error "Abort"))))
  8861. ((string= type "elisp")
  8862. (let ((cmd path))
  8863. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8864. (string-match org-confirm-elisp-link-not-regexp cmd))
  8865. (not org-confirm-elisp-link-function)
  8866. (funcall org-confirm-elisp-link-function
  8867. (format "Execute \"%s\" as elisp? "
  8868. (org-add-props cmd nil
  8869. 'face 'org-warning))))
  8870. (message "%s => %s" cmd
  8871. (if (equal (string-to-char cmd) ?\()
  8872. (eval (read cmd))
  8873. (call-interactively (read cmd))))
  8874. (error "Abort"))))
  8875. ((and (string= type "thisfile")
  8876. (run-hook-with-args-until-success
  8877. 'org-open-link-functions path)))
  8878. ((string= type "thisfile")
  8879. (if arg
  8880. (switch-to-buffer-other-window
  8881. (org-get-buffer-for-internal-link (current-buffer)))
  8882. (org-mark-ring-push))
  8883. (let ((cmd `(org-link-search
  8884. ,path
  8885. ,(cond ((equal arg '(4)) ''occur)
  8886. ((equal arg '(16)) ''org-occur))
  8887. ,pos)))
  8888. (condition-case nil (let ((org-link-search-inhibit-query t))
  8889. (eval cmd))
  8890. (error (progn (widen) (eval cmd))))))
  8891. (t (browse-url-at-point)))))))
  8892. (move-marker org-open-link-marker nil)
  8893. (run-hook-with-args 'org-follow-link-hook)))
  8894. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  8895. "Offer links in the current entry and return the selected link.
  8896. If there is only one link, return it.
  8897. If NTH is an integer, return the NTH link found.
  8898. If ZERO is a string, check also this string for a link, and if
  8899. there is one, return it."
  8900. (with-current-buffer buffer
  8901. (save-excursion
  8902. (save-restriction
  8903. (widen)
  8904. (goto-char marker)
  8905. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8906. "\\(" org-angle-link-re "\\)\\|"
  8907. "\\(" org-plain-link-re "\\)"))
  8908. (cnt ?0)
  8909. (in-emacs (if (integerp nth) nil nth))
  8910. have-zero end links link c)
  8911. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8912. (push (match-string 0 zero) links)
  8913. (setq cnt (1- cnt) have-zero t))
  8914. (save-excursion
  8915. (org-back-to-heading t)
  8916. (setq end (save-excursion (outline-next-heading) (point)))
  8917. (while (re-search-forward re end t)
  8918. (push (match-string 0) links))
  8919. (setq links (org-uniquify (reverse links))))
  8920. (cond
  8921. ((null links)
  8922. (message "No links"))
  8923. ((equal (length links) 1)
  8924. (setq link (car links)))
  8925. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8926. (setq link (nth (if have-zero nth (1- nth)) links)))
  8927. (t ; we have to select a link
  8928. (save-excursion
  8929. (save-window-excursion
  8930. (delete-other-windows)
  8931. (with-output-to-temp-buffer "*Select Link*"
  8932. (mapc (lambda (l)
  8933. (if (not (string-match org-bracket-link-regexp l))
  8934. (princ (format "[%c] %s\n" (incf cnt)
  8935. (org-remove-angle-brackets l)))
  8936. (if (match-end 3)
  8937. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8938. (match-string 3 l) (match-string 1 l)))
  8939. (princ (format "[%c] %s\n" (incf cnt)
  8940. (match-string 1 l))))))
  8941. links))
  8942. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8943. (message "Select link to open, RET to open all:")
  8944. (setq c (read-char-exclusive))
  8945. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8946. (when (equal c ?q) (error "Abort"))
  8947. (if (equal c ?\C-m)
  8948. (setq link links)
  8949. (setq nth (- c ?0))
  8950. (if have-zero (setq nth (1+ nth)))
  8951. (unless (and (integerp nth) (>= (length links) nth))
  8952. (error "Invalid link selection"))
  8953. (setq link (nth (1- nth) links)))))
  8954. (cons link end))))))
  8955. ;; Add special file links that specify the way of opening
  8956. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8957. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8958. (defun org-open-file-with-system (path)
  8959. "Open file at PATH using the system way of opening it."
  8960. (org-open-file path 'system))
  8961. (defun org-open-file-with-emacs (path)
  8962. "Open file at PATH in Emacs."
  8963. (org-open-file path 'emacs))
  8964. (defun org-remove-file-link-modifiers ()
  8965. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8966. (goto-char (point-min))
  8967. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8968. (org-if-unprotected
  8969. (replace-match "file:" t t))))
  8970. (eval-after-load "org-exp"
  8971. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8972. 'org-remove-file-link-modifiers))
  8973. ;;;; Time estimates
  8974. (defun org-get-effort (&optional pom)
  8975. "Get the effort estimate for the current entry."
  8976. (org-entry-get pom org-effort-property))
  8977. ;;; File search
  8978. (defvar org-create-file-search-functions nil
  8979. "List of functions to construct the right search string for a file link.
  8980. These functions are called in turn with point at the location to
  8981. which the link should point.
  8982. A function in the hook should first test if it would like to
  8983. handle this file type, for example by checking the `major-mode'
  8984. or the file extension. If it decides not to handle this file, it
  8985. should just return nil to give other functions a chance. If it
  8986. does handle the file, it must return the search string to be used
  8987. when following the link. The search string will be part of the
  8988. file link, given after a double colon, and `org-open-at-point'
  8989. will automatically search for it. If special measures must be
  8990. taken to make the search successful, another function should be
  8991. added to the companion hook `org-execute-file-search-functions',
  8992. which see.
  8993. A function in this hook may also use `setq' to set the variable
  8994. `description' to provide a suggestion for the descriptive text to
  8995. be used for this link when it gets inserted into an Org-mode
  8996. buffer with \\[org-insert-link].")
  8997. (defvar org-execute-file-search-functions nil
  8998. "List of functions to execute a file search triggered by a link.
  8999. Functions added to this hook must accept a single argument, the
  9000. search string that was part of the file link, the part after the
  9001. double colon. The function must first check if it would like to
  9002. handle this search, for example by checking the `major-mode' or
  9003. the file extension. If it decides not to handle this search, it
  9004. should just return nil to give other functions a chance. If it
  9005. does handle the search, it must return a non-nil value to keep
  9006. other functions from trying.
  9007. Each function can access the current prefix argument through the
  9008. variable `current-prefix-argument'. Note that a single prefix is
  9009. used to force opening a link in Emacs, so it may be good to only
  9010. use a numeric or double prefix to guide the search function.
  9011. In case this is needed, a function in this hook can also restore
  9012. the window configuration before `org-open-at-point' was called using:
  9013. (set-window-configuration org-window-config-before-follow-link)")
  9014. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9015. (defun org-link-search (s &optional type avoid-pos stealth)
  9016. "Search for a link search option.
  9017. If S is surrounded by forward slashes, it is interpreted as a
  9018. regular expression. In org-mode files, this will create an `org-occur'
  9019. sparse tree. In ordinary files, `occur' will be used to list matches.
  9020. If the current buffer is in `dired-mode', grep will be used to search
  9021. in all files. If AVOID-POS is given, ignore matches near that position.
  9022. When optional argument STEALTH is non-nil, do not modify
  9023. visibility around point, thus ignoring
  9024. `org-show-hierarchy-above', `org-show-following-heading' and
  9025. `org-show-siblings' variables."
  9026. (let ((case-fold-search t)
  9027. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9028. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9029. (append '(("") (" ") ("\t") ("\n"))
  9030. org-emphasis-alist)
  9031. "\\|") "\\)"))
  9032. (pos (point))
  9033. (pre nil) (post nil)
  9034. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9035. (cond
  9036. ;; First check if there are any special search functions
  9037. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9038. ;; Now try the builtin stuff
  9039. ((and (equal (string-to-char s0) ?#)
  9040. (> (length s0) 1)
  9041. (save-excursion
  9042. (goto-char (point-min))
  9043. (and
  9044. (re-search-forward
  9045. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9046. (setq type 'dedicated
  9047. pos (match-beginning 0))))
  9048. ;; There is an exact target for this
  9049. (goto-char pos)
  9050. (org-back-to-heading t)))
  9051. ((save-excursion
  9052. (goto-char (point-min))
  9053. (and
  9054. (re-search-forward
  9055. (concat "<<" (regexp-quote s0) ">>") nil t)
  9056. (setq type 'dedicated
  9057. pos (match-beginning 0))))
  9058. ;; There is an exact target for this
  9059. (goto-char pos))
  9060. ((save-excursion
  9061. (goto-char (point-min))
  9062. (and
  9063. (re-search-forward
  9064. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9065. (setq type 'dedicated pos (match-beginning 0))))
  9066. ;; Found an invisible target.
  9067. (goto-char pos))
  9068. ((save-excursion
  9069. (goto-char (point-min))
  9070. (and
  9071. (re-search-forward
  9072. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9073. (setq type 'dedicated pos (match-beginning 0))))
  9074. ;; Found an element with a matching #+name affiliated keyword.
  9075. (goto-char pos))
  9076. ((and (string-match "^(\\(.*\\))$" s0)
  9077. (save-excursion
  9078. (goto-char (point-min))
  9079. (and
  9080. (re-search-forward
  9081. (concat "[^[]" (regexp-quote
  9082. (format org-coderef-label-format
  9083. (match-string 1 s0))))
  9084. nil t)
  9085. (setq type 'dedicated
  9086. pos (1+ (match-beginning 0))))))
  9087. ;; There is a coderef target for this
  9088. (goto-char pos))
  9089. ((string-match "^/\\(.*\\)/$" s)
  9090. ;; A regular expression
  9091. (cond
  9092. ((derived-mode-p 'org-mode)
  9093. (org-occur (match-string 1 s)))
  9094. ;;((eq major-mode 'dired-mode)
  9095. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9096. (t (org-do-occur (match-string 1 s)))))
  9097. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9098. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9099. (goto-char (point-min))
  9100. (cond
  9101. ((let (case-fold-search)
  9102. (re-search-forward (format org-complex-heading-regexp-format
  9103. (regexp-quote s))
  9104. nil t))
  9105. ;; OK, found a match
  9106. (setq type 'dedicated)
  9107. (goto-char (match-beginning 0)))
  9108. ((and (not org-link-search-inhibit-query)
  9109. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9110. (y-or-n-p "No match - create this as a new heading? "))
  9111. (goto-char (point-max))
  9112. (or (bolp) (newline))
  9113. (insert "* " s "\n")
  9114. (beginning-of-line 0))
  9115. (t
  9116. (goto-char pos)
  9117. (error "No match"))))
  9118. (t
  9119. ;; A normal search string
  9120. (when (equal (string-to-char s) ?*)
  9121. ;; Anchor on headlines, post may include tags.
  9122. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9123. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9124. s (substring s 1)))
  9125. (remove-text-properties
  9126. 0 (length s)
  9127. '(face nil mouse-face nil keymap nil fontified nil) s)
  9128. ;; Make a series of regular expressions to find a match
  9129. (setq words (org-split-string s "[ \n\r\t]+")
  9130. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9131. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9132. "\\)" markers)
  9133. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9134. re2a (concat "[ \t\r\n]" re2a_)
  9135. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9136. re4 (concat "[^a-zA-Z_]" re4_)
  9137. re1 (concat pre re2 post)
  9138. re3 (concat pre (if pre re4_ re4) post)
  9139. re5 (concat pre ".*" re4)
  9140. re2 (concat pre re2)
  9141. re2a (concat pre (if pre re2a_ re2a))
  9142. re4 (concat pre (if pre re4_ re4))
  9143. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9144. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9145. re5 "\\)"
  9146. ))
  9147. (cond
  9148. ((eq type 'org-occur) (org-occur reall))
  9149. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9150. (t (goto-char (point-min))
  9151. (setq type 'fuzzy)
  9152. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9153. (org-search-not-self 1 re1 nil t)
  9154. (org-search-not-self 1 re2 nil t)
  9155. (org-search-not-self 1 re2a nil t)
  9156. (org-search-not-self 1 re3 nil t)
  9157. (org-search-not-self 1 re4 nil t)
  9158. (org-search-not-self 1 re5 nil t)
  9159. )
  9160. (goto-char (match-beginning 1))
  9161. (goto-char pos)
  9162. (error "No match"))))))
  9163. (and (derived-mode-p 'org-mode)
  9164. (not stealth)
  9165. (org-show-context 'link-search))
  9166. type))
  9167. (defun org-search-not-self (group &rest args)
  9168. "Execute `re-search-forward', but only accept matches that do not
  9169. enclose the position of `org-open-link-marker'."
  9170. (let ((m org-open-link-marker))
  9171. (catch 'exit
  9172. (while (apply 're-search-forward args)
  9173. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9174. (goto-char (match-end group))
  9175. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9176. (> (match-beginning 0) (marker-position m))
  9177. (< (match-end 0) (marker-position m)))
  9178. (save-match-data
  9179. (or (not (org-in-regexp
  9180. org-bracket-link-analytic-regexp 1))
  9181. (not (match-end 4)) ; no description
  9182. (and (<= (match-beginning 4) (point))
  9183. (>= (match-end 4) (point))))))
  9184. (throw 'exit (point))))))))
  9185. (defun org-get-buffer-for-internal-link (buffer)
  9186. "Return a buffer to be used for displaying the link target of internal links."
  9187. (cond
  9188. ((not org-display-internal-link-with-indirect-buffer)
  9189. buffer)
  9190. ((string-match "(Clone)$" (buffer-name buffer))
  9191. (message "Buffer is already a clone, not making another one")
  9192. ;; we also do not modify visibility in this case
  9193. buffer)
  9194. (t ; make a new indirect buffer for displaying the link
  9195. (let* ((bn (buffer-name buffer))
  9196. (ibn (concat bn "(Clone)"))
  9197. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9198. (with-current-buffer ib (org-overview))
  9199. ib))))
  9200. (defun org-do-occur (regexp &optional cleanup)
  9201. "Call the Emacs command `occur'.
  9202. If CLEANUP is non-nil, remove the printout of the regular expression
  9203. in the *Occur* buffer. This is useful if the regex is long and not useful
  9204. to read."
  9205. (occur regexp)
  9206. (when cleanup
  9207. (let ((cwin (selected-window)) win beg end)
  9208. (when (setq win (get-buffer-window "*Occur*"))
  9209. (select-window win))
  9210. (goto-char (point-min))
  9211. (when (re-search-forward "match[a-z]+" nil t)
  9212. (setq beg (match-end 0))
  9213. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9214. (setq end (1- (match-beginning 0)))))
  9215. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9216. (goto-char (point-min))
  9217. (select-window cwin))))
  9218. ;;; The mark ring for links jumps
  9219. (defvar org-mark-ring nil
  9220. "Mark ring for positions before jumps in Org-mode.")
  9221. (defvar org-mark-ring-last-goto nil
  9222. "Last position in the mark ring used to go back.")
  9223. ;; Fill and close the ring
  9224. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9225. (loop for i from 1 to org-mark-ring-length do
  9226. (push (make-marker) org-mark-ring))
  9227. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9228. org-mark-ring)
  9229. (defun org-mark-ring-push (&optional pos buffer)
  9230. "Put the current position or POS into the mark ring and rotate it."
  9231. (interactive)
  9232. (setq pos (or pos (point)))
  9233. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9234. (move-marker (car org-mark-ring)
  9235. (or pos (point))
  9236. (or buffer (current-buffer)))
  9237. (message "%s"
  9238. (substitute-command-keys
  9239. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9240. (defun org-mark-ring-goto (&optional n)
  9241. "Jump to the previous position in the mark ring.
  9242. With prefix arg N, jump back that many stored positions. When
  9243. called several times in succession, walk through the entire ring.
  9244. Org-mode commands jumping to a different position in the current file,
  9245. or to another Org-mode file, automatically push the old position
  9246. onto the ring."
  9247. (interactive "p")
  9248. (let (p m)
  9249. (if (eq last-command this-command)
  9250. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9251. (setq p org-mark-ring))
  9252. (setq org-mark-ring-last-goto p)
  9253. (setq m (car p))
  9254. (org-pop-to-buffer-same-window (marker-buffer m))
  9255. (goto-char m)
  9256. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9257. (defun org-remove-angle-brackets (s)
  9258. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9259. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9260. s)
  9261. (defun org-add-angle-brackets (s)
  9262. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9263. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9264. s)
  9265. (defun org-remove-double-quotes (s)
  9266. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9267. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9268. s)
  9269. ;;; Following specific links
  9270. (defun org-follow-timestamp-link ()
  9271. "Open an agenda view for the time-stamp date/range at point."
  9272. (cond
  9273. ((org-at-date-range-p t)
  9274. (let ((org-agenda-start-on-weekday)
  9275. (t1 (match-string 1))
  9276. (t2 (match-string 2)) tt1 tt2)
  9277. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9278. tt2 (time-to-days (org-time-string-to-time t2)))
  9279. (let ((org-agenda-buffer-tmp-name
  9280. (format "*Org Agenda(a:%s)"
  9281. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9282. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9283. ((org-at-timestamp-p t)
  9284. (let ((org-agenda-buffer-tmp-name
  9285. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9286. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9287. (substring (match-string 1) 0 10)))
  9288. 1)))
  9289. (t (error "This should not happen"))))
  9290. ;;; Following file links
  9291. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9292. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9293. (declare-function mailcap-mime-info
  9294. "mailcap" (string &optional request no-decode))
  9295. (defvar org-wait nil)
  9296. (defun org-open-file (path &optional in-emacs line search)
  9297. "Open the file at PATH.
  9298. First, this expands any special file name abbreviations. Then the
  9299. configuration variable `org-file-apps' is checked if it contains an
  9300. entry for this file type, and if yes, the corresponding command is launched.
  9301. If no application is found, Emacs simply visits the file.
  9302. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9303. With a double \\[universal-argument] \\[universal-argument] \
  9304. prefix arg, Org tries to avoid opening in Emacs
  9305. and to use an external application to visit the file.
  9306. Optional LINE specifies a line to go to, optional SEARCH a string
  9307. to search for. If LINE or SEARCH is given, the file will be
  9308. opened in Emacs, unless an entry from org-file-apps that makes
  9309. use of groups in a regexp matches.
  9310. If you want to change the way frames are used when following a
  9311. link, please customize `org-link-frame-setup'.
  9312. If the file does not exist, an error is thrown."
  9313. (let* ((file (if (equal path "")
  9314. buffer-file-name
  9315. (substitute-in-file-name (expand-file-name path))))
  9316. (file-apps (append org-file-apps (org-default-apps)))
  9317. (apps (org-remove-if
  9318. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9319. (apps-dlink (org-remove-if-not
  9320. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9321. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9322. (dirp (if remp nil (file-directory-p file)))
  9323. (file (if (and dirp org-open-directory-means-index-dot-org)
  9324. (concat (file-name-as-directory file) "index.org")
  9325. file))
  9326. (a-m-a-p (assq 'auto-mode apps))
  9327. (dfile (downcase file))
  9328. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9329. (link (cond ((and (eq line nil)
  9330. (eq search nil))
  9331. file)
  9332. (line
  9333. (concat file "::" (number-to-string line)))
  9334. (search
  9335. (concat file "::" search))))
  9336. (dlink (downcase link))
  9337. (old-buffer (current-buffer))
  9338. (old-pos (point))
  9339. (old-mode major-mode)
  9340. ext cmd link-match-data)
  9341. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9342. (setq ext (match-string 1 dfile))
  9343. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9344. (setq ext (match-string 1 dfile))))
  9345. (cond
  9346. ((member in-emacs '((16) system))
  9347. (setq cmd (cdr (assoc 'system apps))))
  9348. (in-emacs (setq cmd 'emacs))
  9349. (t
  9350. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9351. (and dirp (cdr (assoc 'directory apps)))
  9352. ; first, try matching against apps-dlink
  9353. ; if we get a match here, store the match data for later
  9354. (let ((match (assoc-default dlink apps-dlink
  9355. 'string-match)))
  9356. (if match
  9357. (progn (setq link-match-data (match-data))
  9358. match)
  9359. (progn (setq in-emacs (or in-emacs line search))
  9360. nil))) ; if we have no match in apps-dlink,
  9361. ; always open the file in emacs if line or search
  9362. ; is given (for backwards compatibility)
  9363. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9364. 'string-match)
  9365. (cdr (assoc ext apps))
  9366. (cdr (assoc t apps))))))
  9367. (when (eq cmd 'system)
  9368. (setq cmd (cdr (assoc 'system apps))))
  9369. (when (eq cmd 'default)
  9370. (setq cmd (cdr (assoc t apps))))
  9371. (when (eq cmd 'mailcap)
  9372. (require 'mailcap)
  9373. (mailcap-parse-mailcaps)
  9374. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9375. (command (mailcap-mime-info mime-type)))
  9376. (if (stringp command)
  9377. (setq cmd command)
  9378. (setq cmd 'emacs))))
  9379. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9380. (not (file-exists-p file))
  9381. (not org-open-non-existing-files))
  9382. (error "No such file: %s" file))
  9383. (cond
  9384. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9385. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9386. (while (string-match "['\"]%s['\"]" cmd)
  9387. (setq cmd (replace-match "%s" t t cmd)))
  9388. (while (string-match "%s" cmd)
  9389. (setq cmd (replace-match
  9390. (save-match-data
  9391. (shell-quote-argument
  9392. (convert-standard-filename file)))
  9393. t t cmd)))
  9394. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9395. (save-match-data
  9396. (let ((match-index 1)
  9397. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9398. (set-match-data link-match-data)
  9399. (while (<= match-index number-of-groups)
  9400. (let ((regex (concat "%" (number-to-string match-index)))
  9401. (replace-with (match-string match-index dlink)))
  9402. (while (string-match regex cmd)
  9403. (setq cmd (replace-match replace-with t t cmd))))
  9404. (setq match-index (+ match-index 1)))))
  9405. (save-window-excursion
  9406. (start-process-shell-command cmd nil cmd)
  9407. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9408. ))
  9409. ((or (stringp cmd)
  9410. (eq cmd 'emacs))
  9411. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9412. (widen)
  9413. (if line (org-goto-line line)
  9414. (if search (org-link-search search))))
  9415. ((consp cmd)
  9416. (let ((file (convert-standard-filename file)))
  9417. (save-match-data
  9418. (set-match-data link-match-data)
  9419. (eval cmd))))
  9420. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9421. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9422. (or (not (equal old-buffer (current-buffer)))
  9423. (not (equal old-pos (point))))
  9424. (org-mark-ring-push old-pos old-buffer))))
  9425. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9426. "This function returns non-nil if `entry' uses a regular
  9427. expression which should be matched against the whole link by
  9428. org-open-file.
  9429. It assumes that is the case when the entry uses a regular
  9430. expression which has at least one grouping construct and the
  9431. action is either a lisp form or a command string containing
  9432. '%1', i.e. using at least one subexpression match as a
  9433. parameter."
  9434. (let ((selector (car entry))
  9435. (action (cdr entry)))
  9436. (if (stringp selector)
  9437. (and (> (regexp-opt-depth selector) 0)
  9438. (or (and (stringp action)
  9439. (string-match "%[0-9]" action))
  9440. (consp action)))
  9441. nil)))
  9442. (defun org-default-apps ()
  9443. "Return the default applications for this operating system."
  9444. (cond
  9445. ((eq system-type 'darwin)
  9446. org-file-apps-defaults-macosx)
  9447. ((eq system-type 'windows-nt)
  9448. org-file-apps-defaults-windowsnt)
  9449. (t org-file-apps-defaults-gnu)))
  9450. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9451. "Convert extensions to regular expressions in the cars of LIST.
  9452. Also, weed out any non-string entries, because the return value is used
  9453. only for regexp matching.
  9454. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9455. point to the symbol `emacs', indicating that the file should
  9456. be opened in Emacs."
  9457. (append
  9458. (delq nil
  9459. (mapcar (lambda (x)
  9460. (if (not (stringp (car x)))
  9461. nil
  9462. (if (string-match "\\W" (car x))
  9463. x
  9464. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9465. list))
  9466. (if add-auto-mode
  9467. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9468. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9469. (defun org-file-remote-p (file)
  9470. "Test whether FILE specifies a location on a remote system.
  9471. Return non-nil if the location is indeed remote.
  9472. For example, the filename \"/user@host:/foo\" specifies a location
  9473. on the system \"/user@host:\"."
  9474. (cond ((fboundp 'file-remote-p)
  9475. (file-remote-p file))
  9476. ((fboundp 'tramp-handle-file-remote-p)
  9477. (tramp-handle-file-remote-p file))
  9478. ((and (boundp 'ange-ftp-name-format)
  9479. (string-match (car ange-ftp-name-format) file))
  9480. t)))
  9481. ;;;; Refiling
  9482. (defun org-get-org-file ()
  9483. "Read a filename, with default directory `org-directory'."
  9484. (let ((default (or org-default-notes-file remember-data-file)))
  9485. (read-file-name (format "File name [%s]: " default)
  9486. (file-name-as-directory org-directory)
  9487. default)))
  9488. (defun org-notes-order-reversed-p ()
  9489. "Check if the current file should receive notes in reversed order."
  9490. (cond
  9491. ((not org-reverse-note-order) nil)
  9492. ((eq t org-reverse-note-order) t)
  9493. ((not (listp org-reverse-note-order)) nil)
  9494. (t (catch 'exit
  9495. (let ((all org-reverse-note-order)
  9496. entry)
  9497. (while (setq entry (pop all))
  9498. (if (string-match (car entry) buffer-file-name)
  9499. (throw 'exit (cdr entry))))
  9500. nil)))))
  9501. (defvar org-refile-target-table nil
  9502. "The list of refile targets, created by `org-refile'.")
  9503. (defvar org-agenda-new-buffers nil
  9504. "Buffers created to visit agenda files.")
  9505. (defvar org-refile-cache nil
  9506. "Cache for refile targets.")
  9507. (defvar org-refile-markers nil
  9508. "All the markers used for caching refile locations.")
  9509. (defun org-refile-marker (pos)
  9510. "Get a new refile marker, but only if caching is in use."
  9511. (if (not org-refile-use-cache)
  9512. pos
  9513. (let ((m (make-marker)))
  9514. (move-marker m pos)
  9515. (push m org-refile-markers)
  9516. m)))
  9517. (defun org-refile-cache-clear ()
  9518. "Clear the refile cache and disable all the markers."
  9519. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9520. (setq org-refile-markers nil)
  9521. (setq org-refile-cache nil)
  9522. (message "Refile cache has been cleared"))
  9523. (defun org-refile-cache-check-set (set)
  9524. "Check if all the markers in the cache still have live buffers."
  9525. (let (marker)
  9526. (catch 'exit
  9527. (while (and set (setq marker (nth 3 (pop set))))
  9528. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9529. (when (and marker (null (marker-buffer marker)))
  9530. (message "not found") (sit-for 3)
  9531. (throw 'exit nil)))
  9532. t)))
  9533. (defun org-refile-cache-put (set &rest identifiers)
  9534. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9535. (let* ((key (sha1 (prin1-to-string identifiers)))
  9536. (entry (assoc key org-refile-cache)))
  9537. (if entry
  9538. (setcdr entry set)
  9539. (push (cons key set) org-refile-cache))))
  9540. (defun org-refile-cache-get (&rest identifiers)
  9541. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9542. (cond
  9543. ((not org-refile-cache) nil)
  9544. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9545. (t
  9546. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9547. org-refile-cache))))
  9548. (and set (org-refile-cache-check-set set) set)))))
  9549. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9550. "Produce a table with refile targets."
  9551. (let ((case-fold-search nil)
  9552. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9553. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9554. targets tgs txt re files f desc descre fast-path-p level pos0)
  9555. (message "Getting targets...")
  9556. (with-current-buffer (or default-buffer (current-buffer))
  9557. (while (setq entry (pop entries))
  9558. (setq files (car entry) desc (cdr entry))
  9559. (setq fast-path-p nil)
  9560. (cond
  9561. ((null files) (setq files (list (current-buffer))))
  9562. ((eq files 'org-agenda-files)
  9563. (setq files (org-agenda-files 'unrestricted)))
  9564. ((and (symbolp files) (fboundp files))
  9565. (setq files (funcall files)))
  9566. ((and (symbolp files) (boundp files))
  9567. (setq files (symbol-value files))))
  9568. (if (stringp files) (setq files (list files)))
  9569. (cond
  9570. ((eq (car desc) :tag)
  9571. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9572. ((eq (car desc) :todo)
  9573. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9574. ((eq (car desc) :regexp)
  9575. (setq descre (cdr desc)))
  9576. ((eq (car desc) :level)
  9577. (setq descre (concat "^\\*\\{" (number-to-string
  9578. (if org-odd-levels-only
  9579. (1- (* 2 (cdr desc)))
  9580. (cdr desc)))
  9581. "\\}[ \t]")))
  9582. ((eq (car desc) :maxlevel)
  9583. (setq fast-path-p t)
  9584. (setq descre (concat "^\\*\\{1," (number-to-string
  9585. (if org-odd-levels-only
  9586. (1- (* 2 (cdr desc)))
  9587. (cdr desc)))
  9588. "\\}[ \t]")))
  9589. (t (error "Bad refiling target description %s" desc)))
  9590. (while (setq f (pop files))
  9591. (with-current-buffer
  9592. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9593. (or
  9594. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9595. (progn
  9596. (if (bufferp f) (setq f (buffer-file-name
  9597. (buffer-base-buffer f))))
  9598. (setq f (and f (expand-file-name f)))
  9599. (if (eq org-refile-use-outline-path 'file)
  9600. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9601. (save-excursion
  9602. (save-restriction
  9603. (widen)
  9604. (goto-char (point-min))
  9605. (while (re-search-forward descre nil t)
  9606. (goto-char (setq pos0 (point-at-bol)))
  9607. (catch 'next
  9608. (when org-refile-target-verify-function
  9609. (save-match-data
  9610. (or (funcall org-refile-target-verify-function)
  9611. (throw 'next t))))
  9612. (when (and (looking-at org-complex-heading-regexp)
  9613. (not (member (match-string 4) excluded-entries))
  9614. (match-string 4))
  9615. (setq level (org-reduced-level
  9616. (- (match-end 1) (match-beginning 1)))
  9617. txt (org-link-display-format (match-string 4))
  9618. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9619. re (format org-complex-heading-regexp-format
  9620. (regexp-quote (match-string 4))))
  9621. (when org-refile-use-outline-path
  9622. (setq txt (mapconcat
  9623. 'org-protect-slash
  9624. (append
  9625. (if (eq org-refile-use-outline-path
  9626. 'file)
  9627. (list (file-name-nondirectory
  9628. (buffer-file-name
  9629. (buffer-base-buffer))))
  9630. (if (eq org-refile-use-outline-path
  9631. 'full-file-path)
  9632. (list (buffer-file-name
  9633. (buffer-base-buffer)))))
  9634. (org-get-outline-path fast-path-p
  9635. level txt)
  9636. (list txt))
  9637. "/")))
  9638. (push (list txt f re (org-refile-marker (point)))
  9639. tgs)))
  9640. (when (= (point) pos0)
  9641. ;; verification function has not moved point
  9642. (goto-char (point-at-eol))))))))
  9643. (when org-refile-use-cache
  9644. (org-refile-cache-put tgs (buffer-file-name) descre))
  9645. (setq targets (append tgs targets))
  9646. ))))
  9647. (message "Getting targets...done")
  9648. (nreverse targets)))
  9649. (defun org-protect-slash (s)
  9650. (while (string-match "/" s)
  9651. (setq s (replace-match "\\" t t s)))
  9652. s)
  9653. (defvar org-olpa (make-vector 20 nil))
  9654. (defun org-get-outline-path (&optional fastp level heading)
  9655. "Return the outline path to the current entry, as a list.
  9656. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9657. routine which makes outline path derivations for an entire file,
  9658. avoiding backtracing. Refile target collection makes use of that."
  9659. (if fastp
  9660. (progn
  9661. (if (> level 19)
  9662. (error "Outline path failure, more than 19 levels"))
  9663. (loop for i from level upto 19 do
  9664. (aset org-olpa i nil))
  9665. (prog1
  9666. (delq nil (append org-olpa nil))
  9667. (aset org-olpa level heading)))
  9668. (let (rtn case-fold-search)
  9669. (save-excursion
  9670. (save-restriction
  9671. (widen)
  9672. (while (org-up-heading-safe)
  9673. (when (looking-at org-complex-heading-regexp)
  9674. (push (org-match-string-no-properties 4) rtn)))
  9675. rtn)))))
  9676. (defun org-format-outline-path (path &optional width prefix)
  9677. "Format the outline path PATH for display.
  9678. Width is the maximum number of characters that is available.
  9679. Prefix is a prefix to be included in the returned string,
  9680. such as the file name."
  9681. (setq width (or width 79))
  9682. (if prefix (setq width (- width (length prefix))))
  9683. (if (not path)
  9684. (or prefix "")
  9685. (let* ((nsteps (length path))
  9686. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9687. (maxwidth (if (<= total-width width)
  9688. 10000 ;; everything fits
  9689. ;; we need to shorten the level headings
  9690. (/ (- width nsteps) nsteps)))
  9691. (org-odd-levels-only nil)
  9692. (n 0)
  9693. (total (1+ (length prefix))))
  9694. (setq maxwidth (max maxwidth 10))
  9695. (concat prefix
  9696. (mapconcat
  9697. (lambda (h)
  9698. (setq n (1+ n))
  9699. (if (and (= n nsteps) (< maxwidth 10000))
  9700. (setq maxwidth (- total-width total)))
  9701. (if (< (length h) maxwidth)
  9702. (progn (setq total (+ total (length h) 1)) h)
  9703. (setq h (substring h 0 (- maxwidth 2))
  9704. total (+ total maxwidth 1))
  9705. (if (string-match "[ \t]+\\'" h)
  9706. (setq h (substring h 0 (match-beginning 0))))
  9707. (setq h (concat h "..")))
  9708. (org-add-props h nil 'face
  9709. (nth (% (1- n) org-n-level-faces)
  9710. org-level-faces))
  9711. h)
  9712. path "/")))))
  9713. (defun org-display-outline-path (&optional file current)
  9714. "Display the current outline path in the echo area."
  9715. (interactive "P")
  9716. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9717. (case-fold-search nil)
  9718. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9719. (if current (setq path (append path
  9720. (save-excursion
  9721. (org-back-to-heading t)
  9722. (if (looking-at org-complex-heading-regexp)
  9723. (list (match-string 4)))))))
  9724. (message "%s"
  9725. (org-format-outline-path
  9726. path
  9727. (1- (frame-width))
  9728. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9729. (defvar org-refile-history nil
  9730. "History for refiling operations.")
  9731. (defvar org-after-refile-insert-hook nil
  9732. "Hook run after `org-refile' has inserted its stuff at the new location.
  9733. Note that this is still *before* the stuff will be removed from
  9734. the *old* location.")
  9735. (defvar org-capture-last-stored-marker)
  9736. (defun org-refile (&optional goto default-buffer rfloc)
  9737. "Move the entry or entries at point to another heading.
  9738. The list of target headings is compiled using the information in
  9739. `org-refile-targets', which see.
  9740. At the target location, the entry is filed as a subitem of the target
  9741. heading. Depending on `org-reverse-note-order', the new subitem will
  9742. either be the first or the last subitem.
  9743. If there is an active region, all entries in that region will be moved.
  9744. However, the region must fulfill the requirement that the first heading
  9745. is the first one sets the top-level of the moved text - at most siblings
  9746. below it are allowed.
  9747. With prefix arg GOTO, the command will only visit the target location
  9748. and not actually move anything.
  9749. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9750. go to the location where the last refiling operation has put the subtree.
  9751. With a prefix argument of `2', refile to the running clock.
  9752. RFLOC can be a refile location obtained in a different way.
  9753. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9754. If you are using target caching (see `org-refile-use-cache'),
  9755. you have to clear the target cache in order to find new targets.
  9756. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9757. prefix argument (`C-u C-u C-u C-c C-w')."
  9758. (interactive "P")
  9759. (if (member goto '(0 (64)))
  9760. (org-refile-cache-clear)
  9761. (let* ((cbuf (current-buffer))
  9762. (regionp (org-region-active-p))
  9763. (region-start (and regionp (region-beginning)))
  9764. (region-end (and regionp (region-end)))
  9765. (region-length (and regionp (- region-end region-start)))
  9766. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9767. pos it nbuf file re level reversed)
  9768. (setq last-command nil)
  9769. (when regionp
  9770. (goto-char region-start)
  9771. (or (bolp) (goto-char (point-at-bol)))
  9772. (setq region-start (point))
  9773. (unless (or (org-kill-is-subtree-p
  9774. (buffer-substring region-start region-end))
  9775. (prog1 org-refile-active-region-within-subtree
  9776. (org-toggle-heading)))
  9777. (error "The region is not a (sequence of) subtree(s)")))
  9778. (if (equal goto '(16))
  9779. (org-refile-goto-last-stored)
  9780. (when (or
  9781. (and (equal goto 2)
  9782. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9783. (prog1
  9784. (setq it (list (or org-clock-heading "running clock")
  9785. (buffer-file-name
  9786. (marker-buffer org-clock-hd-marker))
  9787. ""
  9788. (marker-position org-clock-hd-marker)))
  9789. (setq goto nil)))
  9790. (setq it (or rfloc
  9791. (let (heading-text)
  9792. (save-excursion
  9793. (unless goto
  9794. (org-back-to-heading t)
  9795. (setq heading-text
  9796. (nth 4 (org-heading-components))))
  9797. (org-refile-get-location
  9798. (cond (goto "Goto")
  9799. (regionp "Refile region to")
  9800. (t (concat "Refile subtree \""
  9801. heading-text "\" to")))
  9802. default-buffer
  9803. (and (not (equal '(4) goto))
  9804. org-refile-allow-creating-parent-nodes)
  9805. goto))))))
  9806. (setq file (nth 1 it)
  9807. re (nth 2 it)
  9808. pos (nth 3 it))
  9809. (if (and (not goto)
  9810. pos
  9811. (equal (buffer-file-name) file)
  9812. (if regionp
  9813. (and (>= pos region-start)
  9814. (<= pos region-end))
  9815. (and (>= pos (point))
  9816. (< pos (save-excursion
  9817. (org-end-of-subtree t t))))))
  9818. (error "Cannot refile to position inside the tree or region"))
  9819. (setq nbuf (or (find-buffer-visiting file)
  9820. (find-file-noselect file)))
  9821. (if goto
  9822. (progn
  9823. (org-pop-to-buffer-same-window nbuf)
  9824. (goto-char pos)
  9825. (org-show-context 'org-goto))
  9826. (if regionp
  9827. (progn
  9828. (org-kill-new (buffer-substring region-start region-end))
  9829. (org-save-markers-in-region region-start region-end))
  9830. (org-copy-subtree 1 nil t))
  9831. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9832. (find-file-noselect file)))
  9833. (setq reversed (org-notes-order-reversed-p))
  9834. (save-excursion
  9835. (save-restriction
  9836. (widen)
  9837. (if pos
  9838. (progn
  9839. (goto-char pos)
  9840. (looking-at org-outline-regexp)
  9841. (setq level (org-get-valid-level (funcall outline-level) 1))
  9842. (goto-char
  9843. (if reversed
  9844. (or (outline-next-heading) (point-max))
  9845. (or (save-excursion (org-get-next-sibling))
  9846. (org-end-of-subtree t t)
  9847. (point-max)))))
  9848. (setq level 1)
  9849. (if (not reversed)
  9850. (goto-char (point-max))
  9851. (goto-char (point-min))
  9852. (or (outline-next-heading) (goto-char (point-max)))))
  9853. (if (not (bolp)) (newline))
  9854. (org-paste-subtree level)
  9855. (when org-log-refile
  9856. (org-add-log-setup 'refile nil nil 'findpos
  9857. org-log-refile)
  9858. (unless (eq org-log-refile 'note)
  9859. (save-excursion (org-add-log-note))))
  9860. (and org-auto-align-tags
  9861. (let ((org-loop-over-headlines-in-active-region nil))
  9862. (org-set-tags nil t)))
  9863. (with-demoted-errors
  9864. (bookmark-set "org-refile-last-stored"))
  9865. ;; If we are refiling for capture, make sure that the
  9866. ;; last-capture pointers point here
  9867. (when (org-bound-and-true-p org-refile-for-capture)
  9868. (with-demoted-errors
  9869. (bookmark-set "org-capture-last-stored-marker"))
  9870. (move-marker org-capture-last-stored-marker (point)))
  9871. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9872. (run-hooks 'org-after-refile-insert-hook))))
  9873. (if regionp
  9874. (delete-region (point) (+ (point) region-length))
  9875. (org-cut-subtree))
  9876. (when (featurep 'org-inlinetask)
  9877. (org-inlinetask-remove-END-maybe))
  9878. (setq org-markers-to-move nil)
  9879. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9880. (defun org-refile-goto-last-stored ()
  9881. "Go to the location where the last refile was stored."
  9882. (interactive)
  9883. (bookmark-jump "org-refile-last-stored")
  9884. (message "This is the location of the last refile"))
  9885. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9886. no-exclude)
  9887. "Prompt the user for a refile location, using PROMPT.
  9888. PROMPT should not be suffixed with a colon and a space, because
  9889. this function appends the default value from
  9890. `org-refile-history' automatically, if that is not empty.
  9891. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9892. this is used for the GOTO interface."
  9893. (let ((org-refile-targets org-refile-targets)
  9894. (org-refile-use-outline-path org-refile-use-outline-path)
  9895. excluded-entries)
  9896. (when (and (derived-mode-p 'org-mode)
  9897. (not org-refile-use-cache)
  9898. (not no-exclude))
  9899. (org-map-tree
  9900. (lambda()
  9901. (setq excluded-entries
  9902. (append excluded-entries (list (org-get-heading t t)))))))
  9903. (setq org-refile-target-table
  9904. (org-refile-get-targets default-buffer excluded-entries)))
  9905. (unless org-refile-target-table
  9906. (error "No refile targets"))
  9907. (let* ((prompt (concat prompt
  9908. (and (car org-refile-history)
  9909. (concat " (default " (car org-refile-history) ")"))
  9910. ": "))
  9911. (cbuf (current-buffer))
  9912. (partial-completion-mode nil)
  9913. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9914. (cfunc (if (and org-refile-use-outline-path
  9915. org-outline-path-complete-in-steps)
  9916. 'org-olpath-completing-read
  9917. 'org-icompleting-read))
  9918. (extra (if org-refile-use-outline-path "/" ""))
  9919. (filename (and cfn (expand-file-name cfn)))
  9920. (tbl (mapcar
  9921. (lambda (x)
  9922. (if (and (not (member org-refile-use-outline-path
  9923. '(file full-file-path)))
  9924. (not (equal filename (nth 1 x))))
  9925. (cons (concat (car x) extra " ("
  9926. (file-name-nondirectory (nth 1 x)) ")")
  9927. (cdr x))
  9928. (cons (concat (car x) extra) (cdr x))))
  9929. org-refile-target-table))
  9930. (completion-ignore-case t)
  9931. pa answ parent-target child parent old-hist)
  9932. (setq old-hist org-refile-history)
  9933. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9934. nil 'org-refile-history (car org-refile-history)))
  9935. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9936. (org-refile-check-position pa)
  9937. (if pa
  9938. (progn
  9939. (when (or (not org-refile-history)
  9940. (not (eq old-hist org-refile-history))
  9941. (not (equal (car pa) (car org-refile-history))))
  9942. (setq org-refile-history
  9943. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9944. org-refile-history
  9945. (cdr org-refile-history))))
  9946. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9947. (pop org-refile-history)))
  9948. pa)
  9949. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9950. (progn
  9951. (setq parent (match-string 1 answ)
  9952. child (match-string 2 answ))
  9953. (setq parent-target (or (assoc parent tbl)
  9954. (assoc (concat parent "/") tbl)))
  9955. (when (and parent-target
  9956. (or (eq new-nodes t)
  9957. (and (eq new-nodes 'confirm)
  9958. (y-or-n-p (format "Create new node \"%s\"? "
  9959. child)))))
  9960. (org-refile-new-child parent-target child)))
  9961. (error "Invalid target location")))))
  9962. (declare-function org-string-nw-p "org-macs" (s))
  9963. (defun org-refile-check-position (refile-pointer)
  9964. "Check if the refile pointer matches the headline to which it points."
  9965. (let* ((file (nth 1 refile-pointer))
  9966. (re (nth 2 refile-pointer))
  9967. (pos (nth 3 refile-pointer))
  9968. buffer)
  9969. (if (and (not (markerp pos)) (not file))
  9970. (error "Please save the buffer to a file before refiling")
  9971. (when (org-string-nw-p re)
  9972. (setq buffer (if (markerp pos)
  9973. (marker-buffer pos)
  9974. (or (find-buffer-visiting file)
  9975. (find-file-noselect file))))
  9976. (with-current-buffer buffer
  9977. (save-excursion
  9978. (save-restriction
  9979. (widen)
  9980. (goto-char pos)
  9981. (beginning-of-line 1)
  9982. (unless (org-looking-at-p re)
  9983. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  9984. (defun org-refile-new-child (parent-target child)
  9985. "Use refile target PARENT-TARGET to add new CHILD below it."
  9986. (unless parent-target
  9987. (error "Cannot find parent for new node"))
  9988. (let ((file (nth 1 parent-target))
  9989. (pos (nth 3 parent-target))
  9990. level)
  9991. (with-current-buffer (or (find-buffer-visiting file)
  9992. (find-file-noselect file))
  9993. (save-excursion
  9994. (save-restriction
  9995. (widen)
  9996. (if pos
  9997. (goto-char pos)
  9998. (goto-char (point-max))
  9999. (if (not (bolp)) (newline)))
  10000. (when (looking-at org-outline-regexp)
  10001. (setq level (funcall outline-level))
  10002. (org-end-of-subtree t t))
  10003. (org-back-over-empty-lines)
  10004. (insert "\n" (make-string
  10005. (if pos (org-get-valid-level level 1) 1) ?*)
  10006. " " child "\n")
  10007. (beginning-of-line 0)
  10008. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10009. (defun org-olpath-completing-read (prompt collection &rest args)
  10010. "Read an outline path like a file name."
  10011. (let ((thetable collection)
  10012. (org-completion-use-ido nil) ; does not work with ido.
  10013. (org-completion-use-iswitchb nil)) ; or iswitchb
  10014. (apply
  10015. 'org-icompleting-read prompt
  10016. (lambda (string predicate &optional flag)
  10017. (let (rtn r f (l (length string)))
  10018. (cond
  10019. ((eq flag nil)
  10020. ;; try completion
  10021. (try-completion string thetable))
  10022. ((eq flag t)
  10023. ;; all-completions
  10024. (setq rtn (all-completions string thetable predicate))
  10025. (mapcar
  10026. (lambda (x)
  10027. (setq r (substring x l))
  10028. (if (string-match " ([^)]*)$" x)
  10029. (setq f (match-string 0 x))
  10030. (setq f ""))
  10031. (if (string-match "/" r)
  10032. (concat string (substring r 0 (match-end 0)) f)
  10033. x))
  10034. rtn))
  10035. ((eq flag 'lambda)
  10036. ;; exact match?
  10037. (assoc string thetable)))))
  10038. args)))
  10039. ;;;; Dynamic blocks
  10040. (defun org-find-dblock (name)
  10041. "Find the first dynamic block with name NAME in the buffer.
  10042. If not found, stay at current position and return nil."
  10043. (let ((case-fold-search t) pos)
  10044. (save-excursion
  10045. (goto-char (point-min))
  10046. (setq pos (and (re-search-forward
  10047. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10048. (match-beginning 0))))
  10049. (if pos (goto-char pos))
  10050. pos))
  10051. (defconst org-dblock-start-re
  10052. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10053. "Matches the start line of a dynamic block, with parameters.")
  10054. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10055. "Matches the end of a dynamic block.")
  10056. (defun org-create-dblock (plist)
  10057. "Create a dynamic block section, with parameters taken from PLIST.
  10058. PLIST must contain a :name entry which is used as name of the block."
  10059. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10060. (end-of-line 1)
  10061. (newline))
  10062. (let ((col (current-column))
  10063. (name (plist-get plist :name)))
  10064. (insert "#+BEGIN: " name)
  10065. (while plist
  10066. (if (eq (car plist) :name)
  10067. (setq plist (cddr plist))
  10068. (insert " " (prin1-to-string (pop plist)))))
  10069. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10070. (beginning-of-line -2)))
  10071. (defun org-prepare-dblock ()
  10072. "Prepare dynamic block for refresh.
  10073. This empties the block, puts the cursor at the insert position and returns
  10074. the property list including an extra property :name with the block name."
  10075. (unless (looking-at org-dblock-start-re)
  10076. (error "Not at a dynamic block"))
  10077. (let* ((begdel (1+ (match-end 0)))
  10078. (name (org-no-properties (match-string 1)))
  10079. (params (append (list :name name)
  10080. (read (concat "(" (match-string 3) ")")))))
  10081. (save-excursion
  10082. (beginning-of-line 1)
  10083. (skip-chars-forward " \t")
  10084. (setq params (plist-put params :indentation-column (current-column))))
  10085. (unless (re-search-forward org-dblock-end-re nil t)
  10086. (error "Dynamic block not terminated"))
  10087. (setq params
  10088. (append params
  10089. (list :content (buffer-substring
  10090. begdel (match-beginning 0)))))
  10091. (delete-region begdel (match-beginning 0))
  10092. (goto-char begdel)
  10093. (open-line 1)
  10094. params))
  10095. (defun org-map-dblocks (&optional command)
  10096. "Apply COMMAND to all dynamic blocks in the current buffer.
  10097. If COMMAND is not given, use `org-update-dblock'."
  10098. (let ((cmd (or command 'org-update-dblock)))
  10099. (save-excursion
  10100. (goto-char (point-min))
  10101. (while (re-search-forward org-dblock-start-re nil t)
  10102. (goto-char (match-beginning 0))
  10103. (save-excursion
  10104. (condition-case nil
  10105. (funcall cmd)
  10106. (error (message "Error during update of dynamic block"))))
  10107. (unless (re-search-forward org-dblock-end-re nil t)
  10108. (error "Dynamic block not terminated"))))))
  10109. (defun org-dblock-update (&optional arg)
  10110. "User command for updating dynamic blocks.
  10111. Update the dynamic block at point. With prefix ARG, update all dynamic
  10112. blocks in the buffer."
  10113. (interactive "P")
  10114. (if arg
  10115. (org-update-all-dblocks)
  10116. (or (looking-at org-dblock-start-re)
  10117. (org-beginning-of-dblock))
  10118. (org-update-dblock)))
  10119. (defun org-update-dblock ()
  10120. "Update the dynamic block at point.
  10121. This means to empty the block, parse for parameters and then call
  10122. the correct writing function."
  10123. (interactive)
  10124. (save-window-excursion
  10125. (let* ((pos (point))
  10126. (line (org-current-line))
  10127. (params (org-prepare-dblock))
  10128. (name (plist-get params :name))
  10129. (indent (plist-get params :indentation-column))
  10130. (cmd (intern (concat "org-dblock-write:" name))))
  10131. (message "Updating dynamic block `%s' at line %d..." name line)
  10132. (funcall cmd params)
  10133. (message "Updating dynamic block `%s' at line %d...done" name line)
  10134. (goto-char pos)
  10135. (when (and indent (> indent 0))
  10136. (setq indent (make-string indent ?\ ))
  10137. (save-excursion
  10138. (org-beginning-of-dblock)
  10139. (forward-line 1)
  10140. (while (not (looking-at org-dblock-end-re))
  10141. (insert indent)
  10142. (beginning-of-line 2))
  10143. (when (looking-at org-dblock-end-re)
  10144. (and (looking-at "[ \t]+")
  10145. (replace-match ""))
  10146. (insert indent)))))))
  10147. (defun org-beginning-of-dblock ()
  10148. "Find the beginning of the dynamic block at point.
  10149. Error if there is no such block at point."
  10150. (let ((pos (point))
  10151. beg)
  10152. (end-of-line 1)
  10153. (if (and (re-search-backward org-dblock-start-re nil t)
  10154. (setq beg (match-beginning 0))
  10155. (re-search-forward org-dblock-end-re nil t)
  10156. (> (match-end 0) pos))
  10157. (goto-char beg)
  10158. (goto-char pos)
  10159. (error "Not in a dynamic block"))))
  10160. (defun org-update-all-dblocks ()
  10161. "Update all dynamic blocks in the buffer.
  10162. This function can be used in a hook."
  10163. (interactive)
  10164. (when (derived-mode-p 'org-mode)
  10165. (org-map-dblocks 'org-update-dblock)))
  10166. ;;;; Completion
  10167. (defconst org-additional-option-like-keywords
  10168. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10169. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10170. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10171. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10172. "BEGIN:" "END:"
  10173. "ORGTBL" "TBLFM:" "TBLNAME:"
  10174. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10175. "BEGIN_VERBATIM" "END_VERBATIM"
  10176. "BEGIN_QUOTE" "END_QUOTE"
  10177. "BEGIN_VERSE" "END_VERSE"
  10178. "BEGIN_CENTER" "END_CENTER"
  10179. "BEGIN_SRC" "END_SRC"
  10180. "BEGIN_RESULT" "END_RESULT"
  10181. "BEGIN_lstlisting" "END_lstlisting"
  10182. "NAME:" "RESULTS:"
  10183. "HEADER:" "HEADERS:"
  10184. "COLUMNS:" "PROPERTY:"
  10185. "CAPTION:" "LABEL:"
  10186. "SETUPFILE:"
  10187. "INCLUDE:" "INDEX:"
  10188. "BIND:"
  10189. "MACRO:"))
  10190. (defconst org-options-keywords
  10191. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10192. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10193. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10194. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10195. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10196. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10197. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10198. (defconst org-additional-option-like-keywords-for-flyspell
  10199. (delete-dups
  10200. (split-string
  10201. (mapconcat (lambda(k)
  10202. (replace-regexp-in-string
  10203. "_\\|:" " "
  10204. (concat k " " (downcase k) " " (upcase k))))
  10205. (append org-options-keywords org-additional-option-like-keywords)
  10206. " ")
  10207. " +" t)))
  10208. (defcustom org-structure-template-alist
  10209. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10210. "<src lang=\"?\">\n\n</src>")
  10211. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10212. "<example>\n?\n</example>")
  10213. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10214. "<quote>\n?\n</quote>")
  10215. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10216. "<verse>\n?\n</verse>")
  10217. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10218. "<verbatim>\n?\n</verbatim>")
  10219. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10220. "<center>\n?\n</center>")
  10221. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10222. "<literal style=\"latex\">\n?\n</literal>")
  10223. ("L" "#+LaTeX: "
  10224. "<literal style=\"latex\">?</literal>")
  10225. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10226. "<literal style=\"html\">\n?\n</literal>")
  10227. ("H" "#+HTML: "
  10228. "<literal style=\"html\">?</literal>")
  10229. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10230. ("A" "#+ASCII: ")
  10231. ("i" "#+INDEX: ?"
  10232. "#+INDEX: ?")
  10233. ("I" "#+INCLUDE: %file ?"
  10234. "<include file=%file markup=\"?\">"))
  10235. "Structure completion elements.
  10236. This is a list of abbreviation keys and values. The value gets inserted
  10237. if you type `<' followed by the key and then press the completion key,
  10238. usually `M-TAB'. %file will be replaced by a file name after prompting
  10239. for the file using completion. The cursor will be placed at the position
  10240. of the `?` in the template.
  10241. There are two templates for each key, the first uses the original Org syntax,
  10242. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10243. the default when the /org-mtags.el/ module has been loaded. See also the
  10244. variable `org-mtags-prefer-muse-templates'."
  10245. :group 'org-completion
  10246. :type '(repeat
  10247. (string :tag "Key")
  10248. (string :tag "Template")
  10249. (string :tag "Muse Template")))
  10250. (defun org-try-structure-completion ()
  10251. "Try to complete a structure template before point.
  10252. This looks for strings like \"<e\" on an otherwise empty line and
  10253. expands them."
  10254. (let ((l (buffer-substring (point-at-bol) (point)))
  10255. a)
  10256. (when (and (looking-at "[ \t]*$")
  10257. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10258. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10259. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10260. (match-beginning 1)) a)
  10261. t)))
  10262. (defun org-complete-expand-structure-template (start cell)
  10263. "Expand a structure template."
  10264. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10265. (rpl (nth (if musep 2 1) cell))
  10266. (ind ""))
  10267. (delete-region start (point))
  10268. (when (string-match "\\`#\\+" rpl)
  10269. (cond
  10270. ((bolp))
  10271. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10272. (setq ind (buffer-substring (point-at-bol) (point))))
  10273. (t (newline))))
  10274. (setq start (point))
  10275. (if (string-match "%file" rpl)
  10276. (setq rpl (replace-match
  10277. (concat
  10278. "\""
  10279. (save-match-data
  10280. (abbreviate-file-name (read-file-name "Include file: ")))
  10281. "\"")
  10282. t t rpl)))
  10283. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10284. (concat "\n" ind)))
  10285. (insert rpl)
  10286. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10287. ;;;; TODO, DEADLINE, Comments
  10288. (defun org-toggle-comment ()
  10289. "Change the COMMENT state of an entry."
  10290. (interactive)
  10291. (save-excursion
  10292. (org-back-to-heading)
  10293. (let (case-fold-search)
  10294. (cond
  10295. ((looking-at (format org-heading-keyword-regexp-format
  10296. org-comment-string))
  10297. (goto-char (match-end 1))
  10298. (looking-at (concat " +" org-comment-string))
  10299. (replace-match "" t t)
  10300. (when (eolp) (insert " ")))
  10301. ((looking-at org-outline-regexp)
  10302. (goto-char (match-end 0))
  10303. (insert org-comment-string " "))))))
  10304. (defvar org-last-todo-state-is-todo nil
  10305. "This is non-nil when the last TODO state change led to a TODO state.
  10306. If the last change removed the TODO tag or switched to DONE, then
  10307. this is nil.")
  10308. (defvar org-setting-tags nil) ; dynamically skipped
  10309. (defvar org-todo-setup-filter-hook nil
  10310. "Hook for functions that pre-filter todo specs.
  10311. Each function takes a todo spec and returns either nil or the spec
  10312. transformed into canonical form." )
  10313. (defvar org-todo-get-default-hook nil
  10314. "Hook for functions that get a default item for todo.
  10315. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10316. nil or a string to be used for the todo mark." )
  10317. (defvar org-agenda-headline-snapshot-before-repeat)
  10318. (defun org-current-effective-time ()
  10319. "Return current time adjusted for `org-extend-today-until' variable."
  10320. (let* ((ct (org-current-time))
  10321. (dct (decode-time ct))
  10322. (ct1
  10323. (if (and org-use-effective-time
  10324. (< (nth 2 dct) org-extend-today-until))
  10325. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10326. ct)))
  10327. ct1))
  10328. (defun org-todo-yesterday (&optional arg)
  10329. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10330. (interactive "P")
  10331. (if (eq major-mode 'org-agenda-mode)
  10332. (apply 'org-agenda-todo-yesterday arg)
  10333. (let* ((hour (third (decode-time
  10334. (org-current-time))))
  10335. (org-extend-today-until (1+ hour)))
  10336. (org-todo arg))))
  10337. (defun org-todo (&optional arg)
  10338. "Change the TODO state of an item.
  10339. The state of an item is given by a keyword at the start of the heading,
  10340. like
  10341. *** TODO Write paper
  10342. *** DONE Call mom
  10343. The different keywords are specified in the variable `org-todo-keywords'.
  10344. By default the available states are \"TODO\" and \"DONE\".
  10345. So for this example: when the item starts with TODO, it is changed to DONE.
  10346. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10347. DONE are present, add TODO at the beginning of the heading.
  10348. With \\[universal-argument] prefix arg, use completion to determine the new \
  10349. state.
  10350. With numeric prefix arg, switch to that state.
  10351. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10352. keywords (nextset).
  10353. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10354. With a numeric prefix arg of 0, inhibit note taking for the change.
  10355. For calling through lisp, arg is also interpreted in the following way:
  10356. 'none -> empty state
  10357. \"\"(empty string) -> switch to empty state
  10358. 'done -> switch to DONE
  10359. 'nextset -> switch to the next set of keywords
  10360. 'previousset -> switch to the previous set of keywords
  10361. \"WAITING\" -> switch to the specified keyword, but only if it
  10362. really is a member of `org-todo-keywords'."
  10363. (interactive "P")
  10364. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10365. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10366. 'region-start-level 'region))
  10367. org-loop-over-headlines-in-active-region)
  10368. (org-map-entries
  10369. `(org-todo ,arg)
  10370. org-loop-over-headlines-in-active-region
  10371. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10372. (if (equal arg '(16)) (setq arg 'nextset))
  10373. (let ((org-blocker-hook org-blocker-hook)
  10374. commentp
  10375. case-fold-search)
  10376. (when (equal arg '(64))
  10377. (setq arg nil org-blocker-hook nil))
  10378. (when (and org-blocker-hook
  10379. (or org-inhibit-blocking
  10380. (org-entry-get nil "NOBLOCKING")))
  10381. (setq org-blocker-hook nil))
  10382. (save-excursion
  10383. (catch 'exit
  10384. (org-back-to-heading t)
  10385. (when (looking-at (concat "^\\*+ " org-comment-string))
  10386. (org-toggle-comment)
  10387. (setq commentp t))
  10388. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10389. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10390. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10391. (let* ((match-data (match-data))
  10392. (startpos (point-at-bol))
  10393. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10394. (org-log-done org-log-done)
  10395. (org-log-repeat org-log-repeat)
  10396. (org-todo-log-states org-todo-log-states)
  10397. (org-inhibit-logging
  10398. (if (equal arg 0)
  10399. (progn (setq arg nil) 'note) org-inhibit-logging))
  10400. (this (match-string 1))
  10401. (hl-pos (match-beginning 0))
  10402. (head (org-get-todo-sequence-head this))
  10403. (ass (assoc head org-todo-kwd-alist))
  10404. (interpret (nth 1 ass))
  10405. (done-word (nth 3 ass))
  10406. (final-done-word (nth 4 ass))
  10407. (org-last-state (or this ""))
  10408. (completion-ignore-case t)
  10409. (member (member this org-todo-keywords-1))
  10410. (tail (cdr member))
  10411. (org-state (cond
  10412. ((and org-todo-key-trigger
  10413. (or (and (equal arg '(4))
  10414. (eq org-use-fast-todo-selection 'prefix))
  10415. (and (not arg) org-use-fast-todo-selection
  10416. (not (eq org-use-fast-todo-selection
  10417. 'prefix)))))
  10418. ;; Use fast selection
  10419. (org-fast-todo-selection))
  10420. ((and (equal arg '(4))
  10421. (or (not org-use-fast-todo-selection)
  10422. (not org-todo-key-trigger)))
  10423. ;; Read a state with completion
  10424. (org-icompleting-read
  10425. "State: " (mapcar (lambda(x) (list x))
  10426. org-todo-keywords-1)
  10427. nil t))
  10428. ((eq arg 'right)
  10429. (if this
  10430. (if tail (car tail) nil)
  10431. (car org-todo-keywords-1)))
  10432. ((eq arg 'left)
  10433. (if (equal member org-todo-keywords-1)
  10434. nil
  10435. (if this
  10436. (nth (- (length org-todo-keywords-1)
  10437. (length tail) 2)
  10438. org-todo-keywords-1)
  10439. (org-last org-todo-keywords-1))))
  10440. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10441. (setq arg nil))) ; hack to fall back to cycling
  10442. (arg
  10443. ;; user or caller requests a specific state
  10444. (cond
  10445. ((equal arg "") nil)
  10446. ((eq arg 'none) nil)
  10447. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10448. ((eq arg 'nextset)
  10449. (or (car (cdr (member head org-todo-heads)))
  10450. (car org-todo-heads)))
  10451. ((eq arg 'previousset)
  10452. (let ((org-todo-heads (reverse org-todo-heads)))
  10453. (or (car (cdr (member head org-todo-heads)))
  10454. (car org-todo-heads))))
  10455. ((car (member arg org-todo-keywords-1)))
  10456. ((stringp arg)
  10457. (error "State `%s' not valid in this file" arg))
  10458. ((nth (1- (prefix-numeric-value arg))
  10459. org-todo-keywords-1))))
  10460. ((null member) (or head (car org-todo-keywords-1)))
  10461. ((equal this final-done-word) nil) ;; -> make empty
  10462. ((null tail) nil) ;; -> first entry
  10463. ((memq interpret '(type priority))
  10464. (if (eq this-command last-command)
  10465. (car tail)
  10466. (if (> (length tail) 0)
  10467. (or done-word (car org-done-keywords))
  10468. nil)))
  10469. (t
  10470. (car tail))))
  10471. (org-state (or
  10472. (run-hook-with-args-until-success
  10473. 'org-todo-get-default-hook org-state org-last-state)
  10474. org-state))
  10475. (next (if org-state (concat " " org-state " ") " "))
  10476. (change-plist (list :type 'todo-state-change :from this :to org-state
  10477. :position startpos))
  10478. dolog now-done-p)
  10479. (when org-blocker-hook
  10480. (setq org-last-todo-state-is-todo
  10481. (not (member this org-done-keywords)))
  10482. (unless (save-excursion
  10483. (save-match-data
  10484. (org-with-wide-buffer
  10485. (run-hook-with-args-until-failure
  10486. 'org-blocker-hook change-plist))))
  10487. (if (org-called-interactively-p 'interactive)
  10488. (error "TODO state change from %s to %s blocked" this org-state)
  10489. ;; fail silently
  10490. (message "TODO state change from %s to %s blocked" this org-state)
  10491. (throw 'exit nil))))
  10492. (store-match-data match-data)
  10493. (replace-match next t t)
  10494. (unless (pos-visible-in-window-p hl-pos)
  10495. (message "TODO state changed to %s" (org-trim next)))
  10496. (unless head
  10497. (setq head (org-get-todo-sequence-head org-state)
  10498. ass (assoc head org-todo-kwd-alist)
  10499. interpret (nth 1 ass)
  10500. done-word (nth 3 ass)
  10501. final-done-word (nth 4 ass)))
  10502. (when (memq arg '(nextset previousset))
  10503. (message "Keyword-Set %d/%d: %s"
  10504. (- (length org-todo-sets) -1
  10505. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10506. (length org-todo-sets)
  10507. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10508. (setq org-last-todo-state-is-todo
  10509. (not (member org-state org-done-keywords)))
  10510. (setq now-done-p (and (member org-state org-done-keywords)
  10511. (not (member this org-done-keywords))))
  10512. (and logging (org-local-logging logging))
  10513. (when (and (or org-todo-log-states org-log-done)
  10514. (not (eq org-inhibit-logging t))
  10515. (not (memq arg '(nextset previousset))))
  10516. ;; we need to look at recording a time and note
  10517. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10518. (nth 2 (assoc this org-todo-log-states))))
  10519. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10520. (setq dolog 'time))
  10521. (when (and org-state
  10522. (member org-state org-not-done-keywords)
  10523. (not (member this org-not-done-keywords)))
  10524. ;; This is now a todo state and was not one before
  10525. ;; If there was a CLOSED time stamp, get rid of it.
  10526. (org-add-planning-info nil nil 'closed))
  10527. (when (and now-done-p org-log-done)
  10528. ;; It is now done, and it was not done before
  10529. (org-add-planning-info 'closed (org-current-effective-time))
  10530. (if (and (not dolog) (eq 'note org-log-done))
  10531. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10532. (when (and org-state dolog)
  10533. ;; This is a non-nil state, and we need to log it
  10534. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10535. ;; Fixup tag positioning
  10536. (org-todo-trigger-tag-changes org-state)
  10537. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10538. (when org-provide-todo-statistics
  10539. (org-update-parent-todo-statistics))
  10540. (run-hooks 'org-after-todo-state-change-hook)
  10541. (if (and arg (not (member org-state org-done-keywords)))
  10542. (setq head (org-get-todo-sequence-head org-state)))
  10543. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10544. ;; Do we need to trigger a repeat?
  10545. (when now-done-p
  10546. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10547. ;; This is for the agenda, take a snapshot of the headline.
  10548. (save-match-data
  10549. (setq org-agenda-headline-snapshot-before-repeat
  10550. (org-get-heading))))
  10551. (org-auto-repeat-maybe org-state))
  10552. ;; Fixup cursor location if close to the keyword
  10553. (if (and (outline-on-heading-p)
  10554. (not (bolp))
  10555. (save-excursion (beginning-of-line 1)
  10556. (looking-at org-todo-line-regexp))
  10557. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10558. (progn
  10559. (goto-char (or (match-end 2) (match-end 1)))
  10560. (and (looking-at " ") (just-one-space))))
  10561. (when org-trigger-hook
  10562. (save-excursion
  10563. (run-hook-with-args 'org-trigger-hook change-plist)))
  10564. (when commentp (org-toggle-comment))))))))
  10565. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10566. "Block turning an entry into a TODO, using the hierarchy.
  10567. This checks whether the current task should be blocked from state
  10568. changes. Such blocking occurs when:
  10569. 1. The task has children which are not all in a completed state.
  10570. 2. A task has a parent with the property :ORDERED:, and there
  10571. are siblings prior to the current task with incomplete
  10572. status.
  10573. 3. The parent of the task is blocked because it has siblings that should
  10574. be done first, or is child of a block grandparent TODO entry."
  10575. (if (not org-enforce-todo-dependencies)
  10576. t ; if locally turned off don't block
  10577. (catch 'dont-block
  10578. ;; If this is not a todo state change, or if this entry is already DONE,
  10579. ;; do not block
  10580. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10581. (member (plist-get change-plist :from)
  10582. (cons 'done org-done-keywords))
  10583. (member (plist-get change-plist :to)
  10584. (cons 'todo org-not-done-keywords))
  10585. (not (plist-get change-plist :to)))
  10586. (throw 'dont-block t))
  10587. ;; If this task has children, and any are undone, it's blocked
  10588. (save-excursion
  10589. (org-back-to-heading t)
  10590. (let ((this-level (funcall outline-level)))
  10591. (outline-next-heading)
  10592. (let ((child-level (funcall outline-level)))
  10593. (while (and (not (eobp))
  10594. (> child-level this-level))
  10595. ;; this todo has children, check whether they are all
  10596. ;; completed
  10597. (if (and (not (org-entry-is-done-p))
  10598. (org-entry-is-todo-p))
  10599. (throw 'dont-block nil))
  10600. (outline-next-heading)
  10601. (setq child-level (funcall outline-level))))))
  10602. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10603. ;; any previous siblings are undone, it's blocked
  10604. (save-excursion
  10605. (org-back-to-heading t)
  10606. (let* ((pos (point))
  10607. (parent-pos (and (org-up-heading-safe) (point))))
  10608. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10609. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10610. (forward-line 1)
  10611. (re-search-forward org-not-done-heading-regexp pos t))
  10612. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10613. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10614. (while t
  10615. (goto-char parent-pos)
  10616. (if (not (looking-at org-not-done-heading-regexp))
  10617. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10618. (setq pos (point))
  10619. (setq parent-pos (and (org-up-heading-safe) (point)))
  10620. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10621. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10622. (forward-line 1)
  10623. (re-search-forward org-not-done-heading-regexp pos t))
  10624. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10625. (defcustom org-track-ordered-property-with-tag nil
  10626. "Should the ORDERED property also be shown as a tag?
  10627. The ORDERED property decides if an entry should require subtasks to be
  10628. completed in sequence. Since a property is not very visible, setting
  10629. this option means that toggling the ORDERED property with the command
  10630. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10631. not relevant for the behavior, but it makes things more visible.
  10632. Note that toggling the tag with tags commands will not change the property
  10633. and therefore not influence behavior!
  10634. This can be t, meaning the tag ORDERED should be used, It can also be a
  10635. string to select a different tag for this task."
  10636. :group 'org-todo
  10637. :type '(choice
  10638. (const :tag "No tracking" nil)
  10639. (const :tag "Track with ORDERED tag" t)
  10640. (string :tag "Use other tag")))
  10641. (defun org-toggle-ordered-property ()
  10642. "Toggle the ORDERED property of the current entry.
  10643. For better visibility, you can track the value of this property with a tag.
  10644. See variable `org-track-ordered-property-with-tag'."
  10645. (interactive)
  10646. (let* ((t1 org-track-ordered-property-with-tag)
  10647. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10648. (save-excursion
  10649. (org-back-to-heading)
  10650. (if (org-entry-get nil "ORDERED")
  10651. (progn
  10652. (org-delete-property "ORDERED")
  10653. (and tag (org-toggle-tag tag 'off))
  10654. (message "Subtasks can be completed in arbitrary order"))
  10655. (org-entry-put nil "ORDERED" "t")
  10656. (and tag (org-toggle-tag tag 'on))
  10657. (message "Subtasks must be completed in sequence")))))
  10658. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10659. (defun org-block-todo-from-checkboxes (change-plist)
  10660. "Block turning an entry into a TODO, using checkboxes.
  10661. This checks whether the current task should be blocked from state
  10662. changes because there are unchecked boxes in this entry."
  10663. (if (not org-enforce-todo-checkbox-dependencies)
  10664. t ; if locally turned off don't block
  10665. (catch 'dont-block
  10666. ;; If this is not a todo state change, or if this entry is already DONE,
  10667. ;; do not block
  10668. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10669. (member (plist-get change-plist :from)
  10670. (cons 'done org-done-keywords))
  10671. (member (plist-get change-plist :to)
  10672. (cons 'todo org-not-done-keywords))
  10673. (not (plist-get change-plist :to)))
  10674. (throw 'dont-block t))
  10675. ;; If this task has checkboxes that are not checked, it's blocked
  10676. (save-excursion
  10677. (org-back-to-heading t)
  10678. (let ((beg (point)) end)
  10679. (outline-next-heading)
  10680. (setq end (point))
  10681. (goto-char beg)
  10682. (if (org-list-search-forward
  10683. (concat (org-item-beginning-re)
  10684. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10685. "\\[[- ]\\]")
  10686. end t)
  10687. (progn
  10688. (if (boundp 'org-blocked-by-checkboxes)
  10689. (setq org-blocked-by-checkboxes t))
  10690. (throw 'dont-block nil)))))
  10691. t))) ; do not block
  10692. (defun org-entry-blocked-p ()
  10693. "Is the current entry blocked?"
  10694. (org-with-buffer-modified-unmodified
  10695. (if (org-entry-get nil "NOBLOCKING")
  10696. nil ;; Never block this entry
  10697. (not
  10698. (run-hook-with-args-until-failure
  10699. 'org-blocker-hook
  10700. (list :type 'todo-state-change
  10701. :position (point)
  10702. :from 'todo
  10703. :to 'done))))))
  10704. (defun org-update-statistics-cookies (all)
  10705. "Update the statistics cookie, either from TODO or from checkboxes.
  10706. This should be called with the cursor in a line with a statistics cookie."
  10707. (interactive "P")
  10708. (if all
  10709. (progn
  10710. (org-update-checkbox-count 'all)
  10711. (org-map-entries 'org-update-parent-todo-statistics))
  10712. (if (not (org-at-heading-p))
  10713. (org-update-checkbox-count)
  10714. (let ((pos (point-marker))
  10715. end l1 l2)
  10716. (ignore-errors (org-back-to-heading t))
  10717. (if (not (org-at-heading-p))
  10718. (org-update-checkbox-count)
  10719. (setq l1 (org-outline-level))
  10720. (setq end (save-excursion
  10721. (outline-next-heading)
  10722. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10723. (point)))
  10724. (if (and (save-excursion
  10725. (re-search-forward
  10726. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10727. (not (save-excursion (re-search-forward
  10728. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10729. (org-update-checkbox-count)
  10730. (if (and l2 (> l2 l1))
  10731. (progn
  10732. (goto-char end)
  10733. (org-update-parent-todo-statistics))
  10734. (goto-char pos)
  10735. (beginning-of-line 1)
  10736. (while (re-search-forward
  10737. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10738. (point-at-eol) t)
  10739. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10740. (goto-char pos)
  10741. (move-marker pos nil)))))
  10742. (defvar org-entry-property-inherited-from) ;; defined below
  10743. (defun org-update-parent-todo-statistics ()
  10744. "Update any statistics cookie in the parent of the current headline.
  10745. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10746. the entire subtree and this will travel up the hierarchy and update
  10747. statistics everywhere."
  10748. (let* ((prop (save-excursion (org-up-heading-safe)
  10749. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10750. (recursive (or (not org-hierarchical-todo-statistics)
  10751. (and prop (string-match "\\<recursive\\>" prop))))
  10752. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10753. 0))
  10754. (first t)
  10755. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10756. level ltoggle l1 new ndel
  10757. (cnt-all 0) (cnt-done 0) is-percent kwd
  10758. checkbox-beg ov ovs ove cookie-present)
  10759. (catch 'exit
  10760. (save-excursion
  10761. (beginning-of-line 1)
  10762. (setq ltoggle (funcall outline-level))
  10763. ;; Three situations are to consider:
  10764. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10765. ;; to the top-level ancestor on the headline;
  10766. ;; 2. If parent has "recursive" property, repeat up to the
  10767. ;; headline setting that property, taking inheritance into
  10768. ;; account;
  10769. ;; 3. Else, move up to direct parent and proceed only once.
  10770. (while (and (setq level (org-up-heading-safe))
  10771. (or recursive first)
  10772. (>= (point) lim))
  10773. (setq first nil cookie-present nil)
  10774. (unless (and level
  10775. (not (string-match
  10776. "\\<checkbox\\>"
  10777. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10778. "")))))
  10779. (throw 'exit nil))
  10780. (while (re-search-forward box-re (point-at-eol) t)
  10781. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10782. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10783. (save-match-data
  10784. (unless (outline-next-heading) (throw 'exit nil))
  10785. (while (and (looking-at org-complex-heading-regexp)
  10786. (> (setq l1 (length (match-string 1))) level))
  10787. (setq kwd (and (or recursive (= l1 ltoggle))
  10788. (match-string 2)))
  10789. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10790. (and (listp org-provide-todo-statistics)
  10791. (or (member kwd org-provide-todo-statistics)
  10792. (member kwd org-done-keywords))))
  10793. (setq cnt-all (1+ cnt-all))
  10794. (if (eq org-provide-todo-statistics t)
  10795. (and kwd (setq cnt-all (1+ cnt-all)))))
  10796. (and (member kwd org-done-keywords)
  10797. (setq cnt-done (1+ cnt-done)))
  10798. (outline-next-heading)))
  10799. (setq new
  10800. (if is-percent
  10801. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10802. (format "[%d/%d]" cnt-done cnt-all))
  10803. ndel (- (match-end 0) checkbox-beg))
  10804. ;; handle overlays when updating cookie from column view
  10805. (when (setq ov (car (overlays-at checkbox-beg)))
  10806. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10807. (delete-overlay ov))
  10808. (goto-char checkbox-beg)
  10809. (insert new)
  10810. (delete-region (point) (+ (point) ndel))
  10811. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10812. (when ov (move-overlay ov ovs ove)))
  10813. (when cookie-present
  10814. (run-hook-with-args 'org-after-todo-statistics-hook
  10815. cnt-done (- cnt-all cnt-done))))))
  10816. (run-hooks 'org-todo-statistics-hook)))
  10817. (defvar org-after-todo-statistics-hook nil
  10818. "Hook that is called after a TODO statistics cookie has been updated.
  10819. Each function is called with two arguments: the number of not-done entries
  10820. and the number of done entries.
  10821. For example, the following function, when added to this hook, will switch
  10822. an entry to DONE when all children are done, and back to TODO when new
  10823. entries are set to a TODO status. Note that this hook is only called
  10824. when there is a statistics cookie in the headline!
  10825. (defun org-summary-todo (n-done n-not-done)
  10826. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10827. (let (org-log-done org-log-states) ; turn off logging
  10828. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10829. ")
  10830. (defvar org-todo-statistics-hook nil
  10831. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10832. This hook runs even if there is no statistics cookie present, in which case
  10833. `org-after-todo-statistics-hook' would not run.")
  10834. (defun org-todo-trigger-tag-changes (state)
  10835. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10836. (let ((l org-todo-state-tags-triggers)
  10837. changes)
  10838. (when (or (not state) (equal state ""))
  10839. (setq changes (append changes (cdr (assoc "" l)))))
  10840. (when (and (stringp state) (> (length state) 0))
  10841. (setq changes (append changes (cdr (assoc state l)))))
  10842. (when (member state org-not-done-keywords)
  10843. (setq changes (append changes (cdr (assoc 'todo l)))))
  10844. (when (member state org-done-keywords)
  10845. (setq changes (append changes (cdr (assoc 'done l)))))
  10846. (dolist (c changes)
  10847. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10848. (defun org-local-logging (value)
  10849. "Get logging settings from a property VALUE."
  10850. (let* (words w a)
  10851. ;; directly set the variables, they are already local.
  10852. (setq org-log-done nil
  10853. org-log-repeat nil
  10854. org-todo-log-states nil)
  10855. (setq words (org-split-string value))
  10856. (while (setq w (pop words))
  10857. (cond
  10858. ((setq a (assoc w org-startup-options))
  10859. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10860. (set (nth 1 a) (nth 2 a))))
  10861. ((setq a (org-extract-log-state-settings w))
  10862. (and (member (car a) org-todo-keywords-1)
  10863. (push a org-todo-log-states)))))))
  10864. (defun org-get-todo-sequence-head (kwd)
  10865. "Return the head of the TODO sequence to which KWD belongs.
  10866. If KWD is not set, check if there is a text property remembering the
  10867. right sequence."
  10868. (let (p)
  10869. (cond
  10870. ((not kwd)
  10871. (or (get-text-property (point-at-bol) 'org-todo-head)
  10872. (progn
  10873. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10874. nil (point-at-eol)))
  10875. (get-text-property p 'org-todo-head))))
  10876. ((not (member kwd org-todo-keywords-1))
  10877. (car org-todo-keywords-1))
  10878. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10879. (defun org-fast-todo-selection ()
  10880. "Fast TODO keyword selection with single keys.
  10881. Returns the new TODO keyword, or nil if no state change should occur."
  10882. (let* ((fulltable org-todo-key-alist)
  10883. (done-keywords org-done-keywords) ;; needed for the faces.
  10884. (maxlen (apply 'max (mapcar
  10885. (lambda (x)
  10886. (if (stringp (car x)) (string-width (car x)) 0))
  10887. fulltable)))
  10888. (expert nil)
  10889. (fwidth (+ maxlen 3 1 3))
  10890. (ncol (/ (- (window-width) 4) fwidth))
  10891. tg cnt e c tbl
  10892. groups ingroup)
  10893. (save-excursion
  10894. (save-window-excursion
  10895. (if expert
  10896. (set-buffer (get-buffer-create " *Org todo*"))
  10897. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10898. (erase-buffer)
  10899. (org-set-local 'org-done-keywords done-keywords)
  10900. (setq tbl fulltable cnt 0)
  10901. (while (setq e (pop tbl))
  10902. (cond
  10903. ((equal e '(:startgroup))
  10904. (push '() groups) (setq ingroup t)
  10905. (when (not (= cnt 0))
  10906. (setq cnt 0)
  10907. (insert "\n"))
  10908. (insert "{ "))
  10909. ((equal e '(:endgroup))
  10910. (setq ingroup nil cnt 0)
  10911. (insert "}\n"))
  10912. ((equal e '(:newline))
  10913. (when (not (= cnt 0))
  10914. (setq cnt 0)
  10915. (insert "\n")
  10916. (setq e (car tbl))
  10917. (while (equal (car tbl) '(:newline))
  10918. (insert "\n")
  10919. (setq tbl (cdr tbl)))))
  10920. (t
  10921. (setq tg (car e) c (cdr e))
  10922. (if ingroup (push tg (car groups)))
  10923. (setq tg (org-add-props tg nil 'face
  10924. (org-get-todo-face tg)))
  10925. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10926. (insert "[" c "] " tg (make-string
  10927. (- fwidth 4 (length tg)) ?\ ))
  10928. (when (= (setq cnt (1+ cnt)) ncol)
  10929. (insert "\n")
  10930. (if ingroup (insert " "))
  10931. (setq cnt 0)))))
  10932. (insert "\n")
  10933. (goto-char (point-min))
  10934. (if (not expert) (org-fit-window-to-buffer))
  10935. (message "[a-z..]:Set [SPC]:clear")
  10936. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10937. (cond
  10938. ((or (= c ?\C-g)
  10939. (and (= c ?q) (not (rassoc c fulltable))))
  10940. (setq quit-flag t))
  10941. ((= c ?\ ) nil)
  10942. ((setq e (rassoc c fulltable) tg (car e))
  10943. tg)
  10944. (t (setq quit-flag t)))))))
  10945. (defun org-entry-is-todo-p ()
  10946. (member (org-get-todo-state) org-not-done-keywords))
  10947. (defun org-entry-is-done-p ()
  10948. (member (org-get-todo-state) org-done-keywords))
  10949. (defun org-get-todo-state ()
  10950. (save-excursion
  10951. (org-back-to-heading t)
  10952. (and (looking-at org-todo-line-regexp)
  10953. (match-end 2)
  10954. (match-string 2))))
  10955. (defun org-at-date-range-p (&optional inactive-ok)
  10956. "Is the cursor inside a date range?"
  10957. (interactive)
  10958. (save-excursion
  10959. (catch 'exit
  10960. (let ((pos (point)))
  10961. (skip-chars-backward "^[<\r\n")
  10962. (skip-chars-backward "<[")
  10963. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10964. (>= (match-end 0) pos)
  10965. (throw 'exit t))
  10966. (skip-chars-backward "^<[\r\n")
  10967. (skip-chars-backward "<[")
  10968. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10969. (>= (match-end 0) pos)
  10970. (throw 'exit t)))
  10971. nil)))
  10972. (defun org-get-repeat (&optional tagline)
  10973. "Check if there is a deadline/schedule with repeater in this entry."
  10974. (save-match-data
  10975. (save-excursion
  10976. (org-back-to-heading t)
  10977. (and (re-search-forward (if tagline
  10978. (concat tagline "\\s-*" org-repeat-re)
  10979. org-repeat-re)
  10980. (org-entry-end-position) t)
  10981. (match-string-no-properties 1)))))
  10982. (defvar org-last-changed-timestamp)
  10983. (defvar org-last-inserted-timestamp)
  10984. (defvar org-log-post-message)
  10985. (defvar org-log-note-purpose)
  10986. (defvar org-log-note-how)
  10987. (defvar org-log-note-extra)
  10988. (defun org-auto-repeat-maybe (done-word)
  10989. "Check if the current headline contains a repeated deadline/schedule.
  10990. If yes, set TODO state back to what it was and change the base date
  10991. of repeating deadline/scheduled time stamps to new date.
  10992. This function is run automatically after each state change to a DONE state."
  10993. ;; last-state is dynamically scoped into this function
  10994. (let* ((repeat (org-get-repeat))
  10995. (aa (assoc org-last-state org-todo-kwd-alist))
  10996. (interpret (nth 1 aa))
  10997. (head (nth 2 aa))
  10998. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  10999. (msg "Entry repeats: ")
  11000. (org-log-done nil)
  11001. (org-todo-log-states nil)
  11002. re type n what ts time to-state)
  11003. (when repeat
  11004. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11005. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11006. org-todo-repeat-to-state))
  11007. (unless (and to-state (member to-state org-todo-keywords-1))
  11008. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11009. (org-todo to-state)
  11010. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11011. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11012. (org-time-stamp-format t t))))
  11013. (when org-log-repeat
  11014. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11015. (memq 'org-add-log-note post-command-hook))
  11016. ;; OK, we are already setup for some record
  11017. (if (eq org-log-repeat 'note)
  11018. ;; make sure we take a note, not only a time stamp
  11019. (setq org-log-note-how 'note))
  11020. ;; Set up for taking a record
  11021. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11022. org-last-state
  11023. 'findpos org-log-repeat)))
  11024. (org-back-to-heading t)
  11025. (org-add-planning-info nil nil 'closed)
  11026. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11027. org-deadline-time-regexp "\\)\\|\\("
  11028. org-ts-regexp "\\)"))
  11029. (while (re-search-forward
  11030. re (save-excursion (outline-next-heading) (point)) t)
  11031. (setq type (if (match-end 1) org-scheduled-string
  11032. (if (match-end 3) org-deadline-string "Plain:"))
  11033. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11034. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11035. (setq n (string-to-number (match-string 2 ts))
  11036. what (match-string 3 ts))
  11037. (if (equal what "w") (setq n (* n 7) what "d"))
  11038. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11039. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11040. ;; Preparation, see if we need to modify the start date for the change
  11041. (when (match-end 1)
  11042. (setq time (save-match-data (org-time-string-to-time ts)))
  11043. (cond
  11044. ((equal (match-string 1 ts) ".")
  11045. ;; Shift starting date to today
  11046. (org-timestamp-change
  11047. (- (org-today) (time-to-days time))
  11048. 'day))
  11049. ((equal (match-string 1 ts) "+")
  11050. (let ((nshiftmax 10) (nshift 0))
  11051. (while (or (= nshift 0)
  11052. (<= (time-to-days time)
  11053. (time-to-days (current-time))))
  11054. (when (= (incf nshift) nshiftmax)
  11055. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11056. (error "Abort")))
  11057. (org-timestamp-change n (cdr (assoc what whata)))
  11058. (org-at-timestamp-p t)
  11059. (setq ts (match-string 1))
  11060. (setq time (save-match-data (org-time-string-to-time ts)))))
  11061. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11062. ;; rematch, so that we have everything in place for the real shift
  11063. (org-at-timestamp-p t)
  11064. (setq ts (match-string 1))
  11065. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11066. (org-timestamp-change n (cdr (assoc what whata)))
  11067. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11068. (setq org-log-post-message msg)
  11069. (message "%s" msg))))
  11070. (defun org-show-todo-tree (arg)
  11071. "Make a compact tree which shows all headlines marked with TODO.
  11072. The tree will show the lines where the regexp matches, and all higher
  11073. headlines above the match.
  11074. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11075. With a numeric prefix N, construct a sparse tree for the Nth element
  11076. of `org-todo-keywords-1'."
  11077. (interactive "P")
  11078. (let ((case-fold-search nil)
  11079. (kwd-re
  11080. (cond ((null arg) org-not-done-regexp)
  11081. ((equal arg '(4))
  11082. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11083. (mapcar 'list org-todo-keywords-1))))
  11084. (concat "\\("
  11085. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11086. "\\)\\>")))
  11087. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11088. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11089. org-todo-keywords-1)))
  11090. (t (error "Invalid prefix argument: %s" arg)))))
  11091. (message "%d TODO entries found"
  11092. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11093. (defun org-deadline (&optional remove time)
  11094. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11095. With argument REMOVE, remove any deadline from the item.
  11096. With argument TIME, set the deadline at the corresponding date. TIME
  11097. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11098. (interactive "P")
  11099. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11100. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11101. 'region-start-level 'region))
  11102. org-loop-over-headlines-in-active-region)
  11103. (org-map-entries
  11104. `(org-deadline ',remove ,time)
  11105. org-loop-over-headlines-in-active-region
  11106. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11107. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11108. (repeater (and old-date
  11109. (string-match
  11110. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11111. old-date)
  11112. (match-string 1 old-date))))
  11113. (if remove
  11114. (progn
  11115. (when (and old-date org-log-redeadline)
  11116. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11117. org-log-redeadline))
  11118. (org-remove-timestamp-with-keyword org-deadline-string)
  11119. (message "Item no longer has a deadline."))
  11120. (org-add-planning-info 'deadline time 'closed)
  11121. (when (and old-date org-log-redeadline
  11122. (not (equal old-date
  11123. (substring org-last-inserted-timestamp 1 -1))))
  11124. (org-add-log-setup 'redeadline nil old-date 'findpos
  11125. org-log-redeadline))
  11126. (when repeater
  11127. (save-excursion
  11128. (org-back-to-heading t)
  11129. (when (re-search-forward (concat org-deadline-string " "
  11130. org-last-inserted-timestamp)
  11131. (save-excursion
  11132. (outline-next-heading) (point)) t)
  11133. (goto-char (1- (match-end 0)))
  11134. (insert " " repeater)
  11135. (setq org-last-inserted-timestamp
  11136. (concat (substring org-last-inserted-timestamp 0 -1)
  11137. " " repeater
  11138. (substring org-last-inserted-timestamp -1))))))
  11139. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11140. (defun org-schedule (&optional remove time)
  11141. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11142. With argument REMOVE, remove any scheduling date from the item.
  11143. With argument TIME, scheduled at the corresponding date. TIME can
  11144. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11145. (interactive "P")
  11146. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11147. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11148. 'region-start-level 'region))
  11149. org-loop-over-headlines-in-active-region)
  11150. (org-map-entries
  11151. `(org-schedule ',remove ,time)
  11152. org-loop-over-headlines-in-active-region
  11153. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11154. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11155. (repeater (and old-date
  11156. (string-match
  11157. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11158. old-date)
  11159. (match-string 1 old-date))))
  11160. (if remove
  11161. (progn
  11162. (when (and old-date org-log-reschedule)
  11163. (org-add-log-setup 'delschedule nil old-date 'findpos
  11164. org-log-reschedule))
  11165. (org-remove-timestamp-with-keyword org-scheduled-string)
  11166. (message "Item is no longer scheduled."))
  11167. (org-add-planning-info 'scheduled time 'closed)
  11168. (when (and old-date org-log-reschedule
  11169. (not (equal old-date
  11170. (substring org-last-inserted-timestamp 1 -1))))
  11171. (org-add-log-setup 'reschedule nil old-date 'findpos
  11172. org-log-reschedule))
  11173. (when repeater
  11174. (save-excursion
  11175. (org-back-to-heading t)
  11176. (when (re-search-forward (concat org-scheduled-string " "
  11177. org-last-inserted-timestamp)
  11178. (save-excursion
  11179. (outline-next-heading) (point)) t)
  11180. (goto-char (1- (match-end 0)))
  11181. (insert " " repeater)
  11182. (setq org-last-inserted-timestamp
  11183. (concat (substring org-last-inserted-timestamp 0 -1)
  11184. " " repeater
  11185. (substring org-last-inserted-timestamp -1))))))
  11186. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11187. (defun org-get-scheduled-time (pom &optional inherit)
  11188. "Get the scheduled time as a time tuple, of a format suitable
  11189. for calling org-schedule with, or if there is no scheduling,
  11190. returns nil."
  11191. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11192. (when time
  11193. (apply 'encode-time (org-parse-time-string time)))))
  11194. (defun org-get-deadline-time (pom &optional inherit)
  11195. "Get the deadline as a time tuple, of a format suitable for
  11196. calling org-deadline with, or if there is no scheduling, returns
  11197. nil."
  11198. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11199. (when time
  11200. (apply 'encode-time (org-parse-time-string time)))))
  11201. (defun org-remove-timestamp-with-keyword (keyword)
  11202. "Remove all time stamps with KEYWORD in the current entry."
  11203. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11204. beg)
  11205. (save-excursion
  11206. (org-back-to-heading t)
  11207. (setq beg (point))
  11208. (outline-next-heading)
  11209. (while (re-search-backward re beg t)
  11210. (replace-match "")
  11211. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11212. (equal (char-before) ?\ ))
  11213. (backward-delete-char 1)
  11214. (if (string-match "^[ \t]*$" (buffer-substring
  11215. (point-at-bol) (point-at-eol)))
  11216. (delete-region (point-at-bol)
  11217. (min (point-max) (1+ (point-at-eol))))))))))
  11218. (defun org-add-planning-info (what &optional time &rest remove)
  11219. "Insert new timestamp with keyword in the line directly after the headline.
  11220. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11221. If non is given, the user is prompted for a date.
  11222. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11223. be removed."
  11224. (interactive)
  11225. (let (org-time-was-given org-end-time-was-given ts
  11226. end default-time default-input)
  11227. (catch 'exit
  11228. (when (and (memq what '(scheduled deadline))
  11229. (or (not time)
  11230. (and (stringp time)
  11231. (string-match "^[-+]+[0-9]" time))))
  11232. ;; Try to get a default date/time from existing timestamp
  11233. (save-excursion
  11234. (org-back-to-heading t)
  11235. (setq end (save-excursion (outline-next-heading) (point)))
  11236. (when (re-search-forward (if (eq what 'scheduled)
  11237. org-scheduled-time-regexp
  11238. org-deadline-time-regexp)
  11239. end t)
  11240. (setq ts (match-string 1)
  11241. default-time
  11242. (apply 'encode-time (org-parse-time-string ts))
  11243. default-input (and ts (org-get-compact-tod ts))))))
  11244. (when what
  11245. (setq time
  11246. (if (stringp time)
  11247. ;; This is a string (relative or absolute), set proper date
  11248. (apply 'encode-time
  11249. (org-read-date-analyze
  11250. time default-time (decode-time default-time)))
  11251. ;; If necessary, get the time from the user
  11252. (or time (org-read-date nil 'to-time nil nil
  11253. default-time default-input)))))
  11254. (when (and org-insert-labeled-timestamps-at-point
  11255. (member what '(scheduled deadline)))
  11256. (insert
  11257. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11258. (org-insert-time-stamp time org-time-was-given
  11259. nil nil nil (list org-end-time-was-given))
  11260. (setq what nil))
  11261. (save-excursion
  11262. (save-restriction
  11263. (let (col list elt ts buffer-invisibility-spec)
  11264. (org-back-to-heading t)
  11265. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11266. (goto-char (match-end 1))
  11267. (setq col (current-column))
  11268. (goto-char (match-end 0))
  11269. (if (eobp) (insert "\n") (forward-char 1))
  11270. (when (and (not what)
  11271. (not (looking-at
  11272. (concat "[ \t]*"
  11273. org-keyword-time-not-clock-regexp))))
  11274. ;; Nothing to add, nothing to remove...... :-)
  11275. (throw 'exit nil))
  11276. (if (and (not (looking-at org-outline-regexp))
  11277. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11278. "[^\r\n]*"))
  11279. (not (equal (match-string 1) org-clock-string)))
  11280. (narrow-to-region (match-beginning 0) (match-end 0))
  11281. (insert-before-markers "\n")
  11282. (backward-char 1)
  11283. (narrow-to-region (point) (point))
  11284. (and org-adapt-indentation (org-indent-to-column col)))
  11285. ;; Check if we have to remove something.
  11286. (setq list (cons what remove))
  11287. (while list
  11288. (setq elt (pop list))
  11289. (when (or (and (eq elt 'scheduled)
  11290. (re-search-forward org-scheduled-time-regexp nil t))
  11291. (and (eq elt 'deadline)
  11292. (re-search-forward org-deadline-time-regexp nil t))
  11293. (and (eq elt 'closed)
  11294. (re-search-forward org-closed-time-regexp nil t)))
  11295. (replace-match "")
  11296. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11297. (and (looking-at "[ \t]+") (replace-match ""))
  11298. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11299. (when what
  11300. (insert
  11301. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11302. (cond ((eq what 'scheduled) org-scheduled-string)
  11303. ((eq what 'deadline) org-deadline-string)
  11304. ((eq what 'closed) org-closed-string))
  11305. " ")
  11306. (setq ts (org-insert-time-stamp
  11307. time
  11308. (or org-time-was-given
  11309. (and (eq what 'closed) org-log-done-with-time))
  11310. (eq what 'closed)
  11311. nil nil (list org-end-time-was-given)))
  11312. (insert
  11313. (if (not (or (bolp) (eq (char-before) ?\ )
  11314. (memq (char-after) '(32 10))
  11315. (eobp))) " " ""))
  11316. (end-of-line 1))
  11317. (goto-char (point-min))
  11318. (widen)
  11319. (if (and (looking-at "[ \t]*\n")
  11320. (equal (char-before) ?\n))
  11321. (delete-region (1- (point)) (point-at-eol)))
  11322. ts))))))
  11323. (defvar org-log-note-marker (make-marker))
  11324. (defvar org-log-note-purpose nil)
  11325. (defvar org-log-note-state nil)
  11326. (defvar org-log-note-previous-state nil)
  11327. (defvar org-log-note-how nil)
  11328. (defvar org-log-note-extra nil)
  11329. (defvar org-log-note-window-configuration nil)
  11330. (defvar org-log-note-return-to (make-marker))
  11331. (defvar org-log-note-effective-time nil
  11332. "Remembered current time so that dynamically scoped
  11333. `org-extend-today-until' affects tha timestamps in state change
  11334. log")
  11335. (defvar org-log-post-message nil
  11336. "Message to be displayed after a log note has been stored.
  11337. The auto-repeater uses this.")
  11338. (defun org-add-note ()
  11339. "Add a note to the current entry.
  11340. This is done in the same way as adding a state change note."
  11341. (interactive)
  11342. (org-add-log-setup 'note nil nil 'findpos nil))
  11343. (defvar org-property-end-re)
  11344. (defun org-add-log-setup (&optional purpose state prev-state
  11345. findpos how extra)
  11346. "Set up the post command hook to take a note.
  11347. If this is about to TODO state change, the new state is expected in STATE.
  11348. When FINDPOS is non-nil, find the correct position for the note in
  11349. the current entry. If not, assume that it can be inserted at point.
  11350. HOW is an indicator what kind of note should be created.
  11351. EXTRA is additional text that will be inserted into the notes buffer."
  11352. (let* ((org-log-into-drawer (org-log-into-drawer))
  11353. (drawer (cond ((stringp org-log-into-drawer)
  11354. org-log-into-drawer)
  11355. (org-log-into-drawer "LOGBOOK"))))
  11356. (save-restriction
  11357. (save-excursion
  11358. (when findpos
  11359. (org-back-to-heading t)
  11360. (narrow-to-region (point) (save-excursion
  11361. (outline-next-heading) (point)))
  11362. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11363. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11364. "[^\r\n]*\\)?"))
  11365. (goto-char (match-end 0))
  11366. (cond
  11367. (drawer
  11368. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11369. nil t)
  11370. (progn
  11371. (goto-char (match-end 0))
  11372. (or org-log-states-order-reversed
  11373. (and (re-search-forward org-property-end-re nil t)
  11374. (goto-char (1- (match-beginning 0))))))
  11375. (insert "\n:" drawer ":\n:END:")
  11376. (beginning-of-line 0)
  11377. (org-indent-line)
  11378. (beginning-of-line 2)
  11379. (org-indent-line)
  11380. (end-of-line 0)))
  11381. ((and org-log-state-notes-insert-after-drawers
  11382. (save-excursion
  11383. (forward-line) (looking-at org-drawer-regexp)))
  11384. (forward-line)
  11385. (while (looking-at org-drawer-regexp)
  11386. (goto-char (match-end 0))
  11387. (re-search-forward org-property-end-re (point-max) t)
  11388. (forward-line))
  11389. (forward-line -1)))
  11390. (unless org-log-states-order-reversed
  11391. (and (= (char-after) ?\n) (forward-char 1))
  11392. (org-skip-over-state-notes)
  11393. (skip-chars-backward " \t\n\r")))
  11394. (move-marker org-log-note-marker (point))
  11395. (setq org-log-note-purpose purpose
  11396. org-log-note-state state
  11397. org-log-note-previous-state prev-state
  11398. org-log-note-how how
  11399. org-log-note-extra extra
  11400. org-log-note-effective-time (org-current-effective-time))
  11401. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11402. (defun org-skip-over-state-notes ()
  11403. "Skip past the list of State notes in an entry."
  11404. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11405. (when (ignore-errors (goto-char (org-in-item-p)))
  11406. (let* ((struct (org-list-struct))
  11407. (prevs (org-list-prevs-alist struct)))
  11408. (while (looking-at "[ \t]*- State")
  11409. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11410. (org-list-get-item-end (point) struct)))))))
  11411. (defun org-add-log-note (&optional purpose)
  11412. "Pop up a window for taking a note, and add this note later at point."
  11413. (remove-hook 'post-command-hook 'org-add-log-note)
  11414. (setq org-log-note-window-configuration (current-window-configuration))
  11415. (delete-other-windows)
  11416. (move-marker org-log-note-return-to (point))
  11417. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11418. (goto-char org-log-note-marker)
  11419. (org-switch-to-buffer-other-window "*Org Note*")
  11420. (erase-buffer)
  11421. (if (memq org-log-note-how '(time state))
  11422. (let (current-prefix-arg) (org-store-log-note))
  11423. (let ((org-inhibit-startup t)) (org-mode))
  11424. (insert (format "# Insert note for %s.
  11425. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11426. (cond
  11427. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11428. ((eq org-log-note-purpose 'done) "closed todo item")
  11429. ((eq org-log-note-purpose 'state)
  11430. (format "state change from \"%s\" to \"%s\""
  11431. (or org-log-note-previous-state "")
  11432. (or org-log-note-state "")))
  11433. ((eq org-log-note-purpose 'reschedule)
  11434. "rescheduling")
  11435. ((eq org-log-note-purpose 'delschedule)
  11436. "no longer scheduled")
  11437. ((eq org-log-note-purpose 'redeadline)
  11438. "changing deadline")
  11439. ((eq org-log-note-purpose 'deldeadline)
  11440. "removing deadline")
  11441. ((eq org-log-note-purpose 'refile)
  11442. "refiling")
  11443. ((eq org-log-note-purpose 'note)
  11444. "this entry")
  11445. (t (error "This should not happen")))))
  11446. (if org-log-note-extra (insert org-log-note-extra))
  11447. (org-set-local 'org-finish-function 'org-store-log-note)
  11448. (run-hooks 'org-log-buffer-setup-hook)))
  11449. (defvar org-note-abort nil) ; dynamically scoped
  11450. (defun org-store-log-note ()
  11451. "Finish taking a log note, and insert it to where it belongs."
  11452. (let ((txt (buffer-string))
  11453. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11454. lines ind bul)
  11455. (kill-buffer (current-buffer))
  11456. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11457. (setq txt (replace-match "" t t txt)))
  11458. (if (string-match "\\s-+\\'" txt)
  11459. (setq txt (replace-match "" t t txt)))
  11460. (setq lines (org-split-string txt "\n"))
  11461. (when (and note (string-match "\\S-" note))
  11462. (setq note
  11463. (org-replace-escapes
  11464. note
  11465. (list (cons "%u" (user-login-name))
  11466. (cons "%U" user-full-name)
  11467. (cons "%t" (format-time-string
  11468. (org-time-stamp-format 'long 'inactive)
  11469. org-log-note-effective-time))
  11470. (cons "%T" (format-time-string
  11471. (org-time-stamp-format 'long nil)
  11472. org-log-note-effective-time))
  11473. (cons "%d" (format-time-string
  11474. (org-time-stamp-format nil 'inactive)
  11475. org-log-note-effective-time))
  11476. (cons "%D" (format-time-string
  11477. (org-time-stamp-format nil nil)
  11478. org-log-note-effective-time))
  11479. (cons "%s" (if org-log-note-state
  11480. (concat "\"" org-log-note-state "\"")
  11481. ""))
  11482. (cons "%S" (if org-log-note-previous-state
  11483. (concat "\"" org-log-note-previous-state "\"")
  11484. "\"\"")))))
  11485. (if lines (setq note (concat note " \\\\")))
  11486. (push note lines))
  11487. (when (or current-prefix-arg org-note-abort)
  11488. (when org-log-into-drawer
  11489. (org-remove-empty-drawer-at
  11490. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11491. org-log-note-marker))
  11492. (setq lines nil))
  11493. (when lines
  11494. (with-current-buffer (marker-buffer org-log-note-marker)
  11495. (save-excursion
  11496. (goto-char org-log-note-marker)
  11497. (move-marker org-log-note-marker nil)
  11498. (end-of-line 1)
  11499. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11500. (setq ind (save-excursion
  11501. (if (ignore-errors (goto-char (org-in-item-p)))
  11502. (let ((struct (org-list-struct)))
  11503. (org-list-get-ind
  11504. (org-list-get-top-point struct) struct))
  11505. (skip-chars-backward " \r\t\n")
  11506. (cond
  11507. ((and (org-at-heading-p)
  11508. org-adapt-indentation)
  11509. (1+ (org-current-level)))
  11510. ((org-at-heading-p) 0)
  11511. (t (org-get-indentation))))))
  11512. (setq bul (org-list-bullet-string "-"))
  11513. (org-indent-line-to ind)
  11514. (insert bul (pop lines))
  11515. (let ((ind-body (+ (length bul) ind)))
  11516. (while lines
  11517. (insert "\n")
  11518. (org-indent-line-to ind-body)
  11519. (insert (pop lines))))
  11520. (message "Note stored")
  11521. (org-back-to-heading t)
  11522. (org-cycle-hide-drawers 'children)))))
  11523. (set-window-configuration org-log-note-window-configuration)
  11524. (with-current-buffer (marker-buffer org-log-note-return-to)
  11525. (goto-char org-log-note-return-to))
  11526. (move-marker org-log-note-return-to nil)
  11527. (and org-log-post-message (message "%s" org-log-post-message)))
  11528. (defun org-remove-empty-drawer-at (drawer pos)
  11529. "Remove an empty drawer DRAWER at position POS.
  11530. POS may also be a marker."
  11531. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11532. (save-excursion
  11533. (save-restriction
  11534. (widen)
  11535. (goto-char pos)
  11536. (if (org-in-regexp
  11537. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11538. (replace-match ""))))))
  11539. (defvar org-ts-type nil)
  11540. (defun org-sparse-tree (&optional arg type)
  11541. "Create a sparse tree, prompt for the details.
  11542. This command can create sparse trees. You first need to select the type
  11543. of match used to create the tree:
  11544. t Show all TODO entries.
  11545. T Show entries with a specific TODO keyword.
  11546. m Show entries selected by a tags/property match.
  11547. p Enter a property name and its value (both with completion on existing
  11548. names/values) and show entries with that property.
  11549. r Show entries matching a regular expression (`/' can be used as well).
  11550. b Show deadlines and scheduled items before a date.
  11551. a Show deadlines and scheduled items after a date.
  11552. d Show deadlines due within `org-deadline-warning-days'.
  11553. D Show deadlines and scheduled items between a date range."
  11554. (interactive "P")
  11555. (let (ans kwd value ts-type)
  11556. (setq type (or type org-sparse-tree-default-date-type))
  11557. (setq org-ts-type type)
  11558. (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"
  11559. (cond ((eq type 'all) "all timestamps")
  11560. ((eq type 'scheduled) "only scheduled")
  11561. ((eq type 'deadline) "only deadline")
  11562. ((eq type 'active) "only active timestamps")
  11563. ((eq type 'inactive) "only inactive timestamps")
  11564. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11565. (t "scheduled/deadline")))
  11566. (setq ans (read-char-exclusive))
  11567. (cond
  11568. ((equal ans ?c)
  11569. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11570. ((equal ans ?d)
  11571. (call-interactively 'org-check-deadlines))
  11572. ((equal ans ?b)
  11573. (call-interactively 'org-check-before-date))
  11574. ((equal ans ?a)
  11575. (call-interactively 'org-check-after-date))
  11576. ((equal ans ?D)
  11577. (call-interactively 'org-check-dates-range))
  11578. ((equal ans ?t)
  11579. (call-interactively 'org-show-todo-tree))
  11580. ((equal ans ?T)
  11581. (org-show-todo-tree '(4)))
  11582. ((member ans '(?T ?m))
  11583. (call-interactively 'org-match-sparse-tree))
  11584. ((member ans '(?p ?P))
  11585. (setq kwd (org-icompleting-read "Property: "
  11586. (mapcar 'list (org-buffer-property-keys))))
  11587. (setq value (org-icompleting-read "Value: "
  11588. (mapcar 'list (org-property-values kwd))))
  11589. (unless (string-match "\\`{.*}\\'" value)
  11590. (setq value (concat "\"" value "\"")))
  11591. (org-match-sparse-tree arg (concat kwd "=" value)))
  11592. ((member ans '(?r ?R ?/))
  11593. (call-interactively 'org-occur))
  11594. (t (error "No such sparse tree command \"%c\"" ans)))))
  11595. (defvar org-occur-highlights nil
  11596. "List of overlays used for occur matches.")
  11597. (make-variable-buffer-local 'org-occur-highlights)
  11598. (defvar org-occur-parameters nil
  11599. "Parameters of the active org-occur calls.
  11600. This is a list, each call to org-occur pushes as cons cell,
  11601. containing the regular expression and the callback, onto the list.
  11602. The list can contain several entries if `org-occur' has been called
  11603. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11604. will only contain one set of parameters. When the highlights are
  11605. removed (for example with `C-c C-c', or with the next edit (depending
  11606. on `org-remove-highlights-with-change'), this variable is emptied
  11607. as well.")
  11608. (make-variable-buffer-local 'org-occur-parameters)
  11609. (defun org-occur (regexp &optional keep-previous callback)
  11610. "Make a compact tree which shows all matches of REGEXP.
  11611. The tree will show the lines where the regexp matches, and all higher
  11612. headlines above the match. It will also show the heading after the match,
  11613. to make sure editing the matching entry is easy.
  11614. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11615. call to `org-occur' will be kept, to allow stacking of calls to this
  11616. command.
  11617. If CALLBACK is non-nil, it is a function which is called to confirm
  11618. that the match should indeed be shown."
  11619. (interactive "sRegexp: \nP")
  11620. (when (equal regexp "")
  11621. (error "Regexp cannot be empty"))
  11622. (unless keep-previous
  11623. (org-remove-occur-highlights nil nil t))
  11624. (push (cons regexp callback) org-occur-parameters)
  11625. (let ((cnt 0))
  11626. (save-excursion
  11627. (goto-char (point-min))
  11628. (if (or (not keep-previous) ; do not want to keep
  11629. (not org-occur-highlights)) ; no previous matches
  11630. ;; hide everything
  11631. (org-overview))
  11632. (while (re-search-forward regexp nil t)
  11633. (when (or (not callback)
  11634. (save-match-data (funcall callback)))
  11635. (setq cnt (1+ cnt))
  11636. (when org-highlight-sparse-tree-matches
  11637. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11638. (org-show-context 'occur-tree))))
  11639. (when org-remove-highlights-with-change
  11640. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11641. nil 'local))
  11642. (unless org-sparse-tree-open-archived-trees
  11643. (org-hide-archived-subtrees (point-min) (point-max)))
  11644. (run-hooks 'org-occur-hook)
  11645. (if (org-called-interactively-p 'interactive)
  11646. (message "%d match(es) for regexp %s" cnt regexp))
  11647. cnt))
  11648. (defun org-occur-next-match (&optional n reset)
  11649. "Function for `next-error-function' to find sparse tree matches.
  11650. N is the number of matches to move, when negative move backwards.
  11651. RESET is entirely ignored - this function always goes back to the
  11652. starting point when no match is found."
  11653. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11654. (search-func (if (< n 0)
  11655. 'previous-single-char-property-change
  11656. 'next-single-char-property-change))
  11657. (n (abs n))
  11658. (pos (point))
  11659. p1)
  11660. (catch 'exit
  11661. (while (setq p1 (funcall search-func (point) 'org-type))
  11662. (when (equal p1 limit)
  11663. (goto-char pos)
  11664. (error "No more matches"))
  11665. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11666. (setq n (1- n))
  11667. (when (= n 0)
  11668. (goto-char p1)
  11669. (throw 'exit (point))))
  11670. (goto-char p1))
  11671. (goto-char p1)
  11672. (error "No more matches"))))
  11673. (defun org-show-context (&optional key)
  11674. "Make sure point and context are visible.
  11675. How much context is shown depends upon the variables
  11676. `org-show-hierarchy-above', `org-show-following-heading',
  11677. `org-show-entry-below' and `org-show-siblings'."
  11678. (let ((heading-p (org-at-heading-p t))
  11679. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11680. (following-p (org-get-alist-option org-show-following-heading key))
  11681. (entry-p (org-get-alist-option org-show-entry-below key))
  11682. (siblings-p (org-get-alist-option org-show-siblings key)))
  11683. (catch 'exit
  11684. ;; Show heading or entry text
  11685. (if (and heading-p (not entry-p))
  11686. (org-flag-heading nil) ; only show the heading
  11687. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11688. (org-show-hidden-entry))) ; show entire entry
  11689. (when following-p
  11690. ;; Show next sibling, or heading below text
  11691. (save-excursion
  11692. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11693. (org-flag-heading nil))))
  11694. (when siblings-p (org-show-siblings))
  11695. (when hierarchy-p
  11696. ;; show all higher headings, possibly with siblings
  11697. (save-excursion
  11698. (while (and (condition-case nil
  11699. (progn (org-up-heading-all 1) t)
  11700. (error nil))
  11701. (not (bobp)))
  11702. (org-flag-heading nil)
  11703. (when siblings-p (org-show-siblings))))))))
  11704. (defvar org-reveal-start-hook nil
  11705. "Hook run before revealing a location.")
  11706. (defun org-reveal (&optional siblings)
  11707. "Show current entry, hierarchy above it, and the following headline.
  11708. This can be used to show a consistent set of context around locations
  11709. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11710. not t for the search context.
  11711. With optional argument SIBLINGS, on each level of the hierarchy all
  11712. siblings are shown. This repairs the tree structure to what it would
  11713. look like when opened with hierarchical calls to `org-cycle'.
  11714. With double optional argument \\[universal-argument] \\[universal-argument], \
  11715. go to the parent and show the
  11716. entire tree."
  11717. (interactive "P")
  11718. (run-hooks 'org-reveal-start-hook)
  11719. (let ((org-show-hierarchy-above t)
  11720. (org-show-following-heading t)
  11721. (org-show-siblings (if siblings t org-show-siblings)))
  11722. (org-show-context nil))
  11723. (when (equal siblings '(16))
  11724. (save-excursion
  11725. (when (org-up-heading-safe)
  11726. (org-show-subtree)
  11727. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11728. (defun org-highlight-new-match (beg end)
  11729. "Highlight from BEG to END and mark the highlight is an occur headline."
  11730. (let ((ov (make-overlay beg end)))
  11731. (overlay-put ov 'face 'secondary-selection)
  11732. (overlay-put ov 'org-type 'org-occur)
  11733. (push ov org-occur-highlights)))
  11734. (defun org-remove-occur-highlights (&optional beg end noremove)
  11735. "Remove the occur highlights from the buffer.
  11736. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11737. from the `before-change-functions' in the current buffer."
  11738. (interactive)
  11739. (unless org-inhibit-highlight-removal
  11740. (mapc 'delete-overlay org-occur-highlights)
  11741. (setq org-occur-highlights nil)
  11742. (setq org-occur-parameters nil)
  11743. (unless noremove
  11744. (remove-hook 'before-change-functions
  11745. 'org-remove-occur-highlights 'local))))
  11746. ;;;; Priorities
  11747. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11748. "Regular expression matching the priority indicator.")
  11749. (defvar org-remove-priority-next-time nil)
  11750. (defun org-priority-up ()
  11751. "Increase the priority of the current item."
  11752. (interactive)
  11753. (org-priority 'up))
  11754. (defun org-priority-down ()
  11755. "Decrease the priority of the current item."
  11756. (interactive)
  11757. (org-priority 'down))
  11758. (defun org-priority (&optional action show)
  11759. "Change the priority of an item.
  11760. ACTION can be `set', `up', `down', or a character."
  11761. (interactive "P")
  11762. (if (equal action '(4))
  11763. (org-show-priority)
  11764. (unless org-enable-priority-commands
  11765. (error "Priority commands are disabled"))
  11766. (setq action (or action 'set))
  11767. (let (current new news have remove)
  11768. (save-excursion
  11769. (org-back-to-heading t)
  11770. (if (looking-at org-priority-regexp)
  11771. (setq current (string-to-char (match-string 2))
  11772. have t))
  11773. (cond
  11774. ((eq action 'remove)
  11775. (setq remove t new ?\ ))
  11776. ((or (eq action 'set)
  11777. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11778. (if (not (eq action 'set))
  11779. (setq new action)
  11780. (message "Priority %c-%c, SPC to remove: "
  11781. org-highest-priority org-lowest-priority)
  11782. (save-match-data
  11783. (setq new (read-char-exclusive))))
  11784. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11785. (= (upcase org-lowest-priority) org-lowest-priority))
  11786. (setq new (upcase new)))
  11787. (cond ((equal new ?\ ) (setq remove t))
  11788. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11789. (error "Priority must be between `%c' and `%c'"
  11790. org-highest-priority org-lowest-priority))))
  11791. ((eq action 'up)
  11792. (setq new (if have
  11793. (1- current) ; normal cycling
  11794. ;; last priority was empty
  11795. (if (eq last-command this-command)
  11796. org-lowest-priority ; wrap around empty to lowest
  11797. ;; default
  11798. (if org-priority-start-cycle-with-default
  11799. org-default-priority
  11800. (1- org-default-priority))))))
  11801. ((eq action 'down)
  11802. (setq new (if have
  11803. (1+ current) ; normal cycling
  11804. ;; last priority was empty
  11805. (if (eq last-command this-command)
  11806. org-highest-priority ; wrap around empty to highest
  11807. ;; default
  11808. (if org-priority-start-cycle-with-default
  11809. org-default-priority
  11810. (1+ org-default-priority))))))
  11811. (t (error "Invalid action")))
  11812. (if (or (< (upcase new) org-highest-priority)
  11813. (> (upcase new) org-lowest-priority))
  11814. (if (and (memq action '(up down))
  11815. (not have) (not (eq last-command this-command)))
  11816. ;; `new' is from default priority
  11817. (error
  11818. "The default can not be set, see `org-default-priority' why")
  11819. ;; normal cycling: `new' is beyond highest/lowest priority
  11820. ;; and is wrapped around to the empty priority
  11821. (setq remove t)))
  11822. (setq news (format "%c" new))
  11823. (if have
  11824. (if remove
  11825. (replace-match "" t t nil 1)
  11826. (replace-match news t t nil 2))
  11827. (if remove
  11828. (error "No priority cookie found in line")
  11829. (let ((case-fold-search nil))
  11830. (looking-at org-todo-line-regexp))
  11831. (if (match-end 2)
  11832. (progn
  11833. (goto-char (match-end 2))
  11834. (insert " [#" news "]"))
  11835. (goto-char (match-beginning 3))
  11836. (insert "[#" news "] "))))
  11837. (org-preserve-lc (org-set-tags nil 'align)))
  11838. (if remove
  11839. (message "Priority removed")
  11840. (message "Priority of current item set to %s" news)))))
  11841. (defun org-show-priority ()
  11842. "Show the priority of the current item.
  11843. This priority is composed of the main priority given with the [#A] cookies,
  11844. and by additional input from the age of a schedules or deadline entry."
  11845. (interactive)
  11846. (let ((pri (if (eq major-mode 'org-agenda-mode)
  11847. (org-get-at-bol 'priority)
  11848. (save-excursion
  11849. (save-match-data
  11850. (beginning-of-line)
  11851. (and (looking-at org-heading-regexp)
  11852. (org-get-priority (match-string 0))))))))
  11853. (message "Priority is %d" (if pri pri -1000))))
  11854. (defun org-get-priority (s)
  11855. "Find priority cookie and return priority."
  11856. (if (functionp org-get-priority-function)
  11857. (funcall org-get-priority-function)
  11858. (save-match-data
  11859. (if (not (string-match org-priority-regexp s))
  11860. (* 1000 (- org-lowest-priority org-default-priority))
  11861. (* 1000 (- org-lowest-priority
  11862. (string-to-char (match-string 2 s))))))))
  11863. ;;;; Tags
  11864. (defvar org-agenda-archives-mode)
  11865. (defvar org-map-continue-from nil
  11866. "Position from where mapping should continue.
  11867. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11868. (defvar org-scanner-tags nil
  11869. "The current tag list while the tags scanner is running.")
  11870. (defvar org-trust-scanner-tags nil
  11871. "Should `org-get-tags-at' use the tags for the scanner.
  11872. This is for internal dynamical scoping only.
  11873. When this is non-nil, the function `org-get-tags-at' will return the value
  11874. of `org-scanner-tags' instead of building the list by itself. This
  11875. can lead to large speed-ups when the tags scanner is used in a file with
  11876. many entries, and when the list of tags is retrieved, for example to
  11877. obtain a list of properties. Building the tags list for each entry in such
  11878. a file becomes an N^2 operation - but with this variable set, it scales
  11879. as N.")
  11880. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11881. "Scan headline tags with inheritance and produce output ACTION.
  11882. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11883. or `agenda' to produce an entry list for an agenda view. It can also be
  11884. a Lisp form or a function that should be called at each matched headline, in
  11885. this case the return value is a list of all return values from these calls.
  11886. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11887. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11888. only lines with a not-done TODO keyword are included in the output.
  11889. This should be the same variable that was scoped into
  11890. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11891. START-LEVEL can be a string with asterisks, reducing the scope to
  11892. headlines matching this string."
  11893. (require 'org-agenda)
  11894. (let* ((re (concat "^"
  11895. (if start-level
  11896. ;; Get the correct level to match
  11897. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11898. org-outline-regexp)
  11899. " *\\(\\<\\("
  11900. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11901. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11902. (props (list 'face 'default
  11903. 'done-face 'org-agenda-done
  11904. 'undone-face 'default
  11905. 'mouse-face 'highlight
  11906. 'org-not-done-regexp org-not-done-regexp
  11907. 'org-todo-regexp org-todo-regexp
  11908. 'org-complex-heading-regexp org-complex-heading-regexp
  11909. 'help-echo
  11910. (format "mouse-2 or RET jump to org file %s"
  11911. (abbreviate-file-name
  11912. (or (buffer-file-name (buffer-base-buffer))
  11913. (buffer-name (buffer-base-buffer)))))))
  11914. (case-fold-search nil)
  11915. (org-map-continue-from nil)
  11916. lspos tags tags-list
  11917. (tags-alist (list (cons 0 org-file-tags)))
  11918. (llast 0) rtn rtn1 level category i txt
  11919. todo marker entry priority)
  11920. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11921. (setq action (list 'lambda nil action)))
  11922. (save-excursion
  11923. (goto-char (point-min))
  11924. (when (eq action 'sparse-tree)
  11925. (org-overview)
  11926. (org-remove-occur-highlights))
  11927. (while (re-search-forward re nil t)
  11928. (setq org-map-continue-from nil)
  11929. (catch :skip
  11930. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11931. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11932. (goto-char (setq lspos (match-beginning 0)))
  11933. (setq level (org-reduced-level (funcall outline-level))
  11934. category (org-get-category))
  11935. (setq i llast llast level)
  11936. ;; remove tag lists from same and sublevels
  11937. (while (>= i level)
  11938. (when (setq entry (assoc i tags-alist))
  11939. (setq tags-alist (delete entry tags-alist)))
  11940. (setq i (1- i)))
  11941. ;; add the next tags
  11942. (when tags
  11943. (setq tags (org-split-string tags ":")
  11944. tags-alist
  11945. (cons (cons level tags) tags-alist)))
  11946. ;; compile tags for current headline
  11947. (setq tags-list
  11948. (if org-use-tag-inheritance
  11949. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11950. tags)
  11951. org-scanner-tags tags-list)
  11952. (when org-use-tag-inheritance
  11953. (setcdr (car tags-alist)
  11954. (mapcar (lambda (x)
  11955. (setq x (copy-sequence x))
  11956. (org-add-prop-inherited x))
  11957. (cdar tags-alist))))
  11958. (when (and tags org-use-tag-inheritance
  11959. (or (not (eq t org-use-tag-inheritance))
  11960. org-tags-exclude-from-inheritance))
  11961. ;; selective inheritance, remove uninherited ones
  11962. (setcdr (car tags-alist)
  11963. (org-remove-uninherited-tags (cdar tags-alist))))
  11964. (when (and
  11965. ;; eval matcher only when the todo condition is OK
  11966. (and (or (not todo-only) (member todo org-not-done-keywords))
  11967. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11968. (eval matcher)))
  11969. ;; Call the skipper, but return t if it does not skip,
  11970. ;; so that the `and' form continues evaluating
  11971. (progn
  11972. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11973. t)
  11974. ;; Check if timestamps are deselecting this entry
  11975. (or (not todo-only)
  11976. (and (member todo org-not-done-keywords)
  11977. (or (not org-agenda-tags-todo-honor-ignore-options)
  11978. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11979. ;; Extra check for the archive tag
  11980. ;; FIXME: Does the skipper already do this????
  11981. (or
  11982. (not (member org-archive-tag tags-list))
  11983. ;; we have an archive tag, should we use this anyway?
  11984. (or (not org-agenda-skip-archived-trees)
  11985. (and (eq action 'agenda) org-agenda-archives-mode))))
  11986. ;; select this headline
  11987. (cond
  11988. ((eq action 'sparse-tree)
  11989. (and org-highlight-sparse-tree-matches
  11990. (org-get-heading) (match-end 0)
  11991. (org-highlight-new-match
  11992. (match-beginning 1) (match-end 1)))
  11993. (org-show-context 'tags-tree))
  11994. ((eq action 'agenda)
  11995. (setq txt (org-agenda-format-item
  11996. ""
  11997. (concat
  11998. (if (eq org-tags-match-list-sublevels 'indented)
  11999. (make-string (1- level) ?.) "")
  12000. (org-get-heading))
  12001. category
  12002. tags-list)
  12003. priority (org-get-priority txt))
  12004. (goto-char lspos)
  12005. (setq marker (org-agenda-new-marker))
  12006. (org-add-props txt props
  12007. 'org-marker marker 'org-hd-marker marker 'org-category category
  12008. 'todo-state todo
  12009. 'priority priority 'type "tagsmatch")
  12010. (push txt rtn))
  12011. ((functionp action)
  12012. (setq org-map-continue-from nil)
  12013. (save-excursion
  12014. (setq rtn1 (funcall action))
  12015. (push rtn1 rtn)))
  12016. (t (error "Invalid action")))
  12017. ;; if we are to skip sublevels, jump to end of subtree
  12018. (unless org-tags-match-list-sublevels
  12019. (org-end-of-subtree t)
  12020. (backward-char 1))))
  12021. ;; Get the correct position from where to continue
  12022. (if org-map-continue-from
  12023. (goto-char org-map-continue-from)
  12024. (and (= (point) lspos) (end-of-line 1)))))
  12025. (when (and (eq action 'sparse-tree)
  12026. (not org-sparse-tree-open-archived-trees))
  12027. (org-hide-archived-subtrees (point-min) (point-max)))
  12028. (nreverse rtn)))
  12029. (defun org-remove-uninherited-tags (tags)
  12030. "Remove all tags that are not inherited from the list TAGS."
  12031. (cond
  12032. ((eq org-use-tag-inheritance t)
  12033. (if org-tags-exclude-from-inheritance
  12034. (org-delete-all org-tags-exclude-from-inheritance tags)
  12035. tags))
  12036. ((not org-use-tag-inheritance) nil)
  12037. ((stringp org-use-tag-inheritance)
  12038. (delq nil (mapcar
  12039. (lambda (x)
  12040. (if (and (string-match org-use-tag-inheritance x)
  12041. (not (member x org-tags-exclude-from-inheritance)))
  12042. x nil))
  12043. tags)))
  12044. ((listp org-use-tag-inheritance)
  12045. (delq nil (mapcar
  12046. (lambda (x)
  12047. (if (member x org-use-tag-inheritance) x nil))
  12048. tags)))))
  12049. (defun org-match-sparse-tree (&optional todo-only match)
  12050. "Create a sparse tree according to tags string MATCH.
  12051. MATCH can contain positive and negative selection of tags, like
  12052. \"+WORK+URGENT-WITHBOSS\".
  12053. If optional argument TODO-ONLY is non-nil, only select lines that are
  12054. also TODO lines."
  12055. (interactive "P")
  12056. (org-agenda-prepare-buffers (list (current-buffer)))
  12057. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12058. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12059. (defvar org-cached-props nil)
  12060. (defun org-cached-entry-get (pom property)
  12061. (if (or (eq t org-use-property-inheritance)
  12062. (and (stringp org-use-property-inheritance)
  12063. (string-match org-use-property-inheritance property))
  12064. (and (listp org-use-property-inheritance)
  12065. (member property org-use-property-inheritance)))
  12066. ;; Caching is not possible, check it directly
  12067. (org-entry-get pom property 'inherit)
  12068. ;; Get all properties, so that we can do complicated checks easily
  12069. (cdr (assoc property (or org-cached-props
  12070. (setq org-cached-props
  12071. (org-entry-properties pom)))))))
  12072. (defun org-global-tags-completion-table (&optional files)
  12073. "Return the list of all tags in all agenda buffer/files.
  12074. Optional FILES argument is a list of files which can be used
  12075. instead of the agenda files."
  12076. (save-excursion
  12077. (org-uniquify
  12078. (delq nil
  12079. (apply 'append
  12080. (mapcar
  12081. (lambda (file)
  12082. (set-buffer (find-file-noselect file))
  12083. (append (org-get-buffer-tags)
  12084. (mapcar (lambda (x) (if (stringp (car-safe x))
  12085. (list (car-safe x)) nil))
  12086. org-tag-alist)))
  12087. (if (and files (car files))
  12088. files
  12089. (org-agenda-files))))))))
  12090. (defun org-make-tags-matcher (match)
  12091. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12092. The variable `todo-only' is scoped dynamically into this function.
  12093. It will be set to t if the matcher restricts matching to TODO entries,
  12094. otherwise will not be touched.
  12095. Returns a cons of the selection string MATCH and the constructed
  12096. lisp form implementing the matcher. The matcher is to be evaluated
  12097. at an Org entry, with point on the headline, and returns t if the
  12098. entry matches the selection string MATCH. The returned lisp form
  12099. references two variables with information about the entry, which
  12100. must be bound around the form's evaluation: todo, the TODO keyword
  12101. at the entry (or nil of none); and tags-list, the list of all tags
  12102. at the entry including inherited ones. Additionally, the category
  12103. of the entry (if any) must be specified as the text property
  12104. 'org-category on the headline.
  12105. See also `org-scan-tags'.
  12106. "
  12107. (declare (special todo-only))
  12108. (unless (boundp 'todo-only)
  12109. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12110. (unless match
  12111. ;; Get a new match request, with completion
  12112. (let ((org-last-tags-completion-table
  12113. (org-global-tags-completion-table)))
  12114. (setq match (org-completing-read-no-i
  12115. "Match: " 'org-tags-completion-function nil nil nil
  12116. 'org-tags-history))))
  12117. ;; Parse the string and create a lisp form
  12118. (let ((match0 match)
  12119. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12120. minus tag mm
  12121. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12122. orterms term orlist re-p str-p level-p level-op time-p
  12123. prop-p pn pv po gv rest)
  12124. (if (string-match "/+" match)
  12125. ;; match contains also a todo-matching request
  12126. (progn
  12127. (setq tagsmatch (substring match 0 (match-beginning 0))
  12128. todomatch (substring match (match-end 0)))
  12129. (if (string-match "^!" todomatch)
  12130. (setq todo-only t todomatch (substring todomatch 1)))
  12131. (if (string-match "^\\s-*$" todomatch)
  12132. (setq todomatch nil)))
  12133. ;; only matching tags
  12134. (setq tagsmatch match todomatch nil))
  12135. ;; Make the tags matcher
  12136. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12137. (setq tagsmatcher t)
  12138. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12139. (while (setq term (pop orterms))
  12140. (while (and (equal (substring term -1) "\\") orterms)
  12141. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12142. (while (string-match re term)
  12143. (setq rest (substring term (match-end 0))
  12144. minus (and (match-end 1)
  12145. (equal (match-string 1 term) "-"))
  12146. tag (save-match-data (replace-regexp-in-string
  12147. "\\\\-" "-"
  12148. (match-string 2 term)))
  12149. re-p (equal (string-to-char tag) ?{)
  12150. level-p (match-end 4)
  12151. prop-p (match-end 5)
  12152. mm (cond
  12153. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12154. (level-p
  12155. (setq level-op (org-op-to-function (match-string 3 term)))
  12156. `(,level-op level ,(string-to-number
  12157. (match-string 4 term))))
  12158. (prop-p
  12159. (setq pn (match-string 5 term)
  12160. po (match-string 6 term)
  12161. pv (match-string 7 term)
  12162. re-p (equal (string-to-char pv) ?{)
  12163. str-p (equal (string-to-char pv) ?\")
  12164. time-p (save-match-data
  12165. (string-match "^\"[[<].*[]>]\"$" pv))
  12166. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12167. (if time-p (setq pv (org-matcher-time pv)))
  12168. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12169. (cond
  12170. ((equal pn "CATEGORY")
  12171. (setq gv '(get-text-property (point) 'org-category)))
  12172. ((equal pn "TODO")
  12173. (setq gv 'todo))
  12174. (t
  12175. (setq gv `(org-cached-entry-get nil ,pn))))
  12176. (if re-p
  12177. (if (eq po 'org<>)
  12178. `(not (string-match ,pv (or ,gv "")))
  12179. `(string-match ,pv (or ,gv "")))
  12180. (if str-p
  12181. `(,po (or ,gv "") ,pv)
  12182. `(,po (string-to-number (or ,gv ""))
  12183. ,(string-to-number pv) ))))
  12184. (t `(member ,tag tags-list)))
  12185. mm (if minus (list 'not mm) mm)
  12186. term rest)
  12187. (push mm tagsmatcher))
  12188. (push (if (> (length tagsmatcher) 1)
  12189. (cons 'and tagsmatcher)
  12190. (car tagsmatcher))
  12191. orlist)
  12192. (setq tagsmatcher nil))
  12193. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12194. (setq tagsmatcher
  12195. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12196. ;; Make the todo matcher
  12197. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12198. (setq todomatcher t)
  12199. (setq orterms (org-split-string todomatch "|") orlist nil)
  12200. (while (setq term (pop orterms))
  12201. (while (string-match re term)
  12202. (setq minus (and (match-end 1)
  12203. (equal (match-string 1 term) "-"))
  12204. kwd (match-string 2 term)
  12205. re-p (equal (string-to-char kwd) ?{)
  12206. term (substring term (match-end 0))
  12207. mm (if re-p
  12208. `(string-match ,(substring kwd 1 -1) todo)
  12209. (list 'equal 'todo kwd))
  12210. mm (if minus (list 'not mm) mm))
  12211. (push mm todomatcher))
  12212. (push (if (> (length todomatcher) 1)
  12213. (cons 'and todomatcher)
  12214. (car todomatcher))
  12215. orlist)
  12216. (setq todomatcher nil))
  12217. (setq todomatcher (if (> (length orlist) 1)
  12218. (cons 'or orlist) (car orlist))))
  12219. ;; Return the string and lisp forms of the matcher
  12220. (setq matcher (if todomatcher
  12221. (list 'and tagsmatcher todomatcher)
  12222. tagsmatcher))
  12223. (when todo-only
  12224. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12225. matcher)))
  12226. (cons match0 matcher)))
  12227. (defun org-op-to-function (op &optional stringp)
  12228. "Turn an operator into the appropriate function."
  12229. (setq op
  12230. (cond
  12231. ((equal op "<" ) '(< string< org-time<))
  12232. ((equal op ">" ) '(> org-string> org-time>))
  12233. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12234. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12235. ((member op '("=" "==")) '(= string= org-time=))
  12236. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12237. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12238. (defun org<> (a b) (not (= a b)))
  12239. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12240. (defun org-string>= (a b) (not (string< a b)))
  12241. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12242. (defun org-string<> (a b) (not (string= a b)))
  12243. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12244. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12245. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12246. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12247. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12248. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12249. (defun org-2ft (s)
  12250. "Convert S to a floating point time.
  12251. If S is already a number, just return it. If it is a string, parse
  12252. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12253. (cond
  12254. ((numberp s) s)
  12255. ((stringp s)
  12256. (condition-case nil
  12257. (float-time (apply 'encode-time (org-parse-time-string s)))
  12258. (error 0.)))
  12259. (t 0.)))
  12260. (defun org-time-today ()
  12261. "Time in seconds today at 0:00.
  12262. Returns the float number of seconds since the beginning of the
  12263. epoch to the beginning of today (00:00)."
  12264. (float-time (apply 'encode-time
  12265. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12266. (defun org-matcher-time (s)
  12267. "Interpret a time comparison value."
  12268. (save-match-data
  12269. (cond
  12270. ((string= s "<now>") (float-time))
  12271. ((string= s "<today>") (org-time-today))
  12272. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12273. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12274. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12275. (+ (org-time-today)
  12276. (* (string-to-number (match-string 1 s))
  12277. (cdr (assoc (match-string 2 s)
  12278. '(("d" . 86400.0) ("w" . 604800.0)
  12279. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12280. (t (org-2ft s)))))
  12281. (defun org-match-any-p (re list)
  12282. "Does re match any element of list?"
  12283. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12284. (delq nil list))
  12285. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12286. (defvar org-tags-overlay (make-overlay 1 1))
  12287. (org-detach-overlay org-tags-overlay)
  12288. (defun org-get-local-tags-at (&optional pos)
  12289. "Get a list of tags defined in the current headline."
  12290. (org-get-tags-at pos 'local))
  12291. (defun org-get-local-tags ()
  12292. "Get a list of tags defined in the current headline."
  12293. (org-get-tags-at nil 'local))
  12294. (defun org-get-tags-at (&optional pos local)
  12295. "Get a list of all headline tags applicable at POS.
  12296. POS defaults to point. If tags are inherited, the list contains
  12297. the targets in the same sequence as the headlines appear, i.e.
  12298. the tags of the current headline come last.
  12299. When LOCAL is non-nil, only return tags from the current headline,
  12300. ignore inherited ones."
  12301. (interactive)
  12302. (if (and org-trust-scanner-tags
  12303. (or (not pos) (equal pos (point)))
  12304. (not local))
  12305. org-scanner-tags
  12306. (let (tags ltags lastpos parent)
  12307. (save-excursion
  12308. (save-restriction
  12309. (widen)
  12310. (goto-char (or pos (point)))
  12311. (save-match-data
  12312. (catch 'done
  12313. (condition-case nil
  12314. (progn
  12315. (org-back-to-heading t)
  12316. (while (not (equal lastpos (point)))
  12317. (setq lastpos (point))
  12318. (when (looking-at
  12319. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12320. (setq ltags (org-split-string
  12321. (org-match-string-no-properties 1) ":"))
  12322. (when parent
  12323. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12324. (setq tags (append
  12325. (if parent
  12326. (org-remove-uninherited-tags ltags)
  12327. ltags)
  12328. tags)))
  12329. (or org-use-tag-inheritance (throw 'done t))
  12330. (if local (throw 'done t))
  12331. (or (org-up-heading-safe) (error nil))
  12332. (setq parent t)))
  12333. (error nil)))))
  12334. (if local
  12335. tags
  12336. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12337. (defun org-add-prop-inherited (s)
  12338. (add-text-properties 0 (length s) '(inherited t) s)
  12339. s)
  12340. (defun org-toggle-tag (tag &optional onoff)
  12341. "Toggle the tag TAG for the current line.
  12342. If ONOFF is `on' or `off', don't toggle but set to this state."
  12343. (let (res current)
  12344. (save-excursion
  12345. (org-back-to-heading t)
  12346. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12347. (point-at-eol) t)
  12348. (progn
  12349. (setq current (match-string 1))
  12350. (replace-match ""))
  12351. (setq current ""))
  12352. (setq current (nreverse (org-split-string current ":")))
  12353. (cond
  12354. ((eq onoff 'on)
  12355. (setq res t)
  12356. (or (member tag current) (push tag current)))
  12357. ((eq onoff 'off)
  12358. (or (not (member tag current)) (setq current (delete tag current))))
  12359. (t (if (member tag current)
  12360. (setq current (delete tag current))
  12361. (setq res t)
  12362. (push tag current))))
  12363. (end-of-line 1)
  12364. (if current
  12365. (progn
  12366. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12367. (org-set-tags nil t))
  12368. (delete-horizontal-space))
  12369. (run-hooks 'org-after-tags-change-hook))
  12370. res))
  12371. (defun org-align-tags-here (to-col)
  12372. ;; Assumes that this is a headline
  12373. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12374. (beginning-of-line 1)
  12375. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12376. (< pos (match-beginning 2)))
  12377. (progn
  12378. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12379. (goto-char (match-beginning 1))
  12380. (insert " ")
  12381. (delete-region (point) (1+ (match-beginning 2)))
  12382. (setq ncol (max (current-column)
  12383. (1+ col)
  12384. (if (> to-col 0)
  12385. to-col
  12386. (- (abs to-col) tags-l))))
  12387. (setq p (point))
  12388. (insert (make-string (- ncol (current-column)) ?\ ))
  12389. (setq ncol (current-column))
  12390. (when indent-tabs-mode (tabify p (point-at-eol)))
  12391. (org-move-to-column (min ncol col) t))
  12392. (goto-char pos))))
  12393. (defun org-set-tags-command (&optional arg just-align)
  12394. "Call the set-tags command for the current entry."
  12395. (interactive "P")
  12396. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12397. (org-set-tags arg just-align)
  12398. (save-excursion
  12399. (org-back-to-heading t)
  12400. (org-set-tags arg just-align))))
  12401. (defun org-set-tags-to (data)
  12402. "Set the tags of the current entry to DATA, replacing the current tags.
  12403. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12404. If DATA is nil or the empty string, any tags will be removed."
  12405. (interactive "sTags: ")
  12406. (setq data
  12407. (cond
  12408. ((eq data nil) "")
  12409. ((equal data "") "")
  12410. ((stringp data)
  12411. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12412. ":"))
  12413. ((listp data)
  12414. (concat ":" (mapconcat 'identity data ":") ":"))))
  12415. (when data
  12416. (save-excursion
  12417. (org-back-to-heading t)
  12418. (when (looking-at org-complex-heading-regexp)
  12419. (if (match-end 5)
  12420. (progn
  12421. (goto-char (match-beginning 5))
  12422. (insert data)
  12423. (delete-region (point) (point-at-eol))
  12424. (org-set-tags nil 'align))
  12425. (goto-char (point-at-eol))
  12426. (insert " " data)
  12427. (org-set-tags nil 'align)))
  12428. (beginning-of-line 1)
  12429. (if (looking-at ".*?\\([ \t]+\\)$")
  12430. (delete-region (match-beginning 1) (match-end 1))))))
  12431. (defun org-align-all-tags ()
  12432. "Align the tags i all headings."
  12433. (interactive)
  12434. (save-excursion
  12435. (or (ignore-errors (org-back-to-heading t))
  12436. (outline-next-heading))
  12437. (if (org-at-heading-p)
  12438. (org-set-tags t)
  12439. (message "No headings"))))
  12440. (defvar org-indent-indentation-per-level)
  12441. (defun org-set-tags (&optional arg just-align)
  12442. "Set the tags for the current headline.
  12443. With prefix ARG, realign all tags in headings in the current buffer."
  12444. (interactive "P")
  12445. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12446. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12447. 'region-start-level 'region))
  12448. org-loop-over-headlines-in-active-region)
  12449. (org-map-entries
  12450. ;; We don't use ARG and JUST-ALIGN here these args are not
  12451. ;; useful when looping over headlines
  12452. `(org-set-tags)
  12453. org-loop-over-headlines-in-active-region
  12454. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12455. (let* ((re org-outline-regexp-bol)
  12456. (current (unless arg (org-get-tags-string)))
  12457. (col (current-column))
  12458. (org-setting-tags t)
  12459. table current-tags inherited-tags ; computed below when needed
  12460. tags p0 c0 c1 rpl di tc level)
  12461. (if arg
  12462. (save-excursion
  12463. (goto-char (point-min))
  12464. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12465. (while (re-search-forward re nil t)
  12466. (org-set-tags nil t)
  12467. (end-of-line 1)))
  12468. (message "All tags realigned to column %d" org-tags-column))
  12469. (if just-align
  12470. (setq tags current)
  12471. ;; Get a new set of tags from the user
  12472. (save-excursion
  12473. (setq table (append org-tag-persistent-alist
  12474. (or org-tag-alist (org-get-buffer-tags))
  12475. (and
  12476. org-complete-tags-always-offer-all-agenda-tags
  12477. (org-global-tags-completion-table
  12478. (org-agenda-files))))
  12479. org-last-tags-completion-table table
  12480. current-tags (org-split-string current ":")
  12481. inherited-tags (nreverse
  12482. (nthcdr (length current-tags)
  12483. (nreverse (org-get-tags-at))))
  12484. tags
  12485. (if (or (eq t org-use-fast-tag-selection)
  12486. (and org-use-fast-tag-selection
  12487. (delq nil (mapcar 'cdr table))))
  12488. (org-fast-tag-selection
  12489. current-tags inherited-tags table
  12490. (if org-fast-tag-selection-include-todo
  12491. org-todo-key-alist))
  12492. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12493. (org-trim
  12494. (org-icompleting-read "Tags: "
  12495. 'org-tags-completion-function
  12496. nil nil current 'org-tags-history))))))
  12497. (while (string-match "[-+&]+" tags)
  12498. ;; No boolean logic, just a list
  12499. (setq tags (replace-match ":" t t tags))))
  12500. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12501. (if org-tags-sort-function
  12502. (setq tags (mapconcat 'identity
  12503. (sort (org-split-string
  12504. tags (org-re "[^[:alnum:]_@#%]+"))
  12505. org-tags-sort-function) ":")))
  12506. (if (string-match "\\`[\t ]*\\'" tags)
  12507. (setq tags "")
  12508. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12509. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12510. ;; Insert new tags at the correct column
  12511. (beginning-of-line 1)
  12512. (setq level (or (and (looking-at org-outline-regexp)
  12513. (- (match-end 0) (point) 1))
  12514. 1))
  12515. (cond
  12516. ((and (equal current "") (equal tags "")))
  12517. ((re-search-forward
  12518. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12519. (point-at-eol) t)
  12520. (if (equal tags "")
  12521. (setq rpl "")
  12522. (goto-char (match-beginning 0))
  12523. (setq c0 (current-column)
  12524. ;; compute offset for the case of org-indent-mode active
  12525. di (if org-indent-mode
  12526. (* (1- org-indent-indentation-per-level) (1- level))
  12527. 0)
  12528. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12529. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12530. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12531. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12532. (replace-match rpl t t)
  12533. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12534. tags)
  12535. (t (error "Tags alignment failed")))
  12536. (org-move-to-column col)
  12537. (unless just-align
  12538. (run-hooks 'org-after-tags-change-hook))))))
  12539. (defun org-change-tag-in-region (beg end tag off)
  12540. "Add or remove TAG for each entry in the region.
  12541. This works in the agenda, and also in an org-mode buffer."
  12542. (interactive
  12543. (list (region-beginning) (region-end)
  12544. (let ((org-last-tags-completion-table
  12545. (if (derived-mode-p 'org-mode)
  12546. (org-get-buffer-tags)
  12547. (org-global-tags-completion-table))))
  12548. (org-icompleting-read
  12549. "Tag: " 'org-tags-completion-function nil nil nil
  12550. 'org-tags-history))
  12551. (progn
  12552. (message "[s]et or [r]emove? ")
  12553. (equal (read-char-exclusive) ?r))))
  12554. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12555. (let ((agendap (equal major-mode 'org-agenda-mode))
  12556. l1 l2 m buf pos newhead (cnt 0))
  12557. (goto-char end)
  12558. (setq l2 (1- (org-current-line)))
  12559. (goto-char beg)
  12560. (setq l1 (org-current-line))
  12561. (loop for l from l1 to l2 do
  12562. (org-goto-line l)
  12563. (setq m (get-text-property (point) 'org-hd-marker))
  12564. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12565. (and agendap m))
  12566. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12567. pos (if agendap m (point)))
  12568. (with-current-buffer buf
  12569. (save-excursion
  12570. (save-restriction
  12571. (goto-char pos)
  12572. (setq cnt (1+ cnt))
  12573. (org-toggle-tag tag (if off 'off 'on))
  12574. (setq newhead (org-get-heading)))))
  12575. (and agendap (org-agenda-change-all-lines newhead m))))
  12576. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12577. (defun org-tags-completion-function (string predicate &optional flag)
  12578. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12579. (confirm (lambda (x) (stringp (car x)))))
  12580. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12581. (setq s1 (match-string 1 string)
  12582. s2 (match-string 2 string))
  12583. (setq s1 "" s2 string))
  12584. (cond
  12585. ((eq flag nil)
  12586. ;; try completion
  12587. (setq rtn (try-completion s2 ctable confirm))
  12588. (if (stringp rtn)
  12589. (setq rtn
  12590. (concat s1 s2 (substring rtn (length s2))
  12591. (if (and org-add-colon-after-tag-completion
  12592. (assoc rtn ctable))
  12593. ":" ""))))
  12594. rtn)
  12595. ((eq flag t)
  12596. ;; all-completions
  12597. (all-completions s2 ctable confirm)
  12598. )
  12599. ((eq flag 'lambda)
  12600. ;; exact match?
  12601. (assoc s2 ctable)))
  12602. ))
  12603. (defun org-fast-tag-insert (kwd tags face &optional end)
  12604. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12605. (insert (format "%-12s" (concat kwd ":"))
  12606. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12607. (or end "")))
  12608. (defun org-fast-tag-show-exit (flag)
  12609. (save-excursion
  12610. (org-goto-line 3)
  12611. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12612. (replace-match ""))
  12613. (when flag
  12614. (end-of-line 1)
  12615. (org-move-to-column (- (window-width) 19) t)
  12616. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12617. (defun org-set-current-tags-overlay (current prefix)
  12618. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12619. (if (featurep 'xemacs)
  12620. (org-overlay-display org-tags-overlay (concat prefix s)
  12621. 'secondary-selection)
  12622. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12623. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12624. (defvar org-last-tag-selection-key nil)
  12625. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12626. "Fast tag selection with single keys.
  12627. CURRENT is the current list of tags in the headline, INHERITED is the
  12628. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12629. possibly with grouping information. TODO-TABLE is a similar table with
  12630. TODO keywords, should these have keys assigned to them.
  12631. If the keys are nil, a-z are automatically assigned.
  12632. Returns the new tags string, or nil to not change the current settings."
  12633. (let* ((fulltable (append table todo-table))
  12634. (maxlen (apply 'max (mapcar
  12635. (lambda (x)
  12636. (if (stringp (car x)) (string-width (car x)) 0))
  12637. fulltable)))
  12638. (buf (current-buffer))
  12639. (expert (eq org-fast-tag-selection-single-key 'expert))
  12640. (buffer-tags nil)
  12641. (fwidth (+ maxlen 3 1 3))
  12642. (ncol (/ (- (window-width) 4) fwidth))
  12643. (i-face 'org-done)
  12644. (c-face 'org-todo)
  12645. tg cnt e c char c1 c2 ntable tbl rtn
  12646. ov-start ov-end ov-prefix
  12647. (exit-after-next org-fast-tag-selection-single-key)
  12648. (done-keywords org-done-keywords)
  12649. groups ingroup)
  12650. (save-excursion
  12651. (beginning-of-line 1)
  12652. (if (looking-at
  12653. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12654. (setq ov-start (match-beginning 1)
  12655. ov-end (match-end 1)
  12656. ov-prefix "")
  12657. (setq ov-start (1- (point-at-eol))
  12658. ov-end (1+ ov-start))
  12659. (skip-chars-forward "^\n\r")
  12660. (setq ov-prefix
  12661. (concat
  12662. (buffer-substring (1- (point)) (point))
  12663. (if (> (current-column) org-tags-column)
  12664. " "
  12665. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12666. (move-overlay org-tags-overlay ov-start ov-end)
  12667. (save-window-excursion
  12668. (if expert
  12669. (set-buffer (get-buffer-create " *Org tags*"))
  12670. (delete-other-windows)
  12671. (split-window-vertically)
  12672. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12673. (erase-buffer)
  12674. (org-set-local 'org-done-keywords done-keywords)
  12675. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12676. (org-fast-tag-insert "Current" current c-face "\n\n")
  12677. (org-fast-tag-show-exit exit-after-next)
  12678. (org-set-current-tags-overlay current ov-prefix)
  12679. (setq tbl fulltable char ?a cnt 0)
  12680. (while (setq e (pop tbl))
  12681. (cond
  12682. ((equal (car e) :startgroup)
  12683. (push '() groups) (setq ingroup t)
  12684. (when (not (= cnt 0))
  12685. (setq cnt 0)
  12686. (insert "\n"))
  12687. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12688. ((equal (car e) :endgroup)
  12689. (setq ingroup nil cnt 0)
  12690. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12691. ((equal e '(:newline))
  12692. (when (not (= cnt 0))
  12693. (setq cnt 0)
  12694. (insert "\n")
  12695. (setq e (car tbl))
  12696. (while (equal (car tbl) '(:newline))
  12697. (insert "\n")
  12698. (setq tbl (cdr tbl)))))
  12699. (t
  12700. (setq tg (copy-sequence (car e)) c2 nil)
  12701. (if (cdr e)
  12702. (setq c (cdr e))
  12703. ;; automatically assign a character.
  12704. (setq c1 (string-to-char
  12705. (downcase (substring
  12706. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12707. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12708. (while (or (rassoc char ntable) (rassoc char table))
  12709. (setq char (1+ char)))
  12710. (setq c2 c1))
  12711. (setq c (or c2 char)))
  12712. (if ingroup (push tg (car groups)))
  12713. (setq tg (org-add-props tg nil 'face
  12714. (cond
  12715. ((not (assoc tg table))
  12716. (org-get-todo-face tg))
  12717. ((member tg current) c-face)
  12718. ((member tg inherited) i-face))))
  12719. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12720. (insert "[" c "] " tg (make-string
  12721. (- fwidth 4 (length tg)) ?\ ))
  12722. (push (cons tg c) ntable)
  12723. (when (= (setq cnt (1+ cnt)) ncol)
  12724. (insert "\n")
  12725. (if ingroup (insert " "))
  12726. (setq cnt 0)))))
  12727. (setq ntable (nreverse ntable))
  12728. (insert "\n")
  12729. (goto-char (point-min))
  12730. (if (not expert) (org-fit-window-to-buffer))
  12731. (setq rtn
  12732. (catch 'exit
  12733. (while t
  12734. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12735. (if (not groups) "no " "")
  12736. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12737. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12738. (setq org-last-tag-selection-key c)
  12739. (cond
  12740. ((= c ?\r) (throw 'exit t))
  12741. ((= c ?!)
  12742. (setq groups (not groups))
  12743. (goto-char (point-min))
  12744. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12745. ((= c ?\C-c)
  12746. (if (not expert)
  12747. (org-fast-tag-show-exit
  12748. (setq exit-after-next (not exit-after-next)))
  12749. (setq expert nil)
  12750. (delete-other-windows)
  12751. (set-window-buffer (split-window-vertically) " *Org tags*")
  12752. (org-switch-to-buffer-other-window " *Org tags*")
  12753. (org-fit-window-to-buffer)))
  12754. ((or (= c ?\C-g)
  12755. (and (= c ?q) (not (rassoc c ntable))))
  12756. (org-detach-overlay org-tags-overlay)
  12757. (setq quit-flag t))
  12758. ((= c ?\ )
  12759. (setq current nil)
  12760. (if exit-after-next (setq exit-after-next 'now)))
  12761. ((= c ?\t)
  12762. (condition-case nil
  12763. (setq tg (org-icompleting-read
  12764. "Tag: "
  12765. (or buffer-tags
  12766. (with-current-buffer buf
  12767. (org-get-buffer-tags)))))
  12768. (quit (setq tg "")))
  12769. (when (string-match "\\S-" tg)
  12770. (add-to-list 'buffer-tags (list tg))
  12771. (if (member tg current)
  12772. (setq current (delete tg current))
  12773. (push tg current)))
  12774. (if exit-after-next (setq exit-after-next 'now)))
  12775. ((setq e (rassoc c todo-table) tg (car e))
  12776. (with-current-buffer buf
  12777. (save-excursion (org-todo tg)))
  12778. (if exit-after-next (setq exit-after-next 'now)))
  12779. ((setq e (rassoc c ntable) tg (car e))
  12780. (if (member tg current)
  12781. (setq current (delete tg current))
  12782. (loop for g in groups do
  12783. (if (member tg g)
  12784. (mapc (lambda (x)
  12785. (setq current (delete x current)))
  12786. g)))
  12787. (push tg current))
  12788. (if exit-after-next (setq exit-after-next 'now))))
  12789. ;; Create a sorted list
  12790. (setq current
  12791. (sort current
  12792. (lambda (a b)
  12793. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12794. (if (eq exit-after-next 'now) (throw 'exit t))
  12795. (goto-char (point-min))
  12796. (beginning-of-line 2)
  12797. (delete-region (point) (point-at-eol))
  12798. (org-fast-tag-insert "Current" current c-face)
  12799. (org-set-current-tags-overlay current ov-prefix)
  12800. (while (re-search-forward
  12801. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12802. (setq tg (match-string 1))
  12803. (add-text-properties
  12804. (match-beginning 1) (match-end 1)
  12805. (list 'face
  12806. (cond
  12807. ((member tg current) c-face)
  12808. ((member tg inherited) i-face)
  12809. (t (get-text-property (match-beginning 1) 'face))))))
  12810. (goto-char (point-min)))))
  12811. (org-detach-overlay org-tags-overlay)
  12812. (if rtn
  12813. (mapconcat 'identity current ":")
  12814. nil))))
  12815. (defun org-get-tags-string ()
  12816. "Get the TAGS string in the current headline."
  12817. (unless (org-at-heading-p t)
  12818. (error "Not on a heading"))
  12819. (save-excursion
  12820. (beginning-of-line 1)
  12821. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12822. (org-match-string-no-properties 1)
  12823. "")))
  12824. (defun org-get-tags ()
  12825. "Get the list of tags specified in the current headline."
  12826. (org-split-string (org-get-tags-string) ":"))
  12827. (defun org-get-buffer-tags ()
  12828. "Get a table of all tags used in the buffer, for completion."
  12829. (let (tags)
  12830. (save-excursion
  12831. (goto-char (point-min))
  12832. (while (re-search-forward
  12833. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12834. (when (equal (char-after (point-at-bol 0)) ?*)
  12835. (mapc (lambda (x) (add-to-list 'tags x))
  12836. (org-split-string (org-match-string-no-properties 1) ":")))))
  12837. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12838. (mapcar 'list tags)))
  12839. ;;;; The mapping API
  12840. (defun org-map-entries (func &optional match scope &rest skip)
  12841. "Call FUNC at each headline selected by MATCH in SCOPE.
  12842. FUNC is a function or a lisp form. The function will be called without
  12843. arguments, with the cursor positioned at the beginning of the headline.
  12844. The return values of all calls to the function will be collected and
  12845. returned as a list.
  12846. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12847. does not need to preserve point. After evaluation, the cursor will be
  12848. moved to the end of the line (presumably of the headline of the
  12849. processed entry) and search continues from there. Under some
  12850. circumstances, this may not produce the wanted results. For example,
  12851. if you have removed (e.g. archived) the current (sub)tree it could
  12852. mean that the next entry will be skipped entirely. In such cases, you
  12853. can specify the position from where search should continue by making
  12854. FUNC set the variable `org-map-continue-from' to the desired buffer
  12855. position.
  12856. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12857. Only headlines that are matched by this query will be considered during
  12858. the iteration. When MATCH is nil or t, all headlines will be
  12859. visited by the iteration.
  12860. SCOPE determines the scope of this command. It can be any of:
  12861. nil The current buffer, respecting the restriction if any
  12862. tree The subtree started with the entry at point
  12863. region The entries within the active region, if any
  12864. region-start-level
  12865. The entries within the active region, but only those at
  12866. the same level than the first one.
  12867. file The current buffer, without restriction
  12868. file-with-archives
  12869. The current buffer, and any archives associated with it
  12870. agenda All agenda files
  12871. agenda-with-archives
  12872. All agenda files with any archive files associated with them
  12873. \(file1 file2 ...)
  12874. If this is a list, all files in the list will be scanned
  12875. The remaining args are treated as settings for the skipping facilities of
  12876. the scanner. The following items can be given here:
  12877. archive skip trees with the archive tag.
  12878. comment skip trees with the COMMENT keyword
  12879. function or Emacs Lisp form:
  12880. will be used as value for `org-agenda-skip-function', so whenever
  12881. the function returns t, FUNC will not be called for that
  12882. entry and search will continue from the point where the
  12883. function leaves it.
  12884. If your function needs to retrieve the tags including inherited tags
  12885. at the *current* entry, you can use the value of the variable
  12886. `org-scanner-tags' which will be much faster than getting the value
  12887. with `org-get-tags-at'. If your function gets properties with
  12888. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12889. to t around the call to `org-entry-properties' to get the same speedup.
  12890. Note that if your function moves around to retrieve tags and properties at
  12891. a *different* entry, you cannot use these techniques."
  12892. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12893. (not (org-region-active-p)))
  12894. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12895. (org-agenda-skip-archived-trees (memq 'archive skip))
  12896. (org-agenda-skip-comment-trees (memq 'comment skip))
  12897. (org-agenda-skip-function
  12898. (car (org-delete-all '(comment archive) skip)))
  12899. (org-tags-match-list-sublevels t)
  12900. (start-level (eq scope 'region-start-level))
  12901. matcher file res
  12902. org-todo-keywords-for-agenda
  12903. org-done-keywords-for-agenda
  12904. org-todo-keyword-alist-for-agenda
  12905. org-drawers-for-agenda
  12906. org-tag-alist-for-agenda
  12907. todo-only)
  12908. (cond
  12909. ((eq match t) (setq matcher t))
  12910. ((eq match nil) (setq matcher t))
  12911. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12912. (save-excursion
  12913. (save-restriction
  12914. (cond ((eq scope 'tree)
  12915. (org-back-to-heading t)
  12916. (org-narrow-to-subtree)
  12917. (setq scope nil))
  12918. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12919. (org-region-active-p))
  12920. ;; If needed, set start-level to a string like "2"
  12921. (when start-level
  12922. (save-excursion
  12923. (goto-char (region-beginning))
  12924. (unless (org-at-heading-p) (outline-next-heading))
  12925. (setq start-level (org-current-level))))
  12926. (narrow-to-region (region-beginning)
  12927. (save-excursion
  12928. (goto-char (region-end))
  12929. (unless (and (bolp) (org-at-heading-p))
  12930. (outline-next-heading))
  12931. (point)))
  12932. (setq scope nil)))
  12933. (if (not scope)
  12934. (progn
  12935. (org-agenda-prepare-buffers
  12936. (list (buffer-file-name (current-buffer))))
  12937. (setq res (org-scan-tags func matcher todo-only start-level)))
  12938. ;; Get the right scope
  12939. (cond
  12940. ((and scope (listp scope) (symbolp (car scope)))
  12941. (setq scope (eval scope)))
  12942. ((eq scope 'agenda)
  12943. (setq scope (org-agenda-files t)))
  12944. ((eq scope 'agenda-with-archives)
  12945. (setq scope (org-agenda-files t))
  12946. (setq scope (org-add-archive-files scope)))
  12947. ((eq scope 'file)
  12948. (setq scope (list (buffer-file-name))))
  12949. ((eq scope 'file-with-archives)
  12950. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12951. (org-agenda-prepare-buffers scope)
  12952. (while (setq file (pop scope))
  12953. (with-current-buffer (org-find-base-buffer-visiting file)
  12954. (save-excursion
  12955. (save-restriction
  12956. (widen)
  12957. (goto-char (point-min))
  12958. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12959. res)))
  12960. ;;;; Properties
  12961. ;;; Setting and retrieving properties
  12962. (defconst org-special-properties
  12963. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12964. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  12965. "The special properties valid in Org-mode.
  12966. These are properties that are not defined in the property drawer,
  12967. but in some other way.")
  12968. (defconst org-default-properties
  12969. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12970. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12971. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12972. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12973. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12974. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12975. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12976. "Some properties that are used by Org-mode for various purposes.
  12977. Being in this list makes sure that they are offered for completion.")
  12978. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12979. "Regular expression matching the first line of a property drawer.")
  12980. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12981. "Regular expression matching the last line of a property drawer.")
  12982. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12983. "Regular expression matching the first line of a property drawer.")
  12984. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12985. "Regular expression matching the first line of a property drawer.")
  12986. (defconst org-property-drawer-re
  12987. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12988. org-property-end-re "\\)\n?")
  12989. "Matches an entire property drawer.")
  12990. (defconst org-clock-drawer-re
  12991. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12992. org-property-end-re "\\)\n?")
  12993. "Matches an entire clock drawer.")
  12994. (defsubst org-re-property (property)
  12995. "Return a regexp matching a PROPERTY line.
  12996. Match group 1 will be set to the value."
  12997. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12998. (defsubst org-re-property-keyword (property)
  12999. "Return a regexp matching a PROPERTY line, possibly with no
  13000. value for the property."
  13001. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13002. (defun org-property-action ()
  13003. "Do an action on properties."
  13004. (interactive)
  13005. (let (c)
  13006. (org-at-property-p)
  13007. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13008. (setq c (read-char-exclusive))
  13009. (cond
  13010. ((equal c ?s)
  13011. (call-interactively 'org-set-property))
  13012. ((equal c ?d)
  13013. (call-interactively 'org-delete-property))
  13014. ((equal c ?D)
  13015. (call-interactively 'org-delete-property-globally))
  13016. ((equal c ?c)
  13017. (call-interactively 'org-compute-property-at-point))
  13018. (t (error "No such property action %c" c)))))
  13019. (defun org-inc-effort ()
  13020. "Increment the value of the effort property in the current entry."
  13021. (interactive)
  13022. (org-set-effort nil t))
  13023. (defun org-set-effort (&optional value increment)
  13024. "Set the effort property of the current entry.
  13025. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13026. 10th allowed value.
  13027. When INCREMENT is non-nil, set the property to the next allowed value."
  13028. (interactive "P")
  13029. (if (equal value 0) (setq value 10))
  13030. (let* ((completion-ignore-case t)
  13031. (prop org-effort-property)
  13032. (cur (org-entry-get nil prop))
  13033. (allowed (org-property-get-allowed-values nil prop 'table))
  13034. (existing (mapcar 'list (org-property-values prop)))
  13035. rpl
  13036. (val (cond
  13037. ((stringp value) value)
  13038. ((and allowed (integerp value))
  13039. (or (car (nth (1- value) allowed))
  13040. (car (org-last allowed))))
  13041. ((and allowed increment)
  13042. (or (caadr (member (list cur) allowed))
  13043. (error "Allowed effort values are not set")))
  13044. (allowed
  13045. (message "Select 1-9,0, [RET%s]: %s"
  13046. (if cur (concat "=" cur) "")
  13047. (mapconcat 'car allowed " "))
  13048. (setq rpl (read-char-exclusive))
  13049. (if (equal rpl ?\r)
  13050. cur
  13051. (setq rpl (- rpl ?0))
  13052. (if (equal rpl 0) (setq rpl 10))
  13053. (if (and (> rpl 0) (<= rpl (length allowed)))
  13054. (car (nth (1- rpl) allowed))
  13055. (org-completing-read "Effort: " allowed nil))))
  13056. (t
  13057. (let (org-completion-use-ido org-completion-use-iswitchb)
  13058. (org-completing-read
  13059. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13060. (concat "[" cur "]") "")
  13061. ": ")
  13062. existing nil nil "" nil cur))))))
  13063. (unless (equal (org-entry-get nil prop) val)
  13064. (org-entry-put nil prop val))
  13065. (message "%s is now %s" prop val)))
  13066. (defun org-at-property-p ()
  13067. "Is cursor inside a property drawer?"
  13068. (save-excursion
  13069. (beginning-of-line 1)
  13070. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13071. (save-match-data ;; Used by calling procedures
  13072. (let ((p (point))
  13073. (range (unless (org-before-first-heading-p)
  13074. (org-get-property-block))))
  13075. (and range (<= (car range) p) (< p (cdr range))))))))
  13076. (defun org-get-property-block (&optional beg end force)
  13077. "Return the (beg . end) range of the body of the property drawer.
  13078. BEG and END are the beginning and end of the current subtree, or of
  13079. the part before the first headline. If they are not given, they will
  13080. be found. If the drawer does not exist and FORCE is non-nil, create
  13081. the drawer."
  13082. (catch 'exit
  13083. (save-excursion
  13084. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13085. (progn (org-back-to-heading t) (point))))
  13086. (end (or end (and (not (outline-next-heading)) (point-max))
  13087. (point))))
  13088. (goto-char beg)
  13089. (if (re-search-forward org-property-start-re end t)
  13090. (setq beg (1+ (match-end 0)))
  13091. (if force
  13092. (save-excursion
  13093. (org-insert-property-drawer)
  13094. (setq end (progn (outline-next-heading) (point))))
  13095. (throw 'exit nil))
  13096. (goto-char beg)
  13097. (if (re-search-forward org-property-start-re end t)
  13098. (setq beg (1+ (match-end 0)))))
  13099. (if (re-search-forward org-property-end-re end t)
  13100. (setq end (match-beginning 0))
  13101. (or force (throw 'exit nil))
  13102. (goto-char beg)
  13103. (setq end beg)
  13104. (org-indent-line)
  13105. (insert ":END:\n"))
  13106. (cons beg end)))))
  13107. (defun org-entry-properties (&optional pom which specific)
  13108. "Get all properties of the entry at point-or-marker POM.
  13109. This includes the TODO keyword, the tags, time strings for deadline,
  13110. scheduled, and clocking, and any additional properties defined in the
  13111. entry. The return value is an alist, keys may occur multiple times
  13112. if the property key was used several times.
  13113. POM may also be nil, in which case the current entry is used.
  13114. If WHICH is nil or `all', get all properties. If WHICH is
  13115. `special' or `standard', only get that subclass. If WHICH
  13116. is a string only get exactly this property. SPECIFIC can be a string, the
  13117. specific property we are interested in. Specifying it can speed
  13118. things up because then unnecessary parsing is avoided."
  13119. (setq which (or which 'all))
  13120. (org-with-point-at pom
  13121. (let ((clockstr (substring org-clock-string 0 -1))
  13122. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13123. (case-fold-search nil)
  13124. beg end range props sum-props key key1 value string clocksum clocksumt)
  13125. (save-excursion
  13126. (when (condition-case nil
  13127. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13128. (error nil))
  13129. (setq beg (point))
  13130. (setq sum-props (get-text-property (point) 'org-summaries))
  13131. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13132. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13133. (outline-next-heading)
  13134. (setq end (point))
  13135. (when (memq which '(all special))
  13136. ;; Get the special properties, like TODO and tags
  13137. (goto-char beg)
  13138. (when (and (or (not specific) (string= specific "TODO"))
  13139. (looking-at org-todo-line-regexp) (match-end 2))
  13140. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13141. (when (and (or (not specific) (string= specific "PRIORITY"))
  13142. (looking-at org-priority-regexp))
  13143. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13144. (when (or (not specific) (string= specific "FILE"))
  13145. (push (cons "FILE" buffer-file-name) props))
  13146. (when (and (or (not specific) (string= specific "TAGS"))
  13147. (setq value (org-get-tags-string))
  13148. (string-match "\\S-" value))
  13149. (push (cons "TAGS" value) props))
  13150. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13151. (setq value (org-get-tags-at)))
  13152. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13153. ":"))
  13154. props))
  13155. (when (or (not specific) (string= specific "BLOCKED"))
  13156. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13157. (when (or (not specific)
  13158. (member specific
  13159. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13160. "TIMESTAMP" "TIMESTAMP_IA")))
  13161. (catch 'match
  13162. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13163. (setq key (if (match-end 1)
  13164. (substring (org-match-string-no-properties 1)
  13165. 0 -1))
  13166. string (if (equal key clockstr)
  13167. (org-trim
  13168. (buffer-substring-no-properties
  13169. (match-beginning 3) (goto-char
  13170. (point-at-eol))))
  13171. (substring (org-match-string-no-properties 3)
  13172. 1 -1)))
  13173. ;; Get the correct property name from the key. This is
  13174. ;; necessary if the user has configured time keywords.
  13175. (setq key1 (concat key ":"))
  13176. (cond
  13177. ((not key)
  13178. (setq key
  13179. (if (= (char-after (match-beginning 3)) ?\[)
  13180. "TIMESTAMP_IA" "TIMESTAMP")))
  13181. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13182. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13183. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13184. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13185. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13186. (progn
  13187. (push (cons key string) props)
  13188. ;; no need to search further if match is found
  13189. (throw 'match t))
  13190. (when (or (equal key "CLOCK") (not (assoc key props)))
  13191. (push (cons key string) props)))))))
  13192. (when (memq which '(all standard))
  13193. ;; Get the standard properties, like :PROP: ...
  13194. (setq range (org-get-property-block beg end))
  13195. (when range
  13196. (goto-char (car range))
  13197. (while (re-search-forward
  13198. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13199. (cdr range) t)
  13200. (setq key (org-match-string-no-properties 1)
  13201. value (org-trim (or (org-match-string-no-properties 2) "")))
  13202. (unless (member key excluded)
  13203. (push (cons key (or value "")) props)))))
  13204. (if clocksum
  13205. (push (cons "CLOCKSUM"
  13206. (org-columns-number-to-string (/ (float clocksum) 60.)
  13207. 'add_times))
  13208. props))
  13209. (if clocksumt
  13210. (push (cons "CLOCKSUM_T"
  13211. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13212. 'add_times))
  13213. props))
  13214. (unless (assoc "CATEGORY" props)
  13215. (push (cons "CATEGORY" (org-get-category)) props))
  13216. (append sum-props (nreverse props)))))))
  13217. (defun org-entry-get (pom property &optional inherit literal-nil)
  13218. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13219. If INHERIT is non-nil and the entry does not have the property,
  13220. then also check higher levels of the hierarchy.
  13221. If INHERIT is the symbol `selective', use inheritance only if the setting
  13222. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13223. If the property is present but empty, the return value is the empty string.
  13224. If the property is not present at all, nil is returned.
  13225. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13226. do not interpret it as the list atom nil. This is used for inheritance
  13227. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13228. (org-with-point-at pom
  13229. (if (and inherit (if (eq inherit 'selective)
  13230. (org-property-inherit-p property)
  13231. t))
  13232. (org-entry-get-with-inheritance property literal-nil)
  13233. (if (member property org-special-properties)
  13234. ;; We need a special property. Use `org-entry-properties' to
  13235. ;; retrieve it, but specify the wanted property
  13236. (cdr (assoc property (org-entry-properties nil 'special property)))
  13237. (let* ((range (org-get-property-block))
  13238. (props (list (or (assoc property org-file-properties)
  13239. (assoc property org-global-properties)
  13240. (assoc property org-global-properties-fixed))))
  13241. (ap (lambda (key)
  13242. (when (re-search-forward
  13243. (org-re-property key) (cdr range) t)
  13244. (setq props
  13245. (org-update-property-plist
  13246. key
  13247. (if (match-end 1)
  13248. (org-match-string-no-properties 1) "")
  13249. props)))))
  13250. val)
  13251. (when (and range (goto-char (car range)))
  13252. (funcall ap property)
  13253. (goto-char (car range))
  13254. (while (funcall ap (concat property "+")))
  13255. (setq val (cdr (assoc property props)))
  13256. (when val (if literal-nil val (org-not-nil val)))))))))
  13257. (defun org-property-or-variable-value (var &optional inherit)
  13258. "Check if there is a property fixing the value of VAR.
  13259. If yes, return this value. If not, return the current value of the variable."
  13260. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13261. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13262. (read prop)
  13263. (symbol-value var))))
  13264. (defun org-entry-delete (pom property)
  13265. "Delete the property PROPERTY from entry at point-or-marker POM."
  13266. (org-with-point-at pom
  13267. (if (member property org-special-properties)
  13268. nil ; cannot delete these properties.
  13269. (let ((range (org-get-property-block)))
  13270. (if (and range
  13271. (goto-char (car range))
  13272. (re-search-forward
  13273. (org-re-property property)
  13274. (cdr range) t))
  13275. (progn
  13276. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13277. t)
  13278. nil)))))
  13279. ;; Multi-values properties are properties that contain multiple values
  13280. ;; These values are assumed to be single words, separated by whitespace.
  13281. (defun org-entry-add-to-multivalued-property (pom property value)
  13282. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13283. (let* ((old (org-entry-get pom property))
  13284. (values (and old (org-split-string old "[ \t]"))))
  13285. (setq value (org-entry-protect-space value))
  13286. (unless (member value values)
  13287. (setq values (cons value values))
  13288. (org-entry-put pom property
  13289. (mapconcat 'identity values " ")))))
  13290. (defun org-entry-remove-from-multivalued-property (pom property value)
  13291. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13292. (let* ((old (org-entry-get pom property))
  13293. (values (and old (org-split-string old "[ \t]"))))
  13294. (setq value (org-entry-protect-space value))
  13295. (when (member value values)
  13296. (setq values (delete value values))
  13297. (org-entry-put pom property
  13298. (mapconcat 'identity values " ")))))
  13299. (defun org-entry-member-in-multivalued-property (pom property value)
  13300. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13301. (let* ((old (org-entry-get pom property))
  13302. (values (and old (org-split-string old "[ \t]"))))
  13303. (setq value (org-entry-protect-space value))
  13304. (member value values)))
  13305. (defun org-entry-get-multivalued-property (pom property)
  13306. "Return a list of values in a multivalued property."
  13307. (let* ((value (org-entry-get pom property))
  13308. (values (and value (org-split-string value "[ \t]"))))
  13309. (mapcar 'org-entry-restore-space values)))
  13310. (defun org-entry-put-multivalued-property (pom property &rest values)
  13311. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13312. VALUES should be a list of strings. Spaces will be protected."
  13313. (org-entry-put pom property
  13314. (mapconcat 'org-entry-protect-space values " "))
  13315. (let* ((value (org-entry-get pom property))
  13316. (values (and value (org-split-string value "[ \t]"))))
  13317. (mapcar 'org-entry-restore-space values)))
  13318. (defun org-entry-protect-space (s)
  13319. "Protect spaces and newline in string S."
  13320. (while (string-match " " s)
  13321. (setq s (replace-match "%20" t t s)))
  13322. (while (string-match "\n" s)
  13323. (setq s (replace-match "%0A" t t s)))
  13324. s)
  13325. (defun org-entry-restore-space (s)
  13326. "Restore spaces and newline in string S."
  13327. (while (string-match "%20" s)
  13328. (setq s (replace-match " " t t s)))
  13329. (while (string-match "%0A" s)
  13330. (setq s (replace-match "\n" t t s)))
  13331. s)
  13332. (defvar org-entry-property-inherited-from (make-marker)
  13333. "Marker pointing to the entry from where a property was inherited.
  13334. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13335. location of the entry where the inheritance search matched. If there was
  13336. no match, the marker will point nowhere.
  13337. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13338. is set.")
  13339. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13340. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13341. The search will stop at the first ancestor which has the property defined.
  13342. If the value found is \"nil\", return nil to show that the property
  13343. should be considered as undefined (this is the meaning of nil here).
  13344. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13345. (move-marker org-entry-property-inherited-from nil)
  13346. (let (tmp)
  13347. (save-excursion
  13348. (save-restriction
  13349. (widen)
  13350. (catch 'ex
  13351. (while t
  13352. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13353. (or (ignore-errors (org-back-to-heading t))
  13354. (goto-char (point-min)))
  13355. (move-marker org-entry-property-inherited-from (point))
  13356. (throw 'ex tmp))
  13357. (or (ignore-errors (org-up-heading-safe))
  13358. (throw 'ex nil))))))
  13359. (setq tmp (or tmp
  13360. (cdr (assoc property org-file-properties))
  13361. (cdr (assoc property org-global-properties))
  13362. (cdr (assoc property org-global-properties-fixed))))
  13363. (if literal-nil tmp (org-not-nil tmp))))
  13364. (defvar org-property-changed-functions nil
  13365. "Hook called when the value of a property has changed.
  13366. Each hook function should accept two arguments, the name of the property
  13367. and the new value.")
  13368. (defun org-entry-put (pom property value)
  13369. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13370. (org-with-point-at pom
  13371. (org-back-to-heading t)
  13372. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13373. range)
  13374. (cond
  13375. ((equal property "TODO")
  13376. (when (and (stringp value) (string-match "\\S-" value)
  13377. (not (member value org-todo-keywords-1)))
  13378. (error "\"%s\" is not a valid TODO state" value))
  13379. (if (or (not value)
  13380. (not (string-match "\\S-" value)))
  13381. (setq value 'none))
  13382. (org-todo value)
  13383. (org-set-tags nil 'align))
  13384. ((equal property "PRIORITY")
  13385. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13386. (string-to-char value) ?\ ))
  13387. (org-set-tags nil 'align))
  13388. ((equal property "SCHEDULED")
  13389. (if (re-search-forward org-scheduled-time-regexp end t)
  13390. (cond
  13391. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13392. ((eq value 'later) (org-timestamp-change 1 'day))
  13393. (t (call-interactively 'org-schedule)))
  13394. (call-interactively 'org-schedule)))
  13395. ((equal property "DEADLINE")
  13396. (if (re-search-forward org-deadline-time-regexp end t)
  13397. (cond
  13398. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13399. ((eq value 'later) (org-timestamp-change 1 'day))
  13400. (t (call-interactively 'org-deadline)))
  13401. (call-interactively 'org-deadline)))
  13402. ((member property org-special-properties)
  13403. (error "The %s property can not yet be set with `org-entry-put'"
  13404. property))
  13405. (t ; a non-special property
  13406. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13407. (setq range (org-get-property-block beg end 'force))
  13408. (goto-char (car range))
  13409. (if (re-search-forward
  13410. (org-re-property-keyword property) (cdr range) t)
  13411. (progn
  13412. (delete-region (match-beginning 0) (match-end 0))
  13413. (goto-char (match-beginning 0)))
  13414. (goto-char (cdr range))
  13415. (insert "\n")
  13416. (backward-char 1)
  13417. (org-indent-line))
  13418. (insert ":" property ":")
  13419. (and value (insert " " value))
  13420. (org-indent-line)))))
  13421. (run-hook-with-args 'org-property-changed-functions property value)))
  13422. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13423. "Get all property keys in the current buffer.
  13424. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13425. like tags and TODO state.
  13426. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13427. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13428. and others.
  13429. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13430. formats in the current buffer."
  13431. (let (rtn range cfmt s p)
  13432. (save-excursion
  13433. (save-restriction
  13434. (widen)
  13435. (goto-char (point-min))
  13436. (while (re-search-forward org-property-start-re nil t)
  13437. (setq range (org-get-property-block))
  13438. (goto-char (car range))
  13439. (while (re-search-forward
  13440. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13441. (cdr range) t)
  13442. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13443. (outline-next-heading))))
  13444. (when include-specials
  13445. (setq rtn (append org-special-properties rtn)))
  13446. (when include-defaults
  13447. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13448. (add-to-list 'rtn org-effort-property))
  13449. (when include-columns
  13450. (save-excursion
  13451. (save-restriction
  13452. (widen)
  13453. (goto-char (point-min))
  13454. (while (re-search-forward
  13455. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13456. nil t)
  13457. (setq cfmt (match-string 2) s 0)
  13458. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13459. cfmt s)
  13460. (setq s (match-end 0)
  13461. p (match-string 1 cfmt))
  13462. (unless (or (equal p "ITEM")
  13463. (member p org-special-properties))
  13464. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13465. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13466. (defun org-property-values (key)
  13467. "Return a list of all values of property KEY in the current buffer."
  13468. (save-excursion
  13469. (save-restriction
  13470. (widen)
  13471. (goto-char (point-min))
  13472. (let ((re (org-re-property key))
  13473. values)
  13474. (while (re-search-forward re nil t)
  13475. (add-to-list 'values (org-trim (match-string 1))))
  13476. (delete "" values)))))
  13477. (defun org-insert-property-drawer ()
  13478. "Insert a property drawer into the current entry."
  13479. (org-back-to-heading t)
  13480. (looking-at org-outline-regexp)
  13481. (let ((indent (if org-adapt-indentation
  13482. (- (match-end 0) (match-beginning 0))
  13483. 0))
  13484. (beg (point))
  13485. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13486. end hiddenp)
  13487. (outline-next-heading)
  13488. (setq end (point))
  13489. (goto-char beg)
  13490. (while (re-search-forward re end t))
  13491. (setq hiddenp (outline-invisible-p))
  13492. (end-of-line 1)
  13493. (and (equal (char-after) ?\n) (forward-char 1))
  13494. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13495. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13496. ;; just skip this line
  13497. (beginning-of-line 2)
  13498. ;; Drawer start, find the end
  13499. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13500. (beginning-of-line 1)))
  13501. (org-skip-over-state-notes)
  13502. (skip-chars-backward " \t\n\r")
  13503. (if (eq (char-before) ?*) (forward-char 1))
  13504. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13505. (beginning-of-line 0)
  13506. (org-indent-to-column indent)
  13507. (beginning-of-line 2)
  13508. (org-indent-to-column indent)
  13509. (beginning-of-line 0)
  13510. (if hiddenp
  13511. (save-excursion
  13512. (org-back-to-heading t)
  13513. (hide-entry))
  13514. (org-flag-drawer t))))
  13515. (defun org-insert-drawer (&optional arg drawer)
  13516. "Insert a drawer at point.
  13517. Optional argument DRAWER, when non-nil, is a string representing
  13518. drawer's name. Otherwise, the user is prompted for a name.
  13519. If a region is active, insert the drawer around that region
  13520. instead.
  13521. Point is left between drawer's boundaries."
  13522. (interactive "P")
  13523. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13524. "LOGBOOK"))
  13525. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13526. ;; internally by Org. They are meant to be inserted
  13527. ;; automatically.
  13528. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13529. ;; Remove system drawers from list. Note: For some reason,
  13530. ;; `org-completing-read' ignores the predicate while
  13531. ;; `completing-read' handles it fine.
  13532. (drawer (if arg "PROPERTIES"
  13533. (or drawer
  13534. (completing-read
  13535. "Drawer: " org-drawers
  13536. (lambda (d) (not (member d system-drawers))))))))
  13537. (cond
  13538. ;; With C-u, fall back on `org-insert-property-drawer'
  13539. (arg (org-insert-property-drawer))
  13540. ;; With an active region, insert a drawer at point.
  13541. ((not (org-region-active-p))
  13542. (progn
  13543. (unless (bolp) (insert "\n"))
  13544. (insert (format ":%s:\n\n:END:\n" drawer))
  13545. (forward-line -2)))
  13546. ;; Otherwise, insert the drawer at point
  13547. (t
  13548. (let ((rbeg (region-beginning))
  13549. (rend (copy-marker (region-end))))
  13550. (unwind-protect
  13551. (progn
  13552. (goto-char rbeg)
  13553. (beginning-of-line)
  13554. (when (save-excursion
  13555. (re-search-forward org-outline-regexp-bol rend t))
  13556. (error "Drawers cannot contain headlines"))
  13557. ;; Position point at the beginning of the first
  13558. ;; non-blank line in region. Insert drawer's opening
  13559. ;; there, then indent it.
  13560. (org-skip-whitespace)
  13561. (beginning-of-line)
  13562. (insert ":" drawer ":\n")
  13563. (forward-line -1)
  13564. (indent-for-tab-command)
  13565. ;; Move point to the beginning of the first blank line
  13566. ;; after the last non-blank line in region. Insert
  13567. ;; drawer's closing, then indent it.
  13568. (goto-char rend)
  13569. (skip-chars-backward " \r\t\n")
  13570. (insert "\n:END:")
  13571. (deactivate-mark t)
  13572. (indent-for-tab-command)
  13573. (unless (eolp) (insert "\n")))
  13574. ;; Clear marker, whatever the outcome of insertion is.
  13575. (set-marker rend nil)))))))
  13576. (defvar org-property-set-functions-alist nil
  13577. "Property set function alist.
  13578. Each entry should have the following format:
  13579. (PROPERTY . READ-FUNCTION)
  13580. The read function will be called with the same argument as
  13581. `org-completing-read'.")
  13582. (defun org-set-property-function (property)
  13583. "Get the function that should be used to set PROPERTY.
  13584. This is computed according to `org-property-set-functions-alist'."
  13585. (or (cdr (assoc property org-property-set-functions-alist))
  13586. 'org-completing-read))
  13587. (defun org-read-property-value (property)
  13588. "Read PROPERTY value from user."
  13589. (let* ((completion-ignore-case t)
  13590. (allowed (org-property-get-allowed-values nil property 'table))
  13591. (cur (org-entry-get nil property))
  13592. (prompt (concat property " value"
  13593. (if (and cur (string-match "\\S-" cur))
  13594. (concat " [" cur "]") "") ": "))
  13595. (set-function (org-set-property-function property))
  13596. (val (if allowed
  13597. (funcall set-function prompt allowed nil
  13598. (not (get-text-property 0 'org-unrestricted
  13599. (caar allowed))))
  13600. (let (org-completion-use-ido org-completion-use-iswitchb)
  13601. (funcall set-function prompt
  13602. (mapcar 'list (org-property-values property))
  13603. nil nil "" nil cur)))))
  13604. (if (equal val "")
  13605. cur
  13606. val)))
  13607. (defvar org-last-set-property nil)
  13608. (defun org-read-property-name ()
  13609. "Read a property name."
  13610. (let* ((completion-ignore-case t)
  13611. (keys (org-buffer-property-keys nil t t))
  13612. (default-prop (or (save-excursion
  13613. (save-match-data
  13614. (beginning-of-line)
  13615. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13616. (null (string= (match-string 1) "END"))
  13617. (match-string 1))))
  13618. org-last-set-property))
  13619. (property (org-icompleting-read
  13620. (concat "Property"
  13621. (if default-prop (concat " [" default-prop "]") "")
  13622. ": ")
  13623. (mapcar 'list keys)
  13624. nil nil nil nil
  13625. default-prop
  13626. )))
  13627. (if (member property keys)
  13628. property
  13629. (or (cdr (assoc (downcase property)
  13630. (mapcar (lambda (x) (cons (downcase x) x))
  13631. keys)))
  13632. property))))
  13633. (defun org-set-property (property value)
  13634. "In the current entry, set PROPERTY to VALUE.
  13635. When called interactively, this will prompt for a property name, offering
  13636. completion on existing and default properties. And then it will prompt
  13637. for a value, offering completion either on allowed values (via an inherited
  13638. xxx_ALL property) or on existing values in other instances of this property
  13639. in the current file."
  13640. (interactive (list nil nil))
  13641. (let* ((property (or property (org-read-property-name)))
  13642. (value (or value (org-read-property-value property)))
  13643. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13644. (setq org-last-set-property property)
  13645. ;; Possibly postprocess the inserted value:
  13646. (when fn (setq value (funcall fn value)))
  13647. (unless (equal (org-entry-get nil property) value)
  13648. (org-entry-put nil property value))))
  13649. (defun org-delete-property (property)
  13650. "In the current entry, delete PROPERTY."
  13651. (interactive
  13652. (let* ((completion-ignore-case t)
  13653. (prop (org-icompleting-read "Property: "
  13654. (org-entry-properties nil 'standard))))
  13655. (list prop)))
  13656. (message "Property %s %s" property
  13657. (if (org-entry-delete nil property)
  13658. "deleted"
  13659. "was not present in the entry")))
  13660. (defun org-delete-property-globally (property)
  13661. "Remove PROPERTY globally, from all entries."
  13662. (interactive
  13663. (let* ((completion-ignore-case t)
  13664. (prop (org-icompleting-read
  13665. "Globally remove property: "
  13666. (mapcar 'list (org-buffer-property-keys)))))
  13667. (list prop)))
  13668. (save-excursion
  13669. (save-restriction
  13670. (widen)
  13671. (goto-char (point-min))
  13672. (let ((cnt 0))
  13673. (while (re-search-forward
  13674. (org-re-property property)
  13675. nil t)
  13676. (setq cnt (1+ cnt))
  13677. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13678. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13679. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13680. (defun org-compute-property-at-point ()
  13681. "Compute the property at point.
  13682. This looks for an enclosing column format, extracts the operator and
  13683. then applies it to the property in the column format's scope."
  13684. (interactive)
  13685. (unless (org-at-property-p)
  13686. (error "Not at a property"))
  13687. (let ((prop (org-match-string-no-properties 2)))
  13688. (org-columns-get-format-and-top-level)
  13689. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13690. (error "No operator defined for property %s" prop))
  13691. (org-columns-compute prop)))
  13692. (defvar org-property-allowed-value-functions nil
  13693. "Hook for functions supplying allowed values for a specific property.
  13694. The functions must take a single argument, the name of the property, and
  13695. return a flat list of allowed values. If \":ETC\" is one of
  13696. the values, this means that these values are intended as defaults for
  13697. completion, but that other values should be allowed too.
  13698. The functions must return nil if they are not responsible for this
  13699. property.")
  13700. (defun org-property-get-allowed-values (pom property &optional table)
  13701. "Get allowed values for the property PROPERTY.
  13702. When TABLE is non-nil, return an alist that can directly be used for
  13703. completion."
  13704. (let (vals)
  13705. (cond
  13706. ((equal property "TODO")
  13707. (setq vals (org-with-point-at pom
  13708. (append org-todo-keywords-1 '("")))))
  13709. ((equal property "PRIORITY")
  13710. (let ((n org-lowest-priority))
  13711. (while (>= n org-highest-priority)
  13712. (push (char-to-string n) vals)
  13713. (setq n (1- n)))))
  13714. ((member property org-special-properties))
  13715. ((setq vals (run-hook-with-args-until-success
  13716. 'org-property-allowed-value-functions property)))
  13717. (t
  13718. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13719. (when (and vals (string-match "\\S-" vals))
  13720. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13721. (setq vals (mapcar (lambda (x)
  13722. (cond ((stringp x) x)
  13723. ((numberp x) (number-to-string x))
  13724. ((symbolp x) (symbol-name x))
  13725. (t "???")))
  13726. vals)))))
  13727. (when (member ":ETC" vals)
  13728. (setq vals (remove ":ETC" vals))
  13729. (org-add-props (car vals) '(org-unrestricted t)))
  13730. (if table (mapcar 'list vals) vals)))
  13731. (defun org-property-previous-allowed-value (&optional previous)
  13732. "Switch to the next allowed value for this property."
  13733. (interactive)
  13734. (org-property-next-allowed-value t))
  13735. (defun org-property-next-allowed-value (&optional previous)
  13736. "Switch to the next allowed value for this property."
  13737. (interactive)
  13738. (unless (org-at-property-p)
  13739. (error "Not at a property"))
  13740. (let* ((key (match-string 2))
  13741. (value (match-string 3))
  13742. (allowed (or (org-property-get-allowed-values (point) key)
  13743. (and (member value '("[ ]" "[-]" "[X]"))
  13744. '("[ ]" "[X]"))))
  13745. nval)
  13746. (unless allowed
  13747. (error "Allowed values for this property have not been defined"))
  13748. (if previous (setq allowed (reverse allowed)))
  13749. (if (member value allowed)
  13750. (setq nval (car (cdr (member value allowed)))))
  13751. (setq nval (or nval (car allowed)))
  13752. (if (equal nval value)
  13753. (error "Only one allowed value for this property"))
  13754. (org-at-property-p)
  13755. (replace-match (concat " :" key ": " nval) t t)
  13756. (org-indent-line)
  13757. (beginning-of-line 1)
  13758. (skip-chars-forward " \t")
  13759. (run-hook-with-args 'org-property-changed-functions key nval)))
  13760. (defun org-find-olp (path &optional this-buffer)
  13761. "Return a marker pointing to the entry at outline path OLP.
  13762. If anything goes wrong, throw an error.
  13763. You can wrap this call to catch the error like this:
  13764. (condition-case msg
  13765. (org-mobile-locate-entry (match-string 4))
  13766. (error (nth 1 msg)))
  13767. The return value will then be either a string with the error message,
  13768. or a marker if everything is OK.
  13769. If THIS-BUFFER is set, the outline path does not contain a file,
  13770. only headings."
  13771. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13772. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13773. (level 1)
  13774. (lmin 1)
  13775. (lmax 1)
  13776. limit re end found pos heading cnt flevel)
  13777. (unless buffer (error "File not found :%s" file))
  13778. (with-current-buffer buffer
  13779. (save-excursion
  13780. (save-restriction
  13781. (widen)
  13782. (setq limit (point-max))
  13783. (goto-char (point-min))
  13784. (while (setq heading (pop path))
  13785. (setq re (format org-complex-heading-regexp-format
  13786. (regexp-quote heading)))
  13787. (setq cnt 0 pos (point))
  13788. (while (re-search-forward re end t)
  13789. (setq level (- (match-end 1) (match-beginning 1)))
  13790. (if (and (>= level lmin) (<= level lmax))
  13791. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13792. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13793. lmax heading))
  13794. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13795. lmax heading))
  13796. (goto-char found)
  13797. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13798. (setq end (save-excursion (org-end-of-subtree t t))))
  13799. (when (org-at-heading-p)
  13800. (point-marker)))))))
  13801. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13802. "Find node HEADING in BUFFER.
  13803. Return a marker to the heading if it was found, or nil if not.
  13804. If POS-ONLY is set, return just the position instead of a marker.
  13805. The heading text must match exact, but it may have a TODO keyword,
  13806. a priority cookie and tags in the standard locations."
  13807. (with-current-buffer (or buffer (current-buffer))
  13808. (save-excursion
  13809. (save-restriction
  13810. (widen)
  13811. (goto-char (point-min))
  13812. (let (case-fold-search)
  13813. (if (re-search-forward
  13814. (format org-complex-heading-regexp-format
  13815. (regexp-quote heading)) nil t)
  13816. (if pos-only
  13817. (match-beginning 0)
  13818. (move-marker (make-marker) (match-beginning 0)))))))))
  13819. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13820. "Find Org node headline HEADING in all .org files in directory DIR.
  13821. When the target headline is found, return a marker to this location."
  13822. (let ((files (directory-files (or dir default-directory)
  13823. nil "\\`[^.#].*\\.org\\'"))
  13824. file visiting m buffer)
  13825. (catch 'found
  13826. (while (setq file (pop files))
  13827. (message "trying %s" file)
  13828. (setq visiting (org-find-base-buffer-visiting file))
  13829. (setq buffer (or visiting (find-file-noselect file)))
  13830. (setq m (org-find-exact-headline-in-buffer
  13831. heading buffer))
  13832. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13833. (and m (throw 'found m))))))
  13834. (defun org-find-entry-with-id (ident)
  13835. "Locate the entry that contains the ID property with exact value IDENT.
  13836. IDENT can be a string, a symbol or a number, this function will search for
  13837. the string representation of it.
  13838. Return the position where this entry starts, or nil if there is no such entry."
  13839. (interactive "sID: ")
  13840. (let ((id (cond
  13841. ((stringp ident) ident)
  13842. ((symbol-name ident) (symbol-name ident))
  13843. ((numberp ident) (number-to-string ident))
  13844. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13845. (case-fold-search nil))
  13846. (save-excursion
  13847. (save-restriction
  13848. (widen)
  13849. (goto-char (point-min))
  13850. (when (re-search-forward
  13851. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13852. nil t)
  13853. (org-back-to-heading t)
  13854. (point))))))
  13855. ;;;; Timestamps
  13856. (defvar org-last-changed-timestamp nil)
  13857. (defvar org-last-inserted-timestamp nil
  13858. "The last time stamp inserted with `org-insert-time-stamp'.")
  13859. (defvar org-time-was-given) ; dynamically scoped parameter
  13860. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13861. (defvar org-ts-what) ; dynamically scoped parameter
  13862. (defun org-time-stamp (arg &optional inactive)
  13863. "Prompt for a date/time and insert a time stamp.
  13864. If the user specifies a time like HH:MM or if this command is
  13865. called with at least one prefix argument, the time stamp contains
  13866. the date and the time. Otherwise, only the date is be included.
  13867. All parts of a date not specified by the user is filled in from
  13868. the current date/time. So if you just press return without
  13869. typing anything, the time stamp will represent the current
  13870. date/time.
  13871. If there is already a timestamp at the cursor, it will be
  13872. modified.
  13873. With two universal prefix arguments, insert an active timestamp
  13874. with the current time without prompting the user."
  13875. (interactive "P")
  13876. (let* ((ts nil)
  13877. (default-time
  13878. ;; Default time is either today, or, when entering a range,
  13879. ;; the range start.
  13880. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13881. (save-excursion
  13882. (re-search-backward
  13883. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13884. (- (point) 20) t)))
  13885. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13886. (current-time)))
  13887. (default-input (and ts (org-get-compact-tod ts)))
  13888. (repeater (save-excursion
  13889. (save-match-data
  13890. (beginning-of-line)
  13891. (when (re-search-forward
  13892. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13893. (save-excursion (progn (end-of-line) (point))) t)
  13894. (match-string 0)))))
  13895. org-time-was-given org-end-time-was-given time)
  13896. (cond
  13897. ((and (org-at-timestamp-p t)
  13898. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13899. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13900. (insert "--")
  13901. (setq time (let ((this-command this-command))
  13902. (org-read-date arg 'totime nil nil
  13903. default-time default-input inactive)))
  13904. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13905. ((org-at-timestamp-p t)
  13906. (setq time (let ((this-command this-command))
  13907. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13908. (when (org-at-timestamp-p t) ; just to get the match data
  13909. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13910. (replace-match "")
  13911. (setq org-last-changed-timestamp
  13912. (org-insert-time-stamp
  13913. time (or org-time-was-given arg)
  13914. inactive nil nil (list org-end-time-was-given)))
  13915. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13916. (setq org-last-changed-timestamp
  13917. (concat (substring org-last-inserted-timestamp 0 -1)
  13918. " " repeater ">"))))
  13919. (message "Timestamp updated"))
  13920. ((equal arg '(16))
  13921. (org-insert-time-stamp (current-time) t))
  13922. (t
  13923. (setq time (let ((this-command this-command))
  13924. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13925. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13926. nil nil (list org-end-time-was-given))))))
  13927. ;; FIXME: can we use this for something else, like computing time differences?
  13928. (defun org-get-compact-tod (s)
  13929. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13930. (let* ((t1 (match-string 1 s))
  13931. (h1 (string-to-number (match-string 2 s)))
  13932. (m1 (string-to-number (match-string 3 s)))
  13933. (t2 (and (match-end 4) (match-string 5 s)))
  13934. (h2 (and t2 (string-to-number (match-string 6 s))))
  13935. (m2 (and t2 (string-to-number (match-string 7 s))))
  13936. dh dm)
  13937. (if (not t2)
  13938. t1
  13939. (setq dh (- h2 h1) dm (- m2 m1))
  13940. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13941. (concat t1 "+" (number-to-string dh)
  13942. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13943. (defun org-time-stamp-inactive (&optional arg)
  13944. "Insert an inactive time stamp.
  13945. An inactive time stamp is enclosed in square brackets instead of angle
  13946. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13947. does not link to the calendar and cannot be changed with the S-cursor keys.
  13948. So these are more for recording a certain time/date."
  13949. (interactive "P")
  13950. (org-time-stamp arg 'inactive))
  13951. (defvar org-date-ovl (make-overlay 1 1))
  13952. (overlay-put org-date-ovl 'face 'org-date-selected)
  13953. (org-detach-overlay org-date-ovl)
  13954. (defvar org-ans1) ; dynamically scoped parameter
  13955. (defvar org-ans2) ; dynamically scoped parameter
  13956. (defvar org-plain-time-of-day-regexp) ; defined below
  13957. (defvar org-overriding-default-time nil) ; dynamically scoped
  13958. (defvar org-read-date-overlay nil)
  13959. (defvar org-dcst nil) ; dynamically scoped
  13960. (defvar org-read-date-history nil)
  13961. (defvar org-read-date-final-answer nil)
  13962. (defvar org-read-date-analyze-futurep nil)
  13963. (defvar org-read-date-analyze-forced-year nil)
  13964. (defvar org-read-date-inactive)
  13965. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13966. default-time default-input inactive)
  13967. "Read a date, possibly a time, and make things smooth for the user.
  13968. The prompt will suggest to enter an ISO date, but you can also enter anything
  13969. which will at least partially be understood by `parse-time-string'.
  13970. Unrecognized parts of the date will default to the current day, month, year,
  13971. hour and minute. If this command is called to replace a timestamp at point,
  13972. or to enter the second timestamp of a range, the default time is taken
  13973. from the existing stamp. Furthermore, the command prefers the future,
  13974. so if you are giving a date where the year is not given, and the day-month
  13975. combination is already past in the current year, it will assume you
  13976. mean next year. For details, see the manual. A few examples:
  13977. 3-2-5 --> 2003-02-05
  13978. feb 15 --> currentyear-02-15
  13979. 2/15 --> currentyear-02-15
  13980. sep 12 9 --> 2009-09-12
  13981. 12:45 --> today 12:45
  13982. 22 sept 0:34 --> currentyear-09-22 0:34
  13983. 12 --> currentyear-currentmonth-12
  13984. Fri --> nearest Friday (today or later)
  13985. etc.
  13986. Furthermore you can specify a relative date by giving, as the *first* thing
  13987. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  13988. change in days weeks, months, years.
  13989. With a single plus or minus, the date is relative to today. With a double
  13990. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13991. +4d --> four days from today
  13992. +4 --> same as above
  13993. +2w --> two weeks from today
  13994. ++5 --> five days from default date
  13995. The function understands only English month and weekday abbreviations.
  13996. While prompting, a calendar is popped up - you can also select the
  13997. date with the mouse (button 1). The calendar shows a period of three
  13998. months. To scroll it to other months, use the keys `>' and `<'.
  13999. If you don't like the calendar, turn it off with
  14000. \(setq org-read-date-popup-calendar nil)
  14001. With optional argument TO-TIME, the date will immediately be converted
  14002. to an internal time.
  14003. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14004. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14005. still enter a time, and this function will inform the calling routine
  14006. about this change. The calling routine may then choose to change the
  14007. format used to insert the time stamp into the buffer to include the time.
  14008. With optional argument FROM-STRING, read from this string instead from
  14009. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14010. the time/date that is used for everything that is not specified by the
  14011. user."
  14012. (require 'parse-time)
  14013. (let* ((org-time-stamp-rounding-minutes
  14014. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14015. (org-dcst org-display-custom-times)
  14016. (ct (org-current-time))
  14017. (org-def (or org-overriding-default-time default-time ct))
  14018. (org-defdecode (decode-time org-def))
  14019. (dummy (progn
  14020. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14021. (setcar (nthcdr 2 org-defdecode) -1)
  14022. (setcar (nthcdr 1 org-defdecode) 59)
  14023. (setq org-def (apply 'encode-time org-defdecode)
  14024. org-defdecode (decode-time org-def)))))
  14025. (calendar-frame-setup nil)
  14026. (calendar-setup nil)
  14027. (calendar-move-hook nil)
  14028. (calendar-view-diary-initially-flag nil)
  14029. (calendar-view-holidays-initially-flag nil)
  14030. (timestr (format-time-string
  14031. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14032. (prompt (concat (if prompt (concat prompt " ") "")
  14033. (format "Date+time [%s]: " timestr)))
  14034. ans (org-ans0 "") org-ans1 org-ans2 final)
  14035. (cond
  14036. (from-string (setq ans from-string))
  14037. (org-read-date-popup-calendar
  14038. (save-excursion
  14039. (save-window-excursion
  14040. (calendar)
  14041. (org-eval-in-calendar '(setq cursor-type nil) t)
  14042. (unwind-protect
  14043. (progn
  14044. (calendar-forward-day (- (time-to-days org-def)
  14045. (calendar-absolute-from-gregorian
  14046. (calendar-current-date))))
  14047. (org-eval-in-calendar nil t)
  14048. (let* ((old-map (current-local-map))
  14049. (map (copy-keymap calendar-mode-map))
  14050. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14051. (org-defkey map (kbd "RET") 'org-calendar-select)
  14052. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14053. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14054. (org-defkey minibuffer-local-map [(meta shift left)]
  14055. (lambda () (interactive)
  14056. (org-eval-in-calendar '(calendar-backward-month 1))))
  14057. (org-defkey minibuffer-local-map [(meta shift right)]
  14058. (lambda () (interactive)
  14059. (org-eval-in-calendar '(calendar-forward-month 1))))
  14060. (org-defkey minibuffer-local-map [(meta shift up)]
  14061. (lambda () (interactive)
  14062. (org-eval-in-calendar '(calendar-backward-year 1))))
  14063. (org-defkey minibuffer-local-map [(meta shift down)]
  14064. (lambda () (interactive)
  14065. (org-eval-in-calendar '(calendar-forward-year 1))))
  14066. (org-defkey minibuffer-local-map [?\e (shift left)]
  14067. (lambda () (interactive)
  14068. (org-eval-in-calendar '(calendar-backward-month 1))))
  14069. (org-defkey minibuffer-local-map [?\e (shift right)]
  14070. (lambda () (interactive)
  14071. (org-eval-in-calendar '(calendar-forward-month 1))))
  14072. (org-defkey minibuffer-local-map [?\e (shift up)]
  14073. (lambda () (interactive)
  14074. (org-eval-in-calendar '(calendar-backward-year 1))))
  14075. (org-defkey minibuffer-local-map [?\e (shift down)]
  14076. (lambda () (interactive)
  14077. (org-eval-in-calendar '(calendar-forward-year 1))))
  14078. (org-defkey minibuffer-local-map [(shift up)]
  14079. (lambda () (interactive)
  14080. (org-eval-in-calendar '(calendar-backward-week 1))))
  14081. (org-defkey minibuffer-local-map [(shift down)]
  14082. (lambda () (interactive)
  14083. (org-eval-in-calendar '(calendar-forward-week 1))))
  14084. (org-defkey minibuffer-local-map [(shift left)]
  14085. (lambda () (interactive)
  14086. (org-eval-in-calendar '(calendar-backward-day 1))))
  14087. (org-defkey minibuffer-local-map [(shift right)]
  14088. (lambda () (interactive)
  14089. (org-eval-in-calendar '(calendar-forward-day 1))))
  14090. (org-defkey minibuffer-local-map ">"
  14091. (lambda () (interactive)
  14092. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14093. (org-defkey minibuffer-local-map "<"
  14094. (lambda () (interactive)
  14095. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14096. (org-defkey minibuffer-local-map "\C-v"
  14097. (lambda () (interactive)
  14098. (org-eval-in-calendar
  14099. '(calendar-scroll-left-three-months 1))))
  14100. (org-defkey minibuffer-local-map "\M-v"
  14101. (lambda () (interactive)
  14102. (org-eval-in-calendar
  14103. '(calendar-scroll-right-three-months 1))))
  14104. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14105. (unwind-protect
  14106. (progn
  14107. (use-local-map map)
  14108. (setq org-read-date-inactive inactive)
  14109. (add-hook 'post-command-hook 'org-read-date-display)
  14110. (setq org-ans0 (read-string prompt default-input
  14111. 'org-read-date-history nil))
  14112. ;; org-ans0: from prompt
  14113. ;; org-ans1: from mouse click
  14114. ;; org-ans2: from calendar motion
  14115. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14116. (remove-hook 'post-command-hook 'org-read-date-display)
  14117. (use-local-map old-map)
  14118. (when org-read-date-overlay
  14119. (delete-overlay org-read-date-overlay)
  14120. (setq org-read-date-overlay nil)))))
  14121. (bury-buffer "*Calendar*")))))
  14122. (t ; Naked prompt only
  14123. (unwind-protect
  14124. (setq ans (read-string prompt default-input
  14125. 'org-read-date-history timestr))
  14126. (when org-read-date-overlay
  14127. (delete-overlay org-read-date-overlay)
  14128. (setq org-read-date-overlay nil)))))
  14129. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14130. (when org-read-date-analyze-forced-year
  14131. (message "Year was forced into %s"
  14132. (if org-read-date-force-compatible-dates
  14133. "compatible range (1970-2037)"
  14134. "range representable on this machine"))
  14135. (ding))
  14136. ;; One round trip to get rid of 34th of August and stuff like that....
  14137. (setq final (decode-time (apply 'encode-time final)))
  14138. (setq org-read-date-final-answer ans)
  14139. (if to-time
  14140. (apply 'encode-time final)
  14141. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14142. (format "%04d-%02d-%02d %02d:%02d"
  14143. (nth 5 final) (nth 4 final) (nth 3 final)
  14144. (nth 2 final) (nth 1 final))
  14145. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14146. (defvar org-def)
  14147. (defvar org-defdecode)
  14148. (defvar org-with-time)
  14149. (defun org-read-date-display ()
  14150. "Display the current date prompt interpretation in the minibuffer."
  14151. (when org-read-date-display-live
  14152. (when org-read-date-overlay
  14153. (delete-overlay org-read-date-overlay))
  14154. (when (minibufferp (current-buffer))
  14155. (save-excursion
  14156. (end-of-line 1)
  14157. (while (not (equal (buffer-substring
  14158. (max (point-min) (- (point) 4)) (point))
  14159. " "))
  14160. (insert " ")))
  14161. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14162. " " (or org-ans1 org-ans2)))
  14163. (org-end-time-was-given nil)
  14164. (f (org-read-date-analyze ans org-def org-defdecode))
  14165. (fmts (if org-dcst
  14166. org-time-stamp-custom-formats
  14167. org-time-stamp-formats))
  14168. (fmt (if (or org-with-time
  14169. (and (boundp 'org-time-was-given) org-time-was-given))
  14170. (cdr fmts)
  14171. (car fmts)))
  14172. (txt (format-time-string fmt (apply 'encode-time f)))
  14173. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14174. (txt (concat "=> " txt)))
  14175. (when (and org-end-time-was-given
  14176. (string-match org-plain-time-of-day-regexp txt))
  14177. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14178. org-end-time-was-given
  14179. (substring txt (match-end 0)))))
  14180. (when org-read-date-analyze-futurep
  14181. (setq txt (concat txt " (=>F)")))
  14182. (setq org-read-date-overlay
  14183. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14184. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14185. (defun org-read-date-analyze (ans org-def org-defdecode)
  14186. "Analyze the combined answer of the date prompt."
  14187. ;; FIXME: cleanup and comment
  14188. (let ((nowdecode (decode-time (current-time)))
  14189. delta deltan deltaw deltadef year month day
  14190. hour minute second wday pm h2 m2 tl wday1
  14191. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14192. (setq org-read-date-analyze-futurep nil
  14193. org-read-date-analyze-forced-year nil)
  14194. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14195. (setq ans "+0"))
  14196. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14197. (setq ans (replace-match "" t t ans)
  14198. deltan (car delta)
  14199. deltaw (nth 1 delta)
  14200. deltadef (nth 2 delta)))
  14201. ;; Check if there is an iso week date in there. If yes, store the
  14202. ;; info and postpone interpreting it until the rest of the parsing
  14203. ;; is done.
  14204. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14205. (setq iso-year (if (match-end 1)
  14206. (org-small-year-to-year
  14207. (string-to-number (match-string 1 ans))))
  14208. iso-weekday (if (match-end 3)
  14209. (string-to-number (match-string 3 ans)))
  14210. iso-week (string-to-number (match-string 2 ans)))
  14211. (setq ans (replace-match "" t t ans)))
  14212. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14213. (when (string-match
  14214. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14215. (setq year (if (match-end 2)
  14216. (string-to-number (match-string 2 ans))
  14217. (progn (setq kill-year t)
  14218. (string-to-number (format-time-string "%Y"))))
  14219. month (string-to-number (match-string 3 ans))
  14220. day (string-to-number (match-string 4 ans)))
  14221. (if (< year 100) (setq year (+ 2000 year)))
  14222. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14223. t nil ans)))
  14224. ;; Help matching dotted european dates
  14225. (when (string-match
  14226. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14227. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14228. (setq kill-year t)
  14229. (string-to-number (format-time-string "%Y")))
  14230. day (string-to-number (match-string 1 ans))
  14231. month (string-to-number (match-string 2 ans))
  14232. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14233. t nil ans)))
  14234. ;; Help matching american dates, like 5/30 or 5/30/7
  14235. (when (string-match
  14236. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14237. (setq year (if (match-end 4)
  14238. (string-to-number (match-string 4 ans))
  14239. (progn (setq kill-year t)
  14240. (string-to-number (format-time-string "%Y"))))
  14241. month (string-to-number (match-string 1 ans))
  14242. day (string-to-number (match-string 2 ans)))
  14243. (if (< year 100) (setq year (+ 2000 year)))
  14244. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14245. t nil ans)))
  14246. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14247. ;; If there is a time with am/pm, and *no* time without it, we convert
  14248. ;; so that matching will be successful.
  14249. (loop for i from 1 to 2 do ; twice, for end time as well
  14250. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14251. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14252. (setq hour (string-to-number (match-string 1 ans))
  14253. minute (if (match-end 3)
  14254. (string-to-number (match-string 3 ans))
  14255. 0)
  14256. pm (equal ?p
  14257. (string-to-char (downcase (match-string 4 ans)))))
  14258. (if (and (= hour 12) (not pm))
  14259. (setq hour 0)
  14260. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14261. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14262. t t ans))))
  14263. ;; Check if a time range is given as a duration
  14264. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14265. (setq hour (string-to-number (match-string 1 ans))
  14266. h2 (+ hour (string-to-number (match-string 3 ans)))
  14267. minute (string-to-number (match-string 2 ans))
  14268. m2 (+ minute (if (match-end 5) (string-to-number
  14269. (match-string 5 ans))0)))
  14270. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14271. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14272. t t ans)))
  14273. ;; Check if there is a time range
  14274. (when (boundp 'org-end-time-was-given)
  14275. (setq org-time-was-given nil)
  14276. (when (and (string-match org-plain-time-of-day-regexp ans)
  14277. (match-end 8))
  14278. (setq org-end-time-was-given (match-string 8 ans))
  14279. (setq ans (concat (substring ans 0 (match-beginning 7))
  14280. (substring ans (match-end 7))))))
  14281. (setq tl (parse-time-string ans)
  14282. day (or (nth 3 tl) (nth 3 org-defdecode))
  14283. month (or (nth 4 tl)
  14284. (if (and org-read-date-prefer-future
  14285. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14286. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14287. (nth 4 org-defdecode)))
  14288. year (or (and (not kill-year) (nth 5 tl))
  14289. (if (and org-read-date-prefer-future
  14290. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14291. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14292. (nth 5 org-defdecode)))
  14293. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14294. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14295. second (or (nth 0 tl) 0)
  14296. wday (nth 6 tl))
  14297. (when (and (eq org-read-date-prefer-future 'time)
  14298. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14299. (equal day (nth 3 nowdecode))
  14300. (equal month (nth 4 nowdecode))
  14301. (equal year (nth 5 nowdecode))
  14302. (nth 2 tl)
  14303. (or (< (nth 2 tl) (nth 2 nowdecode))
  14304. (and (= (nth 2 tl) (nth 2 nowdecode))
  14305. (nth 1 tl)
  14306. (< (nth 1 tl) (nth 1 nowdecode)))))
  14307. (setq day (1+ day)
  14308. futurep t))
  14309. ;; Special date definitions below
  14310. (cond
  14311. (iso-week
  14312. ;; There was an iso week
  14313. (require 'cal-iso)
  14314. (setq futurep nil)
  14315. (setq year (or iso-year year)
  14316. day (or iso-weekday wday 1)
  14317. wday nil ; to make sure that the trigger below does not match
  14318. iso-date (calendar-gregorian-from-absolute
  14319. (calendar-absolute-from-iso
  14320. (list iso-week day year))))
  14321. ; FIXME: Should we also push ISO weeks into the future?
  14322. ; (when (and org-read-date-prefer-future
  14323. ; (not iso-year)
  14324. ; (< (calendar-absolute-from-gregorian iso-date)
  14325. ; (time-to-days (current-time))))
  14326. ; (setq year (1+ year)
  14327. ; iso-date (calendar-gregorian-from-absolute
  14328. ; (calendar-absolute-from-iso
  14329. ; (list iso-week day year)))))
  14330. (setq month (car iso-date)
  14331. year (nth 2 iso-date)
  14332. day (nth 1 iso-date)))
  14333. (deltan
  14334. (setq futurep nil)
  14335. (unless deltadef
  14336. (let ((now (decode-time (current-time))))
  14337. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14338. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14339. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14340. ((equal deltaw "m") (setq month (+ month deltan)))
  14341. ((equal deltaw "y") (setq year (+ year deltan)))))
  14342. ((and wday (not (nth 3 tl)))
  14343. ;; Weekday was given, but no day, so pick that day in the week
  14344. ;; on or after the derived date.
  14345. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14346. (unless (equal wday wday1)
  14347. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14348. (if (and (boundp 'org-time-was-given)
  14349. (nth 2 tl))
  14350. (setq org-time-was-given t))
  14351. (if (< year 100) (setq year (+ 2000 year)))
  14352. ;; Check of the date is representable
  14353. (if org-read-date-force-compatible-dates
  14354. (progn
  14355. (if (< year 1970)
  14356. (setq year 1970 org-read-date-analyze-forced-year t))
  14357. (if (> year 2037)
  14358. (setq year 2037 org-read-date-analyze-forced-year t)))
  14359. (condition-case nil
  14360. (ignore (encode-time second minute hour day month year))
  14361. (error
  14362. (setq year (nth 5 org-defdecode))
  14363. (setq org-read-date-analyze-forced-year t))))
  14364. (setq org-read-date-analyze-futurep futurep)
  14365. (list second minute hour day month year)))
  14366. (defvar parse-time-weekdays)
  14367. (defun org-read-date-get-relative (s today default)
  14368. "Check string S for special relative date string.
  14369. TODAY and DEFAULT are internal times, for today and for a default.
  14370. Return shift list (N what def-flag)
  14371. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14372. N is the number of WHATs to shift.
  14373. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14374. the DEFAULT date rather than TODAY."
  14375. (require 'parse-time)
  14376. (when (and
  14377. (string-match
  14378. (concat
  14379. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14380. "\\([0-9]+\\)?"
  14381. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14382. "\\([ \t]\\|$\\)") s)
  14383. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14384. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14385. (string-to-char (substring (match-string 1 s) -1))
  14386. ?+))
  14387. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14388. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14389. (what (if (match-end 3) (match-string 3 s) "d"))
  14390. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14391. (date (if rel default today))
  14392. (wday (nth 6 (decode-time date)))
  14393. delta)
  14394. (if wday1
  14395. (progn
  14396. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14397. (if (= dir ?-) (setq delta (- delta 7)))
  14398. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14399. (list delta "d" rel))
  14400. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14401. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14402. "Turn a user-specified date into the internal representation.
  14403. The internal representation needed by the calendar is (month day year).
  14404. This is a wrapper to handle the brain-dead convention in calendar that
  14405. user function argument order change dependent on argument order."
  14406. (if (boundp 'calendar-date-style)
  14407. (cond
  14408. ((eq calendar-date-style 'american)
  14409. (list arg1 arg2 arg3))
  14410. ((eq calendar-date-style 'european)
  14411. (list arg2 arg1 arg3))
  14412. ((eq calendar-date-style 'iso)
  14413. (list arg2 arg3 arg1)))
  14414. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14415. (if (org-bound-and-true-p european-calendar-style)
  14416. (list arg2 arg1 arg3)
  14417. (list arg1 arg2 arg3)))))
  14418. (defun org-eval-in-calendar (form &optional keepdate)
  14419. "Eval FORM in the calendar window and return to current window.
  14420. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14421. otherwise stick to the current value of `org-ans2'."
  14422. (let ((sf (selected-frame))
  14423. (sw (selected-window)))
  14424. (select-window (get-buffer-window "*Calendar*" t))
  14425. (eval form)
  14426. (when (and (not keepdate) (calendar-cursor-to-date))
  14427. (let* ((date (calendar-cursor-to-date))
  14428. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14429. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14430. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14431. (select-window sw)
  14432. (org-select-frame-set-input-focus sf)))
  14433. (defun org-calendar-select ()
  14434. "Return to `org-read-date' with the date currently selected.
  14435. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14436. (interactive)
  14437. (when (calendar-cursor-to-date)
  14438. (let* ((date (calendar-cursor-to-date))
  14439. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14440. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14441. (if (active-minibuffer-window) (exit-minibuffer))))
  14442. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14443. "Insert a date stamp for the date given by the internal TIME.
  14444. WITH-HM means use the stamp format that includes the time of the day.
  14445. INACTIVE means use square brackets instead of angular ones, so that the
  14446. stamp will not contribute to the agenda.
  14447. PRE and POST are optional strings to be inserted before and after the
  14448. stamp.
  14449. The command returns the inserted time stamp."
  14450. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14451. stamp)
  14452. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14453. (insert-before-markers (or pre ""))
  14454. (when (listp extra)
  14455. (setq extra (car extra))
  14456. (if (and (stringp extra)
  14457. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14458. (setq extra (format "-%02d:%02d"
  14459. (string-to-number (match-string 1 extra))
  14460. (string-to-number (match-string 2 extra))))
  14461. (setq extra nil)))
  14462. (when extra
  14463. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14464. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14465. (insert-before-markers (or post ""))
  14466. (setq org-last-inserted-timestamp stamp)))
  14467. (defun org-toggle-time-stamp-overlays ()
  14468. "Toggle the use of custom time stamp formats."
  14469. (interactive)
  14470. (setq org-display-custom-times (not org-display-custom-times))
  14471. (unless org-display-custom-times
  14472. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14473. (while (setq p (next-single-property-change p 'display))
  14474. (if (and (get-text-property p 'display)
  14475. (eq (get-text-property p 'face) 'org-date))
  14476. (remove-text-properties
  14477. p (setq p (next-single-property-change p 'display))
  14478. '(display t))))
  14479. (set-buffer-modified-p bmp)))
  14480. (if (featurep 'xemacs)
  14481. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14482. (org-restart-font-lock)
  14483. (setq org-table-may-need-update t)
  14484. (if org-display-custom-times
  14485. (message "Time stamps are overlaid with custom format")
  14486. (message "Time stamp overlays removed")))
  14487. (defun org-display-custom-time (beg end)
  14488. "Overlay modified time stamp format over timestamp between BEG and END."
  14489. (let* ((ts (buffer-substring beg end))
  14490. t1 w1 with-hm tf time str w2 (off 0))
  14491. (save-match-data
  14492. (setq t1 (org-parse-time-string ts t))
  14493. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14494. (setq off (- (match-end 0) (match-beginning 0)))))
  14495. (setq end (- end off))
  14496. (setq w1 (- end beg)
  14497. with-hm (and (nth 1 t1) (nth 2 t1))
  14498. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14499. time (org-fix-decoded-time t1)
  14500. str (org-add-props
  14501. (format-time-string
  14502. (substring tf 1 -1) (apply 'encode-time time))
  14503. nil 'mouse-face 'highlight)
  14504. w2 (length str))
  14505. (if (not (= w2 w1))
  14506. (add-text-properties (1+ beg) (+ 2 beg)
  14507. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14508. (if (featurep 'xemacs)
  14509. (progn
  14510. (put-text-property beg end 'invisible t)
  14511. (put-text-property beg end 'end-glyph (make-glyph str)))
  14512. (put-text-property beg end 'display str))))
  14513. (defun org-translate-time (string)
  14514. "Translate all timestamps in STRING to custom format.
  14515. But do this only if the variable `org-display-custom-times' is set."
  14516. (when org-display-custom-times
  14517. (save-match-data
  14518. (let* ((start 0)
  14519. (re org-ts-regexp-both)
  14520. t1 with-hm inactive tf time str beg end)
  14521. (while (setq start (string-match re string start))
  14522. (setq beg (match-beginning 0)
  14523. end (match-end 0)
  14524. t1 (save-match-data
  14525. (org-parse-time-string (substring string beg end) t))
  14526. with-hm (and (nth 1 t1) (nth 2 t1))
  14527. inactive (equal (substring string beg (1+ beg)) "[")
  14528. tf (funcall (if with-hm 'cdr 'car)
  14529. org-time-stamp-custom-formats)
  14530. time (org-fix-decoded-time t1)
  14531. str (format-time-string
  14532. (concat
  14533. (if inactive "[" "<") (substring tf 1 -1)
  14534. (if inactive "]" ">"))
  14535. (apply 'encode-time time))
  14536. string (replace-match str t t string)
  14537. start (+ start (length str)))))))
  14538. string)
  14539. (defun org-fix-decoded-time (time)
  14540. "Set 0 instead of nil for the first 6 elements of time.
  14541. Don't touch the rest."
  14542. (let ((n 0))
  14543. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14544. (defun org-days-to-time (timestamp-string)
  14545. "Difference between TIMESTAMP-STRING and now in days."
  14546. (- (time-to-days (org-time-string-to-time timestamp-string))
  14547. (time-to-days (current-time))))
  14548. (defun org-deadline-close (timestamp-string &optional ndays)
  14549. "Is the time in TIMESTAMP-STRING close to the current date?"
  14550. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14551. (and (< (org-days-to-time timestamp-string) ndays)
  14552. (not (org-entry-is-done-p))))
  14553. (defun org-get-wdays (ts)
  14554. "Get the deadline lead time appropriate for timestring TS."
  14555. (cond
  14556. ((<= org-deadline-warning-days 0)
  14557. ;; 0 or negative, enforce this value no matter what
  14558. (- org-deadline-warning-days))
  14559. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14560. ;; lead time is specified.
  14561. (floor (* (string-to-number (match-string 1 ts))
  14562. (cdr (assoc (match-string 2 ts)
  14563. '(("d" . 1) ("w" . 7)
  14564. ("m" . 30.4) ("y" . 365.25)))))))
  14565. ;; go for the default.
  14566. (t org-deadline-warning-days)))
  14567. (defun org-calendar-select-mouse (ev)
  14568. "Return to `org-read-date' with the date currently selected.
  14569. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14570. (interactive "e")
  14571. (mouse-set-point ev)
  14572. (when (calendar-cursor-to-date)
  14573. (let* ((date (calendar-cursor-to-date))
  14574. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14575. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14576. (if (active-minibuffer-window) (exit-minibuffer))))
  14577. (defun org-check-deadlines (ndays)
  14578. "Check if there are any deadlines due or past due.
  14579. A deadline is considered due if it happens within `org-deadline-warning-days'
  14580. days from today's date. If the deadline appears in an entry marked DONE,
  14581. it is not shown. The prefix arg NDAYS can be used to test that many
  14582. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14583. (interactive "P")
  14584. (let* ((org-warn-days
  14585. (cond
  14586. ((equal ndays '(4)) 100000)
  14587. (ndays (prefix-numeric-value ndays))
  14588. (t (abs org-deadline-warning-days))))
  14589. (case-fold-search nil)
  14590. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14591. (callback
  14592. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14593. (message "%d deadlines past-due or due within %d days"
  14594. (org-occur regexp nil callback)
  14595. org-warn-days)))
  14596. (defsubst org-re-timestamp (type)
  14597. "Return a regexp for timestamp TYPE.
  14598. Allowed values for TYPE are:
  14599. all: all timestamps
  14600. active: only active timestamps (<...>)
  14601. inactive: only inactive timestamps ([...])
  14602. scheduled: only scheduled timestamps
  14603. deadline: only deadline timestamps
  14604. When TYPE is nil, fall back on returning a regexp that matches
  14605. both scheduled and deadline timestamps."
  14606. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14607. ((eq type 'active) org-ts-regexp)
  14608. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14609. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14610. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14611. ((eq type 'scheduled-or-deadline)
  14612. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14613. (defun org-check-before-date (date)
  14614. "Check if there are deadlines or scheduled entries before DATE."
  14615. (interactive (list (org-read-date)))
  14616. (let ((case-fold-search nil)
  14617. (regexp (org-re-timestamp org-ts-type))
  14618. (callback
  14619. (lambda () (time-less-p
  14620. (org-time-string-to-time (match-string 1))
  14621. (org-time-string-to-time date)))))
  14622. (message "%d entries before %s"
  14623. (org-occur regexp nil callback) date)))
  14624. (defun org-check-after-date (date)
  14625. "Check if there are deadlines or scheduled entries after DATE."
  14626. (interactive (list (org-read-date)))
  14627. (let ((case-fold-search nil)
  14628. (regexp (org-re-timestamp org-ts-type))
  14629. (callback
  14630. (lambda () (not
  14631. (time-less-p
  14632. (org-time-string-to-time (match-string 1))
  14633. (org-time-string-to-time date))))))
  14634. (message "%d entries after %s"
  14635. (org-occur regexp nil callback) date)))
  14636. (defun org-check-dates-range (start-date end-date)
  14637. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14638. (interactive (list (org-read-date nil nil nil "Range starts")
  14639. (org-read-date nil nil nil "Range end")))
  14640. (let ((case-fold-search nil)
  14641. (regexp (org-re-timestamp org-ts-type))
  14642. (callback
  14643. (lambda ()
  14644. (let ((match (match-string 1)))
  14645. (and
  14646. (not (time-less-p
  14647. (org-time-string-to-time match)
  14648. (org-time-string-to-time start-date)))
  14649. (time-less-p
  14650. (org-time-string-to-time match)
  14651. (org-time-string-to-time end-date)))))))
  14652. (message "%d entries between %s and %s"
  14653. (org-occur regexp nil callback) start-date end-date)))
  14654. (defun org-evaluate-time-range (&optional to-buffer)
  14655. "Evaluate a time range by computing the difference between start and end.
  14656. Normally the result is just printed in the echo area, but with prefix arg
  14657. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14658. If the time range is actually in a table, the result is inserted into the
  14659. next column.
  14660. For time difference computation, a year is assumed to be exactly 365
  14661. days in order to avoid rounding problems."
  14662. (interactive "P")
  14663. (or
  14664. (org-clock-update-time-maybe)
  14665. (save-excursion
  14666. (unless (org-at-date-range-p t)
  14667. (goto-char (point-at-bol))
  14668. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14669. (if (not (org-at-date-range-p t))
  14670. (error "Not at a time-stamp range, and none found in current line")))
  14671. (let* ((ts1 (match-string 1))
  14672. (ts2 (match-string 2))
  14673. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14674. (match-end (match-end 0))
  14675. (time1 (org-time-string-to-time ts1))
  14676. (time2 (org-time-string-to-time ts2))
  14677. (t1 (org-float-time time1))
  14678. (t2 (org-float-time time2))
  14679. (diff (abs (- t2 t1)))
  14680. (negative (< (- t2 t1) 0))
  14681. ;; (ys (floor (* 365 24 60 60)))
  14682. (ds (* 24 60 60))
  14683. (hs (* 60 60))
  14684. (fy "%dy %dd %02d:%02d")
  14685. (fy1 "%dy %dd")
  14686. (fd "%dd %02d:%02d")
  14687. (fd1 "%dd")
  14688. (fh "%02d:%02d")
  14689. y d h m align)
  14690. (if havetime
  14691. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14692. y 0
  14693. d (floor (/ diff ds)) diff (mod diff ds)
  14694. h (floor (/ diff hs)) diff (mod diff hs)
  14695. m (floor (/ diff 60)))
  14696. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14697. y 0
  14698. d (floor (+ (/ diff ds) 0.5))
  14699. h 0 m 0))
  14700. (if (not to-buffer)
  14701. (message "%s" (org-make-tdiff-string y d h m))
  14702. (if (org-at-table-p)
  14703. (progn
  14704. (goto-char match-end)
  14705. (setq align t)
  14706. (and (looking-at " *|") (goto-char (match-end 0))))
  14707. (goto-char match-end))
  14708. (if (looking-at
  14709. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14710. (replace-match ""))
  14711. (if negative (insert " -"))
  14712. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14713. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14714. (insert " " (format fh h m))))
  14715. (if align (org-table-align))
  14716. (message "Time difference inserted")))))
  14717. (defun org-make-tdiff-string (y d h m)
  14718. (let ((fmt "")
  14719. (l nil))
  14720. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14721. l (push y l)))
  14722. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14723. l (push d l)))
  14724. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14725. l (push h l)))
  14726. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14727. l (push m l)))
  14728. (apply 'format fmt (nreverse l))))
  14729. (defun org-time-string-to-time (s &optional buffer pos)
  14730. "Convert a timestamp string into internal time."
  14731. (condition-case errdata
  14732. (apply 'encode-time (org-parse-time-string s))
  14733. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14734. s (if (not (and buffer pos))
  14735. ""
  14736. (format " at %d in buffer `%s'" pos buffer))
  14737. (cdr errdata)))))
  14738. (defun org-time-string-to-seconds (s)
  14739. "Convert a timestamp string to a number of seconds."
  14740. (org-float-time (org-time-string-to-time s)))
  14741. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14742. "Convert a time stamp to an absolute day number.
  14743. If there is a specifier for a cyclic time stamp, get the closest date to
  14744. DAYNR.
  14745. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14746. The variable date is bound by the calendar when this is called."
  14747. (cond
  14748. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14749. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14750. daynr
  14751. (+ daynr 1000)))
  14752. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14753. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14754. (time-to-days (current-time))) (match-string 0 s)
  14755. prefer show-all))
  14756. (t (time-to-days
  14757. (condition-case errdata
  14758. (apply 'encode-time (org-parse-time-string s))
  14759. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14760. s (if (not (and buffer pos))
  14761. ""
  14762. (format " at %d in buffer `%s'" pos buffer))
  14763. (cdr errdata))))))))
  14764. (defun org-days-to-iso-week (days)
  14765. "Return the iso week number."
  14766. (require 'cal-iso)
  14767. (car (calendar-iso-from-absolute days)))
  14768. (defun org-small-year-to-year (year)
  14769. "Convert 2-digit years into 4-digit years.
  14770. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14771. The year 2000 cannot be abbreviated. Any year larger than 99
  14772. is returned unchanged."
  14773. (if (< year 38)
  14774. (setq year (+ 2000 year))
  14775. (if (< year 100)
  14776. (setq year (+ 1900 year))))
  14777. year)
  14778. (defun org-time-from-absolute (d)
  14779. "Return the time corresponding to date D.
  14780. D may be an absolute day number, or a calendar-type list (month day year)."
  14781. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14782. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14783. (defun org-calendar-holiday ()
  14784. "List of holidays, for Diary display in Org-mode."
  14785. (require 'holidays)
  14786. (let ((hl (funcall
  14787. (if (fboundp 'calendar-check-holidays)
  14788. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14789. (if hl (mapconcat 'identity hl "; "))))
  14790. (defun org-diary-sexp-entry (sexp entry date)
  14791. "Process a SEXP diary ENTRY for DATE."
  14792. (require 'diary-lib)
  14793. (let ((result (if calendar-debug-sexp
  14794. (let ((stack-trace-on-error t))
  14795. (eval (car (read-from-string sexp))))
  14796. (condition-case nil
  14797. (eval (car (read-from-string sexp)))
  14798. (error
  14799. (beep)
  14800. (message "Bad sexp at line %d in %s: %s"
  14801. (org-current-line)
  14802. (buffer-file-name) sexp)
  14803. (sleep-for 2))))))
  14804. (cond ((stringp result) (split-string result "; "))
  14805. ((and (consp result)
  14806. (not (consp (cdr result)))
  14807. (stringp (cdr result))) (cdr result))
  14808. ((and (consp result)
  14809. (stringp (car result))) result)
  14810. (result entry))))
  14811. (defun org-diary-to-ical-string (frombuf)
  14812. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14813. This uses the icalendar.el library."
  14814. (let* ((tmpdir (if (featurep 'xemacs)
  14815. (temp-directory)
  14816. temporary-file-directory))
  14817. (tmpfile (make-temp-name
  14818. (expand-file-name "orgics" tmpdir)))
  14819. buf rtn b e)
  14820. (with-current-buffer frombuf
  14821. (icalendar-export-region (point-min) (point-max) tmpfile)
  14822. (setq buf (find-buffer-visiting tmpfile))
  14823. (set-buffer buf)
  14824. (goto-char (point-min))
  14825. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14826. (setq b (match-beginning 0)))
  14827. (goto-char (point-max))
  14828. (if (re-search-backward "^END:VEVENT" nil t)
  14829. (setq e (match-end 0)))
  14830. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14831. (kill-buffer buf)
  14832. (delete-file tmpfile)
  14833. rtn))
  14834. (defun org-closest-date (start current change prefer show-all)
  14835. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14836. When PREFER is `past', return a date that is either CURRENT or past.
  14837. When PREFER is `future', return a date that is either CURRENT or future.
  14838. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14839. ;; Make the proper lists from the dates
  14840. (catch 'exit
  14841. (let ((a1 '(("h" . hour)
  14842. ("d" . day)
  14843. ("w" . week)
  14844. ("m" . month)
  14845. ("y" . year)))
  14846. (shour (nth 2 (org-parse-time-string start)))
  14847. dn dw sday cday n1 n2 n0
  14848. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14849. (setq start (org-date-to-gregorian start)
  14850. current (org-date-to-gregorian
  14851. (if show-all
  14852. current
  14853. (time-to-days (current-time))))
  14854. sday (calendar-absolute-from-gregorian start)
  14855. cday (calendar-absolute-from-gregorian current))
  14856. (if (<= cday sday) (throw 'exit sday))
  14857. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14858. (setq dn (string-to-number (match-string 1 change))
  14859. dw (cdr (assoc (match-string 2 change) a1)))
  14860. (error "Invalid change specifier: %s" change))
  14861. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14862. (cond
  14863. ((eq dw 'hour)
  14864. (let ((missing-hours
  14865. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  14866. dn)))
  14867. (setq n1 (if (zerop missing-hours) cday
  14868. (- cday (1+ (floor (/ missing-hours 24)))))
  14869. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  14870. ((eq dw 'day)
  14871. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14872. n2 (+ n1 dn)))
  14873. ((eq dw 'year)
  14874. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14875. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14876. (setq date1 (list m d y1)
  14877. n1 (calendar-absolute-from-gregorian date1)
  14878. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14879. n2 (calendar-absolute-from-gregorian date2)))
  14880. ((eq dw 'month)
  14881. ;; approx number of month between the two dates
  14882. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14883. ;; How often does dn fit in there?
  14884. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14885. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14886. m (+ m nm)
  14887. ny (floor (/ m 12))
  14888. y (+ y ny)
  14889. m (- m (* ny 12)))
  14890. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14891. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14892. (setq m2 (+ m dn) y2 y)
  14893. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14894. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14895. (while (<= n2 cday)
  14896. (setq n1 n2 m m2 y y2)
  14897. (setq m2 (+ m dn) y2 y)
  14898. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14899. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14900. ;; Make sure n1 is the earlier date
  14901. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14902. (if show-all
  14903. (cond
  14904. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14905. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14906. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14907. (cond
  14908. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14909. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14910. (t (if (= cday n1) n1 n2)))))))
  14911. (defun org-date-to-gregorian (date)
  14912. "Turn any specification of DATE into a Gregorian date for the calendar."
  14913. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14914. ((and (listp date) (= (length date) 3)) date)
  14915. ((stringp date)
  14916. (setq date (org-parse-time-string date))
  14917. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14918. ((listp date)
  14919. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14920. (defun org-parse-time-string (s &optional nodefault)
  14921. "Parse the standard Org-mode time string.
  14922. This should be a lot faster than the normal `parse-time-string'.
  14923. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14924. hour and minute fields will be nil if not given."
  14925. (if (string-match org-ts-regexp0 s)
  14926. (list 0
  14927. (if (or (match-beginning 8) (not nodefault))
  14928. (string-to-number (or (match-string 8 s) "0")))
  14929. (if (or (match-beginning 7) (not nodefault))
  14930. (string-to-number (or (match-string 7 s) "0")))
  14931. (string-to-number (match-string 4 s))
  14932. (string-to-number (match-string 3 s))
  14933. (string-to-number (match-string 2 s))
  14934. nil nil nil)
  14935. (error "Not a standard Org-mode time string: %s" s)))
  14936. (defun org-timestamp-up (&optional arg)
  14937. "Increase the date item at the cursor by one.
  14938. If the cursor is on the year, change the year. If it is on the month,
  14939. the day or the time, change that.
  14940. With prefix ARG, change by that many units."
  14941. (interactive "p")
  14942. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14943. (defun org-timestamp-down (&optional arg)
  14944. "Decrease the date item at the cursor by one.
  14945. If the cursor is on the year, change the year. If it is on the month,
  14946. the day or the time, change that.
  14947. With prefix ARG, change by that many units."
  14948. (interactive "p")
  14949. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14950. (defun org-timestamp-up-day (&optional arg)
  14951. "Increase the date in the time stamp by one day.
  14952. With prefix ARG, change that many days."
  14953. (interactive "p")
  14954. (if (and (not (org-at-timestamp-p t))
  14955. (org-at-heading-p))
  14956. (org-todo 'up)
  14957. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14958. (defun org-timestamp-down-day (&optional arg)
  14959. "Decrease the date in the time stamp by one day.
  14960. With prefix ARG, change that many days."
  14961. (interactive "p")
  14962. (if (and (not (org-at-timestamp-p t))
  14963. (org-at-heading-p))
  14964. (org-todo 'down)
  14965. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14966. (defun org-at-timestamp-p (&optional inactive-ok)
  14967. "Determine if the cursor is in or at a timestamp."
  14968. (interactive)
  14969. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14970. (pos (point))
  14971. (ans (or (looking-at tsr)
  14972. (save-excursion
  14973. (skip-chars-backward "^[<\n\r\t")
  14974. (if (> (point) (point-min)) (backward-char 1))
  14975. (and (looking-at tsr)
  14976. (> (- (match-end 0) pos) -1))))))
  14977. (and ans
  14978. (boundp 'org-ts-what)
  14979. (setq org-ts-what
  14980. (cond
  14981. ((= pos (match-beginning 0)) 'bracket)
  14982. ;; Point is considered to be "on the bracket" whether
  14983. ;; it's really on it or right after it.
  14984. ((= pos (1- (match-end 0))) 'bracket)
  14985. ((= pos (match-end 0)) 'after)
  14986. ((org-pos-in-match-range pos 2) 'year)
  14987. ((org-pos-in-match-range pos 3) 'month)
  14988. ((org-pos-in-match-range pos 7) 'hour)
  14989. ((org-pos-in-match-range pos 8) 'minute)
  14990. ((or (org-pos-in-match-range pos 4)
  14991. (org-pos-in-match-range pos 5)) 'day)
  14992. ((and (> pos (or (match-end 8) (match-end 5)))
  14993. (< pos (match-end 0)))
  14994. (- pos (or (match-end 8) (match-end 5))))
  14995. (t 'day))))
  14996. ans))
  14997. (defun org-toggle-timestamp-type ()
  14998. "Toggle the type (<active> or [inactive]) of a time stamp."
  14999. (interactive)
  15000. (when (org-at-timestamp-p t)
  15001. (let ((beg (match-beginning 0)) (end (match-end 0))
  15002. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15003. (save-excursion
  15004. (goto-char beg)
  15005. (while (re-search-forward "[][<>]" end t)
  15006. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15007. t t)))
  15008. (message "Timestamp is now %sactive"
  15009. (if (equal (char-after beg) ?<) "" "in")))))
  15010. (defvar org-clock-history) ; defined in org-clock.el
  15011. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15012. (defun org-timestamp-change (n &optional what updown)
  15013. "Change the date in the time stamp at point.
  15014. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15015. `year', `minute', `second'. If WHAT is not given, the cursor position
  15016. in the timestamp determines what will be changed."
  15017. (let ((origin (point)) origin-cat
  15018. with-hm inactive
  15019. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15020. org-ts-what
  15021. extra rem
  15022. ts time time0 fixnext clrgx)
  15023. (if (not (org-at-timestamp-p t))
  15024. (error "Not at a timestamp"))
  15025. (if (and (not what) (eq org-ts-what 'bracket))
  15026. (org-toggle-timestamp-type)
  15027. ;; Point isn't on brackets. Remember the part of the time-stamp
  15028. ;; the point was in. Indeed, size of time-stamps may change,
  15029. ;; but point must be kept in the same category nonetheless.
  15030. (setq origin-cat org-ts-what)
  15031. (if (and (not what) (not (eq org-ts-what 'day))
  15032. org-display-custom-times
  15033. (get-text-property (point) 'display)
  15034. (not (get-text-property (1- (point)) 'display)))
  15035. (setq org-ts-what 'day))
  15036. (setq org-ts-what (or what org-ts-what)
  15037. inactive (= (char-after (match-beginning 0)) ?\[)
  15038. ts (match-string 0))
  15039. (replace-match "")
  15040. (if (string-match
  15041. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15042. ts)
  15043. (setq extra (match-string 1 ts)))
  15044. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15045. (setq with-hm t))
  15046. (setq time0 (org-parse-time-string ts))
  15047. (when (and updown
  15048. (eq org-ts-what 'minute)
  15049. (not current-prefix-arg))
  15050. ;; This looks like s-up and s-down. Change by one rounding step.
  15051. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15052. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15053. (setcar (cdr time0) (+ (nth 1 time0)
  15054. (if (> n 0) (- rem) (- dm rem))))))
  15055. (setq time
  15056. (encode-time (or (car time0) 0)
  15057. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15058. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15059. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15060. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15061. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15062. (nthcdr 6 time0)))
  15063. (when (and (member org-ts-what '(hour minute))
  15064. extra
  15065. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15066. (setq extra (org-modify-ts-extra
  15067. extra
  15068. (if (eq org-ts-what 'hour) 2 5)
  15069. n dm)))
  15070. (when (integerp org-ts-what)
  15071. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15072. (if (eq what 'calendar)
  15073. (let ((cal-date (org-get-date-from-calendar)))
  15074. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15075. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15076. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15077. (setcar time0 (or (car time0) 0))
  15078. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15079. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15080. (setq time (apply 'encode-time time0))))
  15081. ;; Insert the new time-stamp, and ensure point stays in the same
  15082. ;; category as before (i.e. not after the last position in that
  15083. ;; category).
  15084. (let ((pos (point)))
  15085. ;; Stay before inserted string. `save-excursion' is of no use.
  15086. (setq org-last-changed-timestamp
  15087. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15088. (goto-char pos))
  15089. (save-match-data
  15090. (looking-at org-ts-regexp3)
  15091. (goto-char (cond
  15092. ;; `day' category ends before `hour' if any, or at
  15093. ;; the end of the day name.
  15094. ((eq origin-cat 'day)
  15095. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15096. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15097. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15098. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15099. ;; `year' and `month' have both fixed size: point
  15100. ;; couldn't have moved into another part.
  15101. (t origin))))
  15102. ;; Update clock if on a CLOCK line.
  15103. (org-clock-update-time-maybe)
  15104. ;; Maybe adjust the closest clock in `org-clock-history'
  15105. (when org-clock-adjust-closest
  15106. (if (not (and (org-at-clock-log-p)
  15107. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15108. org-clock-history))))))
  15109. (message "No clock to adjust")
  15110. (cond ((save-excursion ; fix previous clock?
  15111. (re-search-backward org-ts-regexp0 nil t)
  15112. (org-looking-back (concat org-clock-string " \\[")))
  15113. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15114. ((save-excursion ; fix next clock?
  15115. (re-search-backward org-ts-regexp0 nil t)
  15116. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15117. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15118. (save-window-excursion
  15119. ;; Find closest clock to point, adjust the previous/next one in history
  15120. (let* ((p (save-excursion (org-back-to-heading t)))
  15121. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15122. (clfixnth
  15123. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15124. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15125. (if (not clfixpos)
  15126. (message "No clock to adjust")
  15127. (save-excursion
  15128. (org-goto-marker-or-bmk clfixpos)
  15129. (org-show-subtree)
  15130. (when (re-search-forward clrgx nil t)
  15131. (goto-char (match-beginning 1))
  15132. (let (org-clock-adjust-closest)
  15133. (org-timestamp-change n org-ts-what updown))
  15134. (message "Clock adjusted in %s for heading: %s"
  15135. (file-name-nondirectory (buffer-file-name))
  15136. (org-get-heading t t)))))))))
  15137. ;; Try to recenter the calendar window, if any.
  15138. (if (and org-calendar-follow-timestamp-change
  15139. (get-buffer-window "*Calendar*" t)
  15140. (memq org-ts-what '(day month year)))
  15141. (org-recenter-calendar (time-to-days time))))))
  15142. (defun org-modify-ts-extra (s pos n dm)
  15143. "Change the different parts of the lead-time and repeat fields in timestamp."
  15144. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15145. ng h m new rem)
  15146. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15147. (cond
  15148. ((or (org-pos-in-match-range pos 2)
  15149. (org-pos-in-match-range pos 3))
  15150. (setq m (string-to-number (match-string 3 s))
  15151. h (string-to-number (match-string 2 s)))
  15152. (if (org-pos-in-match-range pos 2)
  15153. (setq h (+ h n))
  15154. (setq n (* dm (org-no-warnings (signum n))))
  15155. (when (not (= 0 (setq rem (% m dm))))
  15156. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15157. (setq m (+ m n)))
  15158. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15159. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15160. (setq h (min 24 (max 0 h)))
  15161. (setq ng 1 new (format "-%02d:%02d" h m)))
  15162. ((org-pos-in-match-range pos 6)
  15163. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15164. ((org-pos-in-match-range pos 5)
  15165. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15166. ((org-pos-in-match-range pos 9)
  15167. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15168. ((org-pos-in-match-range pos 8)
  15169. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15170. (when ng
  15171. (setq s (concat
  15172. (substring s 0 (match-beginning ng))
  15173. new
  15174. (substring s (match-end ng))))))
  15175. s))
  15176. (defun org-recenter-calendar (date)
  15177. "If the calendar is visible, recenter it to DATE."
  15178. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15179. (when cwin
  15180. (let ((calendar-move-hook nil))
  15181. (with-selected-window cwin
  15182. (calendar-goto-date (if (listp date) date
  15183. (calendar-gregorian-from-absolute date))))))))
  15184. (defun org-goto-calendar (&optional arg)
  15185. "Go to the Emacs calendar at the current date.
  15186. If there is a time stamp in the current line, go to that date.
  15187. A prefix ARG can be used to force the current date."
  15188. (interactive "P")
  15189. (let ((tsr org-ts-regexp) diff
  15190. (calendar-move-hook nil)
  15191. (calendar-view-holidays-initially-flag nil)
  15192. (calendar-view-diary-initially-flag nil))
  15193. (if (or (org-at-timestamp-p)
  15194. (save-excursion
  15195. (beginning-of-line 1)
  15196. (looking-at (concat ".*" tsr))))
  15197. (let ((d1 (time-to-days (current-time)))
  15198. (d2 (time-to-days
  15199. (org-time-string-to-time (match-string 1)))))
  15200. (setq diff (- d2 d1))))
  15201. (calendar)
  15202. (calendar-goto-today)
  15203. (if (and diff (not arg)) (calendar-forward-day diff))))
  15204. (defun org-get-date-from-calendar ()
  15205. "Return a list (month day year) of date at point in calendar."
  15206. (with-current-buffer "*Calendar*"
  15207. (save-match-data
  15208. (calendar-cursor-to-date))))
  15209. (defun org-date-from-calendar ()
  15210. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15211. If there is already a time stamp at the cursor position, update it."
  15212. (interactive)
  15213. (if (org-at-timestamp-p t)
  15214. (org-timestamp-change 0 'calendar)
  15215. (let ((cal-date (org-get-date-from-calendar)))
  15216. (org-insert-time-stamp
  15217. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15218. (defun org-minutes-to-hh:mm-string (m)
  15219. "Compute H:MM from a number of minutes."
  15220. (let ((h (/ m 60)))
  15221. (setq m (- m (* 60 h)))
  15222. (format org-time-clocksum-format h m)))
  15223. (defun org-hh:mm-string-to-minutes (s)
  15224. "Convert a string H:MM to a number of minutes.
  15225. If the string is just a number, interpret it as minutes.
  15226. In fact, the first hh:mm or number in the string will be taken,
  15227. there can be extra stuff in the string.
  15228. If no number is found, the return value is 0."
  15229. (cond
  15230. ((integerp s) s)
  15231. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15232. (+ (* (string-to-number (match-string 1 s)) 60)
  15233. (string-to-number (match-string 2 s))))
  15234. ((string-match "\\([0-9]+\\)" s)
  15235. (string-to-number (match-string 1 s)))
  15236. (t 0)))
  15237. (defcustom org-effort-durations
  15238. `(("h" . 60)
  15239. ("d" . ,(* 60 8))
  15240. ("w" . ,(* 60 8 5))
  15241. ("m" . ,(* 60 8 5 4))
  15242. ("y" . ,(* 60 8 5 40)))
  15243. "Conversion factor to minutes for an effort modifier.
  15244. Each entry has the form (MODIFIER . MINUTES).
  15245. In an effort string, a number followed by MODIFIER is multiplied
  15246. by the specified number of MINUTES to obtain an effort in
  15247. minutes.
  15248. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15249. effort string \"2hours\" is equivalent to 120 minutes."
  15250. :group 'org-agenda
  15251. :version "24.1"
  15252. :type '(alist :key-type (string :tag "Modifier")
  15253. :value-type (number :tag "Minutes")))
  15254. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15255. "Convert a duration string S to minutes.
  15256. A bare number is interpreted as minutes, modifiers can be set by
  15257. customizing `org-effort-durations' (which see).
  15258. Entries containing a colon are interpreted as H:MM by
  15259. `org-hh:mm-string-to-minutes'."
  15260. (let ((result 0)
  15261. (re (concat "\\([0-9.]+\\) *\\("
  15262. (regexp-opt (mapcar 'car org-effort-durations))
  15263. "\\)")))
  15264. (while (string-match re s)
  15265. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15266. (string-to-number (match-string 1 s))))
  15267. (setq s (replace-match "" nil t s)))
  15268. (setq result (floor result))
  15269. (incf result (org-hh:mm-string-to-minutes s))
  15270. (if output-to-string (number-to-string result) result)))
  15271. ;;;; Files
  15272. (defun org-save-all-org-buffers ()
  15273. "Save all Org-mode buffers without user confirmation."
  15274. (interactive)
  15275. (message "Saving all Org-mode buffers...")
  15276. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15277. (when (featurep 'org-id) (org-id-locations-save))
  15278. (message "Saving all Org-mode buffers... done"))
  15279. (defun org-revert-all-org-buffers ()
  15280. "Revert all Org-mode buffers.
  15281. Prompt for confirmation when there are unsaved changes.
  15282. Be sure you know what you are doing before letting this function
  15283. overwrite your changes.
  15284. This function is useful in a setup where one tracks org files
  15285. with a version control system, to revert on one machine after pulling
  15286. changes from another. I believe the procedure must be like this:
  15287. 1. M-x org-save-all-org-buffers
  15288. 2. Pull changes from the other machine, resolve conflicts
  15289. 3. M-x org-revert-all-org-buffers"
  15290. (interactive)
  15291. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15292. (error "Abort"))
  15293. (save-excursion
  15294. (save-window-excursion
  15295. (mapc
  15296. (lambda (b)
  15297. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15298. (with-current-buffer b buffer-file-name))
  15299. (org-pop-to-buffer-same-window b)
  15300. (revert-buffer t 'no-confirm)))
  15301. (buffer-list))
  15302. (when (and (featurep 'org-id) org-id-track-globally)
  15303. (org-id-locations-load)))))
  15304. ;;;; Agenda files
  15305. ;;;###autoload
  15306. (defun org-switchb (&optional arg)
  15307. "Switch between Org buffers.
  15308. With one prefix argument, restrict available buffers to files.
  15309. With two prefix arguments, restrict available buffers to agenda files.
  15310. Defaults to `iswitchb' for buffer name completion.
  15311. Set `org-completion-use-ido' to make it use ido instead."
  15312. (interactive "P")
  15313. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15314. ((equal arg '(16)) (org-buffer-list 'agenda))
  15315. (t (org-buffer-list))))
  15316. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15317. (org-completion-use-ido org-completion-use-ido))
  15318. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15319. (setq org-completion-use-iswitchb t))
  15320. (org-pop-to-buffer-same-window
  15321. (org-icompleting-read "Org buffer: "
  15322. (mapcar 'list (mapcar 'buffer-name blist))
  15323. nil t))))
  15324. ;;; Define some older names previously used for this functionality
  15325. ;;;###autoload
  15326. (defalias 'org-ido-switchb 'org-switchb)
  15327. ;;;###autoload
  15328. (defalias 'org-iswitchb 'org-switchb)
  15329. (defun org-buffer-list (&optional predicate exclude-tmp)
  15330. "Return a list of Org buffers.
  15331. PREDICATE can be `export', `files' or `agenda'.
  15332. export restrict the list to Export buffers.
  15333. files restrict the list to buffers visiting Org files.
  15334. agenda restrict the list to buffers visiting agenda files.
  15335. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15336. (let* ((bfn nil)
  15337. (agenda-files (and (eq predicate 'agenda)
  15338. (mapcar 'file-truename (org-agenda-files t))))
  15339. (filter
  15340. (cond
  15341. ((eq predicate 'files)
  15342. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15343. ((eq predicate 'export)
  15344. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15345. ((eq predicate 'agenda)
  15346. (lambda (b)
  15347. (with-current-buffer b
  15348. (and (derived-mode-p 'org-mode)
  15349. (setq bfn (buffer-file-name b))
  15350. (member (file-truename bfn) agenda-files)))))
  15351. (t (lambda (b) (with-current-buffer b
  15352. (or (derived-mode-p 'org-mode)
  15353. (string-match "\*Org .*Export"
  15354. (buffer-name b)))))))))
  15355. (delq nil
  15356. (mapcar
  15357. (lambda(b)
  15358. (if (and (funcall filter b)
  15359. (or (not exclude-tmp)
  15360. (not (string-match "tmp" (buffer-name b)))))
  15361. b
  15362. nil))
  15363. (buffer-list)))))
  15364. (defun org-agenda-files (&optional unrestricted archives)
  15365. "Get the list of agenda files.
  15366. Optional UNRESTRICTED means return the full list even if a restriction
  15367. is currently in place.
  15368. When ARCHIVES is t, include all archive files that are really being
  15369. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15370. `org-agenda-archives-mode' is t."
  15371. (let ((files
  15372. (cond
  15373. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15374. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15375. ((listp org-agenda-files) org-agenda-files)
  15376. (t (error "Invalid value of `org-agenda-files'")))))
  15377. (setq files (apply 'append
  15378. (mapcar (lambda (f)
  15379. (if (file-directory-p f)
  15380. (directory-files
  15381. f t org-agenda-file-regexp)
  15382. (list f)))
  15383. files)))
  15384. (when org-agenda-skip-unavailable-files
  15385. (setq files (delq nil
  15386. (mapcar (function
  15387. (lambda (file)
  15388. (and (file-readable-p file) file)))
  15389. files))))
  15390. (when (or (eq archives t)
  15391. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15392. (setq files (org-add-archive-files files)))
  15393. files))
  15394. (defun org-agenda-file-p (&optional file)
  15395. "Return non-nil, if FILE is an agenda file.
  15396. If FILE is omitted, use the file associated with the current
  15397. buffer."
  15398. (member (or file (buffer-file-name))
  15399. (org-agenda-files t)))
  15400. (defun org-edit-agenda-file-list ()
  15401. "Edit the list of agenda files.
  15402. Depending on setup, this either uses customize to edit the variable
  15403. `org-agenda-files', or it visits the file that is holding the list. In the
  15404. latter case, the buffer is set up in a way that saving it automatically kills
  15405. the buffer and restores the previous window configuration."
  15406. (interactive)
  15407. (if (stringp org-agenda-files)
  15408. (let ((cw (current-window-configuration)))
  15409. (find-file org-agenda-files)
  15410. (org-set-local 'org-window-configuration cw)
  15411. (org-add-hook 'after-save-hook
  15412. (lambda ()
  15413. (set-window-configuration
  15414. (prog1 org-window-configuration
  15415. (kill-buffer (current-buffer))))
  15416. (org-install-agenda-files-menu)
  15417. (message "New agenda file list installed"))
  15418. nil 'local)
  15419. (message "%s" (substitute-command-keys
  15420. "Edit list and finish with \\[save-buffer]")))
  15421. (customize-variable 'org-agenda-files)))
  15422. (defun org-store-new-agenda-file-list (list)
  15423. "Set new value for the agenda file list and save it correctly."
  15424. (if (stringp org-agenda-files)
  15425. (let ((fe (org-read-agenda-file-list t)) b u)
  15426. (while (setq b (find-buffer-visiting org-agenda-files))
  15427. (kill-buffer b))
  15428. (with-temp-file org-agenda-files
  15429. (insert
  15430. (mapconcat
  15431. (lambda (f) ;; Keep un-expanded entries.
  15432. (if (setq u (assoc f fe))
  15433. (cdr u)
  15434. f))
  15435. list "\n")
  15436. "\n")))
  15437. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15438. (org-insert-mode-line-in-empty-file nil))
  15439. (setq org-agenda-files list)
  15440. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15441. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15442. "Read the list of agenda files from a file.
  15443. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15444. filenames, used by `org-store-new-agenda-file-list' to write back
  15445. un-expanded file names."
  15446. (when (file-directory-p org-agenda-files)
  15447. (error "`org-agenda-files' cannot be a single directory"))
  15448. (when (stringp org-agenda-files)
  15449. (with-temp-buffer
  15450. (insert-file-contents org-agenda-files)
  15451. (mapcar
  15452. (lambda (f)
  15453. (let ((e (expand-file-name (substitute-in-file-name f)
  15454. org-directory)))
  15455. (if pair-with-expansion
  15456. (cons e f)
  15457. e)))
  15458. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15459. ;;;###autoload
  15460. (defun org-cycle-agenda-files ()
  15461. "Cycle through the files in `org-agenda-files'.
  15462. If the current buffer visits an agenda file, find the next one in the list.
  15463. If the current buffer does not, find the first agenda file."
  15464. (interactive)
  15465. (let* ((fs (org-agenda-files t))
  15466. (files (append fs (list (car fs))))
  15467. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15468. file)
  15469. (unless files (error "No agenda files"))
  15470. (catch 'exit
  15471. (while (setq file (pop files))
  15472. (if (equal (file-truename file) tcf)
  15473. (when (car files)
  15474. (find-file (car files))
  15475. (throw 'exit t))))
  15476. (find-file (car fs)))
  15477. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15478. (defun org-agenda-file-to-front (&optional to-end)
  15479. "Move/add the current file to the top of the agenda file list.
  15480. If the file is not present in the list, it is added to the front. If it is
  15481. present, it is moved there. With optional argument TO-END, add/move to the
  15482. end of the list."
  15483. (interactive "P")
  15484. (let ((org-agenda-skip-unavailable-files nil)
  15485. (file-alist (mapcar (lambda (x)
  15486. (cons (file-truename x) x))
  15487. (org-agenda-files t)))
  15488. (ctf (file-truename
  15489. (or buffer-file-name
  15490. (error "Please save the current buffer to a file"))))
  15491. x had)
  15492. (setq x (assoc ctf file-alist) had x)
  15493. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15494. (if to-end
  15495. (setq file-alist (append (delq x file-alist) (list x)))
  15496. (setq file-alist (cons x (delq x file-alist))))
  15497. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15498. (org-install-agenda-files-menu)
  15499. (message "File %s to %s of agenda file list"
  15500. (if had "moved" "added") (if to-end "end" "front"))))
  15501. (defun org-remove-file (&optional file)
  15502. "Remove current file from the list of files in variable `org-agenda-files'.
  15503. These are the files which are being checked for agenda entries.
  15504. Optional argument FILE means use this file instead of the current."
  15505. (interactive)
  15506. (let* ((org-agenda-skip-unavailable-files nil)
  15507. (file (or file buffer-file-name
  15508. (error "Current buffer does not visit a file")))
  15509. (true-file (file-truename file))
  15510. (afile (abbreviate-file-name file))
  15511. (files (delq nil (mapcar
  15512. (lambda (x)
  15513. (if (equal true-file
  15514. (file-truename x))
  15515. nil x))
  15516. (org-agenda-files t)))))
  15517. (if (not (= (length files) (length (org-agenda-files t))))
  15518. (progn
  15519. (org-store-new-agenda-file-list files)
  15520. (org-install-agenda-files-menu)
  15521. (message "Removed file: %s" afile))
  15522. (message "File was not in list: %s (not removed)" afile))))
  15523. (defun org-file-menu-entry (file)
  15524. (vector file (list 'find-file file) t))
  15525. (defun org-check-agenda-file (file)
  15526. "Make sure FILE exists. If not, ask user what to do."
  15527. (when (not (file-exists-p file))
  15528. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15529. (abbreviate-file-name file))
  15530. (let ((r (downcase (read-char-exclusive))))
  15531. (cond
  15532. ((equal r ?r)
  15533. (org-remove-file file)
  15534. (throw 'nextfile t))
  15535. (t (error "Abort"))))))
  15536. (defun org-get-agenda-file-buffer (file)
  15537. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15538. it to the list of buffers which might be released later."
  15539. (let ((buf (org-find-base-buffer-visiting file)))
  15540. (if buf
  15541. buf ; just return it
  15542. ;; Make a new buffer and remember it
  15543. (setq buf (find-file-noselect file))
  15544. (if buf (push buf org-agenda-new-buffers))
  15545. buf)))
  15546. (defun org-release-buffers (blist)
  15547. "Release all buffers in list, asking the user for confirmation when needed.
  15548. When a buffer is unmodified, it is just killed. When modified, it is saved
  15549. \(if the user agrees) and then killed."
  15550. (let (buf file)
  15551. (while (setq buf (pop blist))
  15552. (setq file (buffer-file-name buf))
  15553. (when (and (buffer-modified-p buf)
  15554. file
  15555. (y-or-n-p (format "Save file %s? " file)))
  15556. (with-current-buffer buf (save-buffer)))
  15557. (kill-buffer buf))))
  15558. (defun org-agenda-prepare-buffers (files)
  15559. "Create buffers for all agenda files, protect archived trees and comments."
  15560. (interactive)
  15561. (let ((pa '(:org-archived t))
  15562. (pc '(:org-comment t))
  15563. (pall '(:org-archived t :org-comment t))
  15564. (inhibit-read-only t)
  15565. (rea (concat ":" org-archive-tag ":"))
  15566. bmp file re)
  15567. (save-excursion
  15568. (save-restriction
  15569. (while (setq file (pop files))
  15570. (catch 'nextfile
  15571. (if (bufferp file)
  15572. (set-buffer file)
  15573. (org-check-agenda-file file)
  15574. (set-buffer (org-get-agenda-file-buffer file)))
  15575. (widen)
  15576. (setq bmp (buffer-modified-p))
  15577. (org-refresh-category-properties)
  15578. (setq org-todo-keywords-for-agenda
  15579. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15580. (setq org-done-keywords-for-agenda
  15581. (append org-done-keywords-for-agenda org-done-keywords))
  15582. (setq org-todo-keyword-alist-for-agenda
  15583. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15584. (setq org-drawers-for-agenda
  15585. (append org-drawers-for-agenda org-drawers))
  15586. (setq org-tag-alist-for-agenda
  15587. (append org-tag-alist-for-agenda org-tag-alist))
  15588. (save-excursion
  15589. (remove-text-properties (point-min) (point-max) pall)
  15590. (when org-agenda-skip-archived-trees
  15591. (goto-char (point-min))
  15592. (while (re-search-forward rea nil t)
  15593. (if (org-at-heading-p t)
  15594. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15595. (goto-char (point-min))
  15596. (setq re (format org-heading-keyword-regexp-format
  15597. org-comment-string))
  15598. (while (re-search-forward re nil t)
  15599. (add-text-properties
  15600. (match-beginning 0) (org-end-of-subtree t) pc)))
  15601. (set-buffer-modified-p bmp)))))
  15602. (setq org-todo-keywords-for-agenda
  15603. (org-uniquify org-todo-keywords-for-agenda))
  15604. (setq org-todo-keyword-alist-for-agenda
  15605. (org-uniquify org-todo-keyword-alist-for-agenda)
  15606. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15607. ;;;; Embedded LaTeX
  15608. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15609. "Keymap for the minor `org-cdlatex-mode'.")
  15610. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15611. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15612. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15613. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15614. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15615. (defvar org-cdlatex-texmathp-advice-is-done nil
  15616. "Flag remembering if we have applied the advice to texmathp already.")
  15617. (define-minor-mode org-cdlatex-mode
  15618. "Toggle the minor `org-cdlatex-mode'.
  15619. This mode supports entering LaTeX environment and math in LaTeX fragments
  15620. in Org-mode.
  15621. \\{org-cdlatex-mode-map}"
  15622. nil " OCDL" nil
  15623. (when org-cdlatex-mode
  15624. (require 'cdlatex)
  15625. (run-hooks 'cdlatex-mode-hook)
  15626. (cdlatex-compute-tables))
  15627. (unless org-cdlatex-texmathp-advice-is-done
  15628. (setq org-cdlatex-texmathp-advice-is-done t)
  15629. (defadvice texmathp (around org-math-always-on activate)
  15630. "Always return t in org-mode buffers.
  15631. This is because we want to insert math symbols without dollars even outside
  15632. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15633. an embedded LaTeX fragment, let texmathp do its job.
  15634. \\[org-cdlatex-mode-map]"
  15635. (interactive)
  15636. (let (p)
  15637. (cond
  15638. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15639. ((eq this-command 'cdlatex-math-symbol)
  15640. (setq ad-return-value t
  15641. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15642. (t
  15643. (let ((p (org-inside-LaTeX-fragment-p)))
  15644. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15645. (setq ad-return-value t
  15646. texmathp-why '("Org-mode embedded math" . 0))
  15647. (if p ad-do-it)))))))))
  15648. (defun turn-on-org-cdlatex ()
  15649. "Unconditionally turn on `org-cdlatex-mode'."
  15650. (org-cdlatex-mode 1))
  15651. (defun org-inside-LaTeX-fragment-p ()
  15652. "Test if point is inside a LaTeX fragment.
  15653. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15654. sequence appearing also before point.
  15655. Even though the matchers for math are configurable, this function assumes
  15656. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15657. delimiters are skipped when they have been removed by customization.
  15658. The return value is nil, or a cons cell with the delimiter and the
  15659. position of this delimiter.
  15660. This function does a reasonably good job, but can locally be fooled by
  15661. for example currency specifications. For example it will assume being in
  15662. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15663. fragments that are properly closed, but during editing, we have to live
  15664. with the uncertainty caused by missing closing delimiters. This function
  15665. looks only before point, not after."
  15666. (catch 'exit
  15667. (let ((pos (point))
  15668. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15669. (lim (progn
  15670. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15671. (point)))
  15672. dd-on str (start 0) m re)
  15673. (goto-char pos)
  15674. (when dodollar
  15675. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15676. re (nth 1 (assoc "$" org-latex-regexps)))
  15677. (while (string-match re str start)
  15678. (cond
  15679. ((= (match-end 0) (length str))
  15680. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15681. ((= (match-end 0) (- (length str) 5))
  15682. (throw 'exit nil))
  15683. (t (setq start (match-end 0))))))
  15684. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15685. (goto-char pos)
  15686. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15687. (and (match-beginning 2) (throw 'exit nil))
  15688. ;; count $$
  15689. (while (re-search-backward "\\$\\$" lim t)
  15690. (setq dd-on (not dd-on)))
  15691. (goto-char pos)
  15692. (if dd-on (cons "$$" m))))))
  15693. (defun org-inside-latex-macro-p ()
  15694. "Is point inside a LaTeX macro or its arguments?"
  15695. (save-match-data
  15696. (org-in-regexp
  15697. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15698. (defun org-try-cdlatex-tab ()
  15699. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15700. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15701. - inside a LaTeX fragment, or
  15702. - after the first word in a line, where an abbreviation expansion could
  15703. insert a LaTeX environment."
  15704. (when org-cdlatex-mode
  15705. (cond
  15706. ;; Before any word on the line: No expansion possible.
  15707. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15708. ;; Just after first word on the line: Expand it. Make sure it
  15709. ;; cannot happen on headlines, though.
  15710. ((save-excursion
  15711. (skip-chars-backward "a-zA-Z0-9*")
  15712. (skip-chars-backward " \t")
  15713. (and (bolp) (not (org-at-heading-p))))
  15714. (cdlatex-tab) t)
  15715. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15716. (defun org-cdlatex-underscore-caret (&optional arg)
  15717. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15718. Revert to the normal definition outside of these fragments."
  15719. (interactive "P")
  15720. (if (org-inside-LaTeX-fragment-p)
  15721. (call-interactively 'cdlatex-sub-superscript)
  15722. (let (org-cdlatex-mode)
  15723. (call-interactively (key-binding (vector last-input-event))))))
  15724. (defun org-cdlatex-math-modify (&optional arg)
  15725. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15726. Revert to the normal definition outside of these fragments."
  15727. (interactive "P")
  15728. (if (org-inside-LaTeX-fragment-p)
  15729. (call-interactively 'cdlatex-math-modify)
  15730. (let (org-cdlatex-mode)
  15731. (call-interactively (key-binding (vector last-input-event))))))
  15732. (defvar org-latex-fragment-image-overlays nil
  15733. "List of overlays carrying the images of latex fragments.")
  15734. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15735. (defun org-remove-latex-fragment-image-overlays ()
  15736. "Remove all overlays with LaTeX fragment images in current buffer."
  15737. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15738. (setq org-latex-fragment-image-overlays nil))
  15739. (defun org-preview-latex-fragment (&optional subtree)
  15740. "Preview the LaTeX fragment at point, or all locally or globally.
  15741. If the cursor is in a LaTeX fragment, create the image and overlay
  15742. it over the source code. If there is no fragment at point, display
  15743. all fragments in the current text, from one headline to the next. With
  15744. prefix SUBTREE, display all fragments in the current subtree. With a
  15745. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15746. the cursor is before the first headline,
  15747. display all fragments in the buffer.
  15748. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15749. (interactive "P")
  15750. (unless buffer-file-name
  15751. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15752. (org-remove-latex-fragment-image-overlays)
  15753. (save-excursion
  15754. (save-restriction
  15755. (let (beg end at msg)
  15756. (cond
  15757. ((or (equal subtree '(16))
  15758. (not (save-excursion
  15759. (re-search-backward org-outline-regexp-bol nil t))))
  15760. (setq beg (point-min) end (point-max)
  15761. msg "Creating images for buffer...%s"))
  15762. ((equal subtree '(4))
  15763. (org-back-to-heading)
  15764. (setq beg (point) end (org-end-of-subtree t)
  15765. msg "Creating images for subtree...%s"))
  15766. (t
  15767. (if (setq at (org-inside-LaTeX-fragment-p))
  15768. (goto-char (max (point-min) (- (cdr at) 2)))
  15769. (org-back-to-heading))
  15770. (setq beg (point) end (progn (outline-next-heading) (point))
  15771. msg (if at "Creating image...%s"
  15772. "Creating images for entry...%s"))))
  15773. (message msg "")
  15774. (narrow-to-region beg end)
  15775. (goto-char beg)
  15776. (org-format-latex
  15777. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15778. (file-name-nondirectory
  15779. buffer-file-name)))
  15780. default-directory 'overlays msg at 'forbuffer
  15781. org-latex-create-formula-image-program)
  15782. (message msg "done. Use `C-c C-c' to remove images.")))))
  15783. (defvar org-latex-regexps
  15784. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15785. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15786. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15787. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15788. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15789. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15790. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15791. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15792. "Regular expressions for matching embedded LaTeX.")
  15793. (defvar org-export-have-math nil) ;; dynamic scoping
  15794. (defun org-format-latex (prefix &optional dir overlays msg at
  15795. forbuffer processing-type)
  15796. "Replace LaTeX fragments with links to an image, and produce images.
  15797. Some of the options can be changed using the variable
  15798. `org-format-latex-options'."
  15799. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15800. (let* ((prefixnodir (file-name-nondirectory prefix))
  15801. (absprefix (expand-file-name prefix dir))
  15802. (todir (file-name-directory absprefix))
  15803. (opt org-format-latex-options)
  15804. (matchers (plist-get opt :matchers))
  15805. (re-list org-latex-regexps)
  15806. (org-format-latex-header-extra
  15807. (plist-get (org-infile-export-plist) :latex-header-extra))
  15808. (cnt 0) txt hash link beg end re e checkdir
  15809. executables-checked string
  15810. m n block-type block linkfile movefile ov)
  15811. ;; Check the different regular expressions
  15812. (while (setq e (pop re-list))
  15813. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15814. block (if block-type "\n\n" ""))
  15815. (when (member m matchers)
  15816. (goto-char (point-min))
  15817. (while (re-search-forward re nil t)
  15818. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15819. (not (get-text-property (match-beginning n)
  15820. 'org-protected))
  15821. (or (not overlays)
  15822. (not (eq (get-char-property (match-beginning n)
  15823. 'org-overlay-type)
  15824. 'org-latex-overlay))))
  15825. (setq org-export-have-math t)
  15826. (cond
  15827. ((eq processing-type 'verbatim)
  15828. ;; Leave the text verbatim, just protect it
  15829. (add-text-properties (match-beginning n) (match-end n)
  15830. '(org-protected t)))
  15831. ((eq processing-type 'mathjax)
  15832. ;; Prepare for MathJax processing
  15833. (setq string (match-string n))
  15834. (if (member m '("$" "$1"))
  15835. (save-excursion
  15836. (delete-region (match-beginning n) (match-end n))
  15837. (goto-char (match-beginning n))
  15838. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15839. "\\)")
  15840. '(org-protected t))))
  15841. (add-text-properties (match-beginning n) (match-end n)
  15842. '(org-protected t))))
  15843. ((or (eq processing-type 'dvipng)
  15844. (eq processing-type 'imagemagick))
  15845. ;; Process to an image
  15846. (setq txt (match-string n)
  15847. beg (match-beginning n) end (match-end n)
  15848. cnt (1+ cnt))
  15849. (let (print-length print-level) ; make sure full list is printed
  15850. (setq hash (sha1 (prin1-to-string
  15851. (list org-format-latex-header
  15852. org-format-latex-header-extra
  15853. org-export-latex-default-packages-alist
  15854. org-export-latex-packages-alist
  15855. org-format-latex-options
  15856. forbuffer txt)))
  15857. linkfile (format "%s_%s.png" prefix hash)
  15858. movefile (format "%s_%s.png" absprefix hash)))
  15859. (setq link (concat block "[[file:" linkfile "]]" block))
  15860. (if msg (message msg cnt))
  15861. (goto-char beg)
  15862. (unless checkdir ; make sure the directory exists
  15863. (setq checkdir t)
  15864. (or (file-directory-p todir) (make-directory todir t)))
  15865. (cond
  15866. ((eq processing-type 'dvipng)
  15867. (unless executables-checked
  15868. (org-check-external-command
  15869. "latex" "needed to convert LaTeX fragments to images")
  15870. (org-check-external-command
  15871. "dvipng" "needed to convert LaTeX fragments to images")
  15872. (setq executables-checked t))
  15873. (unless (file-exists-p movefile)
  15874. (org-create-formula-image-with-dvipng
  15875. txt movefile opt forbuffer)))
  15876. ((eq processing-type 'imagemagick)
  15877. (unless executables-checked
  15878. (org-check-external-command
  15879. "convert" "you need to install imagemagick")
  15880. (setq executables-checked t))
  15881. (unless (file-exists-p movefile)
  15882. (org-create-formula-image-with-imagemagick
  15883. txt movefile opt forbuffer))))
  15884. (if overlays
  15885. (progn
  15886. (mapc (lambda (o)
  15887. (if (eq (overlay-get o 'org-overlay-type)
  15888. 'org-latex-overlay)
  15889. (delete-overlay o)))
  15890. (overlays-in beg end))
  15891. (setq ov (make-overlay beg end))
  15892. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15893. (if (featurep 'xemacs)
  15894. (progn
  15895. (overlay-put ov 'invisible t)
  15896. (overlay-put
  15897. ov 'end-glyph
  15898. (make-glyph (vector 'png :file movefile))))
  15899. (overlay-put
  15900. ov 'display
  15901. (list 'image :type 'png :file movefile :ascent 'center)))
  15902. (push ov org-latex-fragment-image-overlays)
  15903. (goto-char end))
  15904. (delete-region beg end)
  15905. (insert (org-add-props link
  15906. (list 'org-latex-src
  15907. (replace-regexp-in-string
  15908. "\"" "" txt)
  15909. 'org-latex-src-embed-type
  15910. (if block-type 'paragraph 'character))))))
  15911. ((eq processing-type 'mathml)
  15912. ;; Process to MathML
  15913. (unless executables-checked
  15914. (unless (save-match-data (org-format-latex-mathml-available-p))
  15915. (error "LaTeX to MathML converter not configured"))
  15916. (setq executables-checked t))
  15917. (setq txt (match-string n)
  15918. beg (match-beginning n) end (match-end n)
  15919. cnt (1+ cnt))
  15920. (if msg (message msg cnt))
  15921. (goto-char beg)
  15922. (delete-region beg end)
  15923. (insert (org-format-latex-as-mathml
  15924. txt block-type prefix dir)))
  15925. (t
  15926. (error "Unknown conversion type %s for latex fragments"
  15927. processing-type)))))))))
  15928. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15929. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15930. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15931. sucessful, return the portion between \"<math...> </math>\"
  15932. elements otherwise return nil. When MATHML-FILE is specified,
  15933. write the results in to that file. When invoked as an
  15934. interactive command, prompt for LATEX-FRAG, with initial value
  15935. set to the current active region and echo the results for user
  15936. inspection."
  15937. (interactive (list (let ((frag (when (org-region-active-p)
  15938. (buffer-substring-no-properties
  15939. (region-beginning) (region-end)))))
  15940. (read-string "LaTeX Fragment: " frag nil frag))))
  15941. (unless latex-frag (error "Invalid latex-frag"))
  15942. (let* ((tmp-in-file (file-relative-name
  15943. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15944. (ignore (write-region latex-frag nil tmp-in-file))
  15945. (tmp-out-file (file-relative-name
  15946. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15947. (cmd (format-spec
  15948. org-latex-to-mathml-convert-command
  15949. `((?j . ,(shell-quote-argument
  15950. (expand-file-name org-latex-to-mathml-jar-file)))
  15951. (?I . ,(shell-quote-argument tmp-in-file))
  15952. (?o . ,(shell-quote-argument tmp-out-file)))))
  15953. mathml shell-command-output)
  15954. (when (org-called-interactively-p 'any)
  15955. (unless (org-format-latex-mathml-available-p)
  15956. (error "LaTeX to MathML converter not configured")))
  15957. (message "Running %s" cmd)
  15958. (setq shell-command-output (shell-command-to-string cmd))
  15959. (setq mathml
  15960. (when (file-readable-p tmp-out-file)
  15961. (with-current-buffer (find-file-noselect tmp-out-file t)
  15962. (goto-char (point-min))
  15963. (when (re-search-forward
  15964. (concat
  15965. (regexp-quote
  15966. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15967. "\\(.\\|\n\\)*"
  15968. (regexp-quote "</math>")) nil t)
  15969. (prog1 (match-string 0) (kill-buffer))))))
  15970. (cond
  15971. (mathml
  15972. (setq mathml
  15973. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15974. (when mathml-file
  15975. (write-region mathml nil mathml-file))
  15976. (when (org-called-interactively-p 'any)
  15977. (message mathml)))
  15978. ((message "LaTeX to MathML conversion failed")
  15979. (message shell-command-output)))
  15980. (delete-file tmp-in-file)
  15981. (when (file-exists-p tmp-out-file)
  15982. (delete-file tmp-out-file))
  15983. mathml))
  15984. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15985. prefix &optional dir)
  15986. "Use `org-create-math-formula' but check local cache first."
  15987. (let* ((absprefix (expand-file-name prefix dir))
  15988. (print-length nil) (print-level nil)
  15989. (formula-id (concat
  15990. "formula-"
  15991. (sha1
  15992. (prin1-to-string
  15993. (list latex-frag
  15994. org-latex-to-mathml-convert-command)))))
  15995. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15996. (formula-cache-dir (file-name-directory formula-cache)))
  15997. (unless (file-directory-p formula-cache-dir)
  15998. (make-directory formula-cache-dir t))
  15999. (unless (file-exists-p formula-cache)
  16000. (org-create-math-formula latex-frag formula-cache))
  16001. (if (file-exists-p formula-cache)
  16002. ;; Successful conversion. Return the link to MathML file.
  16003. (org-add-props
  16004. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16005. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16006. 'org-latex-src-embed-type (if latex-frag-type
  16007. 'paragraph 'character)))
  16008. ;; Failed conversion. Return the LaTeX fragment verbatim
  16009. (add-text-properties
  16010. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16011. latex-frag)))
  16012. ;; This function borrows from Ganesh Swami's latex2png.el
  16013. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16014. "This calls dvipng."
  16015. (require 'org-latex)
  16016. (let* ((tmpdir (if (featurep 'xemacs)
  16017. (temp-directory)
  16018. temporary-file-directory))
  16019. (texfilebase (make-temp-name
  16020. (expand-file-name "orgtex" tmpdir)))
  16021. (texfile (concat texfilebase ".tex"))
  16022. (dvifile (concat texfilebase ".dvi"))
  16023. (pngfile (concat texfilebase ".png"))
  16024. (fnh (if (featurep 'xemacs)
  16025. (font-height (face-font 'default))
  16026. (face-attribute 'default :height nil)))
  16027. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16028. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16029. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16030. "Black"))
  16031. (bg (or (plist-get options (if buffer :background :html-background))
  16032. "Transparent")))
  16033. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  16034. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  16035. (with-temp-file texfile
  16036. (insert (org-splice-latex-header
  16037. org-format-latex-header
  16038. org-export-latex-default-packages-alist
  16039. org-export-latex-packages-alist t
  16040. org-format-latex-header-extra))
  16041. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16042. (require 'org-latex)
  16043. (org-export-latex-fix-inputenc))
  16044. (let ((dir default-directory))
  16045. (condition-case nil
  16046. (progn
  16047. (cd tmpdir)
  16048. (call-process "latex" nil nil nil texfile))
  16049. (error nil))
  16050. (cd dir))
  16051. (if (not (file-exists-p dvifile))
  16052. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16053. (condition-case nil
  16054. (if (featurep 'xemacs)
  16055. (call-process "dvipng" nil nil nil
  16056. "-fg" fg "-bg" bg
  16057. "-T" "tight"
  16058. "-o" pngfile
  16059. dvifile)
  16060. (call-process "dvipng" nil nil nil
  16061. "-fg" fg "-bg" bg
  16062. "-D" dpi
  16063. ;;"-x" scale "-y" scale
  16064. "-T" "tight"
  16065. "-o" pngfile
  16066. dvifile))
  16067. (error nil))
  16068. (if (not (file-exists-p pngfile))
  16069. (if org-format-latex-signal-error
  16070. (error "Failed to create png file from %s" texfile)
  16071. (message "Failed to create png file from %s" texfile)
  16072. nil)
  16073. ;; Use the requested file name and clean up
  16074. (copy-file pngfile tofile 'replace)
  16075. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16076. (if (file-exists-p (concat texfilebase e))
  16077. (delete-file (concat texfilebase e))))
  16078. pngfile))))
  16079. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16080. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16081. "This calls convert, which is included into imagemagick."
  16082. (require 'org-latex)
  16083. (let* ((tmpdir (if (featurep 'xemacs)
  16084. (temp-directory)
  16085. temporary-file-directory))
  16086. (texfilebase (make-temp-name
  16087. (expand-file-name "orgtex" tmpdir)))
  16088. (texfile (concat texfilebase ".tex"))
  16089. (pdffile (concat texfilebase ".pdf"))
  16090. (pngfile (concat texfilebase ".png"))
  16091. (fnh (if (featurep 'xemacs)
  16092. (font-height (face-font 'default))
  16093. (face-attribute 'default :height nil)))
  16094. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16095. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16096. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16097. "black"))
  16098. (bg (or (plist-get options (if buffer :background :html-background))
  16099. "white")))
  16100. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16101. (setq fg (org-latex-color-format fg)))
  16102. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16103. (setq bg (org-latex-color-format
  16104. (if (string= bg "Transparent")(setq bg "white")))))
  16105. (with-temp-file texfile
  16106. (insert (org-splice-latex-header
  16107. org-format-latex-header
  16108. org-export-latex-default-packages-alist
  16109. org-export-latex-packages-alist t
  16110. org-format-latex-header-extra))
  16111. (insert "\n\\begin{document}\n"
  16112. "\\definecolor{fg}{rgb}{" fg "}\n"
  16113. "\\definecolor{bg}{rgb}{" bg "}\n"
  16114. "\n\\pagecolor{bg}\n"
  16115. "\n{\\color{fg}\n"
  16116. string
  16117. "\n}\n"
  16118. "\n\\end{document}\n" )
  16119. (require 'org-latex)
  16120. (org-export-latex-fix-inputenc))
  16121. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16122. (condition-case nil
  16123. (progn
  16124. (cd tmpdir)
  16125. (setq cmds org-latex-to-pdf-process)
  16126. (while cmds
  16127. (setq latex-frags-cmds (pop cmds))
  16128. (if (listp latex-frags-cmds)
  16129. (setq cmds nil)
  16130. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16131. (while latex-frags-cmds
  16132. (setq cmd (pop latex-frags-cmds))
  16133. (while (string-match "%b" cmd)
  16134. (setq cmd (replace-match
  16135. (save-match-data
  16136. (shell-quote-argument texfile))
  16137. t t cmd)))
  16138. (while (string-match "%f" cmd)
  16139. (setq cmd (replace-match
  16140. (save-match-data
  16141. (shell-quote-argument (file-name-nondirectory texfile)))
  16142. t t cmd)))
  16143. (while (string-match "%o" cmd)
  16144. (setq cmd (replace-match
  16145. (save-match-data
  16146. (shell-quote-argument (file-name-directory texfile)))
  16147. t t cmd)))
  16148. (setq cmd (split-string cmd))
  16149. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16150. (error nil))
  16151. (cd dir))
  16152. (if (not (file-exists-p pdffile))
  16153. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16154. (condition-case nil
  16155. (if (featurep 'xemacs)
  16156. (call-process "convert" nil nil nil
  16157. "-density" "96"
  16158. "-trim"
  16159. "-antialias"
  16160. pdffile
  16161. "-quality" "100"
  16162. ;; "-sharpen" "0x1.0"
  16163. pngfile)
  16164. (call-process "convert" nil nil nil
  16165. "-density" dpi
  16166. "-trim"
  16167. "-antialias"
  16168. pdffile
  16169. "-quality" "100"
  16170. ; "-sharpen" "0x1.0"
  16171. pngfile))
  16172. (error nil))
  16173. (if (not (file-exists-p pngfile))
  16174. (if org-format-latex-signal-error
  16175. (error "Failed to create png file from %s" texfile)
  16176. (message "Failed to create png file from %s" texfile)
  16177. nil)
  16178. ;; Use the requested file name and clean up
  16179. (copy-file pngfile tofile 'replace)
  16180. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16181. (if (file-exists-p (concat texfilebase e))
  16182. (delete-file (concat texfilebase e))))
  16183. pngfile))))
  16184. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16185. "Fill a LaTeX header template TPL.
  16186. In the template, the following place holders will be recognized:
  16187. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16188. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16189. [PACKAGES] \\usepackage statements for PKG
  16190. [NO-PACKAGES] do not include PKG
  16191. [EXTRA] the string EXTRA
  16192. [NO-EXTRA] do not include EXTRA
  16193. For backward compatibility, if both the positive and the negative place
  16194. holder is missing, the positive one (without the \"NO-\") will be
  16195. assumed to be present at the end of the template.
  16196. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16197. EXTRA is a string.
  16198. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16199. (let (rpl (end ""))
  16200. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16201. (setq rpl (if (or (match-end 1) (not def-pkg))
  16202. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16203. tpl (replace-match rpl t t tpl))
  16204. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16205. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16206. (setq rpl (if (or (match-end 1) (not pkg))
  16207. "" (org-latex-packages-to-string pkg snippets-p t))
  16208. tpl (replace-match rpl t t tpl))
  16209. (if pkg (setq end
  16210. (concat end "\n"
  16211. (org-latex-packages-to-string pkg snippets-p)))))
  16212. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16213. (setq rpl (if (or (match-end 1) (not extra))
  16214. "" (concat extra "\n"))
  16215. tpl (replace-match rpl t t tpl))
  16216. (if (and extra (string-match "\\S-" extra))
  16217. (setq end (concat end "\n" extra))))
  16218. (if (string-match "\\S-" end)
  16219. (concat tpl "\n" end)
  16220. tpl)))
  16221. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16222. "Turn an alist of packages into a string with the \\usepackage macros."
  16223. (setq pkg (mapconcat (lambda(p)
  16224. (cond
  16225. ((stringp p) p)
  16226. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16227. (format "%% Package %s omitted" (cadr p)))
  16228. ((equal "" (car p))
  16229. (format "\\usepackage{%s}" (cadr p)))
  16230. (t
  16231. (format "\\usepackage[%s]{%s}"
  16232. (car p) (cadr p)))))
  16233. pkg
  16234. "\n"))
  16235. (if newline (concat pkg "\n") pkg))
  16236. (defun org-dvipng-color (attr)
  16237. "Return a RGB color specification for dvipng."
  16238. (apply 'format "rgb %s %s %s"
  16239. (mapcar 'org-normalize-color
  16240. (if (featurep 'xemacs)
  16241. (color-rgb-components
  16242. (face-property 'default
  16243. (cond ((eq attr :foreground) 'foreground)
  16244. ((eq attr :background) 'background))))
  16245. (color-values (face-attribute 'default attr nil))))))
  16246. (defun org-latex-color (attr)
  16247. "Return a RGB color for the LaTeX color package."
  16248. (apply 'format "%s,%s,%s"
  16249. (mapcar 'org-normalize-color
  16250. (if (featurep 'xemacs)
  16251. (color-rgb-components
  16252. (face-property 'default
  16253. (cond ((eq attr :foreground) 'foreground)
  16254. ((eq attr :background) 'background))))
  16255. (color-values (face-attribute 'default attr nil))))))
  16256. (defun org-latex-color-format (color-name)
  16257. "Convert COLOR-NAME to a RGB color value."
  16258. (apply 'format "%s,%s,%s"
  16259. (mapcar 'org-normalize-color
  16260. (color-values color-name))))
  16261. (defun org-normalize-color (value)
  16262. "Return string to be used as color value for an RGB component."
  16263. (format "%g" (/ value 65535.0)))
  16264. ;; Image display
  16265. (defvar org-inline-image-overlays nil)
  16266. (make-variable-buffer-local 'org-inline-image-overlays)
  16267. (defun org-toggle-inline-images (&optional include-linked)
  16268. "Toggle the display of inline images.
  16269. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16270. (interactive "P")
  16271. (if org-inline-image-overlays
  16272. (progn
  16273. (org-remove-inline-images)
  16274. (message "Inline image display turned off"))
  16275. (org-display-inline-images include-linked)
  16276. (if org-inline-image-overlays
  16277. (message "%d images displayed inline"
  16278. (length org-inline-image-overlays))
  16279. (message "No images to display inline"))))
  16280. (defun org-redisplay-inline-images ()
  16281. "Refresh the display of inline images."
  16282. (interactive)
  16283. (if (not org-inline-image-overlays)
  16284. (org-toggle-inline-images)
  16285. (org-toggle-inline-images)
  16286. (org-toggle-inline-images)))
  16287. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16288. "Display inline images.
  16289. Normally only links without a description part are inlined, because this
  16290. is how it will work for export. When INCLUDE-LINKED is set, also links
  16291. with a description part will be inlined. This can be nice for a quick
  16292. look at those images, but it does not reflect what exported files will look
  16293. like.
  16294. When REFRESH is set, refresh existing images between BEG and END.
  16295. This will create new image displays only if necessary.
  16296. BEG and END default to the buffer boundaries."
  16297. (interactive "P")
  16298. (unless refresh
  16299. (org-remove-inline-images)
  16300. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16301. (save-excursion
  16302. (save-restriction
  16303. (widen)
  16304. (setq beg (or beg (point-min)) end (or end (point-max)))
  16305. (goto-char beg)
  16306. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16307. (substring (org-image-file-name-regexp) 0 -2)
  16308. "\\)\\]" (if include-linked "" "\\]")))
  16309. old file ov img)
  16310. (while (re-search-forward re end t)
  16311. (setq old (get-char-property-and-overlay (match-beginning 1)
  16312. 'org-image-overlay))
  16313. (setq file (expand-file-name
  16314. (concat (or (match-string 3) "") (match-string 4))))
  16315. (when (file-exists-p file)
  16316. (if (and (car-safe old) refresh)
  16317. (image-refresh (overlay-get (cdr old) 'display))
  16318. (setq img (save-match-data (create-image file)))
  16319. (when img
  16320. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16321. (overlay-put ov 'display img)
  16322. (overlay-put ov 'face 'default)
  16323. (overlay-put ov 'org-image-overlay t)
  16324. (overlay-put ov 'modification-hooks
  16325. (list 'org-display-inline-remove-overlay))
  16326. (push ov org-inline-image-overlays)))))))))
  16327. (define-obsolete-function-alias
  16328. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16329. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16330. "Remove inline-display overlay if a corresponding region is modified."
  16331. (let ((inhibit-modification-hooks t))
  16332. (when (and ov after)
  16333. (delete ov org-inline-image-overlays)
  16334. (delete-overlay ov))))
  16335. (defun org-remove-inline-images ()
  16336. "Remove inline display of images."
  16337. (interactive)
  16338. (mapc 'delete-overlay org-inline-image-overlays)
  16339. (setq org-inline-image-overlays nil))
  16340. ;;;; Key bindings
  16341. ;; Outline functions from `outline-mode-prefix-map'
  16342. ;; that can be remapped in Org:
  16343. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16344. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16345. (define-key org-mode-map [remap outline-forward-same-level]
  16346. 'org-forward-heading-same-level)
  16347. (define-key org-mode-map [remap outline-backward-same-level]
  16348. 'org-backward-heading-same-level)
  16349. (define-key org-mode-map [remap show-branches]
  16350. 'org-kill-note-or-show-branches)
  16351. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16352. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16353. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16354. ;; Outline functions from `outline-mode-prefix-map' that can not
  16355. ;; be remapped in Org:
  16356. ;;
  16357. ;; - the column "key binding" shows whether the Outline function is still
  16358. ;; available in Org mode on the same key that it has been bound to in
  16359. ;; Outline mode:
  16360. ;; - "overridden": key used for a different functionality in Org mode
  16361. ;; - else: key still bound to the same Outline function in Org mode
  16362. ;;
  16363. ;; | Outline function | key binding | Org replacement |
  16364. ;; |------------------------------------+-------------+-----------------------|
  16365. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16366. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16367. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16368. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16369. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16370. ;; | `show-entry' | overridden | no replacement |
  16371. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16372. ;; | `show-branches' | `C-c C-k' | still same function |
  16373. ;; | `show-subtree' | overridden | visibility cycling |
  16374. ;; | `show-all' | overridden | no replacement |
  16375. ;; | `hide-subtree' | overridden | visibility cycling |
  16376. ;; | `hide-body' | overridden | no replacement |
  16377. ;; | `hide-entry' | overridden | visibility cycling |
  16378. ;; | `hide-leaves' | overridden | no replacement |
  16379. ;; | `hide-sublevels' | overridden | no replacement |
  16380. ;; | `hide-other' | overridden | no replacement |
  16381. ;; Make `C-c C-x' a prefix key
  16382. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16383. ;; TAB key with modifiers
  16384. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16385. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16386. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16387. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16388. ;; The following line is necessary under Suse GNU/Linux
  16389. (unless (featurep 'xemacs)
  16390. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16391. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16392. (define-key org-mode-map [backtab] 'org-shifttab)
  16393. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16394. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16395. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16396. ;; Cursor keys with modifiers
  16397. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16398. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16399. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16400. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16401. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16402. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16403. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16404. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16405. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16406. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16407. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16408. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16409. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16410. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16411. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16412. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16413. ;; Babel keys
  16414. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16415. (mapc (lambda (pair)
  16416. (define-key org-babel-map (car pair) (cdr pair)))
  16417. org-babel-key-bindings)
  16418. ;;; Extra keys for tty access.
  16419. ;; We only set them when really needed because otherwise the
  16420. ;; menus don't show the simple keys
  16421. (when (or org-use-extra-keys
  16422. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16423. (not window-system))
  16424. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16425. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16426. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16427. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16428. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16429. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16430. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16431. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16432. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16433. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16434. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16435. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16436. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16437. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16438. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16439. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16440. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16441. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16442. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16443. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16444. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16445. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16446. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16447. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16448. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16449. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16450. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16451. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16452. ;; All the other keys
  16453. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16454. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16455. (if (boundp 'narrow-map)
  16456. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16457. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16458. (if (boundp 'narrow-map)
  16459. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16460. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16461. (if (boundp 'narrow-map)
  16462. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16463. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16464. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16465. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16466. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16467. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16468. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16469. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16470. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16471. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16472. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16473. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16474. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16475. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16476. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16477. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16478. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16479. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16480. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16481. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16482. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16483. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16484. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16485. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16486. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16487. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16488. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16489. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16490. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16491. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16492. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16493. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16494. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16495. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16496. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16497. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16498. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16499. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16500. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16501. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16502. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16503. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16504. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16505. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16506. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16507. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16508. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16509. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16510. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16511. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16512. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16513. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16514. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16515. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16516. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16517. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16518. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16519. (org-defkey org-mode-map "\C-m" 'org-return)
  16520. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16521. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16522. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16523. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16524. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16525. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16526. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16527. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16528. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16529. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16530. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16531. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16532. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16533. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16534. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16535. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16536. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16537. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16538. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16539. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16540. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16541. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16542. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16543. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16544. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16545. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16546. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16547. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16548. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16549. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16550. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16551. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16552. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16553. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16554. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16555. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16556. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16557. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16558. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16559. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16560. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16561. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16562. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16563. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16564. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16565. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16566. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16567. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16568. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16569. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16570. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16571. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16572. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16573. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16574. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16575. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16576. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16577. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16578. (when (featurep 'xemacs)
  16579. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16580. (defconst org-speed-commands-default
  16581. '(
  16582. ("Outline Navigation")
  16583. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16584. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16585. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16586. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16587. ("u" . (org-speed-move-safe 'outline-up-heading))
  16588. ("j" . org-goto)
  16589. ("g" . (org-refile t))
  16590. ("Outline Visibility")
  16591. ("c" . org-cycle)
  16592. ("C" . org-shifttab)
  16593. (" " . org-display-outline-path)
  16594. ("=" . org-columns)
  16595. ("Outline Structure Editing")
  16596. ("U" . org-shiftmetaup)
  16597. ("D" . org-shiftmetadown)
  16598. ("r" . org-metaright)
  16599. ("l" . org-metaleft)
  16600. ("R" . org-shiftmetaright)
  16601. ("L" . org-shiftmetaleft)
  16602. ("i" . (progn (forward-char 1) (call-interactively
  16603. 'org-insert-heading-respect-content)))
  16604. ("^" . org-sort)
  16605. ("w" . org-refile)
  16606. ("a" . org-archive-subtree-default-with-confirmation)
  16607. ("." . org-mark-subtree)
  16608. ("#" . org-toggle-comment)
  16609. ("Clock Commands")
  16610. ("I" . org-clock-in)
  16611. ("O" . org-clock-out)
  16612. ("Meta Data Editing")
  16613. ("t" . org-todo)
  16614. ("," . (org-priority))
  16615. ("0" . (org-priority ?\ ))
  16616. ("1" . (org-priority ?A))
  16617. ("2" . (org-priority ?B))
  16618. ("3" . (org-priority ?C))
  16619. (":" . org-set-tags-command)
  16620. ("e" . org-set-effort)
  16621. ("E" . org-inc-effort)
  16622. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16623. (org-entry-put (point) "APPT_WARNTIME" m)))
  16624. ("Agenda Views etc")
  16625. ("v" . org-agenda)
  16626. ("/" . org-sparse-tree)
  16627. ("Misc")
  16628. ("o" . org-open-at-point)
  16629. ("?" . org-speed-command-help)
  16630. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16631. (">" . (org-agenda-remove-restriction-lock))
  16632. )
  16633. "The default speed commands.")
  16634. (defun org-print-speed-command (e)
  16635. (if (> (length (car e)) 1)
  16636. (progn
  16637. (princ "\n")
  16638. (princ (car e))
  16639. (princ "\n")
  16640. (princ (make-string (length (car e)) ?-))
  16641. (princ "\n"))
  16642. (princ (car e))
  16643. (princ " ")
  16644. (if (symbolp (cdr e))
  16645. (princ (symbol-name (cdr e)))
  16646. (prin1 (cdr e)))
  16647. (princ "\n")))
  16648. (defun org-speed-command-help ()
  16649. "Show the available speed commands."
  16650. (interactive)
  16651. (if (not org-use-speed-commands)
  16652. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16653. (with-output-to-temp-buffer "*Help*"
  16654. (princ "User-defined Speed commands\n===========================\n")
  16655. (mapc 'org-print-speed-command org-speed-commands-user)
  16656. (princ "\n")
  16657. (princ "Built-in Speed commands\n=======================\n")
  16658. (mapc 'org-print-speed-command org-speed-commands-default))
  16659. (with-current-buffer "*Help*"
  16660. (setq truncate-lines t))))
  16661. (defun org-speed-move-safe (cmd)
  16662. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16663. If not, return to the original position and throw an error."
  16664. (interactive)
  16665. (let ((pos (point)))
  16666. (call-interactively cmd)
  16667. (unless (and (bolp) (org-at-heading-p))
  16668. (goto-char pos)
  16669. (error "Boundary reached while executing %s" cmd))))
  16670. (defvar org-self-insert-command-undo-counter 0)
  16671. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16672. (defvar org-speed-command nil)
  16673. (define-obsolete-function-alias
  16674. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  16675. (defun org-speed-command-activate (keys)
  16676. "Hook for activating single-letter speed commands.
  16677. `org-speed-commands-default' specifies a minimal command set.
  16678. Use `org-speed-commands-user' for further customization."
  16679. (when (or (and (bolp) (looking-at org-outline-regexp))
  16680. (and (functionp org-use-speed-commands)
  16681. (funcall org-use-speed-commands)))
  16682. (cdr (assoc keys (append org-speed-commands-user
  16683. org-speed-commands-default)))))
  16684. (define-obsolete-function-alias
  16685. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  16686. (defun org-babel-speed-command-activate (keys)
  16687. "Hook for activating single-letter code block commands."
  16688. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16689. (cdr (assoc keys org-babel-key-bindings))))
  16690. (defcustom org-speed-command-hook
  16691. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16692. "Hook for activating speed commands at strategic locations.
  16693. Hook functions are called in sequence until a valid handler is
  16694. found.
  16695. Each hook takes a single argument, a user-pressed command key
  16696. which is also a `self-insert-command' from the global map.
  16697. Within the hook, examine the cursor position and the command key
  16698. and return nil or a valid handler as appropriate. Handler could
  16699. be one of an interactive command, a function, or a form.
  16700. Set `org-use-speed-commands' to non-nil value to enable this
  16701. hook. The default setting is `org-speed-command-activate'."
  16702. :group 'org-structure
  16703. :version "24.1"
  16704. :type 'hook)
  16705. (defun org-self-insert-command (N)
  16706. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16707. If the cursor is in a table looking at whitespace, the whitespace is
  16708. overwritten, and the table is not marked as requiring realignment."
  16709. (interactive "p")
  16710. (org-check-before-invisible-edit 'insert)
  16711. (cond
  16712. ((and org-use-speed-commands
  16713. (setq org-speed-command
  16714. (run-hook-with-args-until-success
  16715. 'org-speed-command-hook (this-command-keys))))
  16716. (cond
  16717. ((commandp org-speed-command)
  16718. (setq this-command org-speed-command)
  16719. (call-interactively org-speed-command))
  16720. ((functionp org-speed-command)
  16721. (funcall org-speed-command))
  16722. ((and org-speed-command (listp org-speed-command))
  16723. (eval org-speed-command))
  16724. (t (let (org-use-speed-commands)
  16725. (call-interactively 'org-self-insert-command)))))
  16726. ((and
  16727. (org-table-p)
  16728. (progn
  16729. ;; check if we blank the field, and if that triggers align
  16730. (and (featurep 'org-table) org-table-auto-blank-field
  16731. (member last-command
  16732. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16733. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16734. ;; got extra space, this field does not determine column width
  16735. (let (org-table-may-need-update) (org-table-blank-field))
  16736. ;; no extra space, this field may determine column width
  16737. (org-table-blank-field)))
  16738. t)
  16739. (eq N 1)
  16740. (looking-at "[^|\n]* |"))
  16741. (let (org-table-may-need-update)
  16742. (goto-char (1- (match-end 0)))
  16743. (backward-delete-char 1)
  16744. (goto-char (match-beginning 0))
  16745. (self-insert-command N)))
  16746. (t
  16747. (setq org-table-may-need-update t)
  16748. (self-insert-command N)
  16749. (org-fix-tags-on-the-fly)
  16750. (if org-self-insert-cluster-for-undo
  16751. (if (not (eq last-command 'org-self-insert-command))
  16752. (setq org-self-insert-command-undo-counter 1)
  16753. (if (>= org-self-insert-command-undo-counter 20)
  16754. (setq org-self-insert-command-undo-counter 1)
  16755. (and (> org-self-insert-command-undo-counter 0)
  16756. buffer-undo-list (listp buffer-undo-list)
  16757. (not (cadr buffer-undo-list)) ; remove nil entry
  16758. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16759. (setq org-self-insert-command-undo-counter
  16760. (1+ org-self-insert-command-undo-counter))))))))
  16761. (defun org-check-before-invisible-edit (kind)
  16762. "Check is editing if kind KIND would be dangerous with invisible text around.
  16763. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16764. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16765. (if (and org-catch-invisible-edits
  16766. (or (not (boundp 'visible-mode)) (not visible-mode))
  16767. (or (get-char-property (point) 'invisible)
  16768. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16769. ;; OK, we need to take a closer look
  16770. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16771. (invisible-before-point (if (bobp) nil (get-char-property
  16772. (1- (point)) 'invisible)))
  16773. (border-and-ok-direction
  16774. (or
  16775. ;; Check if we are acting predictably before invisible text
  16776. (and invisible-at-point (not invisible-before-point)
  16777. (memq kind '(insert delete-backward)))
  16778. ;; Check if we are acting predictably after invisible text
  16779. ;; This works not well, and I have turned it off. It seems
  16780. ;; better to always show and stop after invisible text.
  16781. ;; (and (not invisible-at-point) invisible-before-point
  16782. ;; (memq kind '(insert delete)))
  16783. )))
  16784. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16785. (memq invisible-before-point '(outline org-hide-block t)))
  16786. (if (eq org-catch-invisible-edits 'error)
  16787. (error "Editing in invisible areas is prohibited - make visible first"))
  16788. (if (and org-custom-properties-overlays
  16789. (y-or-n-p "Display invisible properties in this buffer? "))
  16790. (org-toggle-custom-properties-visibility)
  16791. ;; Make the area visible
  16792. (save-excursion
  16793. (if invisible-before-point
  16794. (goto-char (previous-single-char-property-change
  16795. (point) 'invisible)))
  16796. (org-cycle))
  16797. (cond
  16798. ((eq org-catch-invisible-edits 'show)
  16799. ;; That's it, we do the edit after showing
  16800. (message
  16801. "Unfolding invisible region around point before editing")
  16802. (sit-for 1))
  16803. ((and (eq org-catch-invisible-edits 'smart)
  16804. border-and-ok-direction)
  16805. (message "Unfolding invisible region around point before editing"))
  16806. (t
  16807. ;; Don't do the edit, make the user repeat it in full visibility
  16808. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  16809. (defun org-fix-tags-on-the-fly ()
  16810. (when (and (equal (char-after (point-at-bol)) ?*)
  16811. (org-at-heading-p))
  16812. (org-align-tags-here org-tags-column)))
  16813. (defun org-delete-backward-char (N)
  16814. "Like `delete-backward-char', insert whitespace at field end in tables.
  16815. When deleting backwards, in tables this function will insert whitespace in
  16816. front of the next \"|\" separator, to keep the table aligned. The table will
  16817. still be marked for re-alignment if the field did fill the entire column,
  16818. because, in this case the deletion might narrow the column."
  16819. (interactive "p")
  16820. (save-match-data
  16821. (org-check-before-invisible-edit 'delete-backward)
  16822. (if (and (org-table-p)
  16823. (eq N 1)
  16824. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16825. (looking-at ".*?|"))
  16826. (let ((pos (point))
  16827. (noalign (looking-at "[^|\n\r]* |"))
  16828. (c org-table-may-need-update))
  16829. (backward-delete-char N)
  16830. (if (not overwrite-mode)
  16831. (progn
  16832. (skip-chars-forward "^|")
  16833. (insert " ")
  16834. (goto-char (1- pos))))
  16835. ;; noalign: if there were two spaces at the end, this field
  16836. ;; does not determine the width of the column.
  16837. (if noalign (setq org-table-may-need-update c)))
  16838. (backward-delete-char N)
  16839. (org-fix-tags-on-the-fly))))
  16840. (defun org-delete-char (N)
  16841. "Like `delete-char', but insert whitespace at field end in tables.
  16842. When deleting characters, in tables this function will insert whitespace in
  16843. front of the next \"|\" separator, to keep the table aligned. The table will
  16844. still be marked for re-alignment if the field did fill the entire column,
  16845. because, in this case the deletion might narrow the column."
  16846. (interactive "p")
  16847. (save-match-data
  16848. (org-check-before-invisible-edit 'delete)
  16849. (if (and (org-table-p)
  16850. (not (bolp))
  16851. (not (= (char-after) ?|))
  16852. (eq N 1))
  16853. (if (looking-at ".*?|")
  16854. (let ((pos (point))
  16855. (noalign (looking-at "[^|\n\r]* |"))
  16856. (c org-table-may-need-update))
  16857. (replace-match (concat
  16858. (substring (match-string 0) 1 -1)
  16859. " |"))
  16860. (goto-char pos)
  16861. ;; noalign: if there were two spaces at the end, this field
  16862. ;; does not determine the width of the column.
  16863. (if noalign (setq org-table-may-need-update c)))
  16864. (delete-char N))
  16865. (delete-char N)
  16866. (org-fix-tags-on-the-fly))))
  16867. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16868. (put 'org-self-insert-command 'delete-selection t)
  16869. (put 'orgtbl-self-insert-command 'delete-selection t)
  16870. (put 'org-delete-char 'delete-selection 'supersede)
  16871. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16872. (put 'org-yank 'delete-selection 'yank)
  16873. ;; Make `flyspell-mode' delay after some commands
  16874. (put 'org-self-insert-command 'flyspell-delayed t)
  16875. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16876. (put 'org-delete-char 'flyspell-delayed t)
  16877. (put 'org-delete-backward-char 'flyspell-delayed t)
  16878. ;; Make pabbrev-mode expand after org-mode commands
  16879. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16880. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16881. ;; How to do this: Measure non-white length of current string
  16882. ;; If equal to column width, we should realign.
  16883. (defun org-remap (map &rest commands)
  16884. "In MAP, remap the functions given in COMMANDS.
  16885. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16886. (let (new old)
  16887. (while commands
  16888. (setq old (pop commands) new (pop commands))
  16889. (if (fboundp 'command-remapping)
  16890. (org-defkey map (vector 'remap old) new)
  16891. (substitute-key-definition old new map global-map)))))
  16892. (when (eq org-enable-table-editor 'optimized)
  16893. ;; If the user wants maximum table support, we need to hijack
  16894. ;; some standard editing functions
  16895. (org-remap org-mode-map
  16896. 'self-insert-command 'org-self-insert-command
  16897. 'delete-char 'org-delete-char
  16898. 'delete-backward-char 'org-delete-backward-char)
  16899. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16900. (defvar org-ctrl-c-ctrl-c-hook nil
  16901. "Hook for functions attaching themselves to `C-c C-c'.
  16902. This can be used to add additional functionality to the C-c C-c
  16903. key which executes context-dependent commands. This hook is run
  16904. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16905. run after the last test.
  16906. Each function will be called with no arguments. The function
  16907. must check if the context is appropriate for it to act. If yes,
  16908. it should do its thing and then return a non-nil value. If the
  16909. context is wrong, just do nothing and return nil.")
  16910. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16911. "Hook for functions attaching themselves to `C-c C-c'.
  16912. This can be used to add additional functionality to the C-c C-c
  16913. key which executes context-dependent commands. This hook is run
  16914. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16915. before the first test.
  16916. Each function will be called with no arguments. The function
  16917. must check if the context is appropriate for it to act. If yes,
  16918. it should do its thing and then return a non-nil value. If the
  16919. context is wrong, just do nothing and return nil.")
  16920. (defvar org-tab-first-hook nil
  16921. "Hook for functions to attach themselves to TAB.
  16922. See `org-ctrl-c-ctrl-c-hook' for more information.
  16923. This hook runs as the first action when TAB is pressed, even before
  16924. `org-cycle' messes around with the `outline-regexp' to cater for
  16925. inline tasks and plain list item folding.
  16926. If any function in this hook returns t, any other actions that
  16927. would have been caused by TAB (such as table field motion or visibility
  16928. cycling) will not occur.")
  16929. (defvar org-tab-after-check-for-table-hook nil
  16930. "Hook for functions to attach themselves to TAB.
  16931. See `org-ctrl-c-ctrl-c-hook' for more information.
  16932. This hook runs after it has been established that the cursor is not in a
  16933. table, but before checking if the cursor is in a headline or if global cycling
  16934. should be done.
  16935. If any function in this hook returns t, not other actions like visibility
  16936. cycling will be done.")
  16937. (defvar org-tab-after-check-for-cycling-hook nil
  16938. "Hook for functions to attach themselves to TAB.
  16939. See `org-ctrl-c-ctrl-c-hook' for more information.
  16940. This hook runs after it has been established that not table field motion and
  16941. not visibility should be done because of current context. This is probably
  16942. the place where a package like yasnippets can hook in.")
  16943. (defvar org-tab-before-tab-emulation-hook nil
  16944. "Hook for functions to attach themselves to TAB.
  16945. See `org-ctrl-c-ctrl-c-hook' for more information.
  16946. This hook runs after every other options for TAB have been exhausted, but
  16947. before indentation and \t insertion takes place.")
  16948. (defvar org-metaleft-hook nil
  16949. "Hook for functions attaching themselves to `M-left'.
  16950. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16951. (defvar org-metaright-hook nil
  16952. "Hook for functions attaching themselves to `M-right'.
  16953. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16954. (defvar org-metaup-hook nil
  16955. "Hook for functions attaching themselves to `M-up'.
  16956. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16957. (defvar org-metadown-hook nil
  16958. "Hook for functions attaching themselves to `M-down'.
  16959. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16960. (defvar org-shiftmetaleft-hook nil
  16961. "Hook for functions attaching themselves to `M-S-left'.
  16962. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16963. (defvar org-shiftmetaright-hook nil
  16964. "Hook for functions attaching themselves to `M-S-right'.
  16965. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16966. (defvar org-shiftmetaup-hook nil
  16967. "Hook for functions attaching themselves to `M-S-up'.
  16968. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16969. (defvar org-shiftmetadown-hook nil
  16970. "Hook for functions attaching themselves to `M-S-down'.
  16971. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16972. (defvar org-metareturn-hook nil
  16973. "Hook for functions attaching themselves to `M-RET'.
  16974. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16975. (defvar org-shiftup-hook nil
  16976. "Hook for functions attaching themselves to `S-up'.
  16977. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16978. (defvar org-shiftup-final-hook nil
  16979. "Hook for functions attaching themselves to `S-up'.
  16980. This one runs after all other options except shift-select have been excluded.
  16981. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16982. (defvar org-shiftdown-hook nil
  16983. "Hook for functions attaching themselves to `S-down'.
  16984. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16985. (defvar org-shiftdown-final-hook nil
  16986. "Hook for functions attaching themselves to `S-down'.
  16987. This one runs after all other options except shift-select have been excluded.
  16988. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16989. (defvar org-shiftleft-hook nil
  16990. "Hook for functions attaching themselves to `S-left'.
  16991. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16992. (defvar org-shiftleft-final-hook nil
  16993. "Hook for functions attaching themselves to `S-left'.
  16994. This one runs after all other options except shift-select have been excluded.
  16995. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16996. (defvar org-shiftright-hook nil
  16997. "Hook for functions attaching themselves to `S-right'.
  16998. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16999. (defvar org-shiftright-final-hook nil
  17000. "Hook for functions attaching themselves to `S-right'.
  17001. This one runs after all other options except shift-select have been excluded.
  17002. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17003. (defun org-modifier-cursor-error ()
  17004. "Throw an error, a modified cursor command was applied in wrong context."
  17005. (error "This command is active in special context like tables, headlines or items"))
  17006. (defun org-shiftselect-error ()
  17007. "Throw an error because Shift-Cursor command was applied in wrong context."
  17008. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17009. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17010. (error "This command works only in special context like headlines or timestamps")))
  17011. (defun org-call-for-shift-select (cmd)
  17012. (let ((this-command-keys-shift-translated t))
  17013. (call-interactively cmd)))
  17014. (defun org-shifttab (&optional arg)
  17015. "Global visibility cycling or move to previous table field.
  17016. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17017. on context.
  17018. See the individual commands for more information."
  17019. (interactive "P")
  17020. (cond
  17021. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17022. ((integerp arg)
  17023. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17024. (message "Content view to level: %d" arg)
  17025. (org-content (prefix-numeric-value arg2))
  17026. (setq org-cycle-global-status 'overview)))
  17027. (t (call-interactively 'org-global-cycle))))
  17028. (defun org-shiftmetaleft ()
  17029. "Promote subtree or delete table column.
  17030. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17031. `org-table-delete-column', depending on context. See the
  17032. individual commands for more information."
  17033. (interactive)
  17034. (cond
  17035. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17036. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17037. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17038. ((if (not (org-region-active-p)) (org-at-item-p)
  17039. (save-excursion (goto-char (region-beginning))
  17040. (org-at-item-p)))
  17041. (call-interactively 'org-outdent-item-tree))
  17042. (t (org-modifier-cursor-error))))
  17043. (defun org-shiftmetaright ()
  17044. "Demote subtree or insert table column.
  17045. Calls `org-demote-subtree', `org-indent-item-tree', or
  17046. `org-table-insert-column', depending on context. See the
  17047. individual commands for more information."
  17048. (interactive)
  17049. (cond
  17050. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17051. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17052. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17053. ((if (not (org-region-active-p)) (org-at-item-p)
  17054. (save-excursion (goto-char (region-beginning))
  17055. (org-at-item-p)))
  17056. (call-interactively 'org-indent-item-tree))
  17057. (t (org-modifier-cursor-error))))
  17058. (defun org-shiftmetaup (&optional arg)
  17059. "Move subtree up or kill table row.
  17060. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17061. `org-move-item-up' or `org-timestamp-up', depending on context.
  17062. See the individual commands for more information."
  17063. (interactive "P")
  17064. (cond
  17065. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17066. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17067. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17068. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17069. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17070. (call-interactively 'org-timestamp-up)))
  17071. (t (org-modifier-cursor-error))))
  17072. (defun org-shiftmetadown (&optional arg)
  17073. "Move subtree down or insert table row.
  17074. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17075. `org-move-item-down' or `org-timestamp-up', depending on context.
  17076. See the individual commands for more information."
  17077. (interactive "P")
  17078. (cond
  17079. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17080. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17081. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17082. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17083. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17084. (call-interactively 'org-timestamp-down)))
  17085. (t (org-modifier-cursor-error))))
  17086. (defsubst org-hidden-tree-error ()
  17087. (error
  17088. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17089. (defun org-metaleft (&optional arg)
  17090. "Promote heading or move table column to left.
  17091. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17092. With no specific context, calls the Emacs default `backward-word'.
  17093. See the individual commands for more information."
  17094. (interactive "P")
  17095. (cond
  17096. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17097. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17098. ((org-with-limited-levels
  17099. (or (org-at-heading-p)
  17100. (and (org-region-active-p)
  17101. (save-excursion
  17102. (goto-char (region-beginning))
  17103. (org-at-heading-p)))))
  17104. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17105. (call-interactively 'org-do-promote))
  17106. ;; At an inline task.
  17107. ((org-at-heading-p)
  17108. (call-interactively 'org-inlinetask-promote))
  17109. ((or (org-at-item-p)
  17110. (and (org-region-active-p)
  17111. (save-excursion
  17112. (goto-char (region-beginning))
  17113. (org-at-item-p))))
  17114. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17115. (call-interactively 'org-outdent-item))
  17116. (t (call-interactively 'backward-word))))
  17117. (defun org-metaright (&optional arg)
  17118. "Demote a subtree, a list item or move table column to right.
  17119. In front of a drawer or a block keyword, indent it correctly.
  17120. With no specific context, calls the Emacs default `forward-word'.
  17121. See the individual commands for more information."
  17122. (interactive "P")
  17123. (cond
  17124. ((run-hook-with-args-until-success 'org-metaright-hook))
  17125. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17126. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17127. ((org-at-block-p) (call-interactively 'org-indent-block))
  17128. ((org-with-limited-levels
  17129. (or (org-at-heading-p)
  17130. (and (org-region-active-p)
  17131. (save-excursion
  17132. (goto-char (region-beginning))
  17133. (org-at-heading-p)))))
  17134. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17135. (call-interactively 'org-do-demote))
  17136. ;; At an inline task.
  17137. ((org-at-heading-p)
  17138. (call-interactively 'org-inlinetask-demote))
  17139. ((or (org-at-item-p)
  17140. (and (org-region-active-p)
  17141. (save-excursion
  17142. (goto-char (region-beginning))
  17143. (org-at-item-p))))
  17144. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17145. (call-interactively 'org-indent-item))
  17146. (t (call-interactively 'forward-word))))
  17147. (defun org-check-for-hidden (what)
  17148. "Check if there are hidden headlines/items in the current visual line.
  17149. WHAT can be either `headlines' or `items'. If the current line is
  17150. an outline or item heading and it has a folded subtree below it,
  17151. this function returns t, nil otherwise."
  17152. (let ((re (cond
  17153. ((eq what 'headlines) org-outline-regexp-bol)
  17154. ((eq what 'items) (org-item-beginning-re))
  17155. (t (error "This should not happen"))))
  17156. beg end)
  17157. (save-excursion
  17158. (catch 'exit
  17159. (unless (org-region-active-p)
  17160. (setq beg (point-at-bol))
  17161. (beginning-of-line 2)
  17162. (while (and (not (eobp)) ;; this is like `next-line'
  17163. (get-char-property (1- (point)) 'invisible))
  17164. (beginning-of-line 2))
  17165. (setq end (point))
  17166. (goto-char beg)
  17167. (goto-char (point-at-eol))
  17168. (setq end (max end (point)))
  17169. (while (re-search-forward re end t)
  17170. (if (get-char-property (match-beginning 0) 'invisible)
  17171. (throw 'exit t))))
  17172. nil))))
  17173. (autoload 'org-element-at-point "org-element")
  17174. (autoload 'org-element-type "org-element")
  17175. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  17176. (declare-function org-element-type "org-element" (element))
  17177. (declare-function org-element-contents "org-element" (element))
  17178. (declare-function org-element-property "org-element" (property element))
  17179. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  17180. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  17181. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  17182. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  17183. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  17184. (defun org-metaup (&optional arg)
  17185. "Move subtree up or move table row up.
  17186. Calls `org-move-subtree-up' or `org-table-move-row' or
  17187. `org-move-item-up', depending on context. See the individual commands
  17188. for more information."
  17189. (interactive "P")
  17190. (cond
  17191. ((run-hook-with-args-until-success 'org-metaup-hook))
  17192. ((org-region-active-p)
  17193. (let* ((a (min (region-beginning) (region-end)))
  17194. (b (1- (max (region-beginning) (region-end))))
  17195. (c (save-excursion (goto-char a)
  17196. (move-beginning-of-line 0)))
  17197. (d (save-excursion (goto-char a)
  17198. (move-end-of-line 0) (point))))
  17199. (transpose-regions a b c d)
  17200. (goto-char c)))
  17201. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17202. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17203. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17204. (t (org-drag-element-backward))))
  17205. (defun org-metadown (&optional arg)
  17206. "Move subtree down or move table row down.
  17207. Calls `org-move-subtree-down' or `org-table-move-row' or
  17208. `org-move-item-down', depending on context. See the individual
  17209. commands for more information."
  17210. (interactive "P")
  17211. (cond
  17212. ((run-hook-with-args-until-success 'org-metadown-hook))
  17213. ((org-region-active-p)
  17214. (let* ((a (min (region-beginning) (region-end)))
  17215. (b (max (region-beginning) (region-end)))
  17216. (c (save-excursion (goto-char b)
  17217. (move-beginning-of-line 1)))
  17218. (d (save-excursion (goto-char b)
  17219. (move-end-of-line 1) (1+ (point)))))
  17220. (transpose-regions a b c d)
  17221. (goto-char d)))
  17222. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17223. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17224. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17225. (t (org-drag-element-forward))))
  17226. (defun org-shiftup (&optional arg)
  17227. "Increase item in timestamp or increase priority of current headline.
  17228. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17229. depending on context. See the individual commands for more information."
  17230. (interactive "P")
  17231. (cond
  17232. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17233. ((and org-support-shift-select (org-region-active-p))
  17234. (org-call-for-shift-select 'previous-line))
  17235. ((org-at-timestamp-p t)
  17236. (call-interactively (if org-edit-timestamp-down-means-later
  17237. 'org-timestamp-down 'org-timestamp-up)))
  17238. ((and (not (eq org-support-shift-select 'always))
  17239. org-enable-priority-commands
  17240. (org-at-heading-p))
  17241. (call-interactively 'org-priority-up))
  17242. ((and (not org-support-shift-select) (org-at-item-p))
  17243. (call-interactively 'org-previous-item))
  17244. ((org-clocktable-try-shift 'up arg))
  17245. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17246. (org-support-shift-select
  17247. (org-call-for-shift-select 'previous-line))
  17248. (t (org-shiftselect-error))))
  17249. (defun org-shiftdown (&optional arg)
  17250. "Decrease item in timestamp or decrease priority of current headline.
  17251. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17252. depending on context. See the individual commands for more information."
  17253. (interactive "P")
  17254. (cond
  17255. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17256. ((and org-support-shift-select (org-region-active-p))
  17257. (org-call-for-shift-select 'next-line))
  17258. ((org-at-timestamp-p t)
  17259. (call-interactively (if org-edit-timestamp-down-means-later
  17260. 'org-timestamp-up 'org-timestamp-down)))
  17261. ((and (not (eq org-support-shift-select 'always))
  17262. org-enable-priority-commands
  17263. (org-at-heading-p))
  17264. (call-interactively 'org-priority-down))
  17265. ((and (not org-support-shift-select) (org-at-item-p))
  17266. (call-interactively 'org-next-item))
  17267. ((org-clocktable-try-shift 'down arg))
  17268. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17269. (org-support-shift-select
  17270. (org-call-for-shift-select 'next-line))
  17271. (t (org-shiftselect-error))))
  17272. (defun org-shiftright (&optional arg)
  17273. "Cycle the thing at point or in the current line, depending on context.
  17274. Depending on context, this does one of the following:
  17275. - switch a timestamp at point one day into the future
  17276. - on a headline, switch to the next TODO keyword.
  17277. - on an item, switch entire list to the next bullet type
  17278. - on a property line, switch to the next allowed value
  17279. - on a clocktable definition line, move time block into the future"
  17280. (interactive "P")
  17281. (cond
  17282. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17283. ((and org-support-shift-select (org-region-active-p))
  17284. (org-call-for-shift-select 'forward-char))
  17285. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17286. ((and (not (eq org-support-shift-select 'always))
  17287. (org-at-heading-p))
  17288. (let ((org-inhibit-logging
  17289. (not org-treat-S-cursor-todo-selection-as-state-change))
  17290. (org-inhibit-blocking
  17291. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17292. (org-call-with-arg 'org-todo 'right)))
  17293. ((or (and org-support-shift-select
  17294. (not (eq org-support-shift-select 'always))
  17295. (org-at-item-bullet-p))
  17296. (and (not org-support-shift-select) (org-at-item-p)))
  17297. (org-call-with-arg 'org-cycle-list-bullet nil))
  17298. ((and (not (eq org-support-shift-select 'always))
  17299. (org-at-property-p))
  17300. (call-interactively 'org-property-next-allowed-value))
  17301. ((org-clocktable-try-shift 'right arg))
  17302. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17303. (org-support-shift-select
  17304. (org-call-for-shift-select 'forward-char))
  17305. (t (org-shiftselect-error))))
  17306. (defun org-shiftleft (&optional arg)
  17307. "Cycle the thing at point or in the current line, depending on context.
  17308. Depending on context, this does one of the following:
  17309. - switch a timestamp at point one day into the past
  17310. - on a headline, switch to the previous TODO keyword.
  17311. - on an item, switch entire list to the previous bullet type
  17312. - on a property line, switch to the previous allowed value
  17313. - on a clocktable definition line, move time block into the past"
  17314. (interactive "P")
  17315. (cond
  17316. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17317. ((and org-support-shift-select (org-region-active-p))
  17318. (org-call-for-shift-select 'backward-char))
  17319. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17320. ((and (not (eq org-support-shift-select 'always))
  17321. (org-at-heading-p))
  17322. (let ((org-inhibit-logging
  17323. (not org-treat-S-cursor-todo-selection-as-state-change))
  17324. (org-inhibit-blocking
  17325. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17326. (org-call-with-arg 'org-todo 'left)))
  17327. ((or (and org-support-shift-select
  17328. (not (eq org-support-shift-select 'always))
  17329. (org-at-item-bullet-p))
  17330. (and (not org-support-shift-select) (org-at-item-p)))
  17331. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17332. ((and (not (eq org-support-shift-select 'always))
  17333. (org-at-property-p))
  17334. (call-interactively 'org-property-previous-allowed-value))
  17335. ((org-clocktable-try-shift 'left arg))
  17336. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17337. (org-support-shift-select
  17338. (org-call-for-shift-select 'backward-char))
  17339. (t (org-shiftselect-error))))
  17340. (defun org-shiftcontrolright ()
  17341. "Switch to next TODO set."
  17342. (interactive)
  17343. (cond
  17344. ((and org-support-shift-select (org-region-active-p))
  17345. (org-call-for-shift-select 'forward-word))
  17346. ((and (not (eq org-support-shift-select 'always))
  17347. (org-at-heading-p))
  17348. (org-call-with-arg 'org-todo 'nextset))
  17349. (org-support-shift-select
  17350. (org-call-for-shift-select 'forward-word))
  17351. (t (org-shiftselect-error))))
  17352. (defun org-shiftcontrolleft ()
  17353. "Switch to previous TODO set."
  17354. (interactive)
  17355. (cond
  17356. ((and org-support-shift-select (org-region-active-p))
  17357. (org-call-for-shift-select 'backward-word))
  17358. ((and (not (eq org-support-shift-select 'always))
  17359. (org-at-heading-p))
  17360. (org-call-with-arg 'org-todo 'previousset))
  17361. (org-support-shift-select
  17362. (org-call-for-shift-select 'backward-word))
  17363. (t (org-shiftselect-error))))
  17364. (defun org-shiftcontrolup ()
  17365. "Change timestamps synchronously up in CLOCK log lines."
  17366. (interactive)
  17367. (cond ((and (not org-support-shift-select)
  17368. (org-at-clock-log-p)
  17369. (org-at-timestamp-p t))
  17370. (org-clock-timestamps-up))
  17371. (t (org-shiftselect-error))))
  17372. (defun org-shiftcontroldown ()
  17373. "Change timestamps synchronously down in CLOCK log lines."
  17374. (interactive)
  17375. (cond ((and (not org-support-shift-select)
  17376. (org-at-clock-log-p)
  17377. (org-at-timestamp-p t))
  17378. (org-clock-timestamps-down))
  17379. (t (org-shiftselect-error))))
  17380. (defun org-ctrl-c-ret ()
  17381. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17382. (interactive)
  17383. (cond
  17384. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17385. (t (call-interactively 'org-insert-heading))))
  17386. (defun org-find-visible ()
  17387. (let ((s (point)))
  17388. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17389. (get-char-property s 'invisible)))
  17390. s))
  17391. (defun org-find-invisible ()
  17392. (let ((s (point)))
  17393. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17394. (not (get-char-property s 'invisible))))
  17395. s))
  17396. (defun org-copy-visible (beg end)
  17397. "Copy the visible parts of the region."
  17398. (interactive "r")
  17399. (let (snippets s)
  17400. (save-excursion
  17401. (save-restriction
  17402. (narrow-to-region beg end)
  17403. (setq s (goto-char (point-min)))
  17404. (while (not (= (point) (point-max)))
  17405. (goto-char (org-find-invisible))
  17406. (push (buffer-substring s (point)) snippets)
  17407. (setq s (goto-char (org-find-visible))))))
  17408. (kill-new (apply 'concat (nreverse snippets)))))
  17409. (defun org-copy-special ()
  17410. "Copy region in table or copy current subtree.
  17411. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17412. See the individual commands for more information."
  17413. (interactive)
  17414. (call-interactively
  17415. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17416. (defun org-cut-special ()
  17417. "Cut region in table or cut current subtree.
  17418. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17419. See the individual commands for more information."
  17420. (interactive)
  17421. (call-interactively
  17422. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17423. (defun org-paste-special (arg)
  17424. "Paste rectangular region into table, or past subtree relative to level.
  17425. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17426. See the individual commands for more information."
  17427. (interactive "P")
  17428. (if (org-at-table-p)
  17429. (org-table-paste-rectangle)
  17430. (org-paste-subtree arg)))
  17431. (defsubst org-in-fixed-width-region-p ()
  17432. "Is point in a fixed-width region?"
  17433. (save-match-data
  17434. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17435. (defun org-edit-special (&optional arg)
  17436. "Call a special editor for the stuff at point.
  17437. When at a table, call the formula editor with `org-table-edit-formulas'.
  17438. When in a source code block, call `org-edit-src-code'.
  17439. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17440. When in an #+include line, visit the included file.
  17441. On a link, call `ffap' to visit the link at point.
  17442. Otherwise, return a user error."
  17443. (interactive)
  17444. ;; possibly prep session before editing source
  17445. (when (and (org-in-src-block-p) arg)
  17446. (let* ((info (org-babel-get-src-block-info))
  17447. (lang (nth 0 info))
  17448. (params (nth 2 info))
  17449. (session (cdr (assoc :session params))))
  17450. (when (and info session) ;; we are in a source-code block with a session
  17451. (funcall
  17452. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17453. (cond ;; proceed with `org-edit-special'
  17454. ((save-excursion
  17455. (beginning-of-line 1)
  17456. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17457. (find-file (org-trim (match-string 1))))
  17458. ((or (org-at-table-p)
  17459. (save-excursion
  17460. (beginning-of-line 1)
  17461. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17462. (call-interactively 'org-table-edit-formulas))
  17463. ((or (org-in-block-p '("src" "example" "latex" "html"))
  17464. (org-at-table.el-p))
  17465. (org-edit-src-code))
  17466. ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
  17467. ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
  17468. (t (user-error "No special environment to edit here"))))
  17469. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17470. (defun org-ctrl-c-ctrl-c (&optional arg)
  17471. "Set tags in headline, or update according to changed information at point.
  17472. This command does many different things, depending on context:
  17473. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17474. this is what we do.
  17475. - If the cursor is on a statistics cookie, update it.
  17476. - If the cursor is in a headline, prompt for tags and insert them
  17477. into the current line, aligned to `org-tags-column'. When called
  17478. with prefix arg, realign all tags in the current buffer.
  17479. - If the cursor is in one of the special #+KEYWORD lines, this
  17480. triggers scanning the buffer for these lines and updating the
  17481. information.
  17482. - If the cursor is inside a table, realign the table. This command
  17483. works even if the automatic table editor has been turned off.
  17484. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17485. the entire table.
  17486. - If the cursor is at a footnote reference or definition, jump to
  17487. the corresponding definition or references, respectively.
  17488. - If the cursor is a the beginning of a dynamic block, update it.
  17489. - If the current buffer is a capture buffer, close note and file it.
  17490. - If the cursor is on a <<<target>>>, update radio targets and
  17491. corresponding links in this buffer.
  17492. - If the cursor is on a numbered item in a plain list, renumber the
  17493. ordered list.
  17494. - If the cursor is on a checkbox, toggle it.
  17495. - If the cursor is on a code block, evaluate it. The variable
  17496. `org-confirm-babel-evaluate' can be used to control prompting
  17497. before code block evaluation, by default every code block
  17498. evaluation requires confirmation. Code block evaluation can be
  17499. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17500. (interactive "P")
  17501. (let ((org-enable-table-editor t))
  17502. (cond
  17503. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17504. org-occur-highlights
  17505. org-latex-fragment-image-overlays)
  17506. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17507. (org-remove-occur-highlights)
  17508. (org-remove-latex-fragment-image-overlays)
  17509. (message "Temporary highlights/overlays removed from current buffer"))
  17510. ((and (local-variable-p 'org-finish-function (current-buffer))
  17511. (fboundp org-finish-function))
  17512. (funcall org-finish-function))
  17513. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17514. ((org-in-regexp org-ts-regexp-both)
  17515. (org-timestamp-change 0 'day))
  17516. ((or (looking-at org-property-start-re)
  17517. (org-at-property-p))
  17518. (call-interactively 'org-property-action))
  17519. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17520. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17521. (or (org-at-heading-p) (org-at-item-p)))
  17522. (call-interactively 'org-update-statistics-cookies))
  17523. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17524. ((org-at-table.el-p)
  17525. (message "Use C-c ' to edit table.el tables"))
  17526. ((org-at-table-p)
  17527. (org-table-maybe-eval-formula)
  17528. (if arg
  17529. (call-interactively 'org-table-recalculate)
  17530. (org-table-maybe-recalculate-line))
  17531. (call-interactively 'org-table-align)
  17532. (orgtbl-send-table 'maybe))
  17533. ((or (org-footnote-at-reference-p)
  17534. (org-footnote-at-definition-p))
  17535. (call-interactively 'org-footnote-action))
  17536. ((org-at-item-checkbox-p)
  17537. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17538. ;; list only if at top item.
  17539. (let* ((cbox (match-string 1))
  17540. (struct (org-list-struct))
  17541. (old-struct (copy-tree struct))
  17542. (parents (org-list-parents-alist struct))
  17543. (orderedp (org-entry-get nil "ORDERED"))
  17544. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17545. block-item)
  17546. ;; Use a light version of `org-toggle-checkbox' to avoid
  17547. ;; computing list structure twice.
  17548. (let ((new-box (cond
  17549. ((equal arg '(16)) "[-]")
  17550. ((equal arg '(4)) nil)
  17551. ((equal "[X]" cbox) "[ ]")
  17552. (t "[X]"))))
  17553. (if (and firstp arg)
  17554. ;; If at first item of sub-list, remove check-box from
  17555. ;; every item at the same level.
  17556. (mapc
  17557. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17558. (org-list-get-all-items
  17559. (point-at-bol) struct (org-list-prevs-alist struct)))
  17560. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17561. ;; Replicate `org-list-write-struct', while grabbing a return
  17562. ;; value from `org-list-struct-fix-box'.
  17563. (org-list-struct-fix-ind struct parents 2)
  17564. (org-list-struct-fix-item-end struct)
  17565. (let ((prevs (org-list-prevs-alist struct)))
  17566. (org-list-struct-fix-bul struct prevs)
  17567. (org-list-struct-fix-ind struct parents)
  17568. (setq block-item
  17569. (org-list-struct-fix-box struct parents prevs orderedp)))
  17570. (if (equal struct old-struct)
  17571. (user-error "Cannot toggle this checkbox (unchecked subitems?)")
  17572. (org-list-struct-apply-struct struct old-struct)
  17573. (org-update-checkbox-count-maybe))
  17574. (when block-item
  17575. (message
  17576. "Checkboxes were removed due to unchecked box at line %d"
  17577. (org-current-line block-item)))
  17578. (when firstp (org-list-send-list 'maybe))))
  17579. ((org-at-item-p)
  17580. ;; Cursor at an item: repair list. Do checkbox related actions
  17581. ;; only if function was called with an argument. Send list only
  17582. ;; if at top item.
  17583. (let* ((struct (org-list-struct))
  17584. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17585. old-struct)
  17586. (when arg
  17587. (setq old-struct (copy-tree struct))
  17588. (if firstp
  17589. ;; If at first item of sub-list, add check-box to every
  17590. ;; item at the same level.
  17591. (mapc
  17592. (lambda (pos)
  17593. (unless (org-list-get-checkbox pos struct)
  17594. (org-list-set-checkbox pos struct "[ ]")))
  17595. (org-list-get-all-items
  17596. (point-at-bol) struct (org-list-prevs-alist struct)))
  17597. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17598. (org-list-write-struct
  17599. struct (org-list-parents-alist struct) old-struct)
  17600. (when arg (org-update-checkbox-count-maybe))
  17601. (when firstp (org-list-send-list 'maybe))))
  17602. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17603. ;; Dynamic block
  17604. (beginning-of-line 1)
  17605. (save-excursion (org-update-dblock)))
  17606. ((save-excursion
  17607. (let ((case-fold-search t))
  17608. (beginning-of-line 1)
  17609. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17610. (cond
  17611. ((or (equal (match-string 1) "TBLFM")
  17612. (equal (match-string 1) "tblfm"))
  17613. ;; Recalculate the table before this line
  17614. (save-excursion
  17615. (beginning-of-line 1)
  17616. (skip-chars-backward " \r\n\t")
  17617. (if (org-at-table-p)
  17618. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17619. (t
  17620. (let ((org-inhibit-startup-visibility-stuff t)
  17621. (org-startup-align-all-tables nil))
  17622. (when (boundp 'org-table-coordinate-overlays)
  17623. (mapc 'delete-overlay org-table-coordinate-overlays)
  17624. (setq org-table-coordinate-overlays nil))
  17625. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17626. (message "Local setup has been refreshed"))))
  17627. ((org-clock-update-time-maybe))
  17628. (t
  17629. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17630. (error "C-c C-c can do nothing useful at this location"))))))
  17631. (defun org-mode-restart ()
  17632. "Restart Org-mode, to scan again for special lines.
  17633. Also updates the keyword regular expressions."
  17634. (interactive)
  17635. (org-mode)
  17636. (message "Org-mode restarted"))
  17637. (defun org-kill-note-or-show-branches ()
  17638. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17639. (interactive)
  17640. (if (not org-finish-function)
  17641. (progn
  17642. (hide-subtree)
  17643. (call-interactively 'show-branches))
  17644. (let ((org-note-abort t))
  17645. (funcall org-finish-function))))
  17646. (defun org-return (&optional indent)
  17647. "Goto next table row or insert a newline.
  17648. Calls `org-table-next-row' or `newline', depending on context.
  17649. See the individual commands for more information."
  17650. (interactive)
  17651. (let (org-ts-what)
  17652. (cond
  17653. ((or (bobp) (org-in-src-block-p))
  17654. (if indent (newline-and-indent) (newline)))
  17655. ((org-at-table-p)
  17656. (org-table-justify-field-maybe)
  17657. (call-interactively 'org-table-next-row))
  17658. ;; when `newline-and-indent' is called within a list, make sure
  17659. ;; text moved stays inside the item.
  17660. ((and (org-in-item-p) indent)
  17661. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17662. (progn
  17663. (save-match-data (newline))
  17664. (org-indent-line-to (length (match-string 0))))
  17665. (let ((ind (org-get-indentation)))
  17666. (newline)
  17667. (if (org-looking-back org-list-end-re)
  17668. (org-indent-line)
  17669. (org-indent-line-to ind)))))
  17670. ((and org-return-follows-link
  17671. (org-at-timestamp-p t)
  17672. (not (eq org-ts-what 'after)))
  17673. (org-follow-timestamp-link))
  17674. ((and org-return-follows-link
  17675. (let ((tprop (get-text-property (point) 'face)))
  17676. (or (eq tprop 'org-link)
  17677. (and (listp tprop) (memq 'org-link tprop)))))
  17678. (call-interactively 'org-open-at-point))
  17679. ((and (org-at-heading-p)
  17680. (looking-at
  17681. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17682. (org-show-entry)
  17683. (end-of-line 1)
  17684. (newline))
  17685. (t (if indent (newline-and-indent) (newline))))))
  17686. (defun org-return-indent ()
  17687. "Goto next table row or insert a newline and indent.
  17688. Calls `org-table-next-row' or `newline-and-indent', depending on
  17689. context. See the individual commands for more information."
  17690. (interactive)
  17691. (org-return t))
  17692. (defun org-ctrl-c-star ()
  17693. "Compute table, or change heading status of lines.
  17694. Calls `org-table-recalculate' or `org-toggle-heading',
  17695. depending on context."
  17696. (interactive)
  17697. (cond
  17698. ((org-at-table-p)
  17699. (call-interactively 'org-table-recalculate))
  17700. (t
  17701. ;; Convert all lines in region to list items
  17702. (call-interactively 'org-toggle-heading))))
  17703. (defun org-ctrl-c-minus ()
  17704. "Insert separator line in table or modify bullet status of line.
  17705. Also turns a plain line or a region of lines into list items.
  17706. Calls `org-table-insert-hline', `org-toggle-item', or
  17707. `org-cycle-list-bullet', depending on context."
  17708. (interactive)
  17709. (cond
  17710. ((org-at-table-p)
  17711. (call-interactively 'org-table-insert-hline))
  17712. ((org-region-active-p)
  17713. (call-interactively 'org-toggle-item))
  17714. ((org-in-item-p)
  17715. (call-interactively 'org-cycle-list-bullet))
  17716. (t
  17717. (call-interactively 'org-toggle-item))))
  17718. (defun org-toggle-item (arg)
  17719. "Convert headings or normal lines to items, items to normal lines.
  17720. If there is no active region, only the current line is considered.
  17721. If the first non blank line in the region is an headline, convert
  17722. all headlines to items, shifting text accordingly.
  17723. If it is an item, convert all items to normal lines.
  17724. If it is normal text, change region into an item. With a prefix
  17725. argument ARG, change each line in region into an item."
  17726. (interactive "P")
  17727. (let ((shift-text
  17728. (function
  17729. ;; Shift text in current section to IND, from point to END.
  17730. ;; The function leaves point to END line.
  17731. (lambda (ind end)
  17732. (let ((min-i 1000) (end (copy-marker end)))
  17733. ;; First determine the minimum indentation (MIN-I) of
  17734. ;; the text.
  17735. (save-excursion
  17736. (catch 'exit
  17737. (while (< (point) end)
  17738. (let ((i (org-get-indentation)))
  17739. (cond
  17740. ;; Skip blank lines and inline tasks.
  17741. ((looking-at "^[ \t]*$"))
  17742. ((looking-at org-outline-regexp-bol))
  17743. ;; We can't find less than 0 indentation.
  17744. ((zerop i) (throw 'exit (setq min-i 0)))
  17745. ((< i min-i) (setq min-i i))))
  17746. (forward-line))))
  17747. ;; Then indent each line so that a line indented to
  17748. ;; MIN-I becomes indented to IND. Ignore blank lines
  17749. ;; and inline tasks in the process.
  17750. (let ((delta (- ind min-i)))
  17751. (while (< (point) end)
  17752. (unless (or (looking-at "^[ \t]*$")
  17753. (looking-at org-outline-regexp-bol))
  17754. (org-indent-line-to (+ (org-get-indentation) delta)))
  17755. (forward-line)))))))
  17756. (skip-blanks
  17757. (function
  17758. ;; Return beginning of first non-blank line, starting from
  17759. ;; line at POS.
  17760. (lambda (pos)
  17761. (save-excursion
  17762. (goto-char pos)
  17763. (skip-chars-forward " \r\t\n")
  17764. (point-at-bol)))))
  17765. beg end)
  17766. ;; Determine boundaries of changes.
  17767. (if (org-region-active-p)
  17768. (setq beg (funcall skip-blanks (region-beginning))
  17769. end (copy-marker (region-end)))
  17770. (setq beg (funcall skip-blanks (point-at-bol))
  17771. end (copy-marker (point-at-eol))))
  17772. ;; Depending on the starting line, choose an action on the text
  17773. ;; between BEG and END.
  17774. (org-with-limited-levels
  17775. (save-excursion
  17776. (goto-char beg)
  17777. (cond
  17778. ;; Case 1. Start at an item: de-itemize. Note that it only
  17779. ;; happens when a region is active: `org-ctrl-c-minus'
  17780. ;; would call `org-cycle-list-bullet' otherwise.
  17781. ((org-at-item-p)
  17782. (while (< (point) end)
  17783. (when (org-at-item-p)
  17784. (skip-chars-forward " \t")
  17785. (delete-region (point) (match-end 0)))
  17786. (forward-line)))
  17787. ;; Case 2. Start at an heading: convert to items.
  17788. ((org-at-heading-p)
  17789. (let* ((bul (org-list-bullet-string "-"))
  17790. (bul-len (length bul))
  17791. ;; Indentation of the first heading. It should be
  17792. ;; relative to the indentation of its parent, if any.
  17793. (start-ind (save-excursion
  17794. (cond
  17795. ((not org-adapt-indentation) 0)
  17796. ((not (outline-previous-heading)) 0)
  17797. (t (length (match-string 0))))))
  17798. ;; Level of first heading. Further headings will be
  17799. ;; compared to it to determine hierarchy in the list.
  17800. (ref-level (org-reduced-level (org-outline-level))))
  17801. (while (< (point) end)
  17802. (let* ((level (org-reduced-level (org-outline-level)))
  17803. (delta (max 0 (- level ref-level))))
  17804. ;; If current headline is less indented than the first
  17805. ;; one, set it as reference, in order to preserve
  17806. ;; subtrees.
  17807. (when (< level ref-level) (setq ref-level level))
  17808. (replace-match bul t t)
  17809. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17810. ;; Ensure all text down to END (or SECTION-END) belongs
  17811. ;; to the newly created item.
  17812. (let ((section-end (save-excursion
  17813. (or (outline-next-heading) (point)))))
  17814. (forward-line)
  17815. (funcall shift-text
  17816. (+ start-ind (* (1+ delta) bul-len))
  17817. (min end section-end)))))))
  17818. ;; Case 3. Normal line with ARG: turn each non-item line into
  17819. ;; an item.
  17820. (arg
  17821. (while (< (point) end)
  17822. (unless (or (org-at-heading-p) (org-at-item-p))
  17823. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17824. (replace-match
  17825. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17826. (forward-line)))
  17827. ;; Case 4. Normal line without ARG: make the first line of
  17828. ;; region an item, and shift indentation of others
  17829. ;; lines to set them as item's body.
  17830. (t (let* ((bul (org-list-bullet-string "-"))
  17831. (bul-len (length bul))
  17832. (ref-ind (org-get-indentation)))
  17833. (skip-chars-forward " \t")
  17834. (insert bul)
  17835. (forward-line)
  17836. (while (< (point) end)
  17837. ;; Ensure that lines less indented than first one
  17838. ;; still get included in item body.
  17839. (funcall shift-text
  17840. (+ ref-ind bul-len)
  17841. (min end (save-excursion (or (outline-next-heading)
  17842. (point)))))
  17843. (forward-line)))))))))
  17844. (defun org-toggle-heading (&optional nstars)
  17845. "Convert headings to normal text, or items or text to headings.
  17846. If there is no active region, only the current line is considered.
  17847. With a \\[universal-argument] prefix, convert the whole list at
  17848. point into heading.
  17849. In a region:
  17850. - If the first non blank line is an headline, remove the stars
  17851. from all headlines in the region.
  17852. - If it is a normal line turn each and every normal line (i.e. not an
  17853. heading or an item) in the region into a heading.
  17854. - If it is a plain list item, turn all plain list items into headings.
  17855. When converting a line into a heading, the number of stars is chosen
  17856. such that the lines become children of the current entry. However,
  17857. when a prefix argument is given, its value determines the number of
  17858. stars to add."
  17859. (interactive "P")
  17860. (let ((skip-blanks
  17861. (function
  17862. ;; Return beginning of first non-blank line, starting from
  17863. ;; line at POS.
  17864. (lambda (pos)
  17865. (save-excursion
  17866. (goto-char pos)
  17867. (while (org-at-comment-p) (forward-line))
  17868. (skip-chars-forward " \r\t\n")
  17869. (point-at-bol)))))
  17870. beg end toggled)
  17871. ;; Determine boundaries of changes. If a universal prefix has
  17872. ;; been given, put the list in a region. If region ends at a bol,
  17873. ;; do not consider the last line to be in the region.
  17874. (when (and current-prefix-arg (org-at-item-p))
  17875. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17876. (org-mark-element))
  17877. (if (org-region-active-p)
  17878. (setq beg (funcall skip-blanks (region-beginning))
  17879. end (copy-marker (save-excursion
  17880. (goto-char (region-end))
  17881. (if (bolp) (point) (point-at-eol)))))
  17882. (setq beg (funcall skip-blanks (point-at-bol))
  17883. end (copy-marker (point-at-eol))))
  17884. ;; Ensure inline tasks don't count as headings.
  17885. (org-with-limited-levels
  17886. (save-excursion
  17887. (goto-char beg)
  17888. (cond
  17889. ;; Case 1. Started at an heading: de-star headings.
  17890. ((org-at-heading-p)
  17891. (while (< (point) end)
  17892. (when (org-at-heading-p t)
  17893. (looking-at org-outline-regexp) (replace-match "")
  17894. (setq toggled t))
  17895. (forward-line)))
  17896. ;; Case 2. Started at an item: change items into headlines.
  17897. ;; One star will be added by `org-list-to-subtree'.
  17898. ((org-at-item-p)
  17899. (let* ((stars (make-string
  17900. (if nstars
  17901. ;; subtract the star that will be added again by
  17902. ;; `org-list-to-subtree'
  17903. (1- (prefix-numeric-value current-prefix-arg))
  17904. (or (org-current-level) 0))
  17905. ?*))
  17906. (add-stars
  17907. (cond (nstars "") ; stars from prefix only
  17908. ((equal stars "") "") ; before first heading
  17909. (org-odd-levels-only "*") ; inside heading, odd
  17910. (t "")))) ; inside heading, oddeven
  17911. (while (< (point) end)
  17912. (when (org-at-item-p)
  17913. ;; Pay attention to cases when region ends before list.
  17914. (let* ((struct (org-list-struct))
  17915. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17916. (save-restriction
  17917. (narrow-to-region (point) list-end)
  17918. (insert
  17919. (org-list-to-subtree
  17920. (org-list-parse-list t)
  17921. '(:istart (concat stars add-stars (funcall get-stars depth))
  17922. :icount (concat stars add-stars (funcall get-stars depth)))))))
  17923. (setq toggled t))
  17924. (forward-line))))
  17925. ;; Case 3. Started at normal text: make every line an heading,
  17926. ;; skipping headlines and items.
  17927. (t (let* ((stars (make-string
  17928. (if nstars
  17929. (prefix-numeric-value current-prefix-arg)
  17930. (or (org-current-level) 0))
  17931. ?*))
  17932. (add-stars
  17933. (cond (nstars "") ; stars from prefix only
  17934. ((equal stars "") "*") ; before first heading
  17935. (org-odd-levels-only "**") ; inside heading, odd
  17936. (t "*"))) ; inside heading, oddeven
  17937. (rpl (concat stars add-stars " ")))
  17938. (while (< (point) end)
  17939. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  17940. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17941. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  17942. (forward-line)))))))
  17943. (unless toggled (message "Cannot toggle heading from here"))))
  17944. (defun org-meta-return (&optional arg)
  17945. "Insert a new heading or wrap a region in a table.
  17946. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17947. See the individual commands for more information."
  17948. (interactive "P")
  17949. (cond
  17950. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17951. ((or (org-at-drawer-p) (org-at-property-p))
  17952. (newline-and-indent))
  17953. ((org-at-table-p)
  17954. (call-interactively 'org-table-wrap-region))
  17955. (t (call-interactively 'org-insert-heading))))
  17956. ;;; Menu entries
  17957. (defsubst org-in-subtree-not-table-p ()
  17958. "Are we in a subtree and not in a table?"
  17959. (and (not (org-before-first-heading-p))
  17960. (not (org-at-table-p))))
  17961. ;; Define the Org-mode menus
  17962. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17963. '("Tbl"
  17964. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17965. ["Next Field" org-cycle (org-at-table-p)]
  17966. ["Previous Field" org-shifttab (org-at-table-p)]
  17967. ["Next Row" org-return (org-at-table-p)]
  17968. "--"
  17969. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17970. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17971. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17972. "--"
  17973. ("Column"
  17974. ["Move Column Left" org-metaleft (org-at-table-p)]
  17975. ["Move Column Right" org-metaright (org-at-table-p)]
  17976. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17977. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17978. ("Row"
  17979. ["Move Row Up" org-metaup (org-at-table-p)]
  17980. ["Move Row Down" org-metadown (org-at-table-p)]
  17981. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17982. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17983. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17984. "--"
  17985. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17986. ("Rectangle"
  17987. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17988. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17989. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17990. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17991. "--"
  17992. ("Calculate"
  17993. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17994. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17995. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17996. "--"
  17997. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17998. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17999. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18000. "--"
  18001. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18002. "--"
  18003. ["Sum Column/Rectangle" org-table-sum
  18004. (or (org-at-table-p) (org-region-active-p))]
  18005. ["Which Column?" org-table-current-column (org-at-table-p)])
  18006. ["Debug Formulas"
  18007. org-table-toggle-formula-debugger
  18008. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18009. ["Show Col/Row Numbers"
  18010. org-table-toggle-coordinate-overlays
  18011. :style toggle
  18012. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18013. "--"
  18014. ["Create" org-table-create (and (not (org-at-table-p))
  18015. org-enable-table-editor)]
  18016. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18017. ["Import from File" org-table-import (not (org-at-table-p))]
  18018. ["Export to File" org-table-export (org-at-table-p)]
  18019. "--"
  18020. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18021. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18022. '("Org"
  18023. ("Show/Hide"
  18024. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18025. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18026. ["Sparse Tree..." org-sparse-tree t]
  18027. ["Reveal Context" org-reveal t]
  18028. ["Show All" show-all t]
  18029. "--"
  18030. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18031. "--"
  18032. ["New Heading" org-insert-heading t]
  18033. ("Navigate Headings"
  18034. ["Up" outline-up-heading t]
  18035. ["Next" outline-next-visible-heading t]
  18036. ["Previous" outline-previous-visible-heading t]
  18037. ["Next Same Level" outline-forward-same-level t]
  18038. ["Previous Same Level" outline-backward-same-level t]
  18039. "--"
  18040. ["Jump" org-goto t])
  18041. ("Edit Structure"
  18042. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18043. "--"
  18044. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18045. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18046. "--"
  18047. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18048. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18049. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18050. "--"
  18051. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18052. "--"
  18053. ["Copy visible text" org-copy-visible t]
  18054. "--"
  18055. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18056. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18057. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18058. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18059. "--"
  18060. ["Sort Region/Children" org-sort t]
  18061. "--"
  18062. ["Convert to odd levels" org-convert-to-odd-levels t]
  18063. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18064. ("Editing"
  18065. ["Emphasis..." org-emphasize t]
  18066. ["Edit Source Example" org-edit-special t]
  18067. "--"
  18068. ["Footnote new/jump" org-footnote-action t]
  18069. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18070. ("Archive"
  18071. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18072. "--"
  18073. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18074. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18075. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18076. )
  18077. "--"
  18078. ("Hyperlinks"
  18079. ["Store Link (Global)" org-store-link t]
  18080. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18081. ["Insert Link" org-insert-link t]
  18082. ["Follow Link" org-open-at-point t]
  18083. "--"
  18084. ["Next link" org-next-link t]
  18085. ["Previous link" org-previous-link t]
  18086. "--"
  18087. ["Descriptive Links"
  18088. org-toggle-link-display
  18089. :style radio
  18090. :selected org-descriptive-links
  18091. ]
  18092. ["Literal Links"
  18093. org-toggle-link-display
  18094. :style radio
  18095. :selected (not org-descriptive-links)])
  18096. "--"
  18097. ("TODO Lists"
  18098. ["TODO/DONE/-" org-todo t]
  18099. ("Select keyword"
  18100. ["Next keyword" org-shiftright (org-at-heading-p)]
  18101. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18102. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18103. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18104. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18105. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18106. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18107. "--"
  18108. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18109. :selected org-enforce-todo-dependencies :style toggle :active t]
  18110. "Settings for tree at point"
  18111. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18112. :selected (org-entry-get nil "ORDERED")
  18113. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18114. ["Do Children parallel" org-toggle-ordered-property :style radio
  18115. :selected (not (org-entry-get nil "ORDERED"))
  18116. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18117. "--"
  18118. ["Set Priority" org-priority t]
  18119. ["Priority Up" org-shiftup t]
  18120. ["Priority Down" org-shiftdown t]
  18121. "--"
  18122. ["Get news from all feeds" org-feed-update-all t]
  18123. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18124. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18125. ("TAGS and Properties"
  18126. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18127. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18128. "--"
  18129. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18130. ["Column view of properties" org-columns t]
  18131. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18132. ("Dates and Scheduling"
  18133. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18134. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18135. ("Change Date"
  18136. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18137. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18138. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18139. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18140. ["Compute Time Range" org-evaluate-time-range t]
  18141. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18142. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18143. "--"
  18144. ["Custom time format" org-toggle-time-stamp-overlays
  18145. :style radio :selected org-display-custom-times]
  18146. "--"
  18147. ["Goto Calendar" org-goto-calendar t]
  18148. ["Date from Calendar" org-date-from-calendar t]
  18149. "--"
  18150. ["Start/Restart Timer" org-timer-start t]
  18151. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18152. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18153. ["Insert Timer String" org-timer t]
  18154. ["Insert Timer Item" org-timer-item t])
  18155. ("Logging work"
  18156. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18157. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18158. ["Clock out" org-clock-out t]
  18159. ["Clock cancel" org-clock-cancel t]
  18160. "--"
  18161. ["Mark as default task" org-clock-mark-default-task t]
  18162. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18163. ["Goto running clock" org-clock-goto t]
  18164. "--"
  18165. ["Display times" org-clock-display t]
  18166. ["Create clock table" org-clock-report t]
  18167. "--"
  18168. ["Record DONE time"
  18169. (progn (setq org-log-done (not org-log-done))
  18170. (message "Switching to %s will %s record a timestamp"
  18171. (car org-done-keywords)
  18172. (if org-log-done "automatically" "not")))
  18173. :style toggle :selected org-log-done])
  18174. "--"
  18175. ["Agenda Command..." org-agenda t]
  18176. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18177. ("File List for Agenda")
  18178. ("Special views current file"
  18179. ["TODO Tree" org-show-todo-tree t]
  18180. ["Check Deadlines" org-check-deadlines t]
  18181. ["Timeline" org-timeline t]
  18182. ["Tags/Property tree" org-match-sparse-tree t])
  18183. "--"
  18184. ["Export/Publish..." org-export t]
  18185. ("LaTeX"
  18186. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18187. :selected org-cdlatex-mode]
  18188. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18189. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18190. ["Modify math symbol" org-cdlatex-math-modify
  18191. (org-inside-LaTeX-fragment-p)]
  18192. ["Insert citation" org-reftex-citation t]
  18193. "--"
  18194. ["Template for BEAMER" (progn (require 'org-beamer)
  18195. (org-insert-beamer-options-template)) t])
  18196. "--"
  18197. ("MobileOrg"
  18198. ["Push Files and Views" org-mobile-push t]
  18199. ["Get Captured and Flagged" org-mobile-pull t]
  18200. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18201. "--"
  18202. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18203. "--"
  18204. ("Documentation"
  18205. ["Show Version" org-version t]
  18206. ["Info Documentation" org-info t])
  18207. ("Customize"
  18208. ["Browse Org Group" org-customize t]
  18209. "--"
  18210. ["Expand This Menu" org-create-customize-menu
  18211. (fboundp 'customize-menu-create)])
  18212. ["Send bug report" org-submit-bug-report t]
  18213. "--"
  18214. ("Refresh/Reload"
  18215. ["Refresh setup current buffer" org-mode-restart t]
  18216. ["Reload Org (after update)" org-reload t]
  18217. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18218. ))
  18219. (defun org-info (&optional node)
  18220. "Read documentation for Org-mode in the info system.
  18221. With optional NODE, go directly to that node."
  18222. (interactive)
  18223. (info (format "(org)%s" (or node ""))))
  18224. ;;;###autoload
  18225. (defun org-submit-bug-report ()
  18226. "Submit a bug report on Org-mode via mail.
  18227. Don't hesitate to report any problems or inaccurate documentation.
  18228. If you don't have setup sending mail from (X)Emacs, please copy the
  18229. output buffer into your mail program, as it gives us important
  18230. information about your Org-mode version and configuration."
  18231. (interactive)
  18232. (require 'reporter)
  18233. (org-load-modules-maybe)
  18234. (org-require-autoloaded-modules)
  18235. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18236. (reporter-submit-bug-report
  18237. "emacs-orgmode@gnu.org"
  18238. (org-version nil 'full)
  18239. (let (list)
  18240. (save-window-excursion
  18241. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18242. (delete-other-windows)
  18243. (erase-buffer)
  18244. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18245. We would like to add your full Org-mode and Outline configuration to the
  18246. bug report. This greatly simplifies the work of the maintainer and
  18247. other experts on the mailing list.
  18248. HOWEVER, some variables you have customized may contain private
  18249. information. The names of customers, colleagues, or friends, might
  18250. appear in the form of file names, tags, todo states, or search strings.
  18251. If you answer yes to the prompt, you might want to check and remove
  18252. such private information before sending the email.")
  18253. (add-text-properties (point-min) (point-max) '(face org-warning))
  18254. (when (yes-or-no-p "Include your Org-mode configuration ")
  18255. (mapatoms
  18256. (lambda (v)
  18257. (and (boundp v)
  18258. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18259. (or (and (symbol-value v)
  18260. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18261. (and
  18262. (get v 'custom-type) (get v 'standard-value)
  18263. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18264. (push v list)))))
  18265. (kill-buffer (get-buffer "*Warn about privacy*"))
  18266. list))
  18267. nil nil
  18268. "Remember to cover the basics, that is, what you expected to happen and
  18269. what in fact did happen. You don't know how to make a good report? See
  18270. http://orgmode.org/manual/Feedback.html#Feedback
  18271. Your bug report will be posted to the Org-mode mailing list.
  18272. ------------------------------------------------------------------------")
  18273. (save-excursion
  18274. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18275. (replace-match "\\1Bug: \\3 [\\2]")))))
  18276. (defun org-install-agenda-files-menu ()
  18277. (let ((bl (buffer-list)))
  18278. (save-excursion
  18279. (while bl
  18280. (set-buffer (pop bl))
  18281. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18282. (when (derived-mode-p 'org-mode)
  18283. (easy-menu-change
  18284. '("Org") "File List for Agenda"
  18285. (append
  18286. (list
  18287. ["Edit File List" (org-edit-agenda-file-list) t]
  18288. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18289. ["Remove Current File from List" org-remove-file t]
  18290. ["Cycle through agenda files" org-cycle-agenda-files t]
  18291. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18292. "--")
  18293. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18294. ;;;; Documentation
  18295. (defun org-require-autoloaded-modules ()
  18296. (interactive)
  18297. (mapc 'require
  18298. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18299. org-docbook org-exp org-html org-icalendar
  18300. org-id org-latex
  18301. org-publish org-remember org-table
  18302. org-timer org-xoxo)))
  18303. ;;;###autoload
  18304. (defun org-reload (&optional uncompiled)
  18305. "Reload all org lisp files.
  18306. With prefix arg UNCOMPILED, load the uncompiled versions."
  18307. (interactive "P")
  18308. (require 'find-func)
  18309. (let* ((file-re "^org\\(-.*\\)?\\.el")
  18310. (dir-org (file-name-directory (org-find-library-dir "org")))
  18311. (dir-org-contrib (ignore-errors
  18312. (file-name-directory
  18313. (org-find-library-dir "org-contribdir"))))
  18314. (babel-files
  18315. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  18316. (append (list nil "comint" "eval" "exp" "keys"
  18317. "lob" "ref" "table" "tangle")
  18318. (delq nil
  18319. (mapcar
  18320. (lambda (lang)
  18321. (when (cdr lang) (symbol-name (car lang))))
  18322. org-babel-load-languages)))))
  18323. (files
  18324. (append babel-files
  18325. (and dir-org-contrib
  18326. (directory-files dir-org-contrib t file-re))
  18327. (directory-files dir-org t file-re)))
  18328. (remove-re (concat (if (featurep 'xemacs)
  18329. "org-colview" "org-colview-xemacs")
  18330. "\\'")))
  18331. (setq files (mapcar 'file-name-sans-extension files))
  18332. (setq files (mapcar
  18333. (lambda (x) (if (string-match remove-re x) nil x))
  18334. files))
  18335. (setq files (delq nil files))
  18336. (mapc
  18337. (lambda (f)
  18338. (when (featurep (intern (file-name-nondirectory f)))
  18339. (if (and (not uncompiled)
  18340. (file-exists-p (concat f ".elc")))
  18341. (load (concat f ".elc") nil nil 'nosuffix)
  18342. (load (concat f ".el") nil nil 'nosuffix))))
  18343. files)
  18344. (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
  18345. (org-version nil 'full 'message))
  18346. ;;;###autoload
  18347. (defun org-customize ()
  18348. "Call the customize function with org as argument."
  18349. (interactive)
  18350. (org-load-modules-maybe)
  18351. (org-require-autoloaded-modules)
  18352. (customize-browse 'org))
  18353. (defun org-create-customize-menu ()
  18354. "Create a full customization menu for Org-mode, insert it into the menu."
  18355. (interactive)
  18356. (org-load-modules-maybe)
  18357. (org-require-autoloaded-modules)
  18358. (if (fboundp 'customize-menu-create)
  18359. (progn
  18360. (easy-menu-change
  18361. '("Org") "Customize"
  18362. `(["Browse Org group" org-customize t]
  18363. "--"
  18364. ,(customize-menu-create 'org)
  18365. ["Set" Custom-set t]
  18366. ["Save" Custom-save t]
  18367. ["Reset to Current" Custom-reset-current t]
  18368. ["Reset to Saved" Custom-reset-saved t]
  18369. ["Reset to Standard Settings" Custom-reset-standard t]))
  18370. (message "\"Org\"-menu now contains full customization menu"))
  18371. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18372. ;;;; Miscellaneous stuff
  18373. ;;; Generally useful functions
  18374. (defun org-get-at-bol (property)
  18375. "Get text property PROPERTY at beginning of line."
  18376. (get-text-property (point-at-bol) property))
  18377. (defun org-find-text-property-in-string (prop s)
  18378. "Return the first non-nil value of property PROP in string S."
  18379. (or (get-text-property 0 prop s)
  18380. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18381. prop s)))
  18382. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18383. "Display the given MESSAGE as a warning."
  18384. (if (fboundp 'display-warning)
  18385. (display-warning 'org message
  18386. (if (featurep 'xemacs) 'warning :warning))
  18387. (let ((buf (get-buffer-create "*Org warnings*")))
  18388. (with-current-buffer buf
  18389. (goto-char (point-max))
  18390. (insert "Warning (Org): " message)
  18391. (unless (bolp)
  18392. (newline)))
  18393. (display-buffer buf)
  18394. (sit-for 0))))
  18395. (defun org-eval (form)
  18396. "Eval FORM and return result."
  18397. (condition-case error
  18398. (eval form)
  18399. (error (format "%%![Error: %s]" error))))
  18400. (defun org-in-clocktable-p ()
  18401. "Check if the cursor is in a clocktable."
  18402. (let ((pos (point)) start)
  18403. (save-excursion
  18404. (end-of-line 1)
  18405. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18406. (setq start (match-beginning 0))
  18407. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18408. (>= (match-end 0) pos)
  18409. start))))
  18410. (defun org-in-commented-line ()
  18411. "Is point in a line starting with `#'?"
  18412. (equal (char-after (point-at-bol)) ?#))
  18413. (defun org-in-indented-comment-line ()
  18414. "Is point in a line starting with `#' after some white space?"
  18415. (save-excursion
  18416. (save-match-data
  18417. (goto-char (point-at-bol))
  18418. (looking-at "[ \t]*#"))))
  18419. (defun org-in-verbatim-emphasis ()
  18420. (save-match-data
  18421. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18422. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18423. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18424. (if (and marker (marker-buffer marker)
  18425. (buffer-live-p (marker-buffer marker)))
  18426. (progn
  18427. (org-pop-to-buffer-same-window (marker-buffer marker))
  18428. (if (or (> marker (point-max)) (< marker (point-min)))
  18429. (widen))
  18430. (goto-char marker)
  18431. (org-show-context 'org-goto))
  18432. (if bookmark
  18433. (bookmark-jump bookmark)
  18434. (error "Cannot find location"))))
  18435. (defun org-quote-csv-field (s)
  18436. "Quote field for inclusion in CSV material."
  18437. (if (string-match "[\",]" s)
  18438. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18439. s))
  18440. (defun org-force-self-insert (N)
  18441. "Needed to enforce self-insert under remapping."
  18442. (interactive "p")
  18443. (self-insert-command N))
  18444. (defun org-string-width (s)
  18445. "Compute width of string, ignoring invisible characters.
  18446. This ignores character with invisibility property `org-link', and also
  18447. characters with property `org-cwidth', because these will become invisible
  18448. upon the next fontification round."
  18449. (let (b l)
  18450. (when (or (eq t buffer-invisibility-spec)
  18451. (assq 'org-link buffer-invisibility-spec))
  18452. (while (setq b (text-property-any 0 (length s)
  18453. 'invisible 'org-link s))
  18454. (setq s (concat (substring s 0 b)
  18455. (substring s (or (next-single-property-change
  18456. b 'invisible s) (length s)))))))
  18457. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18458. (setq s (concat (substring s 0 b)
  18459. (substring s (or (next-single-property-change
  18460. b 'org-cwidth s) (length s))))))
  18461. (setq l (string-width s) b -1)
  18462. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18463. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18464. l))
  18465. (defun org-shorten-string (s maxlength)
  18466. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18467. If the string is shorter or has length MAXLENGTH, just return the
  18468. original string. If it is longer, the functions finds a space in the
  18469. string, breaks this string off at that locations and adds three dots
  18470. as ellipsis. Including the ellipsis, the string will not be longer
  18471. than MAXLENGTH. If finding a good breaking point in the string does
  18472. not work, the string is just chopped off in the middle of a word
  18473. if necessary."
  18474. (if (<= (length s) maxlength)
  18475. s
  18476. (let* ((n (max (- maxlength 4) 1))
  18477. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18478. (if (string-match re s)
  18479. (concat (match-string 1 s) "...")
  18480. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18481. (defun org-get-indentation (&optional line)
  18482. "Get the indentation of the current line, interpreting tabs.
  18483. When LINE is given, assume it represents a line and compute its indentation."
  18484. (if line
  18485. (if (string-match "^ *" (org-remove-tabs line))
  18486. (match-end 0))
  18487. (save-excursion
  18488. (beginning-of-line 1)
  18489. (skip-chars-forward " \t")
  18490. (current-column))))
  18491. (defun org-get-string-indentation (s)
  18492. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18493. (let ((n -1) (i 0) (w tab-width) c)
  18494. (catch 'exit
  18495. (while (< (setq n (1+ n)) (length s))
  18496. (setq c (aref s n))
  18497. (cond ((= c ?\ ) (setq i (1+ i)))
  18498. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18499. (t (throw 'exit t)))))
  18500. i))
  18501. (defun org-remove-tabs (s &optional width)
  18502. "Replace tabulators in S with spaces.
  18503. Assumes that s is a single line, starting in column 0."
  18504. (setq width (or width tab-width))
  18505. (while (string-match "\t" s)
  18506. (setq s (replace-match
  18507. (make-string
  18508. (- (* width (/ (+ (match-beginning 0) width) width))
  18509. (match-beginning 0)) ?\ )
  18510. t t s)))
  18511. s)
  18512. (defun org-fix-indentation (line ind)
  18513. "Fix indentation in LINE.
  18514. IND is a cons cell with target and minimum indentation.
  18515. If the current indentation in LINE is smaller than the minimum,
  18516. leave it alone. If it is larger than ind, set it to the target."
  18517. (let* ((l (org-remove-tabs line))
  18518. (i (org-get-indentation l))
  18519. (i1 (car ind)) (i2 (cdr ind)))
  18520. (if (>= i i2) (setq l (substring line i2)))
  18521. (if (> i1 0)
  18522. (concat (make-string i1 ?\ ) l)
  18523. l)))
  18524. (defun org-remove-indentation (code &optional n)
  18525. "Remove the maximum common indentation from the lines in CODE.
  18526. N may optionally be the number of spaces to remove."
  18527. (with-temp-buffer
  18528. (insert code)
  18529. (org-do-remove-indentation n)
  18530. (buffer-string)))
  18531. (defun org-do-remove-indentation (&optional n)
  18532. "Remove the maximum common indentation from the buffer."
  18533. (untabify (point-min) (point-max))
  18534. (let ((min 10000) re)
  18535. (if n
  18536. (setq min n)
  18537. (goto-char (point-min))
  18538. (while (re-search-forward "^ *[^ \n]" nil t)
  18539. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18540. (unless (or (= min 0) (= min 10000))
  18541. (setq re (format "^ \\{%d\\}" min))
  18542. (goto-char (point-min))
  18543. (while (re-search-forward re nil t)
  18544. (replace-match "")
  18545. (end-of-line 1))
  18546. min)))
  18547. (defun org-fill-template (template alist)
  18548. "Find each %key of ALIST in TEMPLATE and replace it."
  18549. (let ((case-fold-search nil)
  18550. entry key value)
  18551. (setq alist (sort (copy-sequence alist)
  18552. (lambda (a b) (< (length (car a)) (length (car b))))))
  18553. (while (setq entry (pop alist))
  18554. (setq template
  18555. (replace-regexp-in-string
  18556. (concat "%" (regexp-quote (car entry)))
  18557. (or (cdr entry) "") template t t)))
  18558. template))
  18559. (defun org-base-buffer (buffer)
  18560. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18561. (if (not buffer)
  18562. buffer
  18563. (or (buffer-base-buffer buffer)
  18564. buffer)))
  18565. (defun org-trim (s)
  18566. "Remove whitespace at beginning and end of string."
  18567. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18568. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18569. s)
  18570. (defun org-wrap (string &optional width lines)
  18571. "Wrap string to either a number of lines, or a width in characters.
  18572. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18573. that costs. If there is a word longer than WIDTH, the text is actually
  18574. wrapped to the length of that word.
  18575. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18576. many lines, whatever width that takes.
  18577. The return value is a list of lines, without newlines at the end."
  18578. (let* ((words (org-split-string string "[ \t\n]+"))
  18579. (maxword (apply 'max (mapcar 'org-string-width words)))
  18580. w ll)
  18581. (cond (width
  18582. (org-do-wrap words (max maxword width)))
  18583. (lines
  18584. (setq w maxword)
  18585. (setq ll (org-do-wrap words maxword))
  18586. (if (<= (length ll) lines)
  18587. ll
  18588. (setq ll words)
  18589. (while (> (length ll) lines)
  18590. (setq w (1+ w))
  18591. (setq ll (org-do-wrap words w)))
  18592. ll))
  18593. (t (error "Cannot wrap this")))))
  18594. (defun org-do-wrap (words width)
  18595. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18596. (let (lines line)
  18597. (while words
  18598. (setq line (pop words))
  18599. (while (and words (< (+ (length line) (length (car words))) width))
  18600. (setq line (concat line " " (pop words))))
  18601. (setq lines (push line lines)))
  18602. (nreverse lines)))
  18603. (defun org-split-string (string &optional separators)
  18604. "Splits STRING into substrings at SEPARATORS.
  18605. No empty strings are returned if there are matches at the beginning
  18606. and end of string."
  18607. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18608. (start 0)
  18609. notfirst
  18610. (list nil))
  18611. (while (and (string-match rexp string
  18612. (if (and notfirst
  18613. (= start (match-beginning 0))
  18614. (< start (length string)))
  18615. (1+ start) start))
  18616. (< (match-beginning 0) (length string)))
  18617. (setq notfirst t)
  18618. (or (eq (match-beginning 0) 0)
  18619. (and (eq (match-beginning 0) (match-end 0))
  18620. (eq (match-beginning 0) start))
  18621. (setq list
  18622. (cons (substring string start (match-beginning 0))
  18623. list)))
  18624. (setq start (match-end 0)))
  18625. (or (eq start (length string))
  18626. (setq list
  18627. (cons (substring string start)
  18628. list)))
  18629. (nreverse list)))
  18630. (defun org-quote-vert (s)
  18631. "Replace \"|\" with \"\\vert\"."
  18632. (while (string-match "|" s)
  18633. (setq s (replace-match "\\vert" t t s)))
  18634. s)
  18635. (defun org-uuidgen-p (s)
  18636. "Is S an ID created by UUIDGEN?"
  18637. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18638. (defun org-in-src-block-p (&optional inside)
  18639. "Whether point is in a code source block.
  18640. When INSIDE is non-nil, don't consider we are within a src block
  18641. when point is at #+BEGIN_SRC or #+END_SRC."
  18642. (let ((case-fold-search t) ov)
  18643. (or (and (setq ov (overlays-at (point)))
  18644. (memq 'org-block-background
  18645. (overlay-properties (car ov))))
  18646. (and (not inside)
  18647. (save-match-data
  18648. (save-excursion
  18649. (beginning-of-line)
  18650. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  18651. (defun org-context ()
  18652. "Return a list of contexts of the current cursor position.
  18653. If several contexts apply, all are returned.
  18654. Each context entry is a list with a symbol naming the context, and
  18655. two positions indicating start and end of the context. Possible
  18656. contexts are:
  18657. :headline anywhere in a headline
  18658. :headline-stars on the leading stars in a headline
  18659. :todo-keyword on a TODO keyword (including DONE) in a headline
  18660. :tags on the TAGS in a headline
  18661. :priority on the priority cookie in a headline
  18662. :item on the first line of a plain list item
  18663. :item-bullet on the bullet/number of a plain list item
  18664. :checkbox on the checkbox in a plain list item
  18665. :table in an org-mode table
  18666. :table-special on a special filed in a table
  18667. :table-table in a table.el table
  18668. :clocktable in a clocktable
  18669. :src-block in a source block
  18670. :link on a hyperlink
  18671. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18672. :target on a <<target>>
  18673. :radio-target on a <<<radio-target>>>
  18674. :latex-fragment on a LaTeX fragment
  18675. :latex-preview on a LaTeX fragment with overlaid preview image
  18676. This function expects the position to be visible because it uses font-lock
  18677. faces as a help to recognize the following contexts: :table-special, :link,
  18678. and :keyword."
  18679. (let* ((f (get-text-property (point) 'face))
  18680. (faces (if (listp f) f (list f)))
  18681. (case-fold-search t)
  18682. (p (point)) clist o)
  18683. ;; First the large context
  18684. (cond
  18685. ((org-at-heading-p t)
  18686. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18687. (when (progn
  18688. (beginning-of-line 1)
  18689. (looking-at org-todo-line-tags-regexp))
  18690. (push (org-point-in-group p 1 :headline-stars) clist)
  18691. (push (org-point-in-group p 2 :todo-keyword) clist)
  18692. (push (org-point-in-group p 4 :tags) clist))
  18693. (goto-char p)
  18694. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18695. (if (looking-at "\\[#[A-Z0-9]\\]")
  18696. (push (org-point-in-group p 0 :priority) clist)))
  18697. ((org-at-item-p)
  18698. (push (org-point-in-group p 2 :item-bullet) clist)
  18699. (push (list :item (point-at-bol)
  18700. (save-excursion (org-end-of-item) (point)))
  18701. clist)
  18702. (and (org-at-item-checkbox-p)
  18703. (push (org-point-in-group p 0 :checkbox) clist)))
  18704. ((org-at-table-p)
  18705. (push (list :table (org-table-begin) (org-table-end)) clist)
  18706. (if (memq 'org-formula faces)
  18707. (push (list :table-special
  18708. (previous-single-property-change p 'face)
  18709. (next-single-property-change p 'face)) clist)))
  18710. ((org-at-table-p 'any)
  18711. (push (list :table-table) clist)))
  18712. (goto-char p)
  18713. (let ((case-fold-search t))
  18714. ;; New the "medium" contexts: clocktables, source blocks
  18715. (cond ((org-in-clocktable-p)
  18716. (push (list :clocktable
  18717. (and (or (looking-at "#\\+BEGIN: clocktable")
  18718. (search-backward "#+BEGIN: clocktable" nil t))
  18719. (match-beginning 0))
  18720. (and (re-search-forward "#\\+END:?" nil t)
  18721. (match-end 0))) clist))
  18722. ((org-in-src-block-p)
  18723. (push (list :src-block
  18724. (and (or (looking-at "#\\+BEGIN_SRC")
  18725. (search-backward "#+BEGIN_SRC" nil t))
  18726. (match-beginning 0))
  18727. (and (search-forward "#+END_SRC" nil t)
  18728. (match-beginning 0))) clist))))
  18729. (goto-char p)
  18730. ;; Now the small context
  18731. (cond
  18732. ((org-at-timestamp-p)
  18733. (push (org-point-in-group p 0 :timestamp) clist))
  18734. ((memq 'org-link faces)
  18735. (push (list :link
  18736. (previous-single-property-change p 'face)
  18737. (next-single-property-change p 'face)) clist))
  18738. ((memq 'org-special-keyword faces)
  18739. (push (list :keyword
  18740. (previous-single-property-change p 'face)
  18741. (next-single-property-change p 'face)) clist))
  18742. ((org-at-target-p)
  18743. (push (org-point-in-group p 0 :target) clist)
  18744. (goto-char (1- (match-beginning 0)))
  18745. (if (looking-at org-radio-target-regexp)
  18746. (push (org-point-in-group p 0 :radio-target) clist))
  18747. (goto-char p))
  18748. ((setq o (car (delq nil
  18749. (mapcar
  18750. (lambda (x)
  18751. (if (memq x org-latex-fragment-image-overlays) x))
  18752. (overlays-at (point))))))
  18753. (push (list :latex-fragment
  18754. (overlay-start o) (overlay-end o)) clist)
  18755. (push (list :latex-preview
  18756. (overlay-start o) (overlay-end o)) clist))
  18757. ((org-inside-LaTeX-fragment-p)
  18758. ;; FIXME: positions wrong.
  18759. (push (list :latex-fragment (point) (point)) clist)))
  18760. (setq clist (nreverse (delq nil clist)))
  18761. clist))
  18762. ;; FIXME: Compare with at-regexp-p Do we need both?
  18763. (defun org-in-regexp (re &optional nlines visually)
  18764. "Check if point is inside a match of regexp.
  18765. Normally only the current line is checked, but you can include NLINES extra
  18766. lines both before and after point into the search.
  18767. If VISUALLY is set, require that the cursor is not after the match but
  18768. really on, so that the block visually is on the match."
  18769. (catch 'exit
  18770. (let ((pos (point))
  18771. (eol (point-at-eol (+ 1 (or nlines 0))))
  18772. (inc (if visually 1 0)))
  18773. (save-excursion
  18774. (beginning-of-line (- 1 (or nlines 0)))
  18775. (while (re-search-forward re eol t)
  18776. (if (and (<= (match-beginning 0) pos)
  18777. (>= (+ inc (match-end 0)) pos))
  18778. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18779. (defun org-at-regexp-p (regexp)
  18780. "Is point inside a match of REGEXP in the current line?"
  18781. (catch 'exit
  18782. (save-excursion
  18783. (let ((pos (point)) (end (point-at-eol)))
  18784. (beginning-of-line 1)
  18785. (while (re-search-forward regexp end t)
  18786. (if (and (<= (match-beginning 0) pos)
  18787. (>= (match-end 0) pos))
  18788. (throw 'exit t)))
  18789. nil))))
  18790. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18791. "Non-nil when point is between matches of START-RE and END-RE.
  18792. Also return a non-nil value when point is on one of the matches.
  18793. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18794. buffer positions. Default values are the positions of headlines
  18795. surrounding the point.
  18796. The functions returns a cons cell whose car (resp. cdr) is the
  18797. position before START-RE (resp. after END-RE)."
  18798. (save-match-data
  18799. (let ((pos (point))
  18800. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18801. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18802. beg end)
  18803. (save-excursion
  18804. ;; Point is on a block when on START-RE or if START-RE can be
  18805. ;; found before it...
  18806. (and (or (org-at-regexp-p start-re)
  18807. (re-search-backward start-re limit-up t))
  18808. (setq beg (match-beginning 0))
  18809. ;; ... and END-RE after it...
  18810. (goto-char (match-end 0))
  18811. (re-search-forward end-re limit-down t)
  18812. (> (setq end (match-end 0)) pos)
  18813. ;; ... without another START-RE in-between.
  18814. (goto-char (match-beginning 0))
  18815. (not (re-search-backward start-re (1+ beg) t))
  18816. ;; Return value.
  18817. (cons beg end))))))
  18818. (defun org-in-block-p (names)
  18819. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18820. NAMES is a list of strings containing names of blocks.
  18821. Return first block name matched, or nil. Beware that in case of
  18822. nested blocks, the returned name may not belong to the closest
  18823. block from point."
  18824. (save-match-data
  18825. (catch 'exit
  18826. (let ((case-fold-search t)
  18827. (lim-up (save-excursion (outline-previous-heading)))
  18828. (lim-down (save-excursion (outline-next-heading))))
  18829. (mapc (lambda (name)
  18830. (let ((n (regexp-quote name)))
  18831. (when (org-between-regexps-p
  18832. (concat "^[ \t]*#\\+begin_" n)
  18833. (concat "^[ \t]*#\\+end_" n)
  18834. lim-up lim-down)
  18835. (throw 'exit n))))
  18836. names))
  18837. nil)))
  18838. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18839. "Call `multi-occur' with buffers for all agenda files."
  18840. (interactive "sOrg-files matching: \np")
  18841. (let* ((files (org-agenda-files))
  18842. (tnames (mapcar 'file-truename files))
  18843. (extra org-agenda-text-search-extra-files)
  18844. f)
  18845. (when (eq (car extra) 'agenda-archives)
  18846. (setq extra (cdr extra))
  18847. (setq files (org-add-archive-files files)))
  18848. (while (setq f (pop extra))
  18849. (unless (member (file-truename f) tnames)
  18850. (add-to-list 'files f 'append)
  18851. (add-to-list 'tnames (file-truename f) 'append)))
  18852. (multi-occur
  18853. (mapcar (lambda (x)
  18854. (with-current-buffer
  18855. (or (get-file-buffer x) (find-file-noselect x))
  18856. (widen)
  18857. (current-buffer)))
  18858. files)
  18859. regexp)))
  18860. (if (boundp 'occur-mode-find-occurrence-hook)
  18861. ;; Emacs 23
  18862. (add-hook 'occur-mode-find-occurrence-hook
  18863. (lambda ()
  18864. (when (derived-mode-p 'org-mode)
  18865. (org-reveal))))
  18866. ;; Emacs 22
  18867. (defadvice occur-mode-goto-occurrence
  18868. (after org-occur-reveal activate)
  18869. (and (derived-mode-p 'org-mode) (org-reveal)))
  18870. (defadvice occur-mode-goto-occurrence-other-window
  18871. (after org-occur-reveal activate)
  18872. (and (derived-mode-p 'org-mode) (org-reveal)))
  18873. (defadvice occur-mode-display-occurrence
  18874. (after org-occur-reveal activate)
  18875. (when (derived-mode-p 'org-mode)
  18876. (let ((pos (occur-mode-find-occurrence)))
  18877. (with-current-buffer (marker-buffer pos)
  18878. (save-excursion
  18879. (goto-char pos)
  18880. (org-reveal)))))))
  18881. (defun org-occur-link-in-agenda-files ()
  18882. "Create a link and search for it in the agendas.
  18883. The link is not stored in `org-stored-links', it is just created
  18884. for the search purpose."
  18885. (interactive)
  18886. (let ((link (condition-case nil
  18887. (org-store-link nil)
  18888. (error "Unable to create a link to here"))))
  18889. (org-occur-in-agenda-files (regexp-quote link))))
  18890. (defun org-uniquify (list)
  18891. "Remove duplicate elements from LIST."
  18892. (let (res)
  18893. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18894. res))
  18895. (defun org-delete-all (elts list)
  18896. "Remove all elements in ELTS from LIST."
  18897. (while elts
  18898. (setq list (delete (pop elts) list)))
  18899. list)
  18900. (defun org-count (cl-item cl-seq)
  18901. "Count the number of occurrences of ITEM in SEQ.
  18902. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18903. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18904. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18905. (while (< cl-start cl-end)
  18906. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18907. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18908. (setq cl-start (1+ cl-start)))
  18909. cl-count))
  18910. (defun org-remove-if (predicate seq)
  18911. "Remove everything from SEQ that fulfills PREDICATE."
  18912. (let (res e)
  18913. (while seq
  18914. (setq e (pop seq))
  18915. (if (not (funcall predicate e)) (push e res)))
  18916. (nreverse res)))
  18917. (defun org-remove-if-not (predicate seq)
  18918. "Remove everything from SEQ that does not fulfill PREDICATE."
  18919. (let (res e)
  18920. (while seq
  18921. (setq e (pop seq))
  18922. (if (funcall predicate e) (push e res)))
  18923. (nreverse res)))
  18924. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18925. "Reduce two-argument FUNCTION across SEQ.
  18926. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18927. \":initial-value\" removed."
  18928. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18929. (cadr (memq :initial-value cl-keys)))
  18930. (cl-seq (pop cl-seq))
  18931. (t (funcall cl-func)))))
  18932. (while cl-seq
  18933. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18934. cl-accum))
  18935. (defun org-back-over-empty-lines ()
  18936. "Move backwards over whitespace, to the beginning of the first empty line.
  18937. Returns the number of empty lines passed."
  18938. (let ((pos (point)))
  18939. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18940. (skip-chars-backward " \t\n\r")
  18941. (unless (eobp)
  18942. (forward-line -1)))
  18943. (beginning-of-line 2)
  18944. (goto-char (min (point) pos))
  18945. (count-lines (point) pos)))
  18946. (defun org-skip-whitespace ()
  18947. (skip-chars-forward " \t\n\r"))
  18948. (defun org-point-in-group (point group &optional context)
  18949. "Check if POINT is in match-group GROUP.
  18950. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18951. match. If the match group does not exist or point is not inside it,
  18952. return nil."
  18953. (and (match-beginning group)
  18954. (>= point (match-beginning group))
  18955. (<= point (match-end group))
  18956. (if context
  18957. (list context (match-beginning group) (match-end group))
  18958. t)))
  18959. (defun org-switch-to-buffer-other-window (&rest args)
  18960. "Switch to buffer in a second window on the current frame.
  18961. In particular, do not allow pop-up frames.
  18962. Returns the newly created buffer."
  18963. (org-no-popups
  18964. (apply 'switch-to-buffer-other-window args)))
  18965. (defun org-combine-plists (&rest plists)
  18966. "Create a single property list from all plists in PLISTS.
  18967. The process starts by copying the first list, and then setting properties
  18968. from the other lists. Settings in the last list are the most significant
  18969. ones and overrule settings in the other lists."
  18970. (let ((rtn (copy-sequence (pop plists)))
  18971. p v ls)
  18972. (while plists
  18973. (setq ls (pop plists))
  18974. (while ls
  18975. (setq p (pop ls) v (pop ls))
  18976. (setq rtn (plist-put rtn p v))))
  18977. rtn))
  18978. (defun org-replace-escapes (string table)
  18979. "Replace %-escapes in STRING with values in TABLE.
  18980. TABLE is an association list with keys like \"%a\" and string values.
  18981. The sequences in STRING may contain normal field width and padding information,
  18982. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18983. so values can contain further %-escapes if they are define later in TABLE."
  18984. (let ((tbl (copy-alist table))
  18985. (case-fold-search nil)
  18986. (pchg 0)
  18987. e re rpl)
  18988. (while (setq e (pop tbl))
  18989. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18990. (when (and (cdr e) (string-match re (cdr e)))
  18991. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18992. (safe "SREF"))
  18993. (add-text-properties 0 3 (list 'sref sref) safe)
  18994. (setcdr e (replace-match safe t t (cdr e)))))
  18995. (while (string-match re string)
  18996. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18997. (cdr e)))
  18998. (setq string (replace-match rpl t t string))))
  18999. (while (setq pchg (next-property-change pchg string))
  19000. (let ((sref (get-text-property pchg 'sref string)))
  19001. (when (and sref (string-match "SREF" string pchg))
  19002. (setq string (replace-match sref t t string)))))
  19003. string))
  19004. (defun org-sublist (list start end)
  19005. "Return a section of LIST, from START to END.
  19006. Counting starts at 1."
  19007. (let (rtn (c start))
  19008. (setq list (nthcdr (1- start) list))
  19009. (while (and list (<= c end))
  19010. (push (pop list) rtn)
  19011. (setq c (1+ c)))
  19012. (nreverse rtn)))
  19013. (defun org-find-base-buffer-visiting (file)
  19014. "Like `find-buffer-visiting' but always return the base buffer and
  19015. not an indirect buffer."
  19016. (let ((buf (or (get-file-buffer file)
  19017. (find-buffer-visiting file))))
  19018. (if buf
  19019. (or (buffer-base-buffer buf) buf)
  19020. nil)))
  19021. (defun org-image-file-name-regexp (&optional extensions)
  19022. "Return regexp matching the file names of images.
  19023. If EXTENSIONS is given, only match these."
  19024. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19025. (image-file-name-regexp)
  19026. (let ((image-file-name-extensions
  19027. (or extensions
  19028. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19029. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19030. (concat "\\."
  19031. (regexp-opt (nconc (mapcar 'upcase
  19032. image-file-name-extensions)
  19033. image-file-name-extensions)
  19034. t)
  19035. "\\'"))))
  19036. (defun org-file-image-p (file &optional extensions)
  19037. "Return non-nil if FILE is an image."
  19038. (save-match-data
  19039. (string-match (org-image-file-name-regexp extensions) file)))
  19040. (defun org-get-cursor-date ()
  19041. "Return the date at cursor in as a time.
  19042. This works in the calendar and in the agenda, anywhere else it just
  19043. returns the current time."
  19044. (let (date day defd)
  19045. (cond
  19046. ((eq major-mode 'calendar-mode)
  19047. (setq date (calendar-cursor-to-date)
  19048. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19049. ((eq major-mode 'org-agenda-mode)
  19050. (setq day (get-text-property (point) 'day))
  19051. (if day
  19052. (setq date (calendar-gregorian-from-absolute day)
  19053. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19054. (nth 2 date))))))
  19055. (or defd (current-time))))
  19056. (defun org-mark-subtree (&optional up)
  19057. "Mark the current subtree.
  19058. This puts point at the start of the current subtree, and mark at
  19059. the end. If a numeric prefix UP is given, move up into the
  19060. hierarchy of headlines by UP levels before marking the subtree."
  19061. (interactive "P")
  19062. (org-with-limited-levels
  19063. (cond ((org-at-heading-p) (beginning-of-line))
  19064. ((org-before-first-heading-p) (error "Not in a subtree"))
  19065. (t (outline-previous-visible-heading 1))))
  19066. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19067. (if (org-called-interactively-p 'any)
  19068. (call-interactively 'org-mark-element)
  19069. (org-mark-element)))
  19070. ;;; Indentation
  19071. (defun org-indent-line ()
  19072. "Indent line depending on context."
  19073. (interactive)
  19074. (let* ((pos (point))
  19075. (itemp (org-at-item-p))
  19076. (case-fold-search t)
  19077. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19078. (inline-task-p (and (featurep 'org-inlinetask)
  19079. (org-inlinetask-in-task-p)))
  19080. (inline-re (and inline-task-p
  19081. (org-inlinetask-outline-regexp)))
  19082. column)
  19083. (if (and orgstruct-is-++ (eq pos (point)))
  19084. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19085. (indent-according-to-mode))
  19086. (beginning-of-line 1)
  19087. (cond
  19088. ;; Headings
  19089. ((looking-at org-outline-regexp) (setq column 0))
  19090. ;; Included files
  19091. ((looking-at "#\\+include:") (setq column 0))
  19092. ;; Footnote definition
  19093. ((looking-at org-footnote-definition-re) (setq column 0))
  19094. ;; Literal examples
  19095. ((looking-at "[ \t]*:\\( \\|$\\)")
  19096. (setq column (org-get-indentation))) ; do nothing
  19097. ;; Lists
  19098. ((ignore-errors (goto-char (org-in-item-p)))
  19099. (setq column (if itemp
  19100. (org-get-indentation)
  19101. (org-list-item-body-column (point))))
  19102. (goto-char pos))
  19103. ;; Drawers
  19104. ((and (looking-at "[ \t]*:END:")
  19105. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19106. (save-excursion
  19107. (goto-char (1- (match-beginning 1)))
  19108. (setq column (current-column))))
  19109. ;; Special blocks
  19110. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19111. (save-excursion
  19112. (re-search-backward
  19113. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19114. (setq column (org-get-indentation (match-string 0))))
  19115. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19116. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19117. (save-excursion
  19118. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19119. (setq column
  19120. (cond ((equal (downcase (match-string 1)) "src")
  19121. ;; src blocks: let `org-edit-src-exit' handle them
  19122. (org-get-indentation))
  19123. ((equal (downcase (match-string 1)) "example")
  19124. (max (org-get-indentation)
  19125. (org-get-indentation (match-string 0))))
  19126. (t
  19127. (org-get-indentation (match-string 0))))))
  19128. ;; This line has nothing special, look at the previous relevant
  19129. ;; line to compute indentation
  19130. (t
  19131. (beginning-of-line 0)
  19132. (while (and (not (bobp))
  19133. (not (looking-at org-table-line-regexp))
  19134. (not (looking-at org-drawer-regexp))
  19135. ;; When point started in an inline task, do not move
  19136. ;; above task starting line.
  19137. (not (and inline-task-p (looking-at inline-re)))
  19138. ;; Skip drawers, blocks, empty lines, verbatim,
  19139. ;; comments, tables, footnotes definitions, lists,
  19140. ;; inline tasks.
  19141. (or (and (looking-at "[ \t]*:END:")
  19142. (re-search-backward org-drawer-regexp nil t))
  19143. (and (looking-at "[ \t]*#\\+end_")
  19144. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19145. (looking-at "[ \t]*[\n:#|]")
  19146. (looking-at org-footnote-definition-re)
  19147. (and (ignore-errors (goto-char (org-in-item-p)))
  19148. (goto-char
  19149. (org-list-get-top-point (org-list-struct))))
  19150. (and (not inline-task-p)
  19151. (featurep 'org-inlinetask)
  19152. (org-inlinetask-in-task-p)
  19153. (or (org-inlinetask-goto-beginning) t))))
  19154. (beginning-of-line 0))
  19155. (cond
  19156. ;; There was an heading above.
  19157. ((looking-at "\\*+[ \t]+")
  19158. (if (not org-adapt-indentation)
  19159. (setq column 0)
  19160. (goto-char (match-end 0))
  19161. (setq column (current-column))))
  19162. ;; A drawer had started and is unfinished
  19163. ((looking-at org-drawer-regexp)
  19164. (goto-char (1- (match-beginning 1)))
  19165. (setq column (current-column)))
  19166. ;; Else, nothing noticeable found: get indentation and go on.
  19167. (t (setq column (org-get-indentation))))))
  19168. ;; Now apply indentation and move cursor accordingly
  19169. (goto-char pos)
  19170. (if (<= (current-column) (current-indentation))
  19171. (org-indent-line-to column)
  19172. (save-excursion (org-indent-line-to column)))
  19173. ;; Special polishing for properties, see `org-property-format'
  19174. (setq column (current-column))
  19175. (beginning-of-line 1)
  19176. (if (looking-at
  19177. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19178. (replace-match (concat (match-string 1)
  19179. (format org-property-format
  19180. (match-string 2) (match-string 3)))
  19181. t t))
  19182. (org-move-to-column column))))
  19183. (defun org-indent-drawer ()
  19184. "Indent the drawer at point."
  19185. (interactive)
  19186. (let ((p (point))
  19187. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19188. (match-end 0)))
  19189. (folded
  19190. (save-excursion
  19191. (end-of-line)
  19192. (when (overlays-at (point))
  19193. (member 'invisible (overlay-properties
  19194. (car (overlays-at (point)))))))))
  19195. (when folded (org-cycle))
  19196. (indent-for-tab-command)
  19197. (while (and (move-beginning-of-line 2) (< (point) e))
  19198. (indent-for-tab-command))
  19199. (goto-char p)
  19200. (when folded (org-cycle)))
  19201. (message "Drawer at point indented"))
  19202. (defun org-indent-block ()
  19203. "Indent the block at point."
  19204. (interactive)
  19205. (let ((p (point))
  19206. (case-fold-search t)
  19207. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19208. (match-end 0)))
  19209. (folded
  19210. (save-excursion
  19211. (end-of-line)
  19212. (when (overlays-at (point))
  19213. (member 'invisible (overlay-properties
  19214. (car (overlays-at (point)))))))))
  19215. (when folded (org-cycle))
  19216. (indent-for-tab-command)
  19217. (while (and (move-beginning-of-line 2) (< (point) e))
  19218. (indent-for-tab-command))
  19219. (goto-char p)
  19220. (when folded (org-cycle)))
  19221. (message "Block at point indented"))
  19222. (defun org-indent-region (start end)
  19223. "Indent region."
  19224. (interactive "r")
  19225. (save-excursion
  19226. (let ((line-end (org-current-line end)))
  19227. (goto-char start)
  19228. (while (< (org-current-line) line-end)
  19229. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19230. (t (call-interactively 'org-indent-line)))
  19231. (move-beginning-of-line 2)))))
  19232. ;;; Filling
  19233. ;; We use our own fill-paragraph and auto-fill functions.
  19234. ;; `org-fill-paragraph' relies on adaptive filling and context
  19235. ;; checking. Appropriate `fill-prefix' is computed with
  19236. ;; `org-adaptive-fill-function'.
  19237. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19238. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19239. ;; `org-adaptive-fill-function' value. Internally,
  19240. ;; `org-comment-line-break-function' breaks the line.
  19241. ;; `org-setup-filling' installs filling and auto-filling related
  19242. ;; variables during `org-mode' initialization.
  19243. (defun org-setup-filling ()
  19244. (interactive)
  19245. ;; Prevent auto-fill from inserting unwanted new items.
  19246. (when (boundp 'fill-nobreak-predicate)
  19247. (org-set-local
  19248. 'fill-nobreak-predicate
  19249. (org-uniquify
  19250. (append fill-nobreak-predicate
  19251. '(org-fill-paragraph-separate-nobreak-p
  19252. org-fill-line-break-nobreak-p)))))
  19253. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19254. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19255. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19256. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19257. (defvar org-element-paragraph-separate) ; org-element.el
  19258. (defun org-fill-paragraph-separate-nobreak-p ()
  19259. "Non-nil when a line break at point would insert a new item."
  19260. (looking-at (substring org-element-paragraph-separate 1)))
  19261. (defun org-fill-line-break-nobreak-p ()
  19262. "Non-nil when a line break at point would create an Org line break."
  19263. (save-excursion
  19264. (skip-chars-backward "[ \t]")
  19265. (skip-chars-backward "\\\\")
  19266. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19267. (declare-function message-in-body-p "message" ())
  19268. (defvar org-element--affiliated-re) ; From org-element.el
  19269. (defun org-adaptive-fill-function ()
  19270. "Compute a fill prefix for the current line.
  19271. Return fill prefix, as a string, or nil if current line isn't
  19272. meant to be filled."
  19273. (org-with-wide-buffer
  19274. (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
  19275. ;; FIXME: This is really the job of orgstruct++-mode
  19276. (let* ((p (line-beginning-position))
  19277. (element (save-excursion (beginning-of-line)
  19278. (org-element-at-point)))
  19279. (type (org-element-type element))
  19280. (post-affiliated
  19281. (save-excursion
  19282. (goto-char (org-element-property :begin element))
  19283. (while (looking-at org-element--affiliated-re) (forward-line))
  19284. (point))))
  19285. (unless (< p post-affiliated)
  19286. (case type
  19287. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19288. (footnote-definition "")
  19289. ((item plain-list)
  19290. (make-string (org-list-item-body-column post-affiliated) ? ))
  19291. (paragraph
  19292. ;; Fill prefix is usually the same as the current line,
  19293. ;; except if the paragraph is at the beginning of an item.
  19294. (let ((parent (org-element-property :parent element)))
  19295. (cond ((eq (org-element-type parent) 'item)
  19296. (make-string (org-list-item-body-column
  19297. (org-element-property :begin parent))
  19298. ? ))
  19299. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19300. (match-string 0))
  19301. (t ""))))
  19302. (comment-block
  19303. ;; Only fill contents if P is within block boundaries.
  19304. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19305. (forward-line)
  19306. (point)))
  19307. (cend (save-excursion
  19308. (goto-char (org-element-property :end element))
  19309. (skip-chars-backward " \r\t\n")
  19310. (line-beginning-position))))
  19311. (when (and (>= p cbeg) (< p cend))
  19312. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19313. (match-string 0)
  19314. ""))))))))))
  19315. (declare-function message-goto-body "message" ())
  19316. (defvar message-cite-prefix-regexp) ; From message.el
  19317. (defvar org-element-all-objects) ; From org-element.el
  19318. (defun org-fill-paragraph (&optional justify)
  19319. "Fill element at point, when applicable.
  19320. This function only applies to comment blocks, comments, example
  19321. blocks and paragraphs. Also, as a special case, re-align table
  19322. when point is at one.
  19323. If JUSTIFY is non-nil (interactively, with prefix argument),
  19324. justify as well. If `sentence-end-double-space' is non-nil, then
  19325. period followed by one space does not end a sentence, so don't
  19326. break a line there. The variable `fill-column' controls the
  19327. width for filling.
  19328. For convenience, when point is at a plain list, an item or
  19329. a footnote definition, try to fill the first paragraph within."
  19330. (interactive)
  19331. (if (and (derived-mode-p 'message-mode)
  19332. (or (not (message-in-body-p))
  19333. (save-excursion (move-beginning-of-line 1)
  19334. (looking-at message-cite-prefix-regexp))))
  19335. ;; First ensure filling is correct in message-mode.
  19336. (let ((fill-paragraph-function
  19337. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19338. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19339. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19340. (paragraph-separate
  19341. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19342. (fill-paragraph nil))
  19343. (save-excursion
  19344. ;; Move to end of line in order to get the first paragraph
  19345. ;; within a plain list or a footnote definition.
  19346. (end-of-line)
  19347. (let ((element (org-element-at-point)))
  19348. ;; First check if point is in a blank line at the beginning of
  19349. ;; the buffer. In that case, ignore filling.
  19350. (if (< (point) (org-element-property :begin element)) t
  19351. (case (org-element-type element)
  19352. ;; Use major mode filling function is src blocks.
  19353. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19354. ;; Align Org tables, leave table.el tables as-is.
  19355. (table-row (org-table-align) t)
  19356. (table
  19357. (when (eq (org-element-property :type element) 'org)
  19358. (org-table-align))
  19359. t)
  19360. (paragraph
  19361. ;; Paragraphs may contain `line-break' type objects.
  19362. (let ((beg (max (point-min)
  19363. (org-element-property :contents-begin element)))
  19364. (end (min (point-max)
  19365. (org-element-property :contents-end element))))
  19366. ;; Do nothing if point is at an affiliated keyword.
  19367. (if (< (point) beg) t
  19368. (when (derived-mode-p 'message-mode)
  19369. ;; In `message-mode', do not fill following
  19370. ;; citation in current paragraph nor text before
  19371. ;; message body.
  19372. (let ((body-start (save-excursion (message-goto-body))))
  19373. (when body-start (setq beg (max body-start beg))))
  19374. (when (save-excursion
  19375. (re-search-forward
  19376. (concat "^" message-cite-prefix-regexp) end t))
  19377. (setq end (match-beginning 0))))
  19378. ;; Fill paragraph, taking line breaks into
  19379. ;; consideration. For that, slice the paragraph
  19380. ;; using line breaks as separators, and fill the
  19381. ;; parts in reverse order to avoid messing with
  19382. ;; markers.
  19383. (save-excursion
  19384. (goto-char end)
  19385. (mapc
  19386. (lambda (pos)
  19387. (fill-region-as-paragraph pos (point) justify)
  19388. (goto-char pos))
  19389. ;; Find the list of ending positions for line
  19390. ;; breaks in the current paragraph. Add paragraph
  19391. ;; beginning to include first slice.
  19392. (nreverse
  19393. (cons
  19394. beg
  19395. (org-element-map
  19396. (org-element--parse-objects
  19397. beg end nil org-element-all-objects)
  19398. 'line-break
  19399. (lambda (lb) (org-element-property :end lb)))))))
  19400. t)))
  19401. ;; Contents of `comment-block' type elements should be
  19402. ;; filled as plain text, but only if point is within block
  19403. ;; markers.
  19404. (comment-block
  19405. (let* ((case-fold-search t)
  19406. (beg (save-excursion
  19407. (goto-char (org-element-property :begin element))
  19408. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19409. (forward-line)
  19410. (point)))
  19411. (end (save-excursion
  19412. (goto-char (org-element-property :end element))
  19413. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19414. (line-beginning-position))))
  19415. (when (and (>= (point) beg) (< (point) end))
  19416. (fill-region-as-paragraph
  19417. (save-excursion
  19418. (end-of-line)
  19419. (re-search-backward "^[ \t]*$" beg 'move)
  19420. (line-beginning-position))
  19421. (save-excursion
  19422. (beginning-of-line)
  19423. (re-search-forward "^[ \t]*$" end 'move)
  19424. (line-beginning-position))
  19425. justify)))
  19426. t)
  19427. ;; Fill comments.
  19428. (comment (fill-comment-paragraph justify))
  19429. ;; Ignore every other element.
  19430. (otherwise t)))))))
  19431. (defun org-auto-fill-function ()
  19432. "Auto-fill function."
  19433. ;; Check if auto-filling is meaningful.
  19434. (let ((fc (current-fill-column)))
  19435. (when (and fc (> (current-column) fc))
  19436. (let* ((fill-prefix (org-adaptive-fill-function))
  19437. ;; Enforce empty fill prefix, if required. Otherwise, it
  19438. ;; will be computed again.
  19439. (adaptive-fill-mode (not (equal fill-prefix ""))))
  19440. (when fill-prefix (do-auto-fill))))))
  19441. (defun org-comment-line-break-function (&optional soft)
  19442. "Break line at point and indent, continuing comment if within one.
  19443. The inserted newline is marked hard if variable
  19444. `use-hard-newlines' is true, unless optional argument SOFT is
  19445. non-nil."
  19446. (if soft (insert-and-inherit ?\n) (newline 1))
  19447. (save-excursion (forward-char -1) (delete-horizontal-space))
  19448. (delete-horizontal-space)
  19449. (indent-to-left-margin)
  19450. (insert-before-markers-and-inherit fill-prefix))
  19451. ;;; Comments
  19452. ;; Org comments syntax is quite complex. It requires the entire line
  19453. ;; to be just a comment. Also, even with the right syntax at the
  19454. ;; beginning of line, some some elements (i.e. verse-block or
  19455. ;; example-block) don't accept comments. Usual Emacs comment commands
  19456. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19457. ;; Org still relies on `comment-dwim', but cannot trust
  19458. ;; `comment-only-p'. So, `comment-region-function' and
  19459. ;; `uncomment-region-function' both point
  19460. ;; to`org-comment-or-uncomment-region'. Eventually,
  19461. ;; `org-insert-comment' takes care of insertion of comments at the
  19462. ;; beginning of line.
  19463. ;; `org-setup-comments-handling' install comments related variables
  19464. ;; during `org-mode' initialization.
  19465. (defun org-setup-comments-handling ()
  19466. (interactive)
  19467. (org-set-local 'comment-use-syntax nil)
  19468. (org-set-local 'comment-start "# ")
  19469. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19470. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19471. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19472. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19473. (defun org-insert-comment ()
  19474. "Insert an empty comment above current line.
  19475. If the line is empty, insert comment at its beginning."
  19476. (beginning-of-line)
  19477. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19478. (org-indent-line)
  19479. (insert "# "))
  19480. (defvar comment-empty-lines) ; From newcomment.el.
  19481. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19482. "Comment or uncomment each non-blank line in the region.
  19483. Uncomment each non-blank line between BEG and END if it only
  19484. contains commented lines. Otherwise, comment them."
  19485. (save-restriction
  19486. ;; Restrict region
  19487. (narrow-to-region (save-excursion (goto-char beg)
  19488. (skip-chars-forward " \r\t\n" end)
  19489. (line-beginning-position))
  19490. (save-excursion (goto-char end)
  19491. (skip-chars-backward " \r\t\n" beg)
  19492. (line-end-position)))
  19493. (let ((uncommentp
  19494. ;; UNCOMMENTP is non-nil when every non blank line between
  19495. ;; BEG and END is a comment.
  19496. (save-excursion
  19497. (goto-char (point-min))
  19498. (while (and (not (eobp))
  19499. (let ((element (org-element-at-point)))
  19500. (and (eq (org-element-type element) 'comment)
  19501. (goto-char (min (point-max)
  19502. (org-element-property
  19503. :end element)))))))
  19504. (eobp))))
  19505. (if uncommentp
  19506. ;; Only blank lines and comments in region: uncomment it.
  19507. (save-excursion
  19508. (goto-char (point-min))
  19509. (while (not (eobp))
  19510. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19511. (replace-match "" nil nil nil 1))
  19512. (forward-line)))
  19513. ;; Comment each line in region.
  19514. (let ((min-indent (point-max)))
  19515. ;; First find the minimum indentation across all lines.
  19516. (save-excursion
  19517. (goto-char (point-min))
  19518. (while (and (not (eobp)) (not (zerop min-indent)))
  19519. (unless (looking-at "[ \t]*$")
  19520. (setq min-indent (min min-indent (current-indentation))))
  19521. (forward-line)))
  19522. ;; Then loop over all lines.
  19523. (save-excursion
  19524. (goto-char (point-min))
  19525. (while (not (eobp))
  19526. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19527. (org-move-to-column min-indent t)
  19528. (insert comment-start))
  19529. (forward-line))))))))
  19530. ;;; Other stuff.
  19531. (defun org-toggle-fixed-width-section (arg)
  19532. "Toggle the fixed-width export.
  19533. If there is no active region, the QUOTE keyword at the current headline is
  19534. inserted or removed. When present, it causes the text between this headline
  19535. and the next to be exported as fixed-width text, and unmodified.
  19536. If there is an active region, this command adds or removes a colon as the
  19537. first character of this line. If the first character of a line is a colon,
  19538. this line is also exported in fixed-width font."
  19539. (interactive "P")
  19540. (let* ((cc 0)
  19541. (regionp (org-region-active-p))
  19542. (beg (if regionp (region-beginning) (point)))
  19543. (end (if regionp (region-end)))
  19544. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19545. (case-fold-search nil)
  19546. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19547. off)
  19548. (if regionp
  19549. (save-excursion
  19550. (goto-char beg)
  19551. (setq cc (current-column))
  19552. (beginning-of-line 1)
  19553. (setq off (looking-at re))
  19554. (while (> nlines 0)
  19555. (setq nlines (1- nlines))
  19556. (beginning-of-line 1)
  19557. (cond
  19558. (arg
  19559. (org-move-to-column cc t)
  19560. (insert ": \n")
  19561. (forward-line -1))
  19562. ((and off (looking-at re))
  19563. (replace-match "" t t nil 1))
  19564. ((not off) (org-move-to-column cc t) (insert ": ")))
  19565. (forward-line 1)))
  19566. (save-excursion
  19567. (org-back-to-heading)
  19568. (cond
  19569. ((looking-at (format org-heading-keyword-regexp-format
  19570. org-quote-string))
  19571. (goto-char (match-end 1))
  19572. (looking-at (concat " +" org-quote-string))
  19573. (replace-match "" t t)
  19574. (when (eolp) (insert " ")))
  19575. ((looking-at org-outline-regexp)
  19576. (goto-char (match-end 0))
  19577. (insert org-quote-string " ")))))))
  19578. (defun org-reftex-citation ()
  19579. "Use reftex-citation to insert a citation into the buffer.
  19580. This looks for a line like
  19581. #+BIBLIOGRAPHY: foo plain option:-d
  19582. and derives from it that foo.bib is the bibliography file relevant
  19583. for this document. It then installs the necessary environment for RefTeX
  19584. to work in this buffer and calls `reftex-citation' to insert a citation
  19585. into the buffer.
  19586. Export of such citations to both LaTeX and HTML is handled by the contributed
  19587. package org-exp-bibtex by Taru Karttunen."
  19588. (interactive)
  19589. (let ((reftex-docstruct-symbol 'rds)
  19590. (reftex-cite-format "\\cite{%l}")
  19591. rds bib)
  19592. (save-excursion
  19593. (save-restriction
  19594. (widen)
  19595. (let ((case-fold-search t)
  19596. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19597. (if (not (save-excursion
  19598. (or (re-search-forward re nil t)
  19599. (re-search-backward re nil t))))
  19600. (error "No bibliography defined in file")
  19601. (setq bib (concat (match-string 1) ".bib")
  19602. rds (list (list 'bib bib)))))))
  19603. (call-interactively 'reftex-citation)))
  19604. ;;;; Functions extending outline functionality
  19605. (defun org-beginning-of-line (&optional arg)
  19606. "Go to the beginning of the current line. If that is invisible, continue
  19607. to a visible line beginning. This makes the function of C-a more intuitive.
  19608. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19609. first attempt, and only move to after the tags when the cursor is already
  19610. beyond the end of the headline."
  19611. (interactive "P")
  19612. (let ((pos (point))
  19613. (special (if (consp org-special-ctrl-a/e)
  19614. (car org-special-ctrl-a/e)
  19615. org-special-ctrl-a/e))
  19616. refpos)
  19617. (if (org-bound-and-true-p visual-line-mode)
  19618. (beginning-of-visual-line 1)
  19619. (beginning-of-line 1))
  19620. (if (and arg (fboundp 'move-beginning-of-line))
  19621. (call-interactively 'move-beginning-of-line)
  19622. (if (bobp)
  19623. nil
  19624. (backward-char 1)
  19625. (if (org-truely-invisible-p)
  19626. (while (and (not (bobp)) (org-truely-invisible-p))
  19627. (backward-char 1)
  19628. (beginning-of-line 1))
  19629. (forward-char 1))))
  19630. (when special
  19631. (cond
  19632. ((and (looking-at org-complex-heading-regexp)
  19633. (= (char-after (match-end 1)) ?\ ))
  19634. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19635. (point-at-eol)))
  19636. (goto-char
  19637. (if (eq special t)
  19638. (cond ((> pos refpos) refpos)
  19639. ((= pos (point)) refpos)
  19640. (t (point)))
  19641. (cond ((> pos (point)) (point))
  19642. ((not (eq last-command this-command)) (point))
  19643. (t refpos)))))
  19644. ((org-at-item-p)
  19645. ;; Being at an item and not looking at an the item means point
  19646. ;; was previously moved to beginning of a visual line, which
  19647. ;; doesn't contain the item. Therefore, do nothing special,
  19648. ;; just stay here.
  19649. (when (looking-at org-list-full-item-re)
  19650. ;; Set special position at first white space character after
  19651. ;; bullet, and check-box, if any.
  19652. (let ((after-bullet
  19653. (let ((box (match-end 3)))
  19654. (if (not box) (match-end 1)
  19655. (let ((after (char-after box)))
  19656. (if (and after (= after ? )) (1+ box) box))))))
  19657. ;; Special case: Move point to special position when
  19658. ;; currently after it or at beginning of line.
  19659. (if (eq special t)
  19660. (when (or (> pos after-bullet) (= (point) pos))
  19661. (goto-char after-bullet))
  19662. ;; Reversed case: Move point to special position when
  19663. ;; point was already at beginning of line and command is
  19664. ;; repeated.
  19665. (when (and (= (point) pos) (eq last-command this-command))
  19666. (goto-char after-bullet))))))))
  19667. (org-no-warnings
  19668. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19669. (defun org-end-of-line (&optional arg)
  19670. "Go to the end of the line.
  19671. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  19672. tags on the first attempt, and only move to after the tags when
  19673. the cursor is already beyond the end of the headline."
  19674. (interactive "P")
  19675. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  19676. org-special-ctrl-a/e))
  19677. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  19678. 'end-of-visual-line)
  19679. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19680. (t 'end-of-line))))
  19681. (if (or (not special) arg) (call-interactively move-fun)
  19682. (let* ((element (save-excursion (beginning-of-line)
  19683. (org-element-at-point)))
  19684. (type (org-element-type element)))
  19685. (cond
  19686. ((memq type '(headline inlinetask))
  19687. (let ((pos (point)))
  19688. (beginning-of-line 1)
  19689. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19690. (if (eq special t)
  19691. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  19692. (goto-char (match-beginning 1))
  19693. (goto-char (match-end 0)))
  19694. (if (or (< pos (match-end 0))
  19695. (not (eq this-command last-command)))
  19696. (goto-char (match-end 0))
  19697. (goto-char (match-beginning 1))))
  19698. (call-interactively move-fun))))
  19699. ((org-element-property :hiddenp element)
  19700. ;; If element is hidden, `move-end-of-line' would put point
  19701. ;; after it. Use `end-of-line' to stay on current line.
  19702. (call-interactively 'end-of-line))
  19703. (t (call-interactively move-fun)))))
  19704. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19705. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19706. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19707. (defun org-backward-sentence (&optional arg)
  19708. "Go to beginning of sentence, or beginning of table field.
  19709. This will call `backward-sentence' or `org-table-beginning-of-field',
  19710. depending on context."
  19711. (interactive "P")
  19712. (cond
  19713. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19714. (t (call-interactively 'backward-sentence))))
  19715. (defun org-forward-sentence (&optional arg)
  19716. "Go to end of sentence, or end of table field.
  19717. This will call `forward-sentence' or `org-table-end-of-field',
  19718. depending on context."
  19719. (interactive "P")
  19720. (cond
  19721. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19722. (t (call-interactively 'forward-sentence))))
  19723. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19724. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19725. (defun org-kill-line (&optional arg)
  19726. "Kill line, to tags or end of line."
  19727. (interactive "P")
  19728. (cond
  19729. ((or (not org-special-ctrl-k)
  19730. (bolp)
  19731. (not (org-at-heading-p)))
  19732. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19733. org-ctrl-k-protect-subtree)
  19734. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19735. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19736. (error "C-k aborted - would kill hidden subtree")))
  19737. (call-interactively
  19738. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  19739. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19740. (kill-region (point) (match-beginning 1))
  19741. (org-set-tags nil t))
  19742. (t (kill-region (point) (point-at-eol)))))
  19743. (define-key org-mode-map "\C-k" 'org-kill-line)
  19744. (defun org-yank (&optional arg)
  19745. "Yank. If the kill is a subtree, treat it specially.
  19746. This command will look at the current kill and check if is a single
  19747. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19748. cursor is at the beginning of a line or after the stars of a currently
  19749. empty headline, then the yank is handled specially. How exactly depends
  19750. on the value of the following variables, both set by default.
  19751. org-yank-folded-subtrees
  19752. When set, the subtree(s) will be folded after insertion, but only
  19753. if doing so would now swallow text after the yanked text.
  19754. org-yank-adjusted-subtrees
  19755. When set, the subtree will be promoted or demoted in order to
  19756. fit into the local outline tree structure, which means that the level
  19757. will be adjusted so that it becomes the smaller one of the two
  19758. *visible* surrounding headings.
  19759. Any prefix to this command will cause `yank' to be called directly with
  19760. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19761. will just
  19762. plainly yank the text as it is.
  19763. \[1] The test checks if the first non-white line is a heading
  19764. and if there are no other headings with fewer stars."
  19765. (interactive "P")
  19766. (org-yank-generic 'yank arg))
  19767. (defun org-yank-generic (command arg)
  19768. "Perform some yank-like command.
  19769. This function implements the behavior described in the `org-yank'
  19770. documentation. However, it has been generalized to work for any
  19771. interactive command with similar behavior."
  19772. ;; pretend to be command COMMAND
  19773. (setq this-command command)
  19774. (if arg
  19775. (call-interactively command)
  19776. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19777. (and (org-kill-is-subtree-p)
  19778. (or (bolp)
  19779. (and (looking-at "[ \t]*$")
  19780. (string-match
  19781. "\\`\\*+\\'"
  19782. (buffer-substring (point-at-bol) (point)))))))
  19783. swallowp)
  19784. (cond
  19785. ((and subtreep org-yank-folded-subtrees)
  19786. (let ((beg (point))
  19787. end)
  19788. (if (and subtreep org-yank-adjusted-subtrees)
  19789. (org-paste-subtree nil nil 'for-yank)
  19790. (call-interactively command))
  19791. (setq end (point))
  19792. (goto-char beg)
  19793. (when (and (bolp) subtreep
  19794. (not (setq swallowp
  19795. (org-yank-folding-would-swallow-text beg end))))
  19796. (org-with-limited-levels
  19797. (or (looking-at org-outline-regexp)
  19798. (re-search-forward org-outline-regexp-bol end t))
  19799. (while (and (< (point) end) (looking-at org-outline-regexp))
  19800. (hide-subtree)
  19801. (org-cycle-show-empty-lines 'folded)
  19802. (condition-case nil
  19803. (outline-forward-same-level 1)
  19804. (error (goto-char end))))))
  19805. (when swallowp
  19806. (message
  19807. "Inserted text not folded because that would swallow text"))
  19808. (goto-char end)
  19809. (skip-chars-forward " \t\n\r")
  19810. (beginning-of-line 1)
  19811. (push-mark beg 'nomsg)))
  19812. ((and subtreep org-yank-adjusted-subtrees)
  19813. (let ((beg (point-at-bol)))
  19814. (org-paste-subtree nil nil 'for-yank)
  19815. (push-mark beg 'nomsg)))
  19816. (t
  19817. (call-interactively command))))))
  19818. (defun org-yank-folding-would-swallow-text (beg end)
  19819. "Would hide-subtree at BEG swallow any text after END?"
  19820. (let (level)
  19821. (org-with-limited-levels
  19822. (save-excursion
  19823. (goto-char beg)
  19824. (when (or (looking-at org-outline-regexp)
  19825. (re-search-forward org-outline-regexp-bol end t))
  19826. (setq level (org-outline-level)))
  19827. (goto-char end)
  19828. (skip-chars-forward " \t\r\n\v\f")
  19829. (if (or (eobp)
  19830. (and (bolp) (looking-at org-outline-regexp)
  19831. (<= (org-outline-level) level)))
  19832. nil ; Nothing would be swallowed
  19833. t))))) ; something would swallow
  19834. (define-key org-mode-map "\C-y" 'org-yank)
  19835. (defun org-truely-invisible-p ()
  19836. "Check if point is at a character currently not visible.
  19837. This version does not only check the character property, but also
  19838. `visible-mode'."
  19839. ;; Early versions of noutline don't have `outline-invisible-p'.
  19840. (if (org-bound-and-true-p visible-mode)
  19841. nil
  19842. (outline-invisible-p)))
  19843. (defun org-invisible-p2 ()
  19844. "Check if point is at a character currently not visible."
  19845. (save-excursion
  19846. (if (and (eolp) (not (bobp))) (backward-char 1))
  19847. ;; Early versions of noutline don't have `outline-invisible-p'.
  19848. (outline-invisible-p)))
  19849. (defun org-back-to-heading (&optional invisible-ok)
  19850. "Call `outline-back-to-heading', but provide a better error message."
  19851. (condition-case nil
  19852. (outline-back-to-heading invisible-ok)
  19853. (error (error "Before first headline at position %d in buffer %s"
  19854. (point) (current-buffer)))))
  19855. (defun org-before-first-heading-p ()
  19856. "Before first heading?"
  19857. (save-excursion
  19858. (end-of-line)
  19859. (null (re-search-backward org-outline-regexp-bol nil t))))
  19860. (defun org-at-heading-p (&optional ignored)
  19861. (outline-on-heading-p t))
  19862. ;; Compatibility alias with Org versions < 7.8.03
  19863. (defalias 'org-on-heading-p 'org-at-heading-p)
  19864. (defun org-at-comment-p nil
  19865. "Is cursor in a line starting with a # character?"
  19866. (save-excursion
  19867. (beginning-of-line)
  19868. (looking-at "^#")))
  19869. (defun org-at-drawer-p nil
  19870. "Is cursor at a drawer keyword?"
  19871. (save-excursion
  19872. (move-beginning-of-line 1)
  19873. (looking-at org-drawer-regexp)))
  19874. (defun org-at-block-p nil
  19875. "Is cursor at a block keyword?"
  19876. (save-excursion
  19877. (move-beginning-of-line 1)
  19878. (looking-at org-block-regexp)))
  19879. (defun org-point-at-end-of-empty-headline ()
  19880. "If point is at the end of an empty headline, return t, else nil.
  19881. If the heading only contains a TODO keyword, it is still still considered
  19882. empty."
  19883. (and (looking-at "[ \t]*$")
  19884. (when org-todo-line-regexp
  19885. (save-excursion
  19886. (beginning-of-line 1)
  19887. (let ((case-fold-search nil))
  19888. (looking-at org-todo-line-regexp)
  19889. (string= (match-string 3) ""))))))
  19890. (defun org-at-heading-or-item-p ()
  19891. (or (org-at-heading-p) (org-at-item-p)))
  19892. (defun org-at-target-p ()
  19893. (or (org-in-regexp org-radio-target-regexp)
  19894. (org-in-regexp org-target-regexp)))
  19895. ;; Compatibility alias with Org versions < 7.8.03
  19896. (defalias 'org-on-target-p 'org-at-target-p)
  19897. (defun org-up-heading-all (arg)
  19898. "Move to the heading line of which the present line is a subheading.
  19899. This function considers both visible and invisible heading lines.
  19900. With argument, move up ARG levels."
  19901. (if (fboundp 'outline-up-heading-all)
  19902. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19903. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19904. (defun org-up-heading-safe ()
  19905. "Move to the heading line of which the present line is a subheading.
  19906. This version will not throw an error. It will return the level of the
  19907. headline found, or nil if no higher level is found.
  19908. Also, this function will be a lot faster than `outline-up-heading',
  19909. because it relies on stars being the outline starters. This can really
  19910. make a significant difference in outlines with very many siblings."
  19911. (let (start-level re)
  19912. (org-back-to-heading t)
  19913. (setq start-level (funcall outline-level))
  19914. (if (equal start-level 1)
  19915. nil
  19916. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19917. (if (re-search-backward re nil t)
  19918. (funcall outline-level)))))
  19919. (defun org-first-sibling-p ()
  19920. "Is this heading the first child of its parents?"
  19921. (interactive)
  19922. (let ((re org-outline-regexp-bol)
  19923. level l)
  19924. (unless (org-at-heading-p t)
  19925. (error "Not at a heading"))
  19926. (setq level (funcall outline-level))
  19927. (save-excursion
  19928. (if (not (re-search-backward re nil t))
  19929. t
  19930. (setq l (funcall outline-level))
  19931. (< l level)))))
  19932. (defun org-goto-sibling (&optional previous)
  19933. "Goto the next sibling, even if it is invisible.
  19934. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19935. when a sibling was found. When none is found, return nil and don't
  19936. move point."
  19937. (let ((fun (if previous 're-search-backward 're-search-forward))
  19938. (pos (point))
  19939. (re org-outline-regexp-bol)
  19940. level l)
  19941. (when (condition-case nil (org-back-to-heading t) (error nil))
  19942. (setq level (funcall outline-level))
  19943. (catch 'exit
  19944. (or previous (forward-char 1))
  19945. (while (funcall fun re nil t)
  19946. (setq l (funcall outline-level))
  19947. (when (< l level) (goto-char pos) (throw 'exit nil))
  19948. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19949. (goto-char pos)
  19950. nil))))
  19951. (defun org-show-siblings ()
  19952. "Show all siblings of the current headline."
  19953. (save-excursion
  19954. (while (org-goto-sibling) (org-flag-heading nil)))
  19955. (save-excursion
  19956. (while (org-goto-sibling 'previous)
  19957. (org-flag-heading nil))))
  19958. (defun org-goto-first-child ()
  19959. "Goto the first child, even if it is invisible.
  19960. Return t when a child was found. Otherwise don't move point and
  19961. return nil."
  19962. (let (level (pos (point)) (re org-outline-regexp-bol))
  19963. (when (condition-case nil (org-back-to-heading t) (error nil))
  19964. (setq level (outline-level))
  19965. (forward-char 1)
  19966. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19967. (progn (goto-char (match-beginning 0)) t)
  19968. (goto-char pos) nil))))
  19969. (defun org-show-hidden-entry ()
  19970. "Show an entry where even the heading is hidden."
  19971. (save-excursion
  19972. (org-show-entry)))
  19973. (defun org-flag-heading (flag &optional entry)
  19974. "Flag the current heading. FLAG non-nil means make invisible.
  19975. When ENTRY is non-nil, show the entire entry."
  19976. (save-excursion
  19977. (org-back-to-heading t)
  19978. ;; Check if we should show the entire entry
  19979. (if entry
  19980. (progn
  19981. (org-show-entry)
  19982. (save-excursion
  19983. (and (outline-next-heading)
  19984. (org-flag-heading nil))))
  19985. (outline-flag-region (max (point-min) (1- (point)))
  19986. (save-excursion (outline-end-of-heading) (point))
  19987. flag))))
  19988. (defun org-get-next-sibling ()
  19989. "Move to next heading of the same level, and return point.
  19990. If there is no such heading, return nil.
  19991. This is like outline-next-sibling, but invisible headings are ok."
  19992. (let ((level (funcall outline-level)))
  19993. (outline-next-heading)
  19994. (while (and (not (eobp)) (> (funcall outline-level) level))
  19995. (outline-next-heading))
  19996. (if (or (eobp) (< (funcall outline-level) level))
  19997. nil
  19998. (point))))
  19999. (defun org-get-last-sibling ()
  20000. "Move to previous heading of the same level, and return point.
  20001. If there is no such heading, return nil."
  20002. (let ((opoint (point))
  20003. (level (funcall outline-level)))
  20004. (outline-previous-heading)
  20005. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20006. (while (and (> (funcall outline-level) level)
  20007. (not (bobp)))
  20008. (outline-previous-heading))
  20009. (if (< (funcall outline-level) level)
  20010. nil
  20011. (point)))))
  20012. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20013. "Goto to the end of a subtree."
  20014. ;; This contains an exact copy of the original function, but it uses
  20015. ;; `org-back-to-heading', to make it work also in invisible
  20016. ;; trees. And is uses an invisible-ok argument.
  20017. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20018. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20019. ;; with many children and grandchildren etc, this can be much faster
  20020. ;; than the outline version.
  20021. (org-back-to-heading invisible-ok)
  20022. (let ((first t)
  20023. (level (funcall outline-level)))
  20024. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20025. ;; A true heading (not a plain list item), in Org-mode
  20026. ;; This means we can easily find the end by looking
  20027. ;; only for the right number of stars. Using a regexp to do
  20028. ;; this is so much faster than using a Lisp loop.
  20029. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20030. (forward-char 1)
  20031. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20032. ;; something else, do it the slow way
  20033. (while (and (not (eobp))
  20034. (or first (> (funcall outline-level) level)))
  20035. (setq first nil)
  20036. (outline-next-heading)))
  20037. (unless to-heading
  20038. (if (memq (preceding-char) '(?\n ?\^M))
  20039. (progn
  20040. ;; Go to end of line before heading
  20041. (forward-char -1)
  20042. (if (memq (preceding-char) '(?\n ?\^M))
  20043. ;; leave blank line before heading
  20044. (forward-char -1))))))
  20045. (point))
  20046. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20047. "Use Org version in org-mode, for dramatic speed-up."
  20048. (if (derived-mode-p 'org-mode)
  20049. (progn
  20050. (org-end-of-subtree nil t)
  20051. (unless (eobp) (backward-char 1)))
  20052. ad-do-it))
  20053. (defun org-end-of-meta-data-and-drawers ()
  20054. "Jump to the first text after meta data and drawers in the current entry.
  20055. This will move over empty lines, lines with planning time stamps,
  20056. clocking lines, and drawers."
  20057. (org-back-to-heading t)
  20058. (let ((end (save-excursion (outline-next-heading) (point)))
  20059. (re (concat "\\(" org-drawer-regexp "\\)"
  20060. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20061. (forward-line 1)
  20062. (while (re-search-forward re end t)
  20063. (if (not (match-end 1))
  20064. ;; empty or planning line
  20065. (forward-line 1)
  20066. ;; a drawer, find the end
  20067. (re-search-forward "^[ \t]*:END:" end 'move)
  20068. (forward-line 1)))
  20069. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20070. (point)))
  20071. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20072. "Move forward to the arg'th subheading at same level as this one.
  20073. Stop at the first and last subheadings of a superior heading.
  20074. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20075. non-nil it will also look at invisible ones."
  20076. (interactive "p")
  20077. (org-back-to-heading invisible-ok)
  20078. (org-at-heading-p)
  20079. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20080. (re (format "^\\*\\{1,%d\\} " level))
  20081. l)
  20082. (forward-char 1)
  20083. (while (> arg 0)
  20084. (while (and (re-search-forward re nil 'move)
  20085. (setq l (- (match-end 0) (match-beginning 0) 1))
  20086. (= l level)
  20087. (not invisible-ok)
  20088. (progn (backward-char 1) (outline-invisible-p)))
  20089. (if (< l level) (setq arg 1)))
  20090. (setq arg (1- arg)))
  20091. (beginning-of-line 1)))
  20092. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20093. "Move backward to the arg'th subheading at same level as this one.
  20094. Stop at the first and last subheadings of a superior heading."
  20095. (interactive "p")
  20096. (org-back-to-heading)
  20097. (org-at-heading-p)
  20098. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20099. (re (format "^\\*\\{1,%d\\} " level))
  20100. l)
  20101. (while (> arg 0)
  20102. (while (and (re-search-backward re nil 'move)
  20103. (setq l (- (match-end 0) (match-beginning 0) 1))
  20104. (= l level)
  20105. (not invisible-ok)
  20106. (outline-invisible-p))
  20107. (if (< l level) (setq arg 1)))
  20108. (setq arg (1- arg)))))
  20109. (defun org-forward-element ()
  20110. "Move forward by one element.
  20111. Move to the next element at the same level, when possible."
  20112. (interactive)
  20113. (cond ((eobp) (error "Cannot move further down"))
  20114. ((org-with-limited-levels (org-at-heading-p))
  20115. (let ((origin (point)))
  20116. (org-forward-heading-same-level 1)
  20117. (unless (org-with-limited-levels (org-at-heading-p))
  20118. (goto-char origin)
  20119. (error "Cannot move further down"))))
  20120. (t
  20121. (let* ((elem (org-element-at-point))
  20122. (end (org-element-property :end elem))
  20123. (parent (org-element-property :parent elem)))
  20124. (if (and parent (= (org-element-property :contents-end parent) end))
  20125. (goto-char (org-element-property :end parent))
  20126. (goto-char end))))))
  20127. (defun org-backward-element ()
  20128. "Move backward by one element.
  20129. Move to the previous element at the same level, when possible."
  20130. (interactive)
  20131. (cond ((bobp) (error "Cannot move further up"))
  20132. ((org-with-limited-levels (org-at-heading-p))
  20133. ;; At an headline, move to the previous one, if any, or stay
  20134. ;; here.
  20135. (let ((origin (point)))
  20136. (org-backward-heading-same-level 1)
  20137. (unless (org-with-limited-levels (org-at-heading-p))
  20138. (goto-char origin)
  20139. (error "Cannot move further up"))))
  20140. (t
  20141. (let* ((trail (org-element-at-point 'keep-trail))
  20142. (elem (car trail))
  20143. (prev-elem (nth 1 trail))
  20144. (beg (org-element-property :begin elem)))
  20145. (cond
  20146. ;; Move to beginning of current element if point isn't
  20147. ;; there already.
  20148. ((/= (point) beg) (goto-char beg))
  20149. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20150. ((org-before-first-heading-p) (goto-char (point-min)))
  20151. (t (org-back-to-heading)))))))
  20152. (defun org-up-element ()
  20153. "Move to upper element."
  20154. (interactive)
  20155. (if (org-with-limited-levels (org-at-heading-p))
  20156. (unless (org-up-heading-safe) (error "No surrounding element"))
  20157. (let* ((elem (org-element-at-point))
  20158. (parent (org-element-property :parent elem)))
  20159. (if parent (goto-char (org-element-property :begin parent))
  20160. (if (org-with-limited-levels (org-before-first-heading-p))
  20161. (error "No surrounding element")
  20162. (org-with-limited-levels (org-back-to-heading)))))))
  20163. (defvar org-element-greater-elements)
  20164. (defun org-down-element ()
  20165. "Move to inner element."
  20166. (interactive)
  20167. (let ((element (org-element-at-point)))
  20168. (cond
  20169. ((memq (org-element-type element) '(plain-list table))
  20170. (goto-char (org-element-property :contents-begin element))
  20171. (forward-char))
  20172. ((memq (org-element-type element) org-element-greater-elements)
  20173. ;; If contents are hidden, first disclose them.
  20174. (when (org-element-property :hiddenp element) (org-cycle))
  20175. (goto-char (or (org-element-property :contents-begin element)
  20176. (error "No content for this element"))))
  20177. (t (error "No inner element")))))
  20178. (defun org-drag-element-backward ()
  20179. "Move backward element at point."
  20180. (interactive)
  20181. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20182. (let* ((trail (org-element-at-point 'keep-trail))
  20183. (elem (car trail))
  20184. (prev-elem (nth 1 trail)))
  20185. ;; Error out if no previous element or previous element is
  20186. ;; a parent of the current one.
  20187. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20188. (error "Cannot drag element backward")
  20189. (let ((pos (point)))
  20190. (org-element-swap-A-B prev-elem elem)
  20191. (goto-char (+ (org-element-property :begin prev-elem)
  20192. (- pos (org-element-property :begin elem)))))))))
  20193. (defun org-drag-element-forward ()
  20194. "Move forward element at point."
  20195. (interactive)
  20196. (let* ((pos (point))
  20197. (elem (org-element-at-point)))
  20198. (when (= (point-max) (org-element-property :end elem))
  20199. (error "Cannot drag element forward"))
  20200. (goto-char (org-element-property :end elem))
  20201. (let ((next-elem (org-element-at-point)))
  20202. (when (or (org-element-nested-p elem next-elem)
  20203. (and (eq (org-element-type next-elem) 'headline)
  20204. (not (eq (org-element-type elem) 'headline))))
  20205. (goto-char pos)
  20206. (error "Cannot drag element forward"))
  20207. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20208. ;; body's length (without final blanks) and by the length of
  20209. ;; blanks between ELEM and NEXT-ELEM.
  20210. (let ((size-next (- (save-excursion
  20211. (goto-char (org-element-property :end next-elem))
  20212. (skip-chars-backward " \r\t\n")
  20213. (forward-line)
  20214. ;; Small correction if buffer doesn't end
  20215. ;; with a newline character.
  20216. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20217. (org-element-property :begin next-elem)))
  20218. (size-blank (- (org-element-property :end elem)
  20219. (save-excursion
  20220. (goto-char (org-element-property :end elem))
  20221. (skip-chars-backward " \r\t\n")
  20222. (forward-line)
  20223. (point)))))
  20224. (org-element-swap-A-B elem next-elem)
  20225. (goto-char (+ pos size-next size-blank))))))
  20226. (defun org-mark-element ()
  20227. "Put point at beginning of this element, mark at end.
  20228. Interactively, if this command is repeated or (in Transient Mark
  20229. mode) if the mark is active, it marks the next element after the
  20230. ones already marked."
  20231. (interactive)
  20232. (let (deactivate-mark)
  20233. (if (and (org-called-interactively-p 'any)
  20234. (or (and (eq last-command this-command) (mark t))
  20235. (and transient-mark-mode mark-active)))
  20236. (set-mark
  20237. (save-excursion
  20238. (goto-char (mark))
  20239. (goto-char (org-element-property :end (org-element-at-point)))))
  20240. (let ((element (org-element-at-point)))
  20241. (end-of-line)
  20242. (push-mark (org-element-property :end element) t t)
  20243. (goto-char (org-element-property :begin element))))))
  20244. (defun org-narrow-to-element ()
  20245. "Narrow buffer to current element."
  20246. (interactive)
  20247. (let ((elem (org-element-at-point)))
  20248. (cond
  20249. ((eq (car elem) 'headline)
  20250. (narrow-to-region
  20251. (org-element-property :begin elem)
  20252. (org-element-property :end elem)))
  20253. ((memq (car elem) org-element-greater-elements)
  20254. (narrow-to-region
  20255. (org-element-property :contents-begin elem)
  20256. (org-element-property :contents-end elem)))
  20257. (t
  20258. (narrow-to-region
  20259. (org-element-property :begin elem)
  20260. (org-element-property :end elem))))))
  20261. (defun org-transpose-element ()
  20262. "Transpose current and previous elements, keeping blank lines between.
  20263. Point is moved after both elements."
  20264. (interactive)
  20265. (org-skip-whitespace)
  20266. (let ((end (org-element-property :end (org-element-at-point))))
  20267. (org-drag-element-backward)
  20268. (goto-char end)))
  20269. (defun org-unindent-buffer ()
  20270. "Un-indent the visible part of the buffer.
  20271. Relative indentation (between items, inside blocks, etc.) isn't
  20272. modified."
  20273. (interactive)
  20274. (unless (eq major-mode 'org-mode)
  20275. (error "Cannot un-indent a buffer not in Org mode"))
  20276. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20277. unindent-tree ; For byte-compiler.
  20278. (unindent-tree
  20279. (function
  20280. (lambda (contents)
  20281. (mapc
  20282. (lambda (element)
  20283. (if (memq (org-element-type element) '(headline section))
  20284. (funcall unindent-tree (org-element-contents element))
  20285. (save-excursion
  20286. (save-restriction
  20287. (narrow-to-region
  20288. (org-element-property :begin element)
  20289. (org-element-property :end element))
  20290. (org-do-remove-indentation)))))
  20291. (reverse contents))))))
  20292. (funcall unindent-tree (org-element-contents parse-tree))))
  20293. (defun org-show-subtree ()
  20294. "Show everything after this heading at deeper levels."
  20295. (interactive)
  20296. (outline-flag-region
  20297. (point)
  20298. (save-excursion
  20299. (org-end-of-subtree t t))
  20300. nil))
  20301. (defun org-show-entry ()
  20302. "Show the body directly following this heading.
  20303. Show the heading too, if it is currently invisible."
  20304. (interactive)
  20305. (save-excursion
  20306. (condition-case nil
  20307. (progn
  20308. (org-back-to-heading t)
  20309. (outline-flag-region
  20310. (max (point-min) (1- (point)))
  20311. (save-excursion
  20312. (if (re-search-forward
  20313. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20314. (match-beginning 1)
  20315. (point-max)))
  20316. nil)
  20317. (org-cycle-hide-drawers 'children))
  20318. (error nil))))
  20319. (defun org-make-options-regexp (kwds &optional extra)
  20320. "Make a regular expression for keyword lines."
  20321. (concat
  20322. "^#\\+\\("
  20323. (mapconcat 'regexp-quote kwds "\\|")
  20324. (if extra (concat "\\|" extra))
  20325. "\\):[ \t]*\\(.*\\)"))
  20326. ;; Make isearch reveal the necessary context
  20327. (defun org-isearch-end ()
  20328. "Reveal context after isearch exits."
  20329. (when isearch-success ; only if search was successful
  20330. (if (featurep 'xemacs)
  20331. ;; Under XEmacs, the hook is run in the correct place,
  20332. ;; we directly show the context.
  20333. (org-show-context 'isearch)
  20334. ;; In Emacs the hook runs *before* restoring the overlays.
  20335. ;; So we have to use a one-time post-command-hook to do this.
  20336. ;; (Emacs 22 has a special variable, see function `org-mode')
  20337. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20338. isearch-mode-end-hook-quit)
  20339. ;; Only when the isearch was not quitted.
  20340. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20341. 'append 'local)))))
  20342. (defun org-isearch-post-command ()
  20343. "Remove self from hook, and show context."
  20344. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20345. (org-show-context 'isearch))
  20346. ;;;; Integration with and fixes for other packages
  20347. ;;; Imenu support
  20348. (defvar org-imenu-markers nil
  20349. "All markers currently used by Imenu.")
  20350. (make-variable-buffer-local 'org-imenu-markers)
  20351. (defun org-imenu-new-marker (&optional pos)
  20352. "Return a new marker for use by Imenu, and remember the marker."
  20353. (let ((m (make-marker)))
  20354. (move-marker m (or pos (point)))
  20355. (push m org-imenu-markers)
  20356. m))
  20357. (defun org-imenu-get-tree ()
  20358. "Produce the index for Imenu."
  20359. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20360. (setq org-imenu-markers nil)
  20361. (let* ((n org-imenu-depth)
  20362. (re (concat "^" (org-get-limited-outline-regexp)))
  20363. (subs (make-vector (1+ n) nil))
  20364. (last-level 0)
  20365. m level head)
  20366. (save-excursion
  20367. (save-restriction
  20368. (widen)
  20369. (goto-char (point-max))
  20370. (while (re-search-backward re nil t)
  20371. (setq level (org-reduced-level (funcall outline-level)))
  20372. (when (and (<= level n)
  20373. (looking-at org-complex-heading-regexp))
  20374. (setq head (org-link-display-format
  20375. (org-match-string-no-properties 4))
  20376. m (org-imenu-new-marker))
  20377. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20378. (if (>= level last-level)
  20379. (push (cons head m) (aref subs level))
  20380. (push (cons head (aref subs (1+ level))) (aref subs level))
  20381. (loop for i from (1+ level) to n do (aset subs i nil)))
  20382. (setq last-level level)))))
  20383. (aref subs 1)))
  20384. (eval-after-load "imenu"
  20385. '(progn
  20386. (add-hook 'imenu-after-jump-hook
  20387. (lambda ()
  20388. (if (derived-mode-p 'org-mode)
  20389. (org-show-context 'org-goto))))))
  20390. (defun org-link-display-format (link)
  20391. "Replace a link with either the description, or the link target
  20392. if no description is present"
  20393. (save-match-data
  20394. (if (string-match org-bracket-link-analytic-regexp link)
  20395. (replace-match (if (match-end 5)
  20396. (match-string 5 link)
  20397. (concat (match-string 1 link)
  20398. (match-string 3 link)))
  20399. nil t link)
  20400. link)))
  20401. (defun org-toggle-link-display ()
  20402. "Toggle the literal or descriptive display of links."
  20403. (interactive)
  20404. (if org-descriptive-links
  20405. (progn (org-remove-from-invisibility-spec '(org-link))
  20406. (org-restart-font-lock)
  20407. (setq org-descriptive-links nil))
  20408. (progn (add-to-invisibility-spec '(org-link))
  20409. (org-restart-font-lock)
  20410. (setq org-descriptive-links t))))
  20411. ;; Speedbar support
  20412. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20413. "Overlay marking the agenda restriction line in speedbar.")
  20414. (overlay-put org-speedbar-restriction-lock-overlay
  20415. 'face 'org-agenda-restriction-lock)
  20416. (overlay-put org-speedbar-restriction-lock-overlay
  20417. 'help-echo "Agendas are currently limited to this item.")
  20418. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20419. (defun org-speedbar-set-agenda-restriction ()
  20420. "Restrict future agenda commands to the location at point in speedbar.
  20421. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20422. (interactive)
  20423. (require 'org-agenda)
  20424. (let (p m tp np dir txt)
  20425. (cond
  20426. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20427. 'org-imenu t))
  20428. (setq m (get-text-property p 'org-imenu-marker))
  20429. (with-current-buffer (marker-buffer m)
  20430. (goto-char m)
  20431. (org-agenda-set-restriction-lock 'subtree)))
  20432. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20433. 'speedbar-function 'speedbar-find-file))
  20434. (setq tp (previous-single-property-change
  20435. (1+ p) 'speedbar-function)
  20436. np (next-single-property-change
  20437. tp 'speedbar-function)
  20438. dir (speedbar-line-directory)
  20439. txt (buffer-substring-no-properties (or tp (point-min))
  20440. (or np (point-max))))
  20441. (with-current-buffer (find-file-noselect
  20442. (let ((default-directory dir))
  20443. (expand-file-name txt)))
  20444. (unless (derived-mode-p 'org-mode)
  20445. (error "Cannot restrict to non-Org-mode file"))
  20446. (org-agenda-set-restriction-lock 'file)))
  20447. (t (error "Don't know how to restrict Org-mode's agenda")))
  20448. (move-overlay org-speedbar-restriction-lock-overlay
  20449. (point-at-bol) (point-at-eol))
  20450. (setq current-prefix-arg nil)
  20451. (org-agenda-maybe-redo)))
  20452. (eval-after-load "speedbar"
  20453. '(progn
  20454. (speedbar-add-supported-extension ".org")
  20455. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20456. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20457. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20458. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20459. (add-hook 'speedbar-visiting-tag-hook
  20460. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20461. ;;; Fixes and Hacks for problems with other packages
  20462. ;; Make flyspell not check words in links, to not mess up our keymap
  20463. (defun org-mode-flyspell-verify ()
  20464. "Don't let flyspell put overlays at active buttons, or on
  20465. {todo,all-time,additional-option-like}-keywords."
  20466. (let ((pos (max (1- (point)) (point-min)))
  20467. (word (thing-at-point 'word)))
  20468. (and (not (get-text-property pos 'keymap))
  20469. (not (get-text-property pos 'org-no-flyspell))
  20470. (not (member word org-todo-keywords-1))
  20471. (not (member word org-all-time-keywords))
  20472. (not (member word org-options-keywords))
  20473. (not (member word (mapcar 'car org-startup-options)))
  20474. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20475. (defun org-remove-flyspell-overlays-in (beg end)
  20476. "Remove flyspell overlays in region."
  20477. (and (org-bound-and-true-p flyspell-mode)
  20478. (fboundp 'flyspell-delete-region-overlays)
  20479. (flyspell-delete-region-overlays beg end))
  20480. (add-text-properties beg end '(org-no-flyspell t)))
  20481. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20482. (eval-after-load "bookmark"
  20483. '(if (boundp 'bookmark-after-jump-hook)
  20484. ;; We can use the hook
  20485. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20486. ;; Hook not available, use advice
  20487. (defadvice bookmark-jump (after org-make-visible activate)
  20488. "Make the position visible."
  20489. (org-bookmark-jump-unhide))))
  20490. ;; Make sure saveplace shows the location if it was hidden
  20491. (eval-after-load "saveplace"
  20492. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20493. "Make the position visible."
  20494. (org-bookmark-jump-unhide)))
  20495. ;; Make sure ecb shows the location if it was hidden
  20496. (eval-after-load "ecb"
  20497. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20498. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20499. (if (derived-mode-p 'org-mode)
  20500. (org-show-context))))
  20501. (defun org-bookmark-jump-unhide ()
  20502. "Unhide the current position, to show the bookmark location."
  20503. (and (derived-mode-p 'org-mode)
  20504. (or (outline-invisible-p)
  20505. (save-excursion (goto-char (max (point-min) (1- (point))))
  20506. (outline-invisible-p)))
  20507. (org-show-context 'bookmark-jump)))
  20508. ;; Make session.el ignore our circular variable
  20509. (eval-after-load "session"
  20510. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20511. ;;;; Experimental code
  20512. (defun org-closed-in-range ()
  20513. "Sparse tree of items closed in a certain time range.
  20514. Still experimental, may disappear in the future."
  20515. (interactive)
  20516. ;; Get the time interval from the user.
  20517. (let* ((time1 (org-float-time
  20518. (org-read-date nil 'to-time nil "Starting date: ")))
  20519. (time2 (org-float-time
  20520. (org-read-date nil 'to-time nil "End date:")))
  20521. ;; callback function
  20522. (callback (lambda ()
  20523. (let ((time
  20524. (org-float-time
  20525. (apply 'encode-time
  20526. (org-parse-time-string
  20527. (match-string 1))))))
  20528. ;; check if time in interval
  20529. (and (>= time time1) (<= time time2))))))
  20530. ;; make tree, check each match with the callback
  20531. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20532. ;;;; Finish up
  20533. (provide 'org)
  20534. (run-hooks 'org-load-hook)
  20535. ;;; org.el ends here