org.el 775 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2011 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. ;; Version: 7.7
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  100. (declare-function org-at-clock-log-p "org-clock" ())
  101. (declare-function org-clock-timestamps-up "org-clock" ())
  102. (declare-function org-clock-timestamps-down "org-clock" ())
  103. ;; babel
  104. (require 'ob)
  105. (require 'ob-table)
  106. (require 'ob-lob)
  107. (require 'ob-ref)
  108. (require 'ob-tangle)
  109. (require 'ob-comint)
  110. (require 'ob-keys)
  111. ;; load languages based on value of `org-babel-load-languages'
  112. (defvar org-babel-load-languages)
  113. ;;;###autoload
  114. (defun org-babel-do-load-languages (sym value)
  115. "Load the languages defined in `org-babel-load-languages'."
  116. (set-default sym value)
  117. (mapc (lambda (pair)
  118. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  119. (if active
  120. (progn
  121. (require (intern (concat "ob-" lang))))
  122. (progn
  123. (funcall 'fmakunbound
  124. (intern (concat "org-babel-execute:" lang)))
  125. (funcall 'fmakunbound
  126. (intern (concat "org-babel-expand-body:" lang)))))))
  127. org-babel-load-languages))
  128. (defcustom org-babel-load-languages '((emacs-lisp . t))
  129. "Languages which can be evaluated in Org-mode buffers.
  130. This list can be used to load support for any of the languages
  131. below, note that each language will depend on a different set of
  132. system executables and/or Emacs modes. When a language is
  133. \"loaded\", then code blocks in that language can be evaluated
  134. with `org-babel-execute-src-block' bound by default to C-c
  135. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  136. be set to remove code block evaluation from the C-c C-c
  137. keybinding. By default only Emacs Lisp (which has no
  138. requirements) is loaded."
  139. :group 'org-babel
  140. :set 'org-babel-do-load-languages
  141. :type '(alist :tag "Babel Languages"
  142. :key-type
  143. (choice
  144. (const :tag "Awk" awk)
  145. (const :tag "C" C)
  146. (const :tag "R" R)
  147. (const :tag "Asymptote" asymptote)
  148. (const :tag "Calc" calc)
  149. (const :tag "Clojure" clojure)
  150. (const :tag "CSS" css)
  151. (const :tag "Ditaa" ditaa)
  152. (const :tag "Dot" dot)
  153. (const :tag "Emacs Lisp" emacs-lisp)
  154. (const :tag "Gnuplot" gnuplot)
  155. (const :tag "Haskell" haskell)
  156. (const :tag "Java" java)
  157. (const :tag "Javascript" js)
  158. (const :tag "Latex" latex)
  159. (const :tag "Ledger" ledger)
  160. (const :tag "Lilypond" lilypond)
  161. (const :tag "Maxima" maxima)
  162. (const :tag "Matlab" matlab)
  163. (const :tag "Mscgen" mscgen)
  164. (const :tag "Ocaml" ocaml)
  165. (const :tag "Octave" octave)
  166. (const :tag "Org" org)
  167. (const :tag "Perl" perl)
  168. (const :tag "PlantUML" plantuml)
  169. (const :tag "Python" python)
  170. (const :tag "Ruby" ruby)
  171. (const :tag "Sass" sass)
  172. (const :tag "Scheme" scheme)
  173. (const :tag "Screen" screen)
  174. (const :tag "Shell Script" sh)
  175. (const :tag "Sql" sql)
  176. (const :tag "Sqlite" sqlite))
  177. :value-type (boolean :tag "Activate" :value t)))
  178. ;;;; Customization variables
  179. (defcustom org-clone-delete-id nil
  180. "Remove ID property of clones of a subtree.
  181. When non-nil, clones of a subtree don't inherit the ID property.
  182. Otherwise they inherit the ID property with a new unique
  183. identifier."
  184. :type 'boolean
  185. :group 'org-id)
  186. ;;; Version
  187. (defconst org-version "7.7"
  188. "The version number of the file org.el.")
  189. (defun org-version (&optional here)
  190. "Show the org-mode version in the echo area.
  191. With prefix arg HERE, insert it at point."
  192. (interactive "P")
  193. (let* ((origin default-directory)
  194. (version org-version)
  195. (git-version)
  196. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  197. (when (and (file-exists-p (expand-file-name ".git" dir))
  198. (executable-find "git"))
  199. (unwind-protect
  200. (progn
  201. (cd dir)
  202. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  203. (with-current-buffer "*Shell Command Output*"
  204. (goto-char (point-min))
  205. (setq git-version (buffer-substring (point) (point-at-eol))))
  206. (subst-char-in-string ?- ?. git-version t)
  207. (when (string-match "\\S-"
  208. (shell-command-to-string
  209. "git diff-index --name-only HEAD --"))
  210. (setq git-version (concat git-version ".dirty")))
  211. (setq version (concat version " (" git-version ")"))))
  212. (cd origin)))
  213. (setq version (format "Org-mode version %s" version))
  214. (if here (insert version))
  215. (message version)))
  216. ;;; Compatibility constants
  217. ;;; The custom variables
  218. (defgroup org nil
  219. "Outline-based notes management and organizer."
  220. :tag "Org"
  221. :group 'outlines
  222. :group 'calendar)
  223. (defcustom org-mode-hook nil
  224. "Mode hook for Org-mode, run after the mode was turned on."
  225. :group 'org
  226. :type 'hook)
  227. (defcustom org-load-hook nil
  228. "Hook that is run after org.el has been loaded."
  229. :group 'org
  230. :type 'hook)
  231. (defcustom org-log-buffer-setup-hook nil
  232. "Hook that is run after an Org log buffer is created."
  233. :group 'org
  234. :type 'hook)
  235. (defvar org-modules) ; defined below
  236. (defvar org-modules-loaded nil
  237. "Have the modules been loaded already?")
  238. (defun org-load-modules-maybe (&optional force)
  239. "Load all extensions listed in `org-modules'."
  240. (when (or force (not org-modules-loaded))
  241. (mapc (lambda (ext)
  242. (condition-case nil (require ext)
  243. (error (message "Problems while trying to load feature `%s'" ext))))
  244. org-modules)
  245. (setq org-modules-loaded t)))
  246. (defun org-set-modules (var value)
  247. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  248. (set var value)
  249. (when (featurep 'org)
  250. (org-load-modules-maybe 'force)))
  251. (when (org-bound-and-true-p org-modules)
  252. (let ((a (member 'org-infojs org-modules)))
  253. (and a (setcar a 'org-jsinfo))))
  254. (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)
  255. "Modules that should always be loaded together with org.el.
  256. If a description starts with <C>, the file is not part of Emacs
  257. and loading it will require that you have downloaded and properly installed
  258. the org-mode distribution.
  259. You can also use this system to load external packages (i.e. neither Org
  260. core modules, nor modules from the CONTRIB directory). Just add symbols
  261. to the end of the list. If the package is called org-xyz.el, then you need
  262. to add the symbol `xyz', and the package must have a call to
  263. (provide 'org-xyz)"
  264. :group 'org
  265. :set 'org-set-modules
  266. :type
  267. '(set :greedy t
  268. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  269. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  270. (const :tag " crypt: Encryption of subtrees" org-crypt)
  271. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  272. (const :tag " docview: Links to doc-view buffers" org-docview)
  273. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  274. (const :tag " id: Global IDs for identifying entries" org-id)
  275. (const :tag " info: Links to Info nodes" org-info)
  276. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  277. (const :tag " habit: Track your consistency with habits" org-habit)
  278. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  279. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  280. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  281. (const :tag " mew Links to Mew folders/messages" org-mew)
  282. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  283. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  284. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  285. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  286. (const :tag " vm: Links to VM folders/messages" org-vm)
  287. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  288. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  289. (const :tag " mouse: Additional mouse support" org-mouse)
  290. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  291. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  292. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  293. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  294. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  295. (const :tag "C collector: Collect properties into tables" org-collector)
  296. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  297. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  298. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  299. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  300. (const :tag "C eval: Include command output as text" org-eval)
  301. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  302. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  303. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  304. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  305. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  306. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  307. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  308. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  309. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  310. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  311. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  312. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  313. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  314. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  315. (const :tag "C odt: OpenDocumentText exporter for Org-mode" org-odt)
  316. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  317. (const :tag "C registry: A registry for Org-mode links" org-registry)
  318. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  319. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  320. (const :tag "C secretary: Team management with org-mode" org-secretary)
  321. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  322. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  323. (const :tag "C track: Keep up with Org-mode development" org-track)
  324. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  325. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  326. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  327. (defcustom org-support-shift-select nil
  328. "Non-nil means make shift-cursor commands select text when possible.
  329. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  330. start selecting a region, or enlarge regions started in this way.
  331. In Org-mode, in special contexts, these same keys are used for
  332. other purposes, important enough to compete with shift selection.
  333. Org tries to balance these needs by supporting `shift-select-mode'
  334. outside these special contexts, under control of this variable.
  335. The default of this variable is nil, to avoid confusing behavior. Shifted
  336. cursor keys will then execute Org commands in the following contexts:
  337. - on a headline, changing TODO state (left/right) and priority (up/down)
  338. - on a time stamp, changing the time
  339. - in a plain list item, changing the bullet type
  340. - in a property definition line, switching between allowed values
  341. - in the BEGIN line of a clock table (changing the time block).
  342. Outside these contexts, the commands will throw an error.
  343. When this variable is t and the cursor is not in a special
  344. context, Org-mode will support shift-selection for making and
  345. enlarging regions. To make this more effective, the bullet
  346. cycling will no longer happen anywhere in an item line, but only
  347. if the cursor is exactly on the bullet.
  348. If you set this variable to the symbol `always', then the keys
  349. will not be special in headlines, property lines, and item lines,
  350. to make shift selection work there as well. If this is what you
  351. want, you can use the following alternative commands: `C-c C-t'
  352. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  353. can be used to switch TODO sets, `C-c -' to cycle item bullet
  354. types, and properties can be edited by hand or in column view.
  355. However, when the cursor is on a timestamp, shift-cursor commands
  356. will still edit the time stamp - this is just too good to give up.
  357. XEmacs user should have this variable set to nil, because
  358. `shift-select-mode' is in Emacs 23 or later only."
  359. :group 'org
  360. :type '(choice
  361. (const :tag "Never" nil)
  362. (const :tag "When outside special context" t)
  363. (const :tag "Everywhere except timestamps" always)))
  364. (defgroup org-startup nil
  365. "Options concerning startup of Org-mode."
  366. :tag "Org Startup"
  367. :group 'org)
  368. (defcustom org-startup-folded t
  369. "Non-nil means entering Org-mode will switch to OVERVIEW.
  370. This can also be configured on a per-file basis by adding one of
  371. the following lines anywhere in the buffer:
  372. #+STARTUP: fold (or `overview', this is equivalent)
  373. #+STARTUP: nofold (or `showall', this is equivalent)
  374. #+STARTUP: content
  375. #+STARTUP: showeverything"
  376. :group 'org-startup
  377. :type '(choice
  378. (const :tag "nofold: show all" nil)
  379. (const :tag "fold: overview" t)
  380. (const :tag "content: all headlines" content)
  381. (const :tag "show everything, even drawers" showeverything)))
  382. (defcustom org-startup-truncated t
  383. "Non-nil means entering Org-mode will set `truncate-lines'.
  384. This is useful since some lines containing links can be very long and
  385. uninteresting. Also tables look terrible when wrapped."
  386. :group 'org-startup
  387. :type 'boolean)
  388. (defcustom org-startup-indented nil
  389. "Non-nil means turn on `org-indent-mode' on startup.
  390. This can also be configured on a per-file basis by adding one of
  391. the following lines anywhere in the buffer:
  392. #+STARTUP: indent
  393. #+STARTUP: noindent"
  394. :group 'org-structure
  395. :type '(choice
  396. (const :tag "Not" nil)
  397. (const :tag "Globally (slow on startup in large files)" t)))
  398. (defcustom org-use-sub-superscripts t
  399. "Non-nil means interpret \"_\" and \"^\" for export.
  400. When this option is turned on, you can use TeX-like syntax for sub- and
  401. superscripts. Several characters after \"_\" or \"^\" will be
  402. considered as a single item - so grouping with {} is normally not
  403. needed. For example, the following things will be parsed as single
  404. sub- or superscripts.
  405. 10^24 or 10^tau several digits will be considered 1 item.
  406. 10^-12 or 10^-tau a leading sign with digits or a word
  407. x^2-y^3 will be read as x^2 - y^3, because items are
  408. terminated by almost any nonword/nondigit char.
  409. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  410. Still, ambiguity is possible - so when in doubt use {} to enclose the
  411. sub/superscript. If you set this variable to the symbol `{}',
  412. the braces are *required* in order to trigger interpretations as
  413. sub/superscript. This can be helpful in documents that need \"_\"
  414. frequently in plain text.
  415. Not all export backends support this, but HTML does.
  416. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  417. :group 'org-startup
  418. :group 'org-export-translation
  419. :type '(choice
  420. (const :tag "Always interpret" t)
  421. (const :tag "Only with braces" {})
  422. (const :tag "Never interpret" nil)))
  423. (if (fboundp 'defvaralias)
  424. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  425. (defcustom org-startup-with-beamer-mode nil
  426. "Non-nil means turn on `org-beamer-mode' on startup.
  427. This can also be configured on a per-file basis by adding one of
  428. the following lines anywhere in the buffer:
  429. #+STARTUP: beamer"
  430. :group 'org-startup
  431. :type 'boolean)
  432. (defcustom org-startup-align-all-tables nil
  433. "Non-nil means align all tables when visiting a file.
  434. This is useful when the column width in tables is forced with <N> cookies
  435. in table fields. Such tables will look correct only after the first re-align.
  436. This can also be configured on a per-file basis by adding one of
  437. the following lines anywhere in the buffer:
  438. #+STARTUP: align
  439. #+STARTUP: noalign"
  440. :group 'org-startup
  441. :type 'boolean)
  442. (defcustom org-startup-with-inline-images nil
  443. "Non-nil means show inline images when loading a new Org file.
  444. This can also be configured on a per-file basis by adding one of
  445. the following lines anywhere in the buffer:
  446. #+STARTUP: inlineimages
  447. #+STARTUP: noinlineimages"
  448. :group 'org-startup
  449. :type 'boolean)
  450. (defcustom org-insert-mode-line-in-empty-file nil
  451. "Non-nil means insert the first line setting Org-mode in empty files.
  452. When the function `org-mode' is called interactively in an empty file, this
  453. normally means that the file name does not automatically trigger Org-mode.
  454. To ensure that the file will always be in Org-mode in the future, a
  455. line enforcing Org-mode will be inserted into the buffer, if this option
  456. has been set."
  457. :group 'org-startup
  458. :type 'boolean)
  459. (defcustom org-replace-disputed-keys nil
  460. "Non-nil means use alternative key bindings for some keys.
  461. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  462. These keys are also used by other packages like shift-selection-mode'
  463. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  464. If you want to use Org-mode together with one of these other modes,
  465. or more generally if you would like to move some Org-mode commands to
  466. other keys, set this variable and configure the keys with the variable
  467. `org-disputed-keys'.
  468. This option is only relevant at load-time of Org-mode, and must be set
  469. *before* org.el is loaded. Changing it requires a restart of Emacs to
  470. become effective."
  471. :group 'org-startup
  472. :type 'boolean)
  473. (defcustom org-use-extra-keys nil
  474. "Non-nil means use extra key sequence definitions for certain commands.
  475. This happens automatically if you run XEmacs or if `window-system'
  476. is nil. This variable lets you do the same manually. You must
  477. set it before loading org.
  478. Example: on Carbon Emacs 22 running graphically, with an external
  479. keyboard on a Powerbook, the default way of setting M-left might
  480. not work for either Alt or ESC. Setting this variable will make
  481. it work for ESC."
  482. :group 'org-startup
  483. :type 'boolean)
  484. (if (fboundp 'defvaralias)
  485. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  486. (defcustom org-disputed-keys
  487. '(([(shift up)] . [(meta p)])
  488. ([(shift down)] . [(meta n)])
  489. ([(shift left)] . [(meta -)])
  490. ([(shift right)] . [(meta +)])
  491. ([(control shift right)] . [(meta shift +)])
  492. ([(control shift left)] . [(meta shift -)]))
  493. "Keys for which Org-mode and other modes compete.
  494. This is an alist, cars are the default keys, second element specifies
  495. the alternative to use when `org-replace-disputed-keys' is t.
  496. Keys can be specified in any syntax supported by `define-key'.
  497. The value of this option takes effect only at Org-mode's startup,
  498. therefore you'll have to restart Emacs to apply it after changing."
  499. :group 'org-startup
  500. :type 'alist)
  501. (defun org-key (key)
  502. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  503. Or return the original if not disputed.
  504. Also apply the translations defined in `org-xemacs-key-equivalents'."
  505. (when org-replace-disputed-keys
  506. (let* ((nkey (key-description key))
  507. (x (org-find-if (lambda (x)
  508. (equal (key-description (car x)) nkey))
  509. org-disputed-keys)))
  510. (setq key (if x (cdr x) key))))
  511. (when (featurep 'xemacs)
  512. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  513. key)
  514. (defun org-find-if (predicate seq)
  515. (catch 'exit
  516. (while seq
  517. (if (funcall predicate (car seq))
  518. (throw 'exit (car seq))
  519. (pop seq)))))
  520. (defun org-defkey (keymap key def)
  521. "Define a key, possibly translated, as returned by `org-key'."
  522. (define-key keymap (org-key key) def))
  523. (defcustom org-ellipsis nil
  524. "The ellipsis to use in the Org-mode outline.
  525. When nil, just use the standard three dots. When a string, use that instead,
  526. When a face, use the standard 3 dots, but with the specified face.
  527. The change affects only Org-mode (which will then use its own display table).
  528. Changing this requires executing `M-x org-mode' in a buffer to become
  529. effective."
  530. :group 'org-startup
  531. :type '(choice (const :tag "Default" nil)
  532. (face :tag "Face" :value org-warning)
  533. (string :tag "String" :value "...#")))
  534. (defvar org-display-table nil
  535. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  536. (defgroup org-keywords nil
  537. "Keywords in Org-mode."
  538. :tag "Org Keywords"
  539. :group 'org)
  540. (defcustom org-deadline-string "DEADLINE:"
  541. "String to mark deadline entries.
  542. A deadline is this string, followed by a time stamp. Should be a word,
  543. terminated by a colon. You can insert a schedule keyword and
  544. a timestamp with \\[org-deadline].
  545. Changes become only effective after restarting Emacs."
  546. :group 'org-keywords
  547. :type 'string)
  548. (defcustom org-scheduled-string "SCHEDULED:"
  549. "String to mark scheduled TODO entries.
  550. A schedule is this string, followed by a time stamp. Should be a word,
  551. terminated by a colon. You can insert a schedule keyword and
  552. a timestamp with \\[org-schedule].
  553. Changes become only effective after restarting Emacs."
  554. :group 'org-keywords
  555. :type 'string)
  556. (defcustom org-closed-string "CLOSED:"
  557. "String used as the prefix for timestamps logging closing a TODO entry."
  558. :group 'org-keywords
  559. :type 'string)
  560. (defcustom org-clock-string "CLOCK:"
  561. "String used as prefix for timestamps clocking work hours on an item."
  562. :group 'org-keywords
  563. :type 'string)
  564. (defcustom org-comment-string "COMMENT"
  565. "Entries starting with this keyword will never be exported.
  566. An entry can be toggled between COMMENT and normal with
  567. \\[org-toggle-comment].
  568. Changes become only effective after restarting Emacs."
  569. :group 'org-keywords
  570. :type 'string)
  571. (defcustom org-quote-string "QUOTE"
  572. "Entries starting with this keyword will be exported in fixed-width font.
  573. Quoting applies only to the text in the entry following the headline, and does
  574. not extend beyond the next headline, even if that is lower level.
  575. An entry can be toggled between QUOTE and normal with
  576. \\[org-toggle-fixed-width-section]."
  577. :group 'org-keywords
  578. :type 'string)
  579. (defconst org-repeat-re
  580. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  581. "Regular expression for specifying repeated events.
  582. After a match, group 1 contains the repeat expression.")
  583. (defgroup org-structure nil
  584. "Options concerning the general structure of Org-mode files."
  585. :tag "Org Structure"
  586. :group 'org)
  587. (defgroup org-reveal-location nil
  588. "Options about how to make context of a location visible."
  589. :tag "Org Reveal Location"
  590. :group 'org-structure)
  591. (defconst org-context-choice
  592. '(choice
  593. (const :tag "Always" t)
  594. (const :tag "Never" nil)
  595. (repeat :greedy t :tag "Individual contexts"
  596. (cons
  597. (choice :tag "Context"
  598. (const agenda)
  599. (const org-goto)
  600. (const occur-tree)
  601. (const tags-tree)
  602. (const link-search)
  603. (const mark-goto)
  604. (const bookmark-jump)
  605. (const isearch)
  606. (const default))
  607. (boolean))))
  608. "Contexts for the reveal options.")
  609. (defcustom org-show-hierarchy-above '((default . t))
  610. "Non-nil means show full hierarchy when revealing a location.
  611. Org-mode often shows locations in an org-mode file which might have
  612. been invisible before. When this is set, the hierarchy of headings
  613. above the exposed location is shown.
  614. Turning this off for example for sparse trees makes them very compact.
  615. Instead of t, this can also be an alist specifying this option for different
  616. contexts. Valid contexts are
  617. agenda when exposing an entry from the agenda
  618. org-goto when using the command `org-goto' on key C-c C-j
  619. occur-tree when using the command `org-occur' on key C-c /
  620. tags-tree when constructing a sparse tree based on tags matches
  621. link-search when exposing search matches associated with a link
  622. mark-goto when exposing the jump goal of a mark
  623. bookmark-jump when exposing a bookmark location
  624. isearch when exiting from an incremental search
  625. default default for all contexts not set explicitly"
  626. :group 'org-reveal-location
  627. :type org-context-choice)
  628. (defcustom org-show-following-heading '((default . nil))
  629. "Non-nil means show following heading when revealing a location.
  630. Org-mode often shows locations in an org-mode file which might have
  631. been invisible before. When this is set, the heading following the
  632. match is shown.
  633. Turning this off for example for sparse trees makes them very compact,
  634. but makes it harder to edit the location of the match. In such a case,
  635. use the command \\[org-reveal] to show more context.
  636. Instead of t, this can also be an alist specifying this option for different
  637. contexts. See `org-show-hierarchy-above' for valid contexts."
  638. :group 'org-reveal-location
  639. :type org-context-choice)
  640. (defcustom org-show-siblings '((default . nil) (isearch t))
  641. "Non-nil means show all sibling heading when revealing a location.
  642. Org-mode often shows locations in an org-mode file which might have
  643. been invisible before. When this is set, the sibling of the current entry
  644. heading are all made visible. If `org-show-hierarchy-above' is t,
  645. the same happens on each level of the hierarchy above the current entry.
  646. By default this is on for the isearch context, off for all other contexts.
  647. Turning this off for example for sparse trees makes them very compact,
  648. but makes it harder to edit the location of the match. In such a case,
  649. use the command \\[org-reveal] to show more context.
  650. Instead of t, this can also be an alist specifying this option for different
  651. contexts. See `org-show-hierarchy-above' for valid contexts."
  652. :group 'org-reveal-location
  653. :type org-context-choice)
  654. (defcustom org-show-entry-below '((default . nil))
  655. "Non-nil means show the entry below a headline when revealing a location.
  656. Org-mode often shows locations in an org-mode file which might have
  657. been invisible before. When this is set, the text below the headline that is
  658. exposed is also shown.
  659. By default this is off for all contexts.
  660. Instead of t, this can also be an alist specifying this option for different
  661. contexts. See `org-show-hierarchy-above' for valid contexts."
  662. :group 'org-reveal-location
  663. :type org-context-choice)
  664. (defcustom org-indirect-buffer-display 'other-window
  665. "How should indirect tree buffers be displayed?
  666. This applies to indirect buffers created with the commands
  667. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  668. Valid values are:
  669. current-window Display in the current window
  670. other-window Just display in another window.
  671. dedicated-frame Create one new frame, and re-use it each time.
  672. new-frame Make a new frame each time. Note that in this case
  673. previously-made indirect buffers are kept, and you need to
  674. kill these buffers yourself."
  675. :group 'org-structure
  676. :group 'org-agenda-windows
  677. :type '(choice
  678. (const :tag "In current window" current-window)
  679. (const :tag "In current frame, other window" other-window)
  680. (const :tag "Each time a new frame" new-frame)
  681. (const :tag "One dedicated frame" dedicated-frame)))
  682. (defcustom org-use-speed-commands nil
  683. "Non-nil means activate single letter commands at beginning of a headline.
  684. This may also be a function to test for appropriate locations where speed
  685. commands should be active."
  686. :group 'org-structure
  687. :type '(choice
  688. (const :tag "Never" nil)
  689. (const :tag "At beginning of headline stars" t)
  690. (function)))
  691. (defcustom org-speed-commands-user nil
  692. "Alist of additional speed commands.
  693. This list will be checked before `org-speed-commands-default'
  694. when the variable `org-use-speed-commands' is non-nil
  695. and when the cursor is at the beginning of a headline.
  696. The car if each entry is a string with a single letter, which must
  697. be assigned to `self-insert-command' in the global map.
  698. The cdr is either a command to be called interactively, a function
  699. to be called, or a form to be evaluated.
  700. An entry that is just a list with a single string will be interpreted
  701. as a descriptive headline that will be added when listing the speed
  702. commands in the Help buffer using the `?' speed command."
  703. :group 'org-structure
  704. :type '(repeat :value ("k" . ignore)
  705. (choice :value ("k" . ignore)
  706. (list :tag "Descriptive Headline" (string :tag "Headline"))
  707. (cons :tag "Letter and Command"
  708. (string :tag "Command letter")
  709. (choice
  710. (function)
  711. (sexp))))))
  712. (defgroup org-cycle nil
  713. "Options concerning visibility cycling in Org-mode."
  714. :tag "Org Cycle"
  715. :group 'org-structure)
  716. (defcustom org-cycle-skip-children-state-if-no-children t
  717. "Non-nil means skip CHILDREN state in entries that don't have any."
  718. :group 'org-cycle
  719. :type 'boolean)
  720. (defcustom org-cycle-max-level nil
  721. "Maximum level which should still be subject to visibility cycling.
  722. Levels higher than this will, for cycling, be treated as text, not a headline.
  723. When `org-odd-levels-only' is set, a value of N in this variable actually
  724. means 2N-1 stars as the limiting headline.
  725. When nil, cycle all levels.
  726. Note that the limiting level of cycling is also influenced by
  727. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  728. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  729. than its value."
  730. :group 'org-cycle
  731. :type '(choice
  732. (const :tag "No limit" nil)
  733. (integer :tag "Maximum level")))
  734. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  735. "Names of drawers. Drawers are not opened by cycling on the headline above.
  736. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  737. this:
  738. :DRAWERNAME:
  739. .....
  740. :END:
  741. The drawer \"PROPERTIES\" is special for capturing properties through
  742. the property API.
  743. Drawers can be defined on the per-file basis with a line like:
  744. #+DRAWERS: HIDDEN STATE PROPERTIES"
  745. :group 'org-structure
  746. :group 'org-cycle
  747. :type '(repeat (string :tag "Drawer Name")))
  748. (defcustom org-hide-block-startup nil
  749. "Non-nil means entering Org-mode will fold all blocks.
  750. This can also be set in on a per-file basis with
  751. #+STARTUP: hideblocks
  752. #+STARTUP: showblocks"
  753. :group 'org-startup
  754. :group 'org-cycle
  755. :type 'boolean)
  756. (defcustom org-cycle-global-at-bob nil
  757. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  758. This makes it possible to do global cycling without having to use S-TAB or
  759. \\[universal-argument] TAB. For this special case to work, the first line \
  760. of the buffer
  761. must not be a headline - it may be empty or some other text. When used in
  762. this way, `org-cycle-hook' is disables temporarily, to make sure the
  763. cursor stays at the beginning of the buffer.
  764. When this option is nil, don't do anything special at the beginning
  765. of the buffer."
  766. :group 'org-cycle
  767. :type 'boolean)
  768. (defcustom org-cycle-level-after-item/entry-creation t
  769. "Non-nil means cycle entry level or item indentation in new empty entries.
  770. When the cursor is at the end of an empty headline, i.e with only stars
  771. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  772. and then all possible ancestor states, before returning to the original state.
  773. This makes data entry extremely fast: M-RET to create a new headline,
  774. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  775. When the cursor is at the end of an empty plain list item, one TAB will
  776. make it a subitem, two or more tabs will back up to make this an item
  777. higher up in the item hierarchy."
  778. :group 'org-cycle
  779. :type 'boolean)
  780. (defcustom org-cycle-emulate-tab t
  781. "Where should `org-cycle' emulate TAB.
  782. nil Never
  783. white Only in completely white lines
  784. whitestart Only at the beginning of lines, before the first non-white char
  785. t Everywhere except in headlines
  786. exc-hl-bol Everywhere except at the start of a headline
  787. If TAB is used in a place where it does not emulate TAB, the current subtree
  788. visibility is cycled."
  789. :group 'org-cycle
  790. :type '(choice (const :tag "Never" nil)
  791. (const :tag "Only in completely white lines" white)
  792. (const :tag "Before first char in a line" whitestart)
  793. (const :tag "Everywhere except in headlines" t)
  794. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  795. ))
  796. (defcustom org-cycle-separator-lines 2
  797. "Number of empty lines needed to keep an empty line between collapsed trees.
  798. If you leave an empty line between the end of a subtree and the following
  799. headline, this empty line is hidden when the subtree is folded.
  800. Org-mode will leave (exactly) one empty line visible if the number of
  801. empty lines is equal or larger to the number given in this variable.
  802. So the default 2 means at least 2 empty lines after the end of a subtree
  803. are needed to produce free space between a collapsed subtree and the
  804. following headline.
  805. If the number is negative, and the number of empty lines is at least -N,
  806. all empty lines are shown.
  807. Special case: when 0, never leave empty lines in collapsed view."
  808. :group 'org-cycle
  809. :type 'integer)
  810. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  811. (defcustom org-pre-cycle-hook nil
  812. "Hook that is run before visibility cycling is happening.
  813. The function(s) in this hook must accept a single argument which indicates
  814. the new state that will be set right after running this hook. The
  815. argument is a symbol. Before a global state change, it can have the values
  816. `overview', `content', or `all'. Before a local state change, it can have
  817. the values `folded', `children', or `subtree'."
  818. :group 'org-cycle
  819. :type 'hook)
  820. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  821. org-cycle-hide-drawers
  822. org-cycle-show-empty-lines
  823. org-optimize-window-after-visibility-change)
  824. "Hook that is run after `org-cycle' has changed the buffer visibility.
  825. The function(s) in this hook must accept a single argument which indicates
  826. the new state that was set by the most recent `org-cycle' command. The
  827. argument is a symbol. After a global state change, it can have the values
  828. `overview', `content', or `all'. After a local state change, it can have
  829. the values `folded', `children', or `subtree'."
  830. :group 'org-cycle
  831. :type 'hook)
  832. (defgroup org-edit-structure nil
  833. "Options concerning structure editing in Org-mode."
  834. :tag "Org Edit Structure"
  835. :group 'org-structure)
  836. (defcustom org-odd-levels-only nil
  837. "Non-nil means skip even levels and only use odd levels for the outline.
  838. This has the effect that two stars are being added/taken away in
  839. promotion/demotion commands. It also influences how levels are
  840. handled by the exporters.
  841. Changing it requires restart of `font-lock-mode' to become effective
  842. for fontification also in regions already fontified.
  843. You may also set this on a per-file basis by adding one of the following
  844. lines to the buffer:
  845. #+STARTUP: odd
  846. #+STARTUP: oddeven"
  847. :group 'org-edit-structure
  848. :group 'org-appearance
  849. :type 'boolean)
  850. (defcustom org-adapt-indentation t
  851. "Non-nil means adapt indentation to outline node level.
  852. When this variable is set, Org assumes that you write outlines by
  853. indenting text in each node to align with the headline (after the stars).
  854. The following issues are influenced by this variable:
  855. - When this is set and the *entire* text in an entry is indented, the
  856. indentation is increased by one space in a demotion command, and
  857. decreased by one in a promotion command. If any line in the entry
  858. body starts with text at column 0, indentation is not changed at all.
  859. - Property drawers and planning information is inserted indented when
  860. this variable s set. When nil, they will not be indented.
  861. - TAB indents a line relative to context. The lines below a headline
  862. will be indented when this variable is set.
  863. Note that this is all about true indentation, by adding and removing
  864. space characters. See also `org-indent.el' which does level-dependent
  865. indentation in a virtual way, i.e. at display time in Emacs."
  866. :group 'org-edit-structure
  867. :type 'boolean)
  868. (defcustom org-special-ctrl-a/e nil
  869. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  870. When t, `C-a' will bring back the cursor to the beginning of the
  871. headline text, i.e. after the stars and after a possible TODO keyword.
  872. In an item, this will be the position after the bullet.
  873. When the cursor is already at that position, another `C-a' will bring
  874. it to the beginning of the line.
  875. `C-e' will jump to the end of the headline, ignoring the presence of tags
  876. in the headline. A second `C-e' will then jump to the true end of the
  877. line, after any tags. This also means that, when this variable is
  878. non-nil, `C-e' also will never jump beyond the end of the heading of a
  879. folded section, i.e. not after the ellipses.
  880. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  881. going to the true line boundary first. Only a directly following, identical
  882. keypress will bring the cursor to the special positions.
  883. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  884. set separately."
  885. :group 'org-edit-structure
  886. :type '(choice
  887. (const :tag "off" nil)
  888. (const :tag "on: after stars/bullet and before tags first" t)
  889. (const :tag "reversed: true line boundary first" reversed)
  890. (cons :tag "Set C-a and C-e separately"
  891. (choice :tag "Special C-a"
  892. (const :tag "off" nil)
  893. (const :tag "on: after stars/bullet first" t)
  894. (const :tag "reversed: before stars/bullet first" reversed))
  895. (choice :tag "Special C-e"
  896. (const :tag "off" nil)
  897. (const :tag "on: before tags first" t)
  898. (const :tag "reversed: after tags first" reversed)))))
  899. (if (fboundp 'defvaralias)
  900. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  901. (defcustom org-special-ctrl-k nil
  902. "Non-nil means `C-k' will behave specially in headlines.
  903. When nil, `C-k' will call the default `kill-line' command.
  904. When t, the following will happen while the cursor is in the headline:
  905. - When the cursor is at the beginning of a headline, kill the entire
  906. line and possible the folded subtree below the line.
  907. - When in the middle of the headline text, kill the headline up to the tags.
  908. - When after the headline text, kill the tags."
  909. :group 'org-edit-structure
  910. :type 'boolean)
  911. (defcustom org-ctrl-k-protect-subtree nil
  912. "Non-nil means, do not delete a hidden subtree with C-k.
  913. When set to the symbol `error', simply throw an error when C-k is
  914. used to kill (part-of) a headline that has hidden text behind it.
  915. Any other non-nil value will result in a query to the user, if it is
  916. OK to kill that hidden subtree. When nil, kill without remorse."
  917. :group 'org-edit-structure
  918. :type '(choice
  919. (const :tag "Do not protect hidden subtrees" nil)
  920. (const :tag "Protect hidden subtrees with a security query" t)
  921. (const :tag "Never kill a hidden subtree with C-k" error)))
  922. (defcustom org-yank-folded-subtrees t
  923. "Non-nil means when yanking subtrees, fold them.
  924. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  925. it starts with a heading and all other headings in it are either children
  926. or siblings, then fold all the subtrees. However, do this only if no
  927. text after the yank would be swallowed into a folded tree by this action."
  928. :group 'org-edit-structure
  929. :type 'boolean)
  930. (defcustom org-yank-adjusted-subtrees nil
  931. "Non-nil means when yanking subtrees, adjust the level.
  932. With this setting, `org-paste-subtree' is used to insert the subtree, see
  933. this function for details."
  934. :group 'org-edit-structure
  935. :type 'boolean)
  936. (defcustom org-M-RET-may-split-line '((default . t))
  937. "Non-nil means M-RET will split the line at the cursor position.
  938. When nil, it will go to the end of the line before making a
  939. new line.
  940. You may also set this option in a different way for different
  941. contexts. Valid contexts are:
  942. headline when creating a new headline
  943. item when creating a new item
  944. table in a table field
  945. default the value to be used for all contexts not explicitly
  946. customized"
  947. :group 'org-structure
  948. :group 'org-table
  949. :type '(choice
  950. (const :tag "Always" t)
  951. (const :tag "Never" nil)
  952. (repeat :greedy t :tag "Individual contexts"
  953. (cons
  954. (choice :tag "Context"
  955. (const headline)
  956. (const item)
  957. (const table)
  958. (const default))
  959. (boolean)))))
  960. (defcustom org-insert-heading-respect-content nil
  961. "Non-nil means insert new headings after the current subtree.
  962. When nil, the new heading is created directly after the current line.
  963. The commands \\[org-insert-heading-respect-content] and
  964. \\[org-insert-todo-heading-respect-content] turn this variable on
  965. for the duration of the command."
  966. :group 'org-structure
  967. :type 'boolean)
  968. (defcustom org-blank-before-new-entry '((heading . auto)
  969. (plain-list-item . auto))
  970. "Should `org-insert-heading' leave a blank line before new heading/item?
  971. The value is an alist, with `heading' and `plain-list-item' as CAR,
  972. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  973. which case Org will look at the surrounding headings/items and try to
  974. make an intelligent decision whether to insert a blank line or not.
  975. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  976. set, the setting here is ignored and no empty line is inserted, to avoid
  977. breaking the list structure."
  978. :group 'org-edit-structure
  979. :type '(list
  980. (cons (const heading)
  981. (choice (const :tag "Never" nil)
  982. (const :tag "Always" t)
  983. (const :tag "Auto" auto)))
  984. (cons (const plain-list-item)
  985. (choice (const :tag "Never" nil)
  986. (const :tag "Always" t)
  987. (const :tag "Auto" auto)))))
  988. (defcustom org-insert-heading-hook nil
  989. "Hook being run after inserting a new heading."
  990. :group 'org-edit-structure
  991. :type 'hook)
  992. (defcustom org-enable-fixed-width-editor t
  993. "Non-nil means lines starting with \":\" are treated as fixed-width.
  994. This currently only means they are never auto-wrapped.
  995. When nil, such lines will be treated like ordinary lines.
  996. See also the QUOTE keyword."
  997. :group 'org-edit-structure
  998. :type 'boolean)
  999. (defcustom org-goto-auto-isearch t
  1000. "Non-nil means typing characters in `org-goto' starts incremental search."
  1001. :group 'org-edit-structure
  1002. :type 'boolean)
  1003. (defgroup org-sparse-trees nil
  1004. "Options concerning sparse trees in Org-mode."
  1005. :tag "Org Sparse Trees"
  1006. :group 'org-structure)
  1007. (defcustom org-highlight-sparse-tree-matches t
  1008. "Non-nil means highlight all matches that define a sparse tree.
  1009. The highlights will automatically disappear the next time the buffer is
  1010. changed by an edit command."
  1011. :group 'org-sparse-trees
  1012. :type 'boolean)
  1013. (defcustom org-remove-highlights-with-change t
  1014. "Non-nil means any change to the buffer will remove temporary highlights.
  1015. Such highlights are created by `org-occur' and `org-clock-display'.
  1016. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1017. The highlights created by `org-preview-latex-fragment' always need
  1018. `C-c C-c' to be removed."
  1019. :group 'org-sparse-trees
  1020. :group 'org-time
  1021. :type 'boolean)
  1022. (defcustom org-occur-hook '(org-first-headline-recenter)
  1023. "Hook that is run after `org-occur' has constructed a sparse tree.
  1024. This can be used to recenter the window to show as much of the structure
  1025. as possible."
  1026. :group 'org-sparse-trees
  1027. :type 'hook)
  1028. (defgroup org-imenu-and-speedbar nil
  1029. "Options concerning imenu and speedbar in Org-mode."
  1030. :tag "Org Imenu and Speedbar"
  1031. :group 'org-structure)
  1032. (defcustom org-imenu-depth 2
  1033. "The maximum level for Imenu access to Org-mode headlines.
  1034. This also applied for speedbar access."
  1035. :group 'org-imenu-and-speedbar
  1036. :type 'integer)
  1037. (defgroup org-table nil
  1038. "Options concerning tables in Org-mode."
  1039. :tag "Org Table"
  1040. :group 'org)
  1041. (defcustom org-enable-table-editor 'optimized
  1042. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1043. When nil, such lines will be treated like ordinary lines.
  1044. When equal to the symbol `optimized', the table editor will be optimized to
  1045. do the following:
  1046. - Automatic overwrite mode in front of whitespace in table fields.
  1047. This makes the structure of the table stay in tact as long as the edited
  1048. field does not exceed the column width.
  1049. - Minimize the number of realigns. Normally, the table is aligned each time
  1050. TAB or RET are pressed to move to another field. With optimization this
  1051. happens only if changes to a field might have changed the column width.
  1052. Optimization requires replacing the functions `self-insert-command',
  1053. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1054. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1055. very good at guessing when a re-align will be necessary, but you can always
  1056. force one with \\[org-ctrl-c-ctrl-c].
  1057. If you would like to use the optimized version in Org-mode, but the
  1058. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1059. This variable can be used to turn on and off the table editor during a session,
  1060. but in order to toggle optimization, a restart is required.
  1061. See also the variable `org-table-auto-blank-field'."
  1062. :group 'org-table
  1063. :type '(choice
  1064. (const :tag "off" nil)
  1065. (const :tag "on" t)
  1066. (const :tag "on, optimized" optimized)))
  1067. (defcustom org-self-insert-cluster-for-undo t
  1068. "Non-nil means cluster self-insert commands for undo when possible.
  1069. If this is set, then, like in the Emacs command loop, 20 consecutive
  1070. characters will be undone together.
  1071. This is configurable, because there is some impact on typing performance."
  1072. :group 'org-table
  1073. :type 'boolean)
  1074. (defcustom org-table-tab-recognizes-table.el t
  1075. "Non-nil means TAB will automatically notice a table.el table.
  1076. When it sees such a table, it moves point into it and - if necessary -
  1077. calls `table-recognize-table'."
  1078. :group 'org-table-editing
  1079. :type 'boolean)
  1080. (defgroup org-link nil
  1081. "Options concerning links in Org-mode."
  1082. :tag "Org Link"
  1083. :group 'org)
  1084. (defvar org-link-abbrev-alist-local nil
  1085. "Buffer-local version of `org-link-abbrev-alist', which see.
  1086. The value of this is taken from the #+LINK lines.")
  1087. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1088. (defcustom org-link-abbrev-alist nil
  1089. "Alist of link abbreviations.
  1090. The car of each element is a string, to be replaced at the start of a link.
  1091. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1092. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1093. [[linkkey:tag][description]]
  1094. The 'linkkey' must be a word word, starting with a letter, followed
  1095. by letters, numbers, '-' or '_'.
  1096. If REPLACE is a string, the tag will simply be appended to create the link.
  1097. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1098. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1099. be inserted at that point (see the function `url-hexify-string').
  1100. REPLACE may also be a function that will be called with the tag as the
  1101. only argument to create the link, which should be returned as a string.
  1102. See the manual for examples."
  1103. :group 'org-link
  1104. :type '(repeat
  1105. (cons
  1106. (string :tag "Protocol")
  1107. (choice
  1108. (string :tag "Format")
  1109. (function)))))
  1110. (defcustom org-descriptive-links t
  1111. "Non-nil means Org will display descriptive links.
  1112. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1113. \"Org Website\", hiding the link itself and just displaying its
  1114. description. When set to `nil', Org will display the full links
  1115. literally.
  1116. You can interactively set the value of this variable by calling
  1117. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1118. :group 'org-link
  1119. :type 'boolean)
  1120. (defcustom org-link-file-path-type 'adaptive
  1121. "How the path name in file links should be stored.
  1122. Valid values are:
  1123. relative Relative to the current directory, i.e. the directory of the file
  1124. into which the link is being inserted.
  1125. absolute Absolute path, if possible with ~ for home directory.
  1126. noabbrev Absolute path, no abbreviation of home directory.
  1127. adaptive Use relative path for files in the current directory and sub-
  1128. directories of it. For other files, use an absolute path."
  1129. :group 'org-link
  1130. :type '(choice
  1131. (const relative)
  1132. (const absolute)
  1133. (const noabbrev)
  1134. (const adaptive)))
  1135. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1136. "Types of links that should be activated in Org-mode files.
  1137. This is a list of symbols, each leading to the activation of a certain link
  1138. type. In principle, it does not hurt to turn on most link types - there may
  1139. be a small gain when turning off unused link types. The types are:
  1140. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1141. angle Links in angular brackets that may contain whitespace like
  1142. <bbdb:Carsten Dominik>.
  1143. plain Plain links in normal text, no whitespace, like http://google.com.
  1144. radio Text that is matched by a radio target, see manual for details.
  1145. tag Tag settings in a headline (link to tag search).
  1146. date Time stamps (link to calendar).
  1147. footnote Footnote labels.
  1148. Changing this variable requires a restart of Emacs to become effective."
  1149. :group 'org-link
  1150. :type '(set :greedy t
  1151. (const :tag "Double bracket links" bracket)
  1152. (const :tag "Angular bracket links" angle)
  1153. (const :tag "Plain text links" plain)
  1154. (const :tag "Radio target matches" radio)
  1155. (const :tag "Tags" tag)
  1156. (const :tag "Timestamps" date)
  1157. (const :tag "Footnotes" footnote)))
  1158. (defcustom org-make-link-description-function nil
  1159. "Function to use to generate link descriptions from links.
  1160. If nil the link location will be used. This function must take
  1161. two parameters; the first is the link and the second the
  1162. description `org-insert-link' has generated, and should return the
  1163. description to use."
  1164. :group 'org-link
  1165. :type 'function)
  1166. (defgroup org-link-store nil
  1167. "Options concerning storing links in Org-mode."
  1168. :tag "Org Store Link"
  1169. :group 'org-link)
  1170. (defcustom org-email-link-description-format "Email %c: %.30s"
  1171. "Format of the description part of a link to an email or usenet message.
  1172. The following %-escapes will be replaced by corresponding information:
  1173. %F full \"From\" field
  1174. %f name, taken from \"From\" field, address if no name
  1175. %T full \"To\" field
  1176. %t first name in \"To\" field, address if no name
  1177. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1178. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1179. %s subject
  1180. %d date
  1181. %m message-id.
  1182. You may use normal field width specification between the % and the letter.
  1183. This is for example useful to limit the length of the subject.
  1184. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1185. :group 'org-link-store
  1186. :type 'string)
  1187. (defcustom org-from-is-user-regexp
  1188. (let (r1 r2)
  1189. (when (and user-mail-address (not (string= user-mail-address "")))
  1190. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1191. (when (and user-full-name (not (string= user-full-name "")))
  1192. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1193. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1194. "Regexp matched against the \"From:\" header of an email or usenet message.
  1195. It should match if the message is from the user him/herself."
  1196. :group 'org-link-store
  1197. :type 'regexp)
  1198. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1199. "Non-nil means storing a link to an Org file will use entry IDs.
  1200. Note that before this variable is even considered, org-id must be loaded,
  1201. so please customize `org-modules' and turn it on.
  1202. The variable can have the following values:
  1203. t Create an ID if needed to make a link to the current entry.
  1204. create-if-interactive
  1205. If `org-store-link' is called directly (interactively, as a user
  1206. command), do create an ID to support the link. But when doing the
  1207. job for remember, only use the ID if it already exists. The
  1208. purpose of this setting is to avoid proliferation of unwanted
  1209. IDs, just because you happen to be in an Org file when you
  1210. call `org-remember' that automatically and preemptively
  1211. creates a link. If you do want to get an ID link in a remember
  1212. template to an entry not having an ID, create it first by
  1213. explicitly creating a link to it, using `C-c C-l' first.
  1214. create-if-interactive-and-no-custom-id
  1215. Like create-if-interactive, but do not create an ID if there is
  1216. a CUSTOM_ID property defined in the entry. This is the default.
  1217. use-existing
  1218. Use existing ID, do not create one.
  1219. nil Never use an ID to make a link, instead link using a text search for
  1220. the headline text."
  1221. :group 'org-link-store
  1222. :type '(choice
  1223. (const :tag "Create ID to make link" t)
  1224. (const :tag "Create if storing link interactively"
  1225. create-if-interactive)
  1226. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1227. create-if-interactive-and-no-custom-id)
  1228. (const :tag "Only use existing" use-existing)
  1229. (const :tag "Do not use ID to create link" nil)))
  1230. (defcustom org-context-in-file-links t
  1231. "Non-nil means file links from `org-store-link' contain context.
  1232. A search string will be added to the file name with :: as separator and
  1233. used to find the context when the link is activated by the command
  1234. `org-open-at-point'. When this option is t, the entire active region
  1235. will be placed in the search string of the file link. If set to a
  1236. positive integer, only the first n lines of context will be stored.
  1237. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1238. negates this setting for the duration of the command."
  1239. :group 'org-link-store
  1240. :type '(choice boolean integer))
  1241. (defcustom org-keep-stored-link-after-insertion nil
  1242. "Non-nil means keep link in list for entire session.
  1243. The command `org-store-link' adds a link pointing to the current
  1244. location to an internal list. These links accumulate during a session.
  1245. The command `org-insert-link' can be used to insert links into any
  1246. Org-mode file (offering completion for all stored links). When this
  1247. option is nil, every link which has been inserted once using \\[org-insert-link]
  1248. will be removed from the list, to make completing the unused links
  1249. more efficient."
  1250. :group 'org-link-store
  1251. :type 'boolean)
  1252. (defgroup org-link-follow nil
  1253. "Options concerning following links in Org-mode."
  1254. :tag "Org Follow Link"
  1255. :group 'org-link)
  1256. (defcustom org-link-translation-function nil
  1257. "Function to translate links with different syntax to Org syntax.
  1258. This can be used to translate links created for example by the Planner
  1259. or emacs-wiki packages to Org syntax.
  1260. The function must accept two parameters, a TYPE containing the link
  1261. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1262. which is everything after the link protocol. It should return a cons
  1263. with possibly modified values of type and path.
  1264. Org contains a function for this, so if you set this variable to
  1265. `org-translate-link-from-planner', you should be able follow many
  1266. links created by planner."
  1267. :group 'org-link-follow
  1268. :type 'function)
  1269. (defcustom org-follow-link-hook nil
  1270. "Hook that is run after a link has been followed."
  1271. :group 'org-link-follow
  1272. :type 'hook)
  1273. (defcustom org-tab-follows-link nil
  1274. "Non-nil means on links TAB will follow the link.
  1275. Needs to be set before org.el is loaded.
  1276. This really should not be used, it does not make sense, and the
  1277. implementation is bad."
  1278. :group 'org-link-follow
  1279. :type 'boolean)
  1280. (defcustom org-return-follows-link nil
  1281. "Non-nil means on links RET will follow the link."
  1282. :group 'org-link-follow
  1283. :type 'boolean)
  1284. (defcustom org-mouse-1-follows-link
  1285. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1286. "Non-nil means mouse-1 on a link will follow the link.
  1287. A longer mouse click will still set point. Does not work on XEmacs.
  1288. Needs to be set before org.el is loaded."
  1289. :group 'org-link-follow
  1290. :type 'boolean)
  1291. (defcustom org-mark-ring-length 4
  1292. "Number of different positions to be recorded in the ring.
  1293. Changing this requires a restart of Emacs to work correctly."
  1294. :group 'org-link-follow
  1295. :type 'integer)
  1296. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1297. "Non-nil means internal links in Org files must exactly match a headline.
  1298. When nil, the link search tries to match a phrase with all words
  1299. in the search text."
  1300. :group 'org-link-follow
  1301. :type '(choice
  1302. (const :tag "Use fuzy text search" nil)
  1303. (const :tag "Match only exact headline" t)
  1304. (const :tag "Match extact headline or query to create it"
  1305. query-to-create)))
  1306. (defcustom org-link-frame-setup
  1307. '((vm . vm-visit-folder-other-frame)
  1308. (gnus . org-gnus-no-new-news)
  1309. (file . find-file-other-window)
  1310. (wl . wl-other-frame))
  1311. "Setup the frame configuration for following links.
  1312. When following a link with Emacs, it may often be useful to display
  1313. this link in another window or frame. This variable can be used to
  1314. set this up for the different types of links.
  1315. For VM, use any of
  1316. `vm-visit-folder'
  1317. `vm-visit-folder-other-window'
  1318. `vm-visit-folder-other-frame'
  1319. For Gnus, use any of
  1320. `gnus'
  1321. `gnus-other-frame'
  1322. `org-gnus-no-new-news'
  1323. For FILE, use any of
  1324. `find-file'
  1325. `find-file-other-window'
  1326. `find-file-other-frame'
  1327. For Wanderlust use any of
  1328. `wl'
  1329. `wl-other-frame'
  1330. For the calendar, use the variable `calendar-setup'.
  1331. For BBDB, it is currently only possible to display the matches in
  1332. another window."
  1333. :group 'org-link-follow
  1334. :type '(list
  1335. (cons (const vm)
  1336. (choice
  1337. (const vm-visit-folder)
  1338. (const vm-visit-folder-other-window)
  1339. (const vm-visit-folder-other-frame)))
  1340. (cons (const gnus)
  1341. (choice
  1342. (const gnus)
  1343. (const gnus-other-frame)
  1344. (const org-gnus-no-new-news)))
  1345. (cons (const file)
  1346. (choice
  1347. (const find-file)
  1348. (const find-file-other-window)
  1349. (const find-file-other-frame)))
  1350. (cons (const wl)
  1351. (choice
  1352. (const wl)
  1353. (const wl-other-frame)))))
  1354. (defcustom org-display-internal-link-with-indirect-buffer nil
  1355. "Non-nil means use indirect buffer to display infile links.
  1356. Activating internal links (from one location in a file to another location
  1357. in the same file) normally just jumps to the location. When the link is
  1358. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1359. is displayed in
  1360. another window. When this option is set, the other window actually displays
  1361. an indirect buffer clone of the current buffer, to avoid any visibility
  1362. changes to the current buffer."
  1363. :group 'org-link-follow
  1364. :type 'boolean)
  1365. (defcustom org-open-non-existing-files nil
  1366. "Non-nil means `org-open-file' will open non-existing files.
  1367. When nil, an error will be generated.
  1368. This variable applies only to external applications because they
  1369. might choke on non-existing files. If the link is to a file that
  1370. will be opened in Emacs, the variable is ignored."
  1371. :group 'org-link-follow
  1372. :type 'boolean)
  1373. (defcustom org-open-directory-means-index-dot-org nil
  1374. "Non-nil means a link to a directory really means to index.org.
  1375. When nil, following a directory link will run dired or open a finder/explorer
  1376. window on that directory."
  1377. :group 'org-link-follow
  1378. :type 'boolean)
  1379. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1380. "Function and arguments to call for following mailto links.
  1381. This is a list with the first element being a Lisp function, and the
  1382. remaining elements being arguments to the function. In string arguments,
  1383. %a will be replaced by the address, and %s will be replaced by the subject
  1384. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1385. :group 'org-link-follow
  1386. :type '(choice
  1387. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1388. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1389. (const :tag "message-mail" (message-mail "%a" "%s"))
  1390. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1391. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1392. "Non-nil means ask for confirmation before executing shell links.
  1393. Shell links can be dangerous: just think about a link
  1394. [[shell:rm -rf ~/*][Google Search]]
  1395. This link would show up in your Org-mode document as \"Google Search\",
  1396. but really it would remove your entire home directory.
  1397. Therefore we advise against setting this variable to nil.
  1398. Just change it to `y-or-n-p' if you want to confirm with a
  1399. single keystroke rather than having to type \"yes\"."
  1400. :group 'org-link-follow
  1401. :type '(choice
  1402. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1403. (const :tag "with y-or-n (faster)" y-or-n-p)
  1404. (const :tag "no confirmation (dangerous)" nil)))
  1405. (put 'org-confirm-shell-link-function
  1406. 'safe-local-variable
  1407. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1408. (defcustom org-confirm-shell-link-not-regexp ""
  1409. "A regexp to skip confirmation for shell links."
  1410. :group 'org-link-follow
  1411. :type 'regexp)
  1412. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1413. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1414. Elisp links can be dangerous: just think about a link
  1415. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1416. This link would show up in your Org-mode document as \"Google Search\",
  1417. but really it would remove your entire home directory.
  1418. Therefore we advise against setting this variable to nil.
  1419. Just change it to `y-or-n-p' if you want to confirm with a
  1420. single keystroke rather than having to type \"yes\"."
  1421. :group 'org-link-follow
  1422. :type '(choice
  1423. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1424. (const :tag "with y-or-n (faster)" y-or-n-p)
  1425. (const :tag "no confirmation (dangerous)" nil)))
  1426. (put 'org-confirm-shell-link-function
  1427. 'safe-local-variable
  1428. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1429. (defcustom org-confirm-elisp-link-not-regexp ""
  1430. "A regexp to skip confirmation for Elisp links."
  1431. :group 'org-link-follow
  1432. :type 'regexp)
  1433. (defconst org-file-apps-defaults-gnu
  1434. '((remote . emacs)
  1435. (system . mailcap)
  1436. (t . mailcap))
  1437. "Default file applications on a UNIX or GNU/Linux system.
  1438. See `org-file-apps'.")
  1439. (defconst org-file-apps-defaults-macosx
  1440. '((remote . emacs)
  1441. (t . "open %s")
  1442. (system . "open %s")
  1443. ("ps.gz" . "gv %s")
  1444. ("eps.gz" . "gv %s")
  1445. ("dvi" . "xdvi %s")
  1446. ("fig" . "xfig %s"))
  1447. "Default file applications on a MacOS X system.
  1448. The system \"open\" is known as a default, but we use X11 applications
  1449. for some files for which the OS does not have a good default.
  1450. See `org-file-apps'.")
  1451. (defconst org-file-apps-defaults-windowsnt
  1452. (list
  1453. '(remote . emacs)
  1454. (cons t
  1455. (list (if (featurep 'xemacs)
  1456. 'mswindows-shell-execute
  1457. 'w32-shell-execute)
  1458. "open" 'file))
  1459. (cons 'system
  1460. (list (if (featurep 'xemacs)
  1461. 'mswindows-shell-execute
  1462. 'w32-shell-execute)
  1463. "open" 'file)))
  1464. "Default file applications on a Windows NT system.
  1465. The system \"open\" is used for most files.
  1466. See `org-file-apps'.")
  1467. (defcustom org-file-apps
  1468. '(
  1469. (auto-mode . emacs)
  1470. ("\\.mm\\'" . default)
  1471. ("\\.x?html?\\'" . default)
  1472. ("\\.pdf\\'" . default)
  1473. )
  1474. "External applications for opening `file:path' items in a document.
  1475. Org-mode uses system defaults for different file types, but
  1476. you can use this variable to set the application for a given file
  1477. extension. The entries in this list are cons cells where the car identifies
  1478. files and the cdr the corresponding command. Possible values for the
  1479. file identifier are
  1480. \"string\" A string as a file identifier can be interpreted in different
  1481. ways, depending on its contents:
  1482. - Alphanumeric characters only:
  1483. Match links with this file extension.
  1484. Example: (\"pdf\" . \"evince %s\")
  1485. to open PDFs with evince.
  1486. - Regular expression: Match links where the
  1487. filename matches the regexp. If you want to
  1488. use groups here, use shy groups.
  1489. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1490. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1491. to open *.html and *.xhtml with firefox.
  1492. - Regular expression which contains (non-shy) groups:
  1493. Match links where the whole link, including \"::\", and
  1494. anything after that, matches the regexp.
  1495. In a custom command string, %1, %2, etc. are replaced with
  1496. the parts of the link that were matched by the groups.
  1497. For backwards compatibility, if a command string is given
  1498. that does not use any of the group matches, this case is
  1499. handled identically to the second one (i.e. match against
  1500. file name only).
  1501. In a custom lisp form, you can access the group matches with
  1502. (match-string n link).
  1503. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1504. to open [[file:document.pdf::5]] with evince at page 5.
  1505. `directory' Matches a directory
  1506. `remote' Matches a remote file, accessible through tramp or efs.
  1507. Remote files most likely should be visited through Emacs
  1508. because external applications cannot handle such paths.
  1509. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1510. so all files Emacs knows how to handle. Using this with
  1511. command `emacs' will open most files in Emacs. Beware that this
  1512. will also open html files inside Emacs, unless you add
  1513. (\"html\" . default) to the list as well.
  1514. t Default for files not matched by any of the other options.
  1515. `system' The system command to open files, like `open' on Windows
  1516. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1517. that will be selected if you call `C-c C-o' with a double
  1518. \\[universal-argument] \\[universal-argument] prefix.
  1519. Possible values for the command are:
  1520. `emacs' The file will be visited by the current Emacs process.
  1521. `default' Use the default application for this file type, which is the
  1522. association for t in the list, most likely in the system-specific
  1523. part.
  1524. This can be used to overrule an unwanted setting in the
  1525. system-specific variable.
  1526. `system' Use the system command for opening files, like \"open\".
  1527. This command is specified by the entry whose car is `system'.
  1528. Most likely, the system-specific version of this variable
  1529. does define this command, but you can overrule/replace it
  1530. here.
  1531. string A command to be executed by a shell; %s will be replaced
  1532. by the path to the file.
  1533. sexp A Lisp form which will be evaluated. The file path will
  1534. be available in the Lisp variable `file'.
  1535. For more examples, see the system specific constants
  1536. `org-file-apps-defaults-macosx'
  1537. `org-file-apps-defaults-windowsnt'
  1538. `org-file-apps-defaults-gnu'."
  1539. :group 'org-link-follow
  1540. :type '(repeat
  1541. (cons (choice :value ""
  1542. (string :tag "Extension")
  1543. (const :tag "System command to open files" system)
  1544. (const :tag "Default for unrecognized files" t)
  1545. (const :tag "Remote file" remote)
  1546. (const :tag "Links to a directory" directory)
  1547. (const :tag "Any files that have Emacs modes"
  1548. auto-mode))
  1549. (choice :value ""
  1550. (const :tag "Visit with Emacs" emacs)
  1551. (const :tag "Use default" default)
  1552. (const :tag "Use the system command" system)
  1553. (string :tag "Command")
  1554. (sexp :tag "Lisp form")))))
  1555. (defgroup org-refile nil
  1556. "Options concerning refiling entries in Org-mode."
  1557. :tag "Org Refile"
  1558. :group 'org)
  1559. (defcustom org-directory "~/org"
  1560. "Directory with org files.
  1561. This is just a default location to look for Org files. There is no need
  1562. at all to put your files into this directory. It is only used in the
  1563. following situations:
  1564. 1. When a remember template specifies a target file that is not an
  1565. absolute path. The path will then be interpreted relative to
  1566. `org-directory'
  1567. 2. When a remember note is filed away in an interactive way (when exiting the
  1568. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1569. with `org-directory' as the default path."
  1570. :group 'org-refile
  1571. :group 'org-remember
  1572. :type 'directory)
  1573. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1574. "Default target for storing notes.
  1575. Used as a fall back file for org-remember.el and org-capture.el, for
  1576. templates that do not specify a target file."
  1577. :group 'org-refile
  1578. :group 'org-remember
  1579. :type '(choice
  1580. (const :tag "Default from remember-data-file" nil)
  1581. file))
  1582. (defcustom org-goto-interface 'outline
  1583. "The default interface to be used for `org-goto'.
  1584. Allowed values are:
  1585. outline The interface shows an outline of the relevant file
  1586. and the correct heading is found by moving through
  1587. the outline or by searching with incremental search.
  1588. outline-path-completion Headlines in the current buffer are offered via
  1589. completion. This is the interface also used by
  1590. the refile command."
  1591. :group 'org-refile
  1592. :type '(choice
  1593. (const :tag "Outline" outline)
  1594. (const :tag "Outline-path-completion" outline-path-completion)))
  1595. (defcustom org-goto-max-level 5
  1596. "Maximum target level when running `org-goto' with refile interface."
  1597. :group 'org-refile
  1598. :type 'integer)
  1599. (defcustom org-reverse-note-order nil
  1600. "Non-nil means store new notes at the beginning of a file or entry.
  1601. When nil, new notes will be filed to the end of a file or entry.
  1602. This can also be a list with cons cells of regular expressions that
  1603. are matched against file names, and values."
  1604. :group 'org-remember
  1605. :group 'org-refile
  1606. :type '(choice
  1607. (const :tag "Reverse always" t)
  1608. (const :tag "Reverse never" nil)
  1609. (repeat :tag "By file name regexp"
  1610. (cons regexp boolean))))
  1611. (defcustom org-log-refile nil
  1612. "Information to record when a task is refiled.
  1613. Possible values are:
  1614. nil Don't add anything
  1615. time Add a time stamp to the task
  1616. note Prompt for a note and add it with template `org-log-note-headings'
  1617. This option can also be set with on a per-file-basis with
  1618. #+STARTUP: nologrefile
  1619. #+STARTUP: logrefile
  1620. #+STARTUP: lognoterefile
  1621. You can have local logging settings for a subtree by setting the LOGGING
  1622. property to one or more of these keywords.
  1623. When bulk-refiling from the agenda, the value `note' is forbidden and
  1624. will temporarily be changed to `time'."
  1625. :group 'org-refile
  1626. :group 'org-progress
  1627. :type '(choice
  1628. (const :tag "No logging" nil)
  1629. (const :tag "Record timestamp" time)
  1630. (const :tag "Record timestamp with note." note)))
  1631. (defcustom org-refile-targets nil
  1632. "Targets for refiling entries with \\[org-refile].
  1633. This is list of cons cells. Each cell contains:
  1634. - a specification of the files to be considered, either a list of files,
  1635. or a symbol whose function or variable value will be used to retrieve
  1636. a file name or a list of file names. If you use `org-agenda-files' for
  1637. that, all agenda files will be scanned for targets. Nil means consider
  1638. headings in the current buffer.
  1639. - A specification of how to find candidate refile targets. This may be
  1640. any of:
  1641. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1642. This tag has to be present in all target headlines, inheritance will
  1643. not be considered.
  1644. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1645. todo keyword.
  1646. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1647. headlines that are refiling targets.
  1648. - a cons cell (:level . N). Any headline of level N is considered a target.
  1649. Note that, when `org-odd-levels-only' is set, level corresponds to
  1650. order in hierarchy, not to the number of stars.
  1651. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1652. Note that, when `org-odd-levels-only' is set, level corresponds to
  1653. order in hierarchy, not to the number of stars.
  1654. You can set the variable `org-refile-target-verify-function' to a function
  1655. to verify each headline found by the simple criteria above.
  1656. When this variable is nil, all top-level headlines in the current buffer
  1657. are used, equivalent to the value `((nil . (:level . 1))'."
  1658. :group 'org-refile
  1659. :type '(repeat
  1660. (cons
  1661. (choice :value org-agenda-files
  1662. (const :tag "All agenda files" org-agenda-files)
  1663. (const :tag "Current buffer" nil)
  1664. (function) (variable) (file))
  1665. (choice :tag "Identify target headline by"
  1666. (cons :tag "Specific tag" (const :value :tag) (string))
  1667. (cons :tag "TODO keyword" (const :value :todo) (string))
  1668. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1669. (cons :tag "Level number" (const :value :level) (integer))
  1670. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1671. (defcustom org-refile-target-verify-function nil
  1672. "Function to verify if the headline at point should be a refile target.
  1673. The function will be called without arguments, with point at the
  1674. beginning of the headline. It should return t and leave point
  1675. where it is if the headline is a valid target for refiling.
  1676. If the target should not be selected, the function must return nil.
  1677. In addition to this, it may move point to a place from where the search
  1678. should be continued. For example, the function may decide that the entire
  1679. subtree of the current entry should be excluded and move point to the end
  1680. of the subtree."
  1681. :group 'org-refile
  1682. :type 'function)
  1683. (defcustom org-refile-use-cache nil
  1684. "Non-nil means cache refile targets to speed up the process.
  1685. The cache for a particular file will be updated automatically when
  1686. the buffer has been killed, or when any of the marker used for flagging
  1687. refile targets no longer points at a live buffer.
  1688. If you have added new entries to a buffer that might themselves be targets,
  1689. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1690. find that easier, `C-u C-u C-u C-c C-w'."
  1691. :group 'org-refile
  1692. :type 'boolean)
  1693. (defcustom org-refile-use-outline-path nil
  1694. "Non-nil means provide refile targets as paths.
  1695. So a level 3 headline will be available as level1/level2/level3.
  1696. When the value is `file', also include the file name (without directory)
  1697. into the path. In this case, you can also stop the completion after
  1698. the file name, to get entries inserted as top level in the file.
  1699. When `full-file-path', include the full file path."
  1700. :group 'org-refile
  1701. :type '(choice
  1702. (const :tag "Not" nil)
  1703. (const :tag "Yes" t)
  1704. (const :tag "Start with file name" file)
  1705. (const :tag "Start with full file path" full-file-path)))
  1706. (defcustom org-outline-path-complete-in-steps t
  1707. "Non-nil means complete the outline path in hierarchical steps.
  1708. When Org-mode uses the refile interface to select an outline path
  1709. \(see variable `org-refile-use-outline-path'), the completion of
  1710. the path can be done is a single go, or if can be done in steps down
  1711. the headline hierarchy. Going in steps is probably the best if you
  1712. do not use a special completion package like `ido' or `icicles'.
  1713. However, when using these packages, going in one step can be very
  1714. fast, while still showing the whole path to the entry."
  1715. :group 'org-refile
  1716. :type 'boolean)
  1717. (defcustom org-refile-allow-creating-parent-nodes nil
  1718. "Non-nil means allow to create new nodes as refile targets.
  1719. New nodes are then created by adding \"/new node name\" to the completion
  1720. of an existing node. When the value of this variable is `confirm',
  1721. new node creation must be confirmed by the user (recommended)
  1722. When nil, the completion must match an existing entry.
  1723. Note that, if the new heading is not seen by the criteria
  1724. listed in `org-refile-targets', multiple instances of the same
  1725. heading would be created by trying again to file under the new
  1726. heading."
  1727. :group 'org-refile
  1728. :type '(choice
  1729. (const :tag "Never" nil)
  1730. (const :tag "Always" t)
  1731. (const :tag "Prompt for confirmation" confirm)))
  1732. (defcustom org-refile-active-region-within-subtree nil
  1733. "Non-nil means also refile active region within a subtree.
  1734. By default `org-refile' doesn't allow refiling regions if they
  1735. don't contain a set of subtrees, but it might be convenient to
  1736. do so sometimes: in that case, the first line of the region is
  1737. converted to a headline before refiling."
  1738. :group 'org-refile
  1739. :type 'boolean)
  1740. (defgroup org-todo nil
  1741. "Options concerning TODO items in Org-mode."
  1742. :tag "Org TODO"
  1743. :group 'org)
  1744. (defgroup org-progress nil
  1745. "Options concerning Progress logging in Org-mode."
  1746. :tag "Org Progress"
  1747. :group 'org-time)
  1748. (defvar org-todo-interpretation-widgets
  1749. '((:tag "Sequence (cycling hits every state)" sequence)
  1750. (:tag "Type (cycling directly to DONE)" type))
  1751. "The available interpretation symbols for customizing `org-todo-keywords'.
  1752. Interested libraries should add to this list.")
  1753. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1754. "List of TODO entry keyword sequences and their interpretation.
  1755. \\<org-mode-map>This is a list of sequences.
  1756. Each sequence starts with a symbol, either `sequence' or `type',
  1757. indicating if the keywords should be interpreted as a sequence of
  1758. action steps, or as different types of TODO items. The first
  1759. keywords are states requiring action - these states will select a headline
  1760. for inclusion into the global TODO list Org-mode produces. If one of
  1761. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1762. signify that no further action is necessary. If \"|\" is not found,
  1763. the last keyword is treated as the only DONE state of the sequence.
  1764. The command \\[org-todo] cycles an entry through these states, and one
  1765. additional state where no keyword is present. For details about this
  1766. cycling, see the manual.
  1767. TODO keywords and interpretation can also be set on a per-file basis with
  1768. the special #+SEQ_TODO and #+TYP_TODO lines.
  1769. Each keyword can optionally specify a character for fast state selection
  1770. \(in combination with the variable `org-use-fast-todo-selection')
  1771. and specifiers for state change logging, using the same syntax
  1772. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1773. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1774. indicates to record a time stamp each time this state is selected.
  1775. Each keyword may also specify if a timestamp or a note should be
  1776. recorded when entering or leaving the state, by adding additional
  1777. characters in the parenthesis after the keyword. This looks like this:
  1778. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1779. record only the time of the state change. With X and Y being either
  1780. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1781. Y when leaving the state if and only if the *target* state does not
  1782. define X. You may omit any of the fast-selection key or X or /Y,
  1783. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1784. For backward compatibility, this variable may also be just a list
  1785. of keywords - in this case the interpretation (sequence or type) will be
  1786. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1787. :group 'org-todo
  1788. :group 'org-keywords
  1789. :type '(choice
  1790. (repeat :tag "Old syntax, just keywords"
  1791. (string :tag "Keyword"))
  1792. (repeat :tag "New syntax"
  1793. (cons
  1794. (choice
  1795. :tag "Interpretation"
  1796. ;;Quick and dirty way to see
  1797. ;;`org-todo-interpretations'. This takes the
  1798. ;;place of item arguments
  1799. :convert-widget
  1800. (lambda (widget)
  1801. (widget-put widget
  1802. :args (mapcar
  1803. #'(lambda (x)
  1804. (widget-convert
  1805. (cons 'const x)))
  1806. org-todo-interpretation-widgets))
  1807. widget))
  1808. (repeat
  1809. (string :tag "Keyword"))))))
  1810. (defvar org-todo-keywords-1 nil
  1811. "All TODO and DONE keywords active in a buffer.")
  1812. (make-variable-buffer-local 'org-todo-keywords-1)
  1813. (defvar org-todo-keywords-for-agenda nil)
  1814. (defvar org-done-keywords-for-agenda nil)
  1815. (defvar org-drawers-for-agenda nil)
  1816. (defvar org-todo-keyword-alist-for-agenda nil)
  1817. (defvar org-tag-alist-for-agenda nil)
  1818. (defvar org-agenda-contributing-files nil)
  1819. (defvar org-not-done-keywords nil)
  1820. (make-variable-buffer-local 'org-not-done-keywords)
  1821. (defvar org-done-keywords nil)
  1822. (make-variable-buffer-local 'org-done-keywords)
  1823. (defvar org-todo-heads nil)
  1824. (make-variable-buffer-local 'org-todo-heads)
  1825. (defvar org-todo-sets nil)
  1826. (make-variable-buffer-local 'org-todo-sets)
  1827. (defvar org-todo-log-states nil)
  1828. (make-variable-buffer-local 'org-todo-log-states)
  1829. (defvar org-todo-kwd-alist nil)
  1830. (make-variable-buffer-local 'org-todo-kwd-alist)
  1831. (defvar org-todo-key-alist nil)
  1832. (make-variable-buffer-local 'org-todo-key-alist)
  1833. (defvar org-todo-key-trigger nil)
  1834. (make-variable-buffer-local 'org-todo-key-trigger)
  1835. (defcustom org-todo-interpretation 'sequence
  1836. "Controls how TODO keywords are interpreted.
  1837. This variable is in principle obsolete and is only used for
  1838. backward compatibility, if the interpretation of todo keywords is
  1839. not given already in `org-todo-keywords'. See that variable for
  1840. more information."
  1841. :group 'org-todo
  1842. :group 'org-keywords
  1843. :type '(choice (const sequence)
  1844. (const type)))
  1845. (defcustom org-use-fast-todo-selection t
  1846. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1847. This variable describes if and under what circumstances the cycling
  1848. mechanism for TODO keywords will be replaced by a single-key, direct
  1849. selection scheme.
  1850. When nil, fast selection is never used.
  1851. When the symbol `prefix', it will be used when `org-todo' is called with
  1852. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1853. in an agenda buffer.
  1854. When t, fast selection is used by default. In this case, the prefix
  1855. argument forces cycling instead.
  1856. In all cases, the special interface is only used if access keys have actually
  1857. been assigned by the user, i.e. if keywords in the configuration are followed
  1858. by a letter in parenthesis, like TODO(t)."
  1859. :group 'org-todo
  1860. :type '(choice
  1861. (const :tag "Never" nil)
  1862. (const :tag "By default" t)
  1863. (const :tag "Only with C-u C-c C-t" prefix)))
  1864. (defcustom org-provide-todo-statistics t
  1865. "Non-nil means update todo statistics after insert and toggle.
  1866. ALL-HEADLINES means update todo statistics by including headlines
  1867. with no TODO keyword as well, counting them as not done.
  1868. A list of TODO keywords means the same, but skip keywords that are
  1869. not in this list.
  1870. When this is set, todo statistics is updated in the parent of the
  1871. current entry each time a todo state is changed."
  1872. :group 'org-todo
  1873. :type '(choice
  1874. (const :tag "Yes, only for TODO entries" t)
  1875. (const :tag "Yes, including all entries" 'all-headlines)
  1876. (repeat :tag "Yes, for TODOs in this list"
  1877. (string :tag "TODO keyword"))
  1878. (other :tag "No TODO statistics" nil)))
  1879. (defcustom org-hierarchical-todo-statistics t
  1880. "Non-nil means TODO statistics covers just direct children.
  1881. When nil, all entries in the subtree are considered.
  1882. This has only an effect if `org-provide-todo-statistics' is set.
  1883. To set this to nil for only a single subtree, use a COOKIE_DATA
  1884. property and include the word \"recursive\" into the value."
  1885. :group 'org-todo
  1886. :type 'boolean)
  1887. (defcustom org-after-todo-state-change-hook nil
  1888. "Hook which is run after the state of a TODO item was changed.
  1889. The new state (a string with a TODO keyword, or nil) is available in the
  1890. Lisp variable `state'."
  1891. :group 'org-todo
  1892. :type 'hook)
  1893. (defvar org-blocker-hook nil
  1894. "Hook for functions that are allowed to block a state change.
  1895. Each function gets as its single argument a property list, see
  1896. `org-trigger-hook' for more information about this list.
  1897. If any of the functions in this hook returns nil, the state change
  1898. is blocked.")
  1899. (defvar org-trigger-hook nil
  1900. "Hook for functions that are triggered by a state change.
  1901. Each function gets as its single argument a property list with at least
  1902. the following elements:
  1903. (:type type-of-change :position pos-at-entry-start
  1904. :from old-state :to new-state)
  1905. Depending on the type, more properties may be present.
  1906. This mechanism is currently implemented for:
  1907. TODO state changes
  1908. ------------------
  1909. :type todo-state-change
  1910. :from previous state (keyword as a string), or nil, or a symbol
  1911. 'todo' or 'done', to indicate the general type of state.
  1912. :to new state, like in :from")
  1913. (defcustom org-enforce-todo-dependencies nil
  1914. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1915. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1916. be blocked if any prior sibling is not yet done.
  1917. Finally, if the parent is blocked because of ordered siblings of its own,
  1918. the child will also be blocked.
  1919. This variable needs to be set before org.el is loaded, and you need to
  1920. restart Emacs after a change to make the change effective. The only way
  1921. to change is while Emacs is running is through the customize interface."
  1922. :set (lambda (var val)
  1923. (set var val)
  1924. (if val
  1925. (add-hook 'org-blocker-hook
  1926. 'org-block-todo-from-children-or-siblings-or-parent)
  1927. (remove-hook 'org-blocker-hook
  1928. 'org-block-todo-from-children-or-siblings-or-parent)))
  1929. :group 'org-todo
  1930. :type 'boolean)
  1931. (defcustom org-enforce-todo-checkbox-dependencies nil
  1932. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1933. When this is nil, checkboxes have no influence on switching TODO states.
  1934. When non-nil, you first need to check off all check boxes before the TODO
  1935. entry can be switched to DONE.
  1936. This variable needs to be set before org.el is loaded, and you need to
  1937. restart Emacs after a change to make the change effective. The only way
  1938. to change is while Emacs is running is through the customize interface."
  1939. :set (lambda (var val)
  1940. (set var val)
  1941. (if val
  1942. (add-hook 'org-blocker-hook
  1943. 'org-block-todo-from-checkboxes)
  1944. (remove-hook 'org-blocker-hook
  1945. 'org-block-todo-from-checkboxes)))
  1946. :group 'org-todo
  1947. :type 'boolean)
  1948. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1949. "Non-nil means inserting a TODO heading is treated as state change.
  1950. So when the command \\[org-insert-todo-heading] is used, state change
  1951. logging will apply if appropriate. When nil, the new TODO item will
  1952. be inserted directly, and no logging will take place."
  1953. :group 'org-todo
  1954. :type 'boolean)
  1955. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1956. "Non-nil means switching TODO states with S-cursor counts as state change.
  1957. This is the default behavior. However, setting this to nil allows a
  1958. convenient way to select a TODO state and bypass any logging associated
  1959. with that."
  1960. :group 'org-todo
  1961. :type 'boolean)
  1962. (defcustom org-todo-state-tags-triggers nil
  1963. "Tag changes that should be triggered by TODO state changes.
  1964. This is a list. Each entry is
  1965. (state-change (tag . flag) .......)
  1966. State-change can be a string with a state, and empty string to indicate the
  1967. state that has no TODO keyword, or it can be one of the symbols `todo'
  1968. or `done', meaning any not-done or done state, respectively."
  1969. :group 'org-todo
  1970. :group 'org-tags
  1971. :type '(repeat
  1972. (cons (choice :tag "When changing to"
  1973. (const :tag "Not-done state" todo)
  1974. (const :tag "Done state" done)
  1975. (string :tag "State"))
  1976. (repeat
  1977. (cons :tag "Tag action"
  1978. (string :tag "Tag")
  1979. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1980. (defcustom org-log-done nil
  1981. "Information to record when a task moves to the DONE state.
  1982. Possible values are:
  1983. nil Don't add anything, just change the keyword
  1984. time Add a time stamp to the task
  1985. note Prompt for a note and add it with template `org-log-note-headings'
  1986. This option can also be set with on a per-file-basis with
  1987. #+STARTUP: nologdone
  1988. #+STARTUP: logdone
  1989. #+STARTUP: lognotedone
  1990. You can have local logging settings for a subtree by setting the LOGGING
  1991. property to one or more of these keywords."
  1992. :group 'org-todo
  1993. :group 'org-progress
  1994. :type '(choice
  1995. (const :tag "No logging" nil)
  1996. (const :tag "Record CLOSED timestamp" time)
  1997. (const :tag "Record CLOSED timestamp with note." note)))
  1998. ;; Normalize old uses of org-log-done.
  1999. (cond
  2000. ((eq org-log-done t) (setq org-log-done 'time))
  2001. ((and (listp org-log-done) (memq 'done org-log-done))
  2002. (setq org-log-done 'note)))
  2003. (defcustom org-log-reschedule nil
  2004. "Information to record when the scheduling date of a tasks is modified.
  2005. Possible values are:
  2006. nil Don't add anything, just change the date
  2007. time Add a time stamp to the task
  2008. note Prompt for a note and add it with template `org-log-note-headings'
  2009. This option can also be set with on a per-file-basis with
  2010. #+STARTUP: nologreschedule
  2011. #+STARTUP: logreschedule
  2012. #+STARTUP: lognotereschedule"
  2013. :group 'org-todo
  2014. :group 'org-progress
  2015. :type '(choice
  2016. (const :tag "No logging" nil)
  2017. (const :tag "Record timestamp" time)
  2018. (const :tag "Record timestamp with note." note)))
  2019. (defcustom org-log-redeadline nil
  2020. "Information to record when the deadline date of a tasks is modified.
  2021. Possible values are:
  2022. nil Don't add anything, just change the date
  2023. time Add a time stamp to the task
  2024. note Prompt for a note and add it with template `org-log-note-headings'
  2025. This option can also be set with on a per-file-basis with
  2026. #+STARTUP: nologredeadline
  2027. #+STARTUP: logredeadline
  2028. #+STARTUP: lognoteredeadline
  2029. You can have local logging settings for a subtree by setting the LOGGING
  2030. property to one or more of these keywords."
  2031. :group 'org-todo
  2032. :group 'org-progress
  2033. :type '(choice
  2034. (const :tag "No logging" nil)
  2035. (const :tag "Record timestamp" time)
  2036. (const :tag "Record timestamp with note." note)))
  2037. (defcustom org-log-note-clock-out nil
  2038. "Non-nil means record a note when clocking out of an item.
  2039. This can also be configured on a per-file basis by adding one of
  2040. the following lines anywhere in the buffer:
  2041. #+STARTUP: lognoteclock-out
  2042. #+STARTUP: nolognoteclock-out"
  2043. :group 'org-todo
  2044. :group 'org-progress
  2045. :type 'boolean)
  2046. (defcustom org-log-done-with-time t
  2047. "Non-nil means the CLOSED time stamp will contain date and time.
  2048. When nil, only the date will be recorded."
  2049. :group 'org-progress
  2050. :type 'boolean)
  2051. (defcustom org-log-note-headings
  2052. '((done . "CLOSING NOTE %t")
  2053. (state . "State %-12s from %-12S %t")
  2054. (note . "Note taken on %t")
  2055. (reschedule . "Rescheduled from %S on %t")
  2056. (delschedule . "Not scheduled, was %S on %t")
  2057. (redeadline . "New deadline from %S on %t")
  2058. (deldeadline . "Removed deadline, was %S on %t")
  2059. (refile . "Refiled on %t")
  2060. (clock-out . ""))
  2061. "Headings for notes added to entries.
  2062. The value is an alist, with the car being a symbol indicating the note
  2063. context, and the cdr is the heading to be used. The heading may also be the
  2064. empty string.
  2065. %t in the heading will be replaced by a time stamp.
  2066. %T will be an active time stamp instead the default inactive one
  2067. %s will be replaced by the new TODO state, in double quotes.
  2068. %S will be replaced by the old TODO state, in double quotes.
  2069. %u will be replaced by the user name.
  2070. %U will be replaced by the full user name.
  2071. In fact, it is not a good idea to change the `state' entry, because
  2072. agenda log mode depends on the format of these entries."
  2073. :group 'org-todo
  2074. :group 'org-progress
  2075. :type '(list :greedy t
  2076. (cons (const :tag "Heading when closing an item" done) string)
  2077. (cons (const :tag
  2078. "Heading when changing todo state (todo sequence only)"
  2079. state) string)
  2080. (cons (const :tag "Heading when just taking a note" note) string)
  2081. (cons (const :tag "Heading when clocking out" clock-out) string)
  2082. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2083. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2084. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2085. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2086. (cons (const :tag "Heading when refiling" refile) string)))
  2087. (unless (assq 'note org-log-note-headings)
  2088. (push '(note . "%t") org-log-note-headings))
  2089. (defcustom org-log-into-drawer nil
  2090. "Non-nil means insert state change notes and time stamps into a drawer.
  2091. When nil, state changes notes will be inserted after the headline and
  2092. any scheduling and clock lines, but not inside a drawer.
  2093. The value of this variable should be the name of the drawer to use.
  2094. LOGBOOK is proposed as the default drawer for this purpose, you can
  2095. also set this to a string to define the drawer of your choice.
  2096. A value of t is also allowed, representing \"LOGBOOK\".
  2097. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2098. will be ignored.
  2099. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2100. a subtree."
  2101. :group 'org-todo
  2102. :group 'org-progress
  2103. :type '(choice
  2104. (const :tag "Not into a drawer" nil)
  2105. (const :tag "LOGBOOK" t)
  2106. (string :tag "Other")))
  2107. (if (fboundp 'defvaralias)
  2108. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2109. (defun org-log-into-drawer ()
  2110. "Return the value of `org-log-into-drawer', but let properties overrule.
  2111. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2112. used instead of the default value."
  2113. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2114. (cond
  2115. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2116. ((equal p "t") "LOGBOOK")
  2117. (t p))))
  2118. (defcustom org-log-state-notes-insert-after-drawers nil
  2119. "Non-nil means insert state change notes after any drawers in entry.
  2120. Only the drawers that *immediately* follow the headline and the
  2121. deadline/scheduled line are skipped.
  2122. When nil, insert notes right after the heading and perhaps the line
  2123. with deadline/scheduling if present.
  2124. This variable will have no effect if `org-log-into-drawer' is
  2125. set."
  2126. :group 'org-todo
  2127. :group 'org-progress
  2128. :type 'boolean)
  2129. (defcustom org-log-states-order-reversed t
  2130. "Non-nil means the latest state note will be directly after heading.
  2131. When nil, the state change notes will be ordered according to time."
  2132. :group 'org-todo
  2133. :group 'org-progress
  2134. :type 'boolean)
  2135. (defcustom org-todo-repeat-to-state nil
  2136. "The TODO state to which a repeater should return the repeating task.
  2137. By default this is the first task in a TODO sequence, or the previous state
  2138. in a TODO_TYP set. But you can specify another task here.
  2139. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2140. :group 'org-todo
  2141. :type '(choice (const :tag "Head of sequence" nil)
  2142. (string :tag "Specific state")))
  2143. (defcustom org-log-repeat 'time
  2144. "Non-nil means record moving through the DONE state when triggering repeat.
  2145. An auto-repeating task is immediately switched back to TODO when
  2146. marked DONE. If you are not logging state changes (by adding \"@\"
  2147. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2148. record a closing note, there will be no record of the task moving
  2149. through DONE. This variable forces taking a note anyway.
  2150. nil Don't force a record
  2151. time Record a time stamp
  2152. note Record a note
  2153. This option can also be set with on a per-file-basis with
  2154. #+STARTUP: logrepeat
  2155. #+STARTUP: lognoterepeat
  2156. #+STARTUP: nologrepeat
  2157. You can have local logging settings for a subtree by setting the LOGGING
  2158. property to one or more of these keywords."
  2159. :group 'org-todo
  2160. :group 'org-progress
  2161. :type '(choice
  2162. (const :tag "Don't force a record" nil)
  2163. (const :tag "Force recording the DONE state" time)
  2164. (const :tag "Force recording a note with the DONE state" note)))
  2165. (defgroup org-priorities nil
  2166. "Priorities in Org-mode."
  2167. :tag "Org Priorities"
  2168. :group 'org-todo)
  2169. (defcustom org-enable-priority-commands t
  2170. "Non-nil means priority commands are active.
  2171. When nil, these commands will be disabled, so that you never accidentally
  2172. set a priority."
  2173. :group 'org-priorities
  2174. :type 'boolean)
  2175. (defcustom org-highest-priority ?A
  2176. "The highest priority of TODO items. A character like ?A, ?B etc.
  2177. Must have a smaller ASCII number than `org-lowest-priority'."
  2178. :group 'org-priorities
  2179. :type 'character)
  2180. (defcustom org-lowest-priority ?C
  2181. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2182. Must have a larger ASCII number than `org-highest-priority'."
  2183. :group 'org-priorities
  2184. :type 'character)
  2185. (defcustom org-default-priority ?B
  2186. "The default priority of TODO items.
  2187. This is the priority an item gets if no explicit priority is given.
  2188. When starting to cycle on an empty priority the first step in the cycle
  2189. depends on `org-priority-start-cycle-with-default'. The resulting first
  2190. step priority must not exceed the range from `org-highest-priority' to
  2191. `org-lowest-priority' which means that `org-default-priority' has to be
  2192. in this range exclusive or inclusive the range boundaries. Else the
  2193. first step refuses to set the default and the second will fall back
  2194. to (depending on the command used) the highest or lowest priority."
  2195. :group 'org-priorities
  2196. :type 'character)
  2197. (defcustom org-priority-start-cycle-with-default t
  2198. "Non-nil means start with default priority when starting to cycle.
  2199. When this is nil, the first step in the cycle will be (depending on the
  2200. command used) one higher or lower than the default priority.
  2201. See also `org-default-priority'."
  2202. :group 'org-priorities
  2203. :type 'boolean)
  2204. (defcustom org-get-priority-function nil
  2205. "Function to extract the priority from a string.
  2206. The string is normally the headline. If this is nil Org computes the
  2207. priority from the priority cookie like [#A] in the headline. It returns
  2208. an integer, increasing by 1000 for each priority level.
  2209. The user can set a different function here, which should take a string
  2210. as an argument and return the numeric priority."
  2211. :group 'org-priorities
  2212. :type 'function)
  2213. (defgroup org-time nil
  2214. "Options concerning time stamps and deadlines in Org-mode."
  2215. :tag "Org Time"
  2216. :group 'org)
  2217. (defcustom org-insert-labeled-timestamps-at-point nil
  2218. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2219. When nil, these labeled time stamps are forces into the second line of an
  2220. entry, just after the headline. When scheduling from the global TODO list,
  2221. the time stamp will always be forced into the second line."
  2222. :group 'org-time
  2223. :type 'boolean)
  2224. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2225. "Formats for `format-time-string' which are used for time stamps.
  2226. It is not recommended to change this constant.")
  2227. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2228. "Number of minutes to round time stamps to.
  2229. These are two values, the first applies when first creating a time stamp.
  2230. The second applies when changing it with the commands `S-up' and `S-down'.
  2231. When changing the time stamp, this means that it will change in steps
  2232. of N minutes, as given by the second value.
  2233. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2234. numbers should be factors of 60, so for example 5, 10, 15.
  2235. When this is larger than 1, you can still force an exact time stamp by using
  2236. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2237. and by using a prefix arg to `S-up/down' to specify the exact number
  2238. of minutes to shift."
  2239. :group 'org-time
  2240. :get #'(lambda (var) ; Make sure both elements are there
  2241. (if (integerp (default-value var))
  2242. (list (default-value var) 5)
  2243. (default-value var)))
  2244. :type '(list
  2245. (integer :tag "when inserting times")
  2246. (integer :tag "when modifying times")))
  2247. ;; Normalize old customizations of this variable.
  2248. (when (integerp org-time-stamp-rounding-minutes)
  2249. (setq org-time-stamp-rounding-minutes
  2250. (list org-time-stamp-rounding-minutes
  2251. org-time-stamp-rounding-minutes)))
  2252. (defcustom org-display-custom-times nil
  2253. "Non-nil means overlay custom formats over all time stamps.
  2254. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2255. To turn this on on a per-file basis, insert anywhere in the file:
  2256. #+STARTUP: customtime"
  2257. :group 'org-time
  2258. :set 'set-default
  2259. :type 'sexp)
  2260. (make-variable-buffer-local 'org-display-custom-times)
  2261. (defcustom org-time-stamp-custom-formats
  2262. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2263. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2264. These are overlayed over the default ISO format if the variable
  2265. `org-display-custom-times' is set. Time like %H:%M should be at the
  2266. end of the second format. The custom formats are also honored by export
  2267. commands, if custom time display is turned on at the time of export."
  2268. :group 'org-time
  2269. :type 'sexp)
  2270. (defun org-time-stamp-format (&optional long inactive)
  2271. "Get the right format for a time string."
  2272. (let ((f (if long (cdr org-time-stamp-formats)
  2273. (car org-time-stamp-formats))))
  2274. (if inactive
  2275. (concat "[" (substring f 1 -1) "]")
  2276. f)))
  2277. (defcustom org-time-clocksum-format "%d:%02d"
  2278. "The format string used when creating CLOCKSUM lines.
  2279. This is also used when org-mode generates a time duration."
  2280. :group 'org-time
  2281. :type 'string)
  2282. (defcustom org-time-clocksum-use-fractional nil
  2283. "If non-nil, \\[org-clock-display] uses fractional times.
  2284. org-mode generates a time duration."
  2285. :group 'org-time
  2286. :type 'boolean)
  2287. (defcustom org-time-clocksum-fractional-format "%.2f"
  2288. "The format string used when creating CLOCKSUM lines, or when
  2289. org-mode generates a time duration."
  2290. :group 'org-time
  2291. :type 'string)
  2292. (defcustom org-deadline-warning-days 14
  2293. "No. of days before expiration during which a deadline becomes active.
  2294. This variable governs the display in sparse trees and in the agenda.
  2295. When 0 or negative, it means use this number (the absolute value of it)
  2296. even if a deadline has a different individual lead time specified.
  2297. Custom commands can set this variable in the options section."
  2298. :group 'org-time
  2299. :group 'org-agenda-daily/weekly
  2300. :type 'integer)
  2301. (defcustom org-read-date-prefer-future t
  2302. "Non-nil means assume future for incomplete date input from user.
  2303. This affects the following situations:
  2304. 1. The user gives a month but not a year.
  2305. For example, if it is April and you enter \"feb 2\", this will be read
  2306. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2307. 2. The user gives a day, but no month.
  2308. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2309. read this as the third of *next* month. However, if you enter \"17\",
  2310. it will be considered as *this* month.
  2311. If you set this variable to the symbol `time', then also the following
  2312. will work:
  2313. 3. If the user gives a time, but no day. If the time is before now,
  2314. to will be interpreted as tomorrow.
  2315. Currently none of this works for ISO week specifications.
  2316. When this option is nil, the current day, month and year will always be
  2317. used as defaults.
  2318. See also `org-agenda-jump-prefer-future'."
  2319. :group 'org-time
  2320. :type '(choice
  2321. (const :tag "Never" nil)
  2322. (const :tag "Check month and day" t)
  2323. (const :tag "Check month, day, and time" time)))
  2324. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2325. "Should the agenda jump command prefer the future for incomplete dates?
  2326. The default is to do the same as configured in `org-read-date-prefer-future'.
  2327. But you can also set a deviating value here.
  2328. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2329. :group 'org-agenda
  2330. :group 'org-time
  2331. :type '(choice
  2332. (const :tag "Use org-read-date-prefer-future"
  2333. org-read-date-prefer-future)
  2334. (const :tag "Never" nil)
  2335. (const :tag "Always" t)))
  2336. (defcustom org-read-date-force-compatible-dates t
  2337. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2338. Depending on the system Emacs is running on, certain dates cannot
  2339. be represented with the type used internally to represent time.
  2340. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2341. correctly. Some systems allow for earlier dates, some for later,
  2342. some for both. One way to find out it to insert any date into an
  2343. Org buffer, putting the cursor on the year and hitting S-up and
  2344. S-down to test the range.
  2345. When this variable is set to t, the date/time prompt will not let
  2346. you specify dates outside the 1970-2037 range, so it is certain that
  2347. these dates will work in whatever version of Emacs you are
  2348. running, and also that you can move a file from one Emacs implementation
  2349. to another. WHenever Org is forcing the year for you, it will display
  2350. a message and beep.
  2351. When this variable is nil, Org will check if the date is
  2352. representable in the specific Emacs implementation you are using.
  2353. If not, it will force a year, usually the current year, and beep
  2354. to remind you. Currently this setting is not recommended because
  2355. the likelihood that you will open your Org files in an Emacs that
  2356. has limited date range is not negligible.
  2357. A workaround for this problem is to use diary sexp dates for time
  2358. stamps outside of this range."
  2359. :group 'org-time
  2360. :type 'boolean)
  2361. (defcustom org-read-date-display-live t
  2362. "Non-nil means display current interpretation of date prompt live.
  2363. This display will be in an overlay, in the minibuffer."
  2364. :group 'org-time
  2365. :type 'boolean)
  2366. (defcustom org-read-date-popup-calendar t
  2367. "Non-nil means pop up a calendar when prompting for a date.
  2368. In the calendar, the date can be selected with mouse-1. However, the
  2369. minibuffer will also be active, and you can simply enter the date as well.
  2370. When nil, only the minibuffer will be available."
  2371. :group 'org-time
  2372. :type 'boolean)
  2373. (if (fboundp 'defvaralias)
  2374. (defvaralias 'org-popup-calendar-for-date-prompt
  2375. 'org-read-date-popup-calendar))
  2376. (defcustom org-read-date-minibuffer-setup-hook nil
  2377. "Hook to be used to set up keys for the date/time interface.
  2378. Add key definitions to `minibuffer-local-map', which will be a temporary
  2379. copy."
  2380. :group 'org-time
  2381. :type 'hook)
  2382. (defcustom org-extend-today-until 0
  2383. "The hour when your day really ends. Must be an integer.
  2384. This has influence for the following applications:
  2385. - When switching the agenda to \"today\". It it is still earlier than
  2386. the time given here, the day recognized as TODAY is actually yesterday.
  2387. - When a date is read from the user and it is still before the time given
  2388. here, the current date and time will be assumed to be yesterday, 23:59.
  2389. Also, timestamps inserted in remember templates follow this rule.
  2390. IMPORTANT: This is a feature whose implementation is and likely will
  2391. remain incomplete. Really, it is only here because past midnight seems to
  2392. be the favorite working time of John Wiegley :-)"
  2393. :group 'org-time
  2394. :type 'integer)
  2395. (defcustom org-use-effective-time nil
  2396. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2397. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2398. \"effective time\" of any timestamps between midnight and 8am will be
  2399. 23:59 of the previous day."
  2400. :group 'boolean
  2401. :type 'integer)
  2402. (defcustom org-edit-timestamp-down-means-later nil
  2403. "Non-nil means S-down will increase the time in a time stamp.
  2404. When nil, S-up will increase."
  2405. :group 'org-time
  2406. :type 'boolean)
  2407. (defcustom org-calendar-follow-timestamp-change t
  2408. "Non-nil means make the calendar window follow timestamp changes.
  2409. When a timestamp is modified and the calendar window is visible, it will be
  2410. moved to the new date."
  2411. :group 'org-time
  2412. :type 'boolean)
  2413. (defgroup org-tags nil
  2414. "Options concerning tags in Org-mode."
  2415. :tag "Org Tags"
  2416. :group 'org)
  2417. (defcustom org-tag-alist nil
  2418. "List of tags allowed in Org-mode files.
  2419. When this list is nil, Org-mode will base TAG input on what is already in the
  2420. buffer.
  2421. The value of this variable is an alist, the car of each entry must be a
  2422. keyword as a string, the cdr may be a character that is used to select
  2423. that tag through the fast-tag-selection interface.
  2424. See the manual for details."
  2425. :group 'org-tags
  2426. :type '(repeat
  2427. (choice
  2428. (cons (string :tag "Tag name")
  2429. (character :tag "Access char"))
  2430. (list :tag "Start radio group"
  2431. (const :startgroup)
  2432. (option (string :tag "Group description")))
  2433. (list :tag "End radio group"
  2434. (const :endgroup)
  2435. (option (string :tag "Group description")))
  2436. (const :tag "New line" (:newline)))))
  2437. (defcustom org-tag-persistent-alist nil
  2438. "List of tags that will always appear in all Org-mode files.
  2439. This is in addition to any in buffer settings or customizations
  2440. of `org-tag-alist'.
  2441. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2442. The value of this variable is an alist, the car of each entry must be a
  2443. keyword as a string, the cdr may be a character that is used to select
  2444. that tag through the fast-tag-selection interface.
  2445. See the manual for details.
  2446. To disable these tags on a per-file basis, insert anywhere in the file:
  2447. #+STARTUP: noptag"
  2448. :group 'org-tags
  2449. :type '(repeat
  2450. (choice
  2451. (cons (string :tag "Tag name")
  2452. (character :tag "Access char"))
  2453. (const :tag "Start radio group" (:startgroup))
  2454. (const :tag "End radio group" (:endgroup))
  2455. (const :tag "New line" (:newline)))))
  2456. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2457. "If non-nil, always offer completion for all tags of all agenda files.
  2458. Instead of customizing this variable directly, you might want to
  2459. set it locally for capture buffers, because there no list of
  2460. tags in that file can be created dynamically (there are none).
  2461. (add-hook 'org-capture-mode-hook
  2462. (lambda ()
  2463. (set (make-local-variable
  2464. 'org-complete-tags-always-offer-all-agenda-tags)
  2465. t)))"
  2466. :group 'org-tags
  2467. :type 'boolean)
  2468. (defvar org-file-tags nil
  2469. "List of tags that can be inherited by all entries in the file.
  2470. The tags will be inherited if the variable `org-use-tag-inheritance'
  2471. says they should be.
  2472. This variable is populated from #+FILETAGS lines.")
  2473. (defcustom org-use-fast-tag-selection 'auto
  2474. "Non-nil means use fast tag selection scheme.
  2475. This is a special interface to select and deselect tags with single keys.
  2476. When nil, fast selection is never used.
  2477. When the symbol `auto', fast selection is used if and only if selection
  2478. characters for tags have been configured, either through the variable
  2479. `org-tag-alist' or through a #+TAGS line in the buffer.
  2480. When t, fast selection is always used and selection keys are assigned
  2481. automatically if necessary."
  2482. :group 'org-tags
  2483. :type '(choice
  2484. (const :tag "Always" t)
  2485. (const :tag "Never" nil)
  2486. (const :tag "When selection characters are configured" 'auto)))
  2487. (defcustom org-fast-tag-selection-single-key nil
  2488. "Non-nil means fast tag selection exits after first change.
  2489. When nil, you have to press RET to exit it.
  2490. During fast tag selection, you can toggle this flag with `C-c'.
  2491. This variable can also have the value `expert'. In this case, the window
  2492. displaying the tags menu is not even shown, until you press C-c again."
  2493. :group 'org-tags
  2494. :type '(choice
  2495. (const :tag "No" nil)
  2496. (const :tag "Yes" t)
  2497. (const :tag "Expert" expert)))
  2498. (defvar org-fast-tag-selection-include-todo nil
  2499. "Non-nil means fast tags selection interface will also offer TODO states.
  2500. This is an undocumented feature, you should not rely on it.")
  2501. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2502. "The column to which tags should be indented in a headline.
  2503. If this number is positive, it specifies the column. If it is negative,
  2504. it means that the tags should be flushright to that column. For example,
  2505. -80 works well for a normal 80 character screen."
  2506. :group 'org-tags
  2507. :type 'integer)
  2508. (defcustom org-auto-align-tags t
  2509. "Non-nil keeps tags aligned when modifying headlines.
  2510. Some operations (i.e. demoting) change the length of a headline and
  2511. therefore shift the tags around. With this option turned on, after
  2512. each such operation the tags are again aligned to `org-tags-column'."
  2513. :group 'org-tags
  2514. :type 'boolean)
  2515. (defcustom org-use-tag-inheritance t
  2516. "Non-nil means tags in levels apply also for sublevels.
  2517. When nil, only the tags directly given in a specific line apply there.
  2518. This may also be a list of tags that should be inherited, or a regexp that
  2519. matches tags that should be inherited. Additional control is possible
  2520. with the variable `org-tags-exclude-from-inheritance' which gives an
  2521. explicit list of tags to be excluded from inheritance., even if the value of
  2522. `org-use-tag-inheritance' would select it for inheritance.
  2523. If this option is t, a match early-on in a tree can lead to a large
  2524. number of matches in the subtree when constructing the agenda or creating
  2525. a sparse tree. If you only want to see the first match in a tree during
  2526. a search, check out the variable `org-tags-match-list-sublevels'."
  2527. :group 'org-tags
  2528. :type '(choice
  2529. (const :tag "Not" nil)
  2530. (const :tag "Always" t)
  2531. (repeat :tag "Specific tags" (string :tag "Tag"))
  2532. (regexp :tag "Tags matched by regexp")))
  2533. (defcustom org-tags-exclude-from-inheritance nil
  2534. "List of tags that should never be inherited.
  2535. This is a way to exclude a few tags from inheritance. For way to do
  2536. the opposite, to actively allow inheritance for selected tags,
  2537. see the variable `org-use-tag-inheritance'."
  2538. :group 'org-tags
  2539. :type '(repeat (string :tag "Tag")))
  2540. (defun org-tag-inherit-p (tag)
  2541. "Check if TAG is one that should be inherited."
  2542. (cond
  2543. ((member tag org-tags-exclude-from-inheritance) nil)
  2544. ((eq org-use-tag-inheritance t) t)
  2545. ((not org-use-tag-inheritance) nil)
  2546. ((stringp org-use-tag-inheritance)
  2547. (string-match org-use-tag-inheritance tag))
  2548. ((listp org-use-tag-inheritance)
  2549. (member tag org-use-tag-inheritance))
  2550. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2551. (defcustom org-tags-match-list-sublevels t
  2552. "Non-nil means list also sublevels of headlines matching a search.
  2553. This variable applies to tags/property searches, and also to stuck
  2554. projects because this search is based on a tags match as well.
  2555. When set to the symbol `indented', sublevels are indented with
  2556. leading dots.
  2557. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2558. the sublevels of a headline matching a tag search often also match
  2559. the same search. Listing all of them can create very long lists.
  2560. Setting this variable to nil causes subtrees of a match to be skipped.
  2561. This variable is semi-obsolete and probably should always be true. It
  2562. is better to limit inheritance to certain tags using the variables
  2563. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2564. :group 'org-tags
  2565. :type '(choice
  2566. (const :tag "No, don't list them" nil)
  2567. (const :tag "Yes, do list them" t)
  2568. (const :tag "List them, indented with leading dots" indented)))
  2569. (defcustom org-tags-sort-function nil
  2570. "When set, tags are sorted using this function as a comparator."
  2571. :group 'org-tags
  2572. :type '(choice
  2573. (const :tag "No sorting" nil)
  2574. (const :tag "Alphabetical" string<)
  2575. (const :tag "Reverse alphabetical" string>)
  2576. (function :tag "Custom function" nil)))
  2577. (defvar org-tags-history nil
  2578. "History of minibuffer reads for tags.")
  2579. (defvar org-last-tags-completion-table nil
  2580. "The last used completion table for tags.")
  2581. (defvar org-after-tags-change-hook nil
  2582. "Hook that is run after the tags in a line have changed.")
  2583. (defgroup org-properties nil
  2584. "Options concerning properties in Org-mode."
  2585. :tag "Org Properties"
  2586. :group 'org)
  2587. (defcustom org-property-format "%-10s %s"
  2588. "How property key/value pairs should be formatted by `indent-line'.
  2589. When `indent-line' hits a property definition, it will format the line
  2590. according to this format, mainly to make sure that the values are
  2591. lined-up with respect to each other."
  2592. :group 'org-properties
  2593. :type 'string)
  2594. (defcustom org-properties-postprocess-alist nil
  2595. "Alist of properties and functions to adjust inserted values.
  2596. Elements of this alist must be of the form
  2597. ([string] [function])
  2598. where [string] must be a property name and [function] must be a
  2599. lambda expression: this lambda expression must take one argument,
  2600. the value to adjust, and return the new value as a string.
  2601. For example, this element will allow the property \"Remaining\"
  2602. to be updated wrt the relation between the \"Effort\" property
  2603. and the clock summary:
  2604. ((\"Remaining\" (lambda(value)
  2605. (let ((clocksum (org-clock-sum-current-item))
  2606. (effort (org-duration-string-to-minutes
  2607. (org-entry-get (point) \"Effort\"))))
  2608. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2609. :group 'org-properties
  2610. :type 'alist)
  2611. (defcustom org-use-property-inheritance nil
  2612. "Non-nil means properties apply also for sublevels.
  2613. This setting is chiefly used during property searches. Turning it on can
  2614. cause significant overhead when doing a search, which is why it is not
  2615. on by default.
  2616. When nil, only the properties directly given in the current entry count.
  2617. When t, every property is inherited. The value may also be a list of
  2618. properties that should have inheritance, or a regular expression matching
  2619. properties that should be inherited.
  2620. However, note that some special properties use inheritance under special
  2621. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2622. and the properties ending in \"_ALL\" when they are used as descriptor
  2623. for valid values of a property.
  2624. Note for programmers:
  2625. When querying an entry with `org-entry-get', you can control if inheritance
  2626. should be used. By default, `org-entry-get' looks only at the local
  2627. properties. You can request inheritance by setting the inherit argument
  2628. to t (to force inheritance) or to `selective' (to respect the setting
  2629. in this variable)."
  2630. :group 'org-properties
  2631. :type '(choice
  2632. (const :tag "Not" nil)
  2633. (const :tag "Always" t)
  2634. (repeat :tag "Specific properties" (string :tag "Property"))
  2635. (regexp :tag "Properties matched by regexp")))
  2636. (defun org-property-inherit-p (property)
  2637. "Check if PROPERTY is one that should be inherited."
  2638. (cond
  2639. ((eq org-use-property-inheritance t) t)
  2640. ((not org-use-property-inheritance) nil)
  2641. ((stringp org-use-property-inheritance)
  2642. (string-match org-use-property-inheritance property))
  2643. ((listp org-use-property-inheritance)
  2644. (member property org-use-property-inheritance))
  2645. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2646. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2647. "The default column format, if no other format has been defined.
  2648. This variable can be set on the per-file basis by inserting a line
  2649. #+COLUMNS: %25ITEM ....."
  2650. :group 'org-properties
  2651. :type 'string)
  2652. (defcustom org-columns-ellipses ".."
  2653. "The ellipses to be used when a field in column view is truncated.
  2654. When this is the empty string, as many characters as possible are shown,
  2655. but then there will be no visual indication that the field has been truncated.
  2656. When this is a string of length N, the last N characters of a truncated
  2657. field are replaced by this string. If the column is narrower than the
  2658. ellipses string, only part of the ellipses string will be shown."
  2659. :group 'org-properties
  2660. :type 'string)
  2661. (defcustom org-columns-modify-value-for-display-function nil
  2662. "Function that modifies values for display in column view.
  2663. For example, it can be used to cut out a certain part from a time stamp.
  2664. The function must take 2 arguments:
  2665. column-title The title of the column (*not* the property name)
  2666. value The value that should be modified.
  2667. The function should return the value that should be displayed,
  2668. or nil if the normal value should be used."
  2669. :group 'org-properties
  2670. :type 'function)
  2671. (defcustom org-effort-property "Effort"
  2672. "The property that is being used to keep track of effort estimates.
  2673. Effort estimates given in this property need to have the format H:MM."
  2674. :group 'org-properties
  2675. :group 'org-progress
  2676. :type '(string :tag "Property"))
  2677. (defconst org-global-properties-fixed
  2678. '(("VISIBILITY_ALL" . "folded children content all")
  2679. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2680. "List of property/value pairs that can be inherited by any entry.
  2681. These are fixed values, for the preset properties. The user variable
  2682. that can be used to add to this list is `org-global-properties'.
  2683. The entries in this list are cons cells where the car is a property
  2684. name and cdr is a string with the value. If the value represents
  2685. multiple items like an \"_ALL\" property, separate the items by
  2686. spaces.")
  2687. (defcustom org-global-properties nil
  2688. "List of property/value pairs that can be inherited by any entry.
  2689. This list will be combined with the constant `org-global-properties-fixed'.
  2690. The entries in this list are cons cells where the car is a property
  2691. name and cdr is a string with the value.
  2692. You can set buffer-local values for the same purpose in the variable
  2693. `org-file-properties' this by adding lines like
  2694. #+PROPERTY: NAME VALUE"
  2695. :group 'org-properties
  2696. :type '(repeat
  2697. (cons (string :tag "Property")
  2698. (string :tag "Value"))))
  2699. (defvar org-file-properties nil
  2700. "List of property/value pairs that can be inherited by any entry.
  2701. Valid for the current buffer.
  2702. This variable is populated from #+PROPERTY lines.")
  2703. (make-variable-buffer-local 'org-file-properties)
  2704. (defgroup org-agenda nil
  2705. "Options concerning agenda views in Org-mode."
  2706. :tag "Org Agenda"
  2707. :group 'org)
  2708. (defvar org-category nil
  2709. "Variable used by org files to set a category for agenda display.
  2710. Such files should use a file variable to set it, for example
  2711. # -*- mode: org; org-category: \"ELisp\"
  2712. or contain a special line
  2713. #+CATEGORY: ELisp
  2714. If the file does not specify a category, then file's base name
  2715. is used instead.")
  2716. (make-variable-buffer-local 'org-category)
  2717. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2718. (defcustom org-agenda-files nil
  2719. "The files to be used for agenda display.
  2720. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2721. \\[org-remove-file]. You can also use customize to edit the list.
  2722. If an entry is a directory, all files in that directory that are matched by
  2723. `org-agenda-file-regexp' will be part of the file list.
  2724. If the value of the variable is not a list but a single file name, then
  2725. the list of agenda files is actually stored and maintained in that file, one
  2726. agenda file per line. In this file paths can be given relative to
  2727. `org-directory'. Tilde expansion and environment variable substitution
  2728. are also made."
  2729. :group 'org-agenda
  2730. :type '(choice
  2731. (repeat :tag "List of files and directories" file)
  2732. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2733. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2734. "Regular expression to match files for `org-agenda-files'.
  2735. If any element in the list in that variable contains a directory instead
  2736. of a normal file, all files in that directory that are matched by this
  2737. regular expression will be included."
  2738. :group 'org-agenda
  2739. :type 'regexp)
  2740. (defcustom org-agenda-text-search-extra-files nil
  2741. "List of extra files to be searched by text search commands.
  2742. These files will be search in addition to the agenda files by the
  2743. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2744. Note that these files will only be searched for text search commands,
  2745. not for the other agenda views like todo lists, tag searches or the weekly
  2746. agenda. This variable is intended to list notes and possibly archive files
  2747. that should also be searched by these two commands.
  2748. In fact, if the first element in the list is the symbol `agenda-archives',
  2749. than all archive files of all agenda files will be added to the search
  2750. scope."
  2751. :group 'org-agenda
  2752. :type '(set :greedy t
  2753. (const :tag "Agenda Archives" agenda-archives)
  2754. (repeat :inline t (file))))
  2755. (if (fboundp 'defvaralias)
  2756. (defvaralias 'org-agenda-multi-occur-extra-files
  2757. 'org-agenda-text-search-extra-files))
  2758. (defcustom org-agenda-skip-unavailable-files nil
  2759. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2760. A nil value means to remove them, after a query, from the list."
  2761. :group 'org-agenda
  2762. :type 'boolean)
  2763. (defcustom org-calendar-to-agenda-key [?c]
  2764. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2765. The command `org-calendar-goto-agenda' will be bound to this key. The
  2766. default is the character `c' because then `c' can be used to switch back and
  2767. forth between agenda and calendar."
  2768. :group 'org-agenda
  2769. :type 'sexp)
  2770. (defcustom org-calendar-agenda-action-key [?k]
  2771. "The key to be installed in `calendar-mode-map' for agenda-action.
  2772. The command `org-agenda-action' will be bound to this key. The
  2773. default is the character `k' because we use the same key in the agenda."
  2774. :group 'org-agenda
  2775. :type 'sexp)
  2776. (defcustom org-calendar-insert-diary-entry-key [?i]
  2777. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2778. This option is irrelevant until `org-agenda-diary-file' has been configured
  2779. to point to an Org-mode file. When that is the case, the command
  2780. `org-agenda-diary-entry' will be bound to the key given here, by default
  2781. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2782. if you want to continue doing this, you need to change this to a different
  2783. key."
  2784. :group 'org-agenda
  2785. :type 'sexp)
  2786. (defcustom org-agenda-diary-file 'diary-file
  2787. "File to which to add new entries with the `i' key in agenda and calendar.
  2788. When this is the symbol `diary-file', the functionality in the Emacs
  2789. calendar will be used to add entries to the `diary-file'. But when this
  2790. points to a file, `org-agenda-diary-entry' will be used instead."
  2791. :group 'org-agenda
  2792. :type '(choice
  2793. (const :tag "The standard Emacs diary file" diary-file)
  2794. (file :tag "Special Org file diary entries")))
  2795. (eval-after-load "calendar"
  2796. '(progn
  2797. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2798. 'org-calendar-goto-agenda)
  2799. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2800. 'org-agenda-action)
  2801. (add-hook 'calendar-mode-hook
  2802. (lambda ()
  2803. (unless (eq org-agenda-diary-file 'diary-file)
  2804. (define-key calendar-mode-map
  2805. org-calendar-insert-diary-entry-key
  2806. 'org-agenda-diary-entry))))))
  2807. (defgroup org-latex nil
  2808. "Options for embedding LaTeX code into Org-mode."
  2809. :tag "Org LaTeX"
  2810. :group 'org)
  2811. (defcustom org-format-latex-options
  2812. '(:foreground default :background default :scale 1.0
  2813. :html-foreground "Black" :html-background "Transparent"
  2814. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2815. "Options for creating images from LaTeX fragments.
  2816. This is a property list with the following properties:
  2817. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2818. `default' means use the foreground of the default face.
  2819. :background the background color, or \"Transparent\".
  2820. `default' means use the background of the default face.
  2821. :scale a scaling factor for the size of the images, to get more pixels
  2822. :html-foreground, :html-background, :html-scale
  2823. the same numbers for HTML export.
  2824. :matchers a list indicating which matchers should be used to
  2825. find LaTeX fragments. Valid members of this list are:
  2826. \"begin\" find environments
  2827. \"$1\" find single characters surrounded by $.$
  2828. \"$\" find math expressions surrounded by $...$
  2829. \"$$\" find math expressions surrounded by $$....$$
  2830. \"\\(\" find math expressions surrounded by \\(...\\)
  2831. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2832. :group 'org-latex
  2833. :type 'plist)
  2834. (defcustom org-format-latex-signal-error t
  2835. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2836. When nil, just push out a message."
  2837. :group 'org-latex
  2838. :type 'boolean)
  2839. (defcustom org-format-latex-header "\\documentclass{article}
  2840. \\usepackage[usenames]{color}
  2841. \\usepackage{amsmath}
  2842. \\usepackage[mathscr]{eucal}
  2843. \\pagestyle{empty} % do not remove
  2844. \[PACKAGES]
  2845. \[DEFAULT-PACKAGES]
  2846. % The settings below are copied from fullpage.sty
  2847. \\setlength{\\textwidth}{\\paperwidth}
  2848. \\addtolength{\\textwidth}{-3cm}
  2849. \\setlength{\\oddsidemargin}{1.5cm}
  2850. \\addtolength{\\oddsidemargin}{-2.54cm}
  2851. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2852. \\setlength{\\textheight}{\\paperheight}
  2853. \\addtolength{\\textheight}{-\\headheight}
  2854. \\addtolength{\\textheight}{-\\headsep}
  2855. \\addtolength{\\textheight}{-\\footskip}
  2856. \\addtolength{\\textheight}{-3cm}
  2857. \\setlength{\\topmargin}{1.5cm}
  2858. \\addtolength{\\topmargin}{-2.54cm}"
  2859. "The document header used for processing LaTeX fragments.
  2860. It is imperative that this header make sure that no page number
  2861. appears on the page. The package defined in the variables
  2862. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2863. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2864. will be appended."
  2865. :group 'org-latex
  2866. :type 'string)
  2867. (defvar org-format-latex-header-extra nil)
  2868. (defun org-set-packages-alist (var val)
  2869. "Set the packages alist and make sure it has 3 elements per entry."
  2870. (set var (mapcar (lambda (x)
  2871. (if (and (consp x) (= (length x) 2))
  2872. (list (car x) (nth 1 x) t)
  2873. x))
  2874. val)))
  2875. (defun org-get-packages-alist (var)
  2876. "Get the packages alist and make sure it has 3 elements per entry."
  2877. (mapcar (lambda (x)
  2878. (if (and (consp x) (= (length x) 2))
  2879. (list (car x) (nth 1 x) t)
  2880. x))
  2881. (default-value var)))
  2882. ;; The following variables are defined here because is it also used
  2883. ;; when formatting latex fragments. Originally it was part of the
  2884. ;; LaTeX exporter, which is why the name includes "export".
  2885. (defcustom org-export-latex-default-packages-alist
  2886. '(("AUTO" "inputenc" t)
  2887. ("T1" "fontenc" t)
  2888. ("" "fixltx2e" nil)
  2889. ("" "graphicx" t)
  2890. ("" "longtable" nil)
  2891. ("" "float" nil)
  2892. ("" "wrapfig" nil)
  2893. ("" "soul" t)
  2894. ("" "textcomp" t)
  2895. ("" "marvosym" t)
  2896. ("" "wasysym" t)
  2897. ("" "latexsym" t)
  2898. ("" "amssymb" t)
  2899. ("" "hyperref" nil)
  2900. "\\tolerance=1000"
  2901. )
  2902. "Alist of default packages to be inserted in the header.
  2903. Change this only if one of the packages here causes an incompatibility
  2904. with another package you are using.
  2905. The packages in this list are needed by one part or another of Org-mode
  2906. to function properly.
  2907. - inputenc, fontenc: for basic font and character selection
  2908. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2909. for interpreting the entities in `org-entities'. You can skip some of these
  2910. packages if you don't use any of the symbols in it.
  2911. - graphicx: for including images
  2912. - float, wrapfig: for figure placement
  2913. - longtable: for long tables
  2914. - hyperref: for cross references
  2915. Therefore you should not modify this variable unless you know what you
  2916. are doing. The one reason to change it anyway is that you might be loading
  2917. some other package that conflicts with one of the default packages.
  2918. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2919. If SNIPPET-FLAG is t, the package also needs to be included when
  2920. compiling LaTeX snippets into images for inclusion into HTML."
  2921. :group 'org-export-latex
  2922. :set 'org-set-packages-alist
  2923. :get 'org-get-packages-alist
  2924. :type '(repeat
  2925. (choice
  2926. (list :tag "options/package pair"
  2927. (string :tag "options")
  2928. (string :tag "package")
  2929. (boolean :tag "Snippet"))
  2930. (string :tag "A line of LaTeX"))))
  2931. (defcustom org-export-latex-packages-alist nil
  2932. "Alist of packages to be inserted in every LaTeX header.
  2933. These will be inserted after `org-export-latex-default-packages-alist'.
  2934. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2935. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2936. turning LaTeX snippets into images for inclusion into HTML.
  2937. Make sure that you only list packages here which:
  2938. - you want in every file
  2939. - do not conflict with the default packages in
  2940. `org-export-latex-default-packages-alist'
  2941. - do not conflict with the setup in `org-format-latex-header'."
  2942. :group 'org-export-latex
  2943. :set 'org-set-packages-alist
  2944. :get 'org-get-packages-alist
  2945. :type '(repeat
  2946. (choice
  2947. (list :tag "options/package pair"
  2948. (string :tag "options")
  2949. (string :tag "package")
  2950. (boolean :tag "Snippet"))
  2951. (string :tag "A line of LaTeX"))))
  2952. (defgroup org-appearance nil
  2953. "Settings for Org-mode appearance."
  2954. :tag "Org Appearance"
  2955. :group 'org)
  2956. (defcustom org-level-color-stars-only nil
  2957. "Non-nil means fontify only the stars in each headline.
  2958. When nil, the entire headline is fontified.
  2959. Changing it requires restart of `font-lock-mode' to become effective
  2960. also in regions already fontified."
  2961. :group 'org-appearance
  2962. :type 'boolean)
  2963. (defcustom org-hide-leading-stars nil
  2964. "Non-nil means hide the first N-1 stars in a headline.
  2965. This works by using the face `org-hide' for these stars. This
  2966. face is white for a light background, and black for a dark
  2967. background. You may have to customize the face `org-hide' to
  2968. make this work.
  2969. Changing it requires restart of `font-lock-mode' to become effective
  2970. also in regions already fontified.
  2971. You may also set this on a per-file basis by adding one of the following
  2972. lines to the buffer:
  2973. #+STARTUP: hidestars
  2974. #+STARTUP: showstars"
  2975. :group 'org-appearance
  2976. :type 'boolean)
  2977. (defcustom org-hidden-keywords nil
  2978. "List of symbols corresponding to keywords to be hidden the org buffer.
  2979. For example, a value '(title) for this list will make the document's title
  2980. appear in the buffer without the initial #+TITLE: keyword."
  2981. :group 'org-appearance
  2982. :type '(set (const :tag "#+AUTHOR" author)
  2983. (const :tag "#+DATE" date)
  2984. (const :tag "#+EMAIL" email)
  2985. (const :tag "#+TITLE" title)))
  2986. (defcustom org-fontify-done-headline nil
  2987. "Non-nil means change the face of a headline if it is marked DONE.
  2988. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2989. When this is non-nil, the headline after the keyword is set to the
  2990. `org-headline-done' as an additional indication."
  2991. :group 'org-appearance
  2992. :type 'boolean)
  2993. (defcustom org-fontify-emphasized-text t
  2994. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2995. Changing this variable requires a restart of Emacs to take effect."
  2996. :group 'org-appearance
  2997. :type 'boolean)
  2998. (defcustom org-fontify-whole-heading-line nil
  2999. "Non-nil means fontify the whole line for headings.
  3000. This is useful when setting a background color for the
  3001. org-level-* faces."
  3002. :group 'org-appearance
  3003. :type 'boolean)
  3004. (defcustom org-highlight-latex-fragments-and-specials nil
  3005. "Non-nil means fontify what is treated specially by the exporters."
  3006. :group 'org-appearance
  3007. :type 'boolean)
  3008. (defcustom org-hide-emphasis-markers nil
  3009. "Non-nil mean font-lock should hide the emphasis marker characters."
  3010. :group 'org-appearance
  3011. :type 'boolean)
  3012. (defcustom org-pretty-entities nil
  3013. "Non-nil means show entities as UTF8 characters.
  3014. When nil, the \\name form remains in the buffer."
  3015. :group 'org-appearance
  3016. :type 'boolean)
  3017. (defcustom org-pretty-entities-include-sub-superscripts t
  3018. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3019. :group 'org-appearance
  3020. :type 'boolean)
  3021. (defvar org-emph-re nil
  3022. "Regular expression for matching emphasis.
  3023. After a match, the match groups contain these elements:
  3024. 0 The match of the full regular expression, including the characters
  3025. before and after the proper match
  3026. 1 The character before the proper match, or empty at beginning of line
  3027. 2 The proper match, including the leading and trailing markers
  3028. 3 The leading marker like * or /, indicating the type of highlighting
  3029. 4 The text between the emphasis markers, not including the markers
  3030. 5 The character after the match, empty at the end of a line")
  3031. (defvar org-verbatim-re nil
  3032. "Regular expression for matching verbatim text.")
  3033. (defvar org-emphasis-regexp-components) ; defined just below
  3034. (defvar org-emphasis-alist) ; defined just below
  3035. (defun org-set-emph-re (var val)
  3036. "Set variable and compute the emphasis regular expression."
  3037. (set var val)
  3038. (when (and (boundp 'org-emphasis-alist)
  3039. (boundp 'org-emphasis-regexp-components)
  3040. org-emphasis-alist org-emphasis-regexp-components)
  3041. (let* ((e org-emphasis-regexp-components)
  3042. (pre (car e))
  3043. (post (nth 1 e))
  3044. (border (nth 2 e))
  3045. (body (nth 3 e))
  3046. (nl (nth 4 e))
  3047. (body1 (concat body "*?"))
  3048. (markers (mapconcat 'car org-emphasis-alist ""))
  3049. (vmarkers (mapconcat
  3050. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3051. org-emphasis-alist "")))
  3052. ;; make sure special characters appear at the right position in the class
  3053. (if (string-match "\\^" markers)
  3054. (setq markers (concat (replace-match "" t t markers) "^")))
  3055. (if (string-match "-" markers)
  3056. (setq markers (concat (replace-match "" t t markers) "-")))
  3057. (if (string-match "\\^" vmarkers)
  3058. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3059. (if (string-match "-" vmarkers)
  3060. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3061. (if (> nl 0)
  3062. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3063. (int-to-string nl) "\\}")))
  3064. ;; Make the regexp
  3065. (setq org-emph-re
  3066. (concat "\\([" pre "]\\|^\\)"
  3067. "\\("
  3068. "\\([" markers "]\\)"
  3069. "\\("
  3070. "[^" border "]\\|"
  3071. "[^" border "]"
  3072. body1
  3073. "[^" border "]"
  3074. "\\)"
  3075. "\\3\\)"
  3076. "\\([" post "]\\|$\\)"))
  3077. (setq org-verbatim-re
  3078. (concat "\\([" pre "]\\|^\\)"
  3079. "\\("
  3080. "\\([" vmarkers "]\\)"
  3081. "\\("
  3082. "[^" border "]\\|"
  3083. "[^" border "]"
  3084. body1
  3085. "[^" border "]"
  3086. "\\)"
  3087. "\\3\\)"
  3088. "\\([" post "]\\|$\\)")))))
  3089. (defcustom org-emphasis-regexp-components
  3090. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3091. "Components used to build the regular expression for emphasis.
  3092. This is a list with five entries. Terminology: In an emphasis string
  3093. like \" *strong word* \", we call the initial space PREMATCH, the final
  3094. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3095. and \"trong wor\" is the body. The different components in this variable
  3096. specify what is allowed/forbidden in each part:
  3097. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3098. post Chars allowed as postmatch. End of line will be allowed too.
  3099. border The chars *forbidden* as border characters.
  3100. body-regexp A regexp like \".\" to match a body character. Don't use
  3101. non-shy groups here, and don't allow newline here.
  3102. newline The maximum number of newlines allowed in an emphasis exp.
  3103. Use customize to modify this, or restart Emacs after changing it."
  3104. :group 'org-appearance
  3105. :set 'org-set-emph-re
  3106. :type '(list
  3107. (sexp :tag "Allowed chars in pre ")
  3108. (sexp :tag "Allowed chars in post ")
  3109. (sexp :tag "Forbidden chars in border ")
  3110. (sexp :tag "Regexp for body ")
  3111. (integer :tag "number of newlines allowed")
  3112. (option (boolean :tag "Please ignore this button"))))
  3113. (defcustom org-emphasis-alist
  3114. `(("*" bold "<b>" "</b>")
  3115. ("/" italic "<i>" "</i>")
  3116. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3117. ("=" org-code "<code>" "</code>" verbatim)
  3118. ("~" org-verbatim "<code>" "</code>" verbatim)
  3119. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3120. "<del>" "</del>")
  3121. )
  3122. "Special syntax for emphasized text.
  3123. Text starting and ending with a special character will be emphasized, for
  3124. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3125. characters, the face to be used by font-lock for highlighting in Org-mode
  3126. Emacs buffers, and the HTML tags to be used for this.
  3127. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3128. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3129. Use customize to modify this, or restart Emacs after changing it."
  3130. :group 'org-appearance
  3131. :set 'org-set-emph-re
  3132. :type '(repeat
  3133. (list
  3134. (string :tag "Marker character")
  3135. (choice
  3136. (face :tag "Font-lock-face")
  3137. (plist :tag "Face property list"))
  3138. (string :tag "HTML start tag")
  3139. (string :tag "HTML end tag")
  3140. (option (const verbatim)))))
  3141. (defvar org-protecting-blocks
  3142. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3143. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3144. This is needed for font-lock setup.")
  3145. ;;; Miscellaneous options
  3146. (defgroup org-completion nil
  3147. "Completion in Org-mode."
  3148. :tag "Org Completion"
  3149. :group 'org)
  3150. (defcustom org-completion-use-ido nil
  3151. "Non-nil means use ido completion wherever possible.
  3152. Note that `ido-mode' must be active for this variable to be relevant.
  3153. If you decide to turn this variable on, you might well want to turn off
  3154. `org-outline-path-complete-in-steps'.
  3155. See also `org-completion-use-iswitchb'."
  3156. :group 'org-completion
  3157. :type 'boolean)
  3158. (defcustom org-completion-use-iswitchb nil
  3159. "Non-nil means use iswitchb completion wherever possible.
  3160. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3161. If you decide to turn this variable on, you might well want to turn off
  3162. `org-outline-path-complete-in-steps'.
  3163. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3164. :group 'org-completion
  3165. :type 'boolean)
  3166. (defcustom org-completion-fallback-command 'hippie-expand
  3167. "The expansion command called by \\[pcomplete] in normal context.
  3168. Normal means, no org-mode-specific context."
  3169. :group 'org-completion
  3170. :type 'function)
  3171. ;;; Functions and variables from their packages
  3172. ;; Declared here to avoid compiler warnings
  3173. ;; XEmacs only
  3174. (defvar outline-mode-menu-heading)
  3175. (defvar outline-mode-menu-show)
  3176. (defvar outline-mode-menu-hide)
  3177. (defvar zmacs-regions) ; XEmacs regions
  3178. ;; Emacs only
  3179. (defvar mark-active)
  3180. ;; Various packages
  3181. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3182. (declare-function calendar-forward-day "cal-move" (arg))
  3183. (declare-function calendar-goto-date "cal-move" (date))
  3184. (declare-function calendar-goto-today "cal-move" ())
  3185. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3186. (defvar calc-embedded-close-formula)
  3187. (defvar calc-embedded-open-formula)
  3188. (declare-function cdlatex-tab "ext:cdlatex" ())
  3189. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3190. (defvar font-lock-unfontify-region-function)
  3191. (declare-function iswitchb-read-buffer "iswitchb"
  3192. (prompt &optional default require-match start matches-set))
  3193. (defvar iswitchb-temp-buflist)
  3194. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3195. (defvar org-agenda-tags-todo-honor-ignore-options)
  3196. (declare-function org-agenda-skip "org-agenda" ())
  3197. (declare-function
  3198. org-format-agenda-item "org-agenda"
  3199. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3200. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3201. (declare-function org-agenda-change-all-lines "org-agenda"
  3202. (newhead hdmarker &optional fixface just-this))
  3203. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3204. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3205. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3206. (beg end))
  3207. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3208. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3209. "org-agenda" (&optional end))
  3210. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3211. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3212. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3213. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3214. (declare-function org-indent-mode "org-indent" (&optional arg))
  3215. (declare-function parse-time-string "parse-time" (string))
  3216. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3217. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3218. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3219. (defvar remember-data-file)
  3220. (defvar texmathp-why)
  3221. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3222. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3223. (defvar w3m-current-url)
  3224. (defvar w3m-current-title)
  3225. (defvar org-latex-regexps)
  3226. ;;; Autoload and prepare some org modules
  3227. ;; Some table stuff that needs to be defined here, because it is used
  3228. ;; by the functions setting up org-mode or checking for table context.
  3229. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3230. "Detect an org-type or table-type table.")
  3231. (defconst org-table-line-regexp "^[ \t]*|"
  3232. "Detect an org-type table line.")
  3233. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3234. "Detect an org-type table line.")
  3235. (defconst org-table-hline-regexp "^[ \t]*|-"
  3236. "Detect an org-type table hline.")
  3237. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3238. "Detect a table-type table hline.")
  3239. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3240. "Detect the first line outside a table when searching from within it.
  3241. This works for both table types.")
  3242. ;; Autoload the functions in org-table.el that are needed by functions here.
  3243. (eval-and-compile
  3244. (org-autoload "org-table"
  3245. '(org-table-align org-table-begin org-table-blank-field
  3246. org-table-convert org-table-convert-region org-table-copy-down
  3247. org-table-copy-region org-table-create
  3248. org-table-create-or-convert-from-region
  3249. org-table-create-with-table.el org-table-current-dline
  3250. org-table-cut-region org-table-delete-column org-table-edit-field
  3251. org-table-edit-formulas org-table-end org-table-eval-formula
  3252. org-table-export org-table-field-info
  3253. org-table-get-stored-formulas org-table-goto-column
  3254. org-table-hline-and-move org-table-import org-table-insert-column
  3255. org-table-insert-hline org-table-insert-row org-table-iterate
  3256. org-table-justify-field-maybe org-table-kill-row
  3257. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3258. org-table-move-column org-table-move-column-left
  3259. org-table-move-column-right org-table-move-row
  3260. org-table-move-row-down org-table-move-row-up
  3261. org-table-next-field org-table-next-row org-table-paste-rectangle
  3262. org-table-previous-field org-table-recalculate
  3263. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3264. org-table-toggle-coordinate-overlays
  3265. org-table-toggle-formula-debugger org-table-wrap-region
  3266. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3267. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3268. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3269. (defun org-at-table-p (&optional table-type)
  3270. "Return t if the cursor is inside an org-type table.
  3271. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3272. (if org-enable-table-editor
  3273. (save-excursion
  3274. (beginning-of-line 1)
  3275. (looking-at (if table-type org-table-any-line-regexp
  3276. org-table-line-regexp)))
  3277. nil))
  3278. (defsubst org-table-p () (org-at-table-p))
  3279. (defun org-at-table.el-p ()
  3280. "Return t if and only if we are at a table.el table."
  3281. (and (org-at-table-p 'any)
  3282. (save-excursion
  3283. (goto-char (org-table-begin 'any))
  3284. (looking-at org-table1-hline-regexp))))
  3285. (defun org-table-recognize-table.el ()
  3286. "If there is a table.el table nearby, recognize it and move into it."
  3287. (if org-table-tab-recognizes-table.el
  3288. (if (org-at-table.el-p)
  3289. (progn
  3290. (beginning-of-line 1)
  3291. (if (looking-at org-table-dataline-regexp)
  3292. nil
  3293. (if (looking-at org-table1-hline-regexp)
  3294. (progn
  3295. (beginning-of-line 2)
  3296. (if (looking-at org-table-any-border-regexp)
  3297. (beginning-of-line -1)))))
  3298. (if (re-search-forward "|" (org-table-end t) t)
  3299. (progn
  3300. (require 'table)
  3301. (if (table--at-cell-p (point))
  3302. t
  3303. (message "recognizing table.el table...")
  3304. (table-recognize-table)
  3305. (message "recognizing table.el table...done")))
  3306. (error "This should not happen"))
  3307. t)
  3308. nil)
  3309. nil))
  3310. (defun org-at-table-hline-p ()
  3311. "Return t if the cursor is inside a hline in a table."
  3312. (if org-enable-table-editor
  3313. (save-excursion
  3314. (beginning-of-line 1)
  3315. (looking-at org-table-hline-regexp))
  3316. nil))
  3317. (defvar org-table-clean-did-remove-column nil)
  3318. (defun org-table-map-tables (function &optional quietly)
  3319. "Apply FUNCTION to the start of all tables in the buffer."
  3320. (save-excursion
  3321. (save-restriction
  3322. (widen)
  3323. (goto-char (point-min))
  3324. (while (re-search-forward org-table-any-line-regexp nil t)
  3325. (unless quietly
  3326. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3327. (beginning-of-line 1)
  3328. (when (looking-at org-table-line-regexp)
  3329. (save-excursion (funcall function))
  3330. (or (looking-at org-table-line-regexp)
  3331. (forward-char 1)))
  3332. (re-search-forward org-table-any-border-regexp nil 1))))
  3333. (unless quietly (message "Mapping tables: done")))
  3334. ;; Declare and autoload functions from org-exp.el & Co
  3335. (declare-function org-default-export-plist "org-exp")
  3336. (declare-function org-infile-export-plist "org-exp")
  3337. (declare-function org-get-current-options "org-exp")
  3338. (eval-and-compile
  3339. (org-autoload "org-exp"
  3340. '(org-export org-export-visible
  3341. org-insert-export-options-template
  3342. org-table-clean-before-export))
  3343. (org-autoload "org-ascii"
  3344. '(org-export-as-ascii org-export-ascii-preprocess
  3345. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3346. org-export-region-as-ascii))
  3347. (org-autoload "org-latex"
  3348. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3349. org-replace-region-by-latex org-export-region-as-latex
  3350. org-export-as-latex org-export-as-pdf
  3351. org-export-as-pdf-and-open))
  3352. (org-autoload "org-html"
  3353. '(org-export-as-html-and-open
  3354. org-export-as-html-batch org-export-as-html-to-buffer
  3355. org-replace-region-by-html org-export-region-as-html
  3356. org-export-as-html))
  3357. (org-autoload "org-docbook"
  3358. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3359. org-replace-region-by-docbook org-export-region-as-docbook
  3360. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3361. org-export-as-docbook))
  3362. (org-autoload "org-icalendar"
  3363. '(org-export-icalendar-this-file
  3364. org-export-icalendar-all-agenda-files
  3365. org-export-icalendar-combine-agenda-files))
  3366. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3367. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3368. ;; Declare and autoload functions from org-agenda.el
  3369. (eval-and-compile
  3370. (org-autoload "org-agenda"
  3371. '(org-agenda org-agenda-list org-search-view
  3372. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3373. org-diary org-agenda-to-appt
  3374. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3375. ;; Autoload org-remember
  3376. (eval-and-compile
  3377. (org-autoload "org-remember"
  3378. '(org-remember-insinuate org-remember-annotation
  3379. org-remember-apply-template org-remember org-remember-handler)))
  3380. (eval-and-compile
  3381. (org-autoload "org-capture"
  3382. '(org-capture org-capture-insert-template-here
  3383. org-capture-import-remember-templates)))
  3384. ;; Autoload org-clock.el
  3385. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3386. (beg end))
  3387. (declare-function org-clock-update-mode-line "org-clock" ())
  3388. (declare-function org-resolve-clocks "org-clock"
  3389. (&optional also-non-dangling-p prompt last-valid))
  3390. (defvar org-clock-start-time)
  3391. (defvar org-clock-marker (make-marker)
  3392. "Marker recording the last clock-in.")
  3393. (defvar org-clock-hd-marker (make-marker)
  3394. "Marker recording the last clock-in, but the headline position.")
  3395. (defvar org-clock-heading ""
  3396. "The heading of the current clock entry.")
  3397. (defun org-clock-is-active ()
  3398. "Return non-nil if clock is currently running.
  3399. The return value is actually the clock marker."
  3400. (marker-buffer org-clock-marker))
  3401. (eval-and-compile
  3402. (org-autoload
  3403. "org-clock"
  3404. '(org-clock-in org-clock-out org-clock-cancel
  3405. org-clock-goto org-clock-sum org-clock-display
  3406. org-clock-remove-overlays org-clock-report
  3407. org-clocktable-shift org-dblock-write:clocktable
  3408. org-get-clocktable org-resolve-clocks)))
  3409. (defun org-clock-update-time-maybe ()
  3410. "If this is a CLOCK line, update it and return t.
  3411. Otherwise, return nil."
  3412. (interactive)
  3413. (save-excursion
  3414. (beginning-of-line 1)
  3415. (skip-chars-forward " \t")
  3416. (when (looking-at org-clock-string)
  3417. (let ((re (concat "[ \t]*" org-clock-string
  3418. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3419. "\\([ \t]*=>.*\\)?\\)?"))
  3420. ts te h m s neg)
  3421. (cond
  3422. ((not (looking-at re))
  3423. nil)
  3424. ((not (match-end 2))
  3425. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3426. (> org-clock-marker (point))
  3427. (<= org-clock-marker (point-at-eol)))
  3428. ;; The clock is running here
  3429. (setq org-clock-start-time
  3430. (apply 'encode-time
  3431. (org-parse-time-string (match-string 1))))
  3432. (org-clock-update-mode-line)))
  3433. (t
  3434. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3435. (end-of-line 1)
  3436. (setq ts (match-string 1)
  3437. te (match-string 3))
  3438. (setq s (- (org-float-time
  3439. (apply 'encode-time (org-parse-time-string te)))
  3440. (org-float-time
  3441. (apply 'encode-time (org-parse-time-string ts))))
  3442. neg (< s 0)
  3443. s (abs s)
  3444. h (floor (/ s 3600))
  3445. s (- s (* 3600 h))
  3446. m (floor (/ s 60))
  3447. s (- s (* 60 s)))
  3448. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3449. t))))))
  3450. (defun org-check-running-clock ()
  3451. "Check if the current buffer contains the running clock.
  3452. If yes, offer to stop it and to save the buffer with the changes."
  3453. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3454. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3455. (buffer-name))))
  3456. (org-clock-out)
  3457. (when (y-or-n-p "Save changed buffer?")
  3458. (save-buffer))))
  3459. (defun org-clocktable-try-shift (dir n)
  3460. "Check if this line starts a clock table, if yes, shift the time block."
  3461. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3462. (org-clocktable-shift dir n)))
  3463. ;; Autoload org-timer.el
  3464. (eval-and-compile
  3465. (org-autoload
  3466. "org-timer"
  3467. '(org-timer-start org-timer org-timer-item
  3468. org-timer-change-times-in-region
  3469. org-timer-set-timer
  3470. org-timer-reset-timers
  3471. org-timer-show-remaining-time)))
  3472. ;; Autoload org-feed.el
  3473. (eval-and-compile
  3474. (org-autoload
  3475. "org-feed"
  3476. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3477. ;; Autoload org-indent.el
  3478. ;; Define the variable already here, to make sure we have it.
  3479. (defvar org-indent-mode nil
  3480. "Non-nil if Org-Indent mode is enabled.
  3481. Use the command `org-indent-mode' to change this variable.")
  3482. (eval-and-compile
  3483. (org-autoload
  3484. "org-indent"
  3485. '(org-indent-mode)))
  3486. ;; Autoload org-mobile.el
  3487. (eval-and-compile
  3488. (org-autoload
  3489. "org-mobile"
  3490. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3491. ;; Autoload archiving code
  3492. ;; The stuff that is needed for cycling and tags has to be defined here.
  3493. (defgroup org-archive nil
  3494. "Options concerning archiving in Org-mode."
  3495. :tag "Org Archive"
  3496. :group 'org-structure)
  3497. (defcustom org-archive-location "%s_archive::"
  3498. "The location where subtrees should be archived.
  3499. The value of this variable is a string, consisting of two parts,
  3500. separated by a double-colon. The first part is a filename and
  3501. the second part is a headline.
  3502. When the filename is omitted, archiving happens in the same file.
  3503. %s in the filename will be replaced by the current file
  3504. name (without the directory part). Archiving to a different file
  3505. is useful to keep archived entries from contributing to the
  3506. Org-mode Agenda.
  3507. The archived entries will be filed as subtrees of the specified
  3508. headline. When the headline is omitted, the subtrees are simply
  3509. filed away at the end of the file, as top-level entries. Also in
  3510. the heading you can use %s to represent the file name, this can be
  3511. useful when using the same archive for a number of different files.
  3512. Here are a few examples:
  3513. \"%s_archive::\"
  3514. If the current file is Projects.org, archive in file
  3515. Projects.org_archive, as top-level trees. This is the default.
  3516. \"::* Archived Tasks\"
  3517. Archive in the current file, under the top-level headline
  3518. \"* Archived Tasks\".
  3519. \"~/org/archive.org::\"
  3520. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3521. \"~/org/archive.org::* From %s\"
  3522. Archive in file ~/org/archive.org (absolute path), under headlines
  3523. \"From FILENAME\" where file name is the current file name.
  3524. \"basement::** Finished Tasks\"
  3525. Archive in file ./basement (relative path), as level 3 trees
  3526. below the level 2 heading \"** Finished Tasks\".
  3527. You may set this option on a per-file basis by adding to the buffer a
  3528. line like
  3529. #+ARCHIVE: basement::** Finished Tasks
  3530. You may also define it locally for a subtree by setting an ARCHIVE property
  3531. in the entry. If such a property is found in an entry, or anywhere up
  3532. the hierarchy, it will be used."
  3533. :group 'org-archive
  3534. :type 'string)
  3535. (defcustom org-archive-tag "ARCHIVE"
  3536. "The tag that marks a subtree as archived.
  3537. An archived subtree does not open during visibility cycling, and does
  3538. not contribute to the agenda listings.
  3539. After changing this, font-lock must be restarted in the relevant buffers to
  3540. get the proper fontification."
  3541. :group 'org-archive
  3542. :group 'org-keywords
  3543. :type 'string)
  3544. (defcustom org-agenda-skip-archived-trees t
  3545. "Non-nil means the agenda will skip any items located in archived trees.
  3546. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3547. variable is no longer recommended, you should leave it at the value t.
  3548. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3549. :group 'org-archive
  3550. :group 'org-agenda-skip
  3551. :type 'boolean)
  3552. (defcustom org-columns-skip-archived-trees t
  3553. "Non-nil means ignore archived trees when creating column view."
  3554. :group 'org-archive
  3555. :group 'org-properties
  3556. :type 'boolean)
  3557. (defcustom org-cycle-open-archived-trees nil
  3558. "Non-nil means `org-cycle' will open archived trees.
  3559. An archived tree is a tree marked with the tag ARCHIVE.
  3560. When nil, archived trees will stay folded. You can still open them with
  3561. normal outline commands like `show-all', but not with the cycling commands."
  3562. :group 'org-archive
  3563. :group 'org-cycle
  3564. :type 'boolean)
  3565. (defcustom org-sparse-tree-open-archived-trees nil
  3566. "Non-nil means sparse tree construction shows matches in archived trees.
  3567. When nil, matches in these trees are highlighted, but the trees are kept in
  3568. collapsed state."
  3569. :group 'org-archive
  3570. :group 'org-sparse-trees
  3571. :type 'boolean)
  3572. (defun org-cycle-hide-archived-subtrees (state)
  3573. "Re-hide all archived subtrees after a visibility state change."
  3574. (when (and (not org-cycle-open-archived-trees)
  3575. (not (memq state '(overview folded))))
  3576. (save-excursion
  3577. (let* ((globalp (memq state '(contents all)))
  3578. (beg (if globalp (point-min) (point)))
  3579. (end (if globalp (point-max) (org-end-of-subtree t))))
  3580. (org-hide-archived-subtrees beg end)
  3581. (goto-char beg)
  3582. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3583. (message "%s" (substitute-command-keys
  3584. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3585. (defun org-force-cycle-archived ()
  3586. "Cycle subtree even if it is archived."
  3587. (interactive)
  3588. (setq this-command 'org-cycle)
  3589. (let ((org-cycle-open-archived-trees t))
  3590. (call-interactively 'org-cycle)))
  3591. (defun org-hide-archived-subtrees (beg end)
  3592. "Re-hide all archived subtrees after a visibility state change."
  3593. (save-excursion
  3594. (let* ((re (concat ":" org-archive-tag ":")))
  3595. (goto-char beg)
  3596. (while (re-search-forward re end t)
  3597. (when (org-on-heading-p)
  3598. (org-flag-subtree t)
  3599. (org-end-of-subtree t))))))
  3600. (defun org-flag-subtree (flag)
  3601. (save-excursion
  3602. (org-back-to-heading t)
  3603. (outline-end-of-heading)
  3604. (outline-flag-region (point)
  3605. (progn (org-end-of-subtree t) (point))
  3606. flag)))
  3607. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3608. (eval-and-compile
  3609. (org-autoload "org-archive"
  3610. '(org-add-archive-files org-archive-subtree
  3611. org-archive-to-archive-sibling org-toggle-archive-tag
  3612. org-archive-subtree-default
  3613. org-archive-subtree-default-with-confirmation)))
  3614. ;; Autoload Column View Code
  3615. (declare-function org-columns-number-to-string "org-colview")
  3616. (declare-function org-columns-get-format-and-top-level "org-colview")
  3617. (declare-function org-columns-compute "org-colview")
  3618. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3619. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3620. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3621. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3622. ;; Autoload ID code
  3623. (declare-function org-id-store-link "org-id")
  3624. (declare-function org-id-locations-load "org-id")
  3625. (declare-function org-id-locations-save "org-id")
  3626. (defvar org-id-track-globally)
  3627. (org-autoload "org-id"
  3628. '(org-id-get-create org-id-new org-id-copy org-id-get
  3629. org-id-get-with-outline-path-completion
  3630. org-id-get-with-outline-drilling org-id-store-link
  3631. org-id-goto org-id-find org-id-store-link))
  3632. ;; Autoload Plotting Code
  3633. (org-autoload "org-plot"
  3634. '(org-plot/gnuplot))
  3635. ;;; Variables for pre-computed regular expressions, all buffer local
  3636. (defvar org-drawer-regexp nil
  3637. "Matches first line of a hidden block.")
  3638. (make-variable-buffer-local 'org-drawer-regexp)
  3639. (defvar org-todo-regexp nil
  3640. "Matches any of the TODO state keywords.")
  3641. (make-variable-buffer-local 'org-todo-regexp)
  3642. (defvar org-not-done-regexp nil
  3643. "Matches any of the TODO state keywords except the last one.")
  3644. (make-variable-buffer-local 'org-not-done-regexp)
  3645. (defvar org-not-done-heading-regexp nil
  3646. "Matches a TODO headline that is not done.")
  3647. (make-variable-buffer-local 'org-not-done-regexp)
  3648. (defvar org-todo-line-regexp nil
  3649. "Matches a headline and puts TODO state into group 2 if present.")
  3650. (make-variable-buffer-local 'org-todo-line-regexp)
  3651. (defvar org-complex-heading-regexp nil
  3652. "Matches a headline and puts everything into groups:
  3653. group 1: the stars
  3654. group 2: The todo keyword, maybe
  3655. group 3: Priority cookie
  3656. group 4: True headline
  3657. group 5: Tags")
  3658. (make-variable-buffer-local 'org-complex-heading-regexp)
  3659. (defvar org-complex-heading-regexp-format nil
  3660. "Printf format to make regexp to match an exact headline.
  3661. This regexp will match the headline of any node which hase the exact
  3662. headline text that is put into the format, but may have any TODO state,
  3663. priority and tags.")
  3664. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3665. (defvar org-todo-line-tags-regexp nil
  3666. "Matches a headline and puts TODO state into group 2 if present.
  3667. Also put tags into group 4 if tags are present.")
  3668. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3669. (defvar org-nl-done-regexp nil
  3670. "Matches newline followed by a headline with the DONE keyword.")
  3671. (make-variable-buffer-local 'org-nl-done-regexp)
  3672. (defvar org-looking-at-done-regexp nil
  3673. "Matches the DONE keyword a point.")
  3674. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3675. (defvar org-ds-keyword-length 12
  3676. "Maximum length of the Deadline and SCHEDULED keywords.")
  3677. (make-variable-buffer-local 'org-ds-keyword-length)
  3678. (defvar org-deadline-regexp nil
  3679. "Matches the DEADLINE keyword.")
  3680. (make-variable-buffer-local 'org-deadline-regexp)
  3681. (defvar org-deadline-time-regexp nil
  3682. "Matches the DEADLINE keyword together with a time stamp.")
  3683. (make-variable-buffer-local 'org-deadline-time-regexp)
  3684. (defvar org-deadline-line-regexp nil
  3685. "Matches the DEADLINE keyword and the rest of the line.")
  3686. (make-variable-buffer-local 'org-deadline-line-regexp)
  3687. (defvar org-scheduled-regexp nil
  3688. "Matches the SCHEDULED keyword.")
  3689. (make-variable-buffer-local 'org-scheduled-regexp)
  3690. (defvar org-scheduled-time-regexp nil
  3691. "Matches the SCHEDULED keyword together with a time stamp.")
  3692. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3693. (defvar org-closed-time-regexp nil
  3694. "Matches the CLOSED keyword together with a time stamp.")
  3695. (make-variable-buffer-local 'org-closed-time-regexp)
  3696. (defvar org-keyword-time-regexp nil
  3697. "Matches any of the 4 keywords, together with the time stamp.")
  3698. (make-variable-buffer-local 'org-keyword-time-regexp)
  3699. (defvar org-keyword-time-not-clock-regexp nil
  3700. "Matches any of the 3 keywords, together with the time stamp.")
  3701. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3702. (defvar org-maybe-keyword-time-regexp nil
  3703. "Matches a timestamp, possibly preceded by a keyword.")
  3704. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3705. (defvar org-planning-or-clock-line-re nil
  3706. "Matches a line with planning or clock info.")
  3707. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3708. (defvar org-all-time-keywords nil
  3709. "List of time keywords.")
  3710. (make-variable-buffer-local 'org-all-time-keywords)
  3711. (defconst org-plain-time-of-day-regexp
  3712. (concat
  3713. "\\(\\<[012]?[0-9]"
  3714. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3715. "\\(--?"
  3716. "\\(\\<[012]?[0-9]"
  3717. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3718. "\\)?")
  3719. "Regular expression to match a plain time or time range.
  3720. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3721. groups carry important information:
  3722. 0 the full match
  3723. 1 the first time, range or not
  3724. 8 the second time, if it is a range.")
  3725. (defconst org-plain-time-extension-regexp
  3726. (concat
  3727. "\\(\\<[012]?[0-9]"
  3728. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3729. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3730. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3731. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3732. groups carry important information:
  3733. 0 the full match
  3734. 7 hours of duration
  3735. 9 minutes of duration")
  3736. (defconst org-stamp-time-of-day-regexp
  3737. (concat
  3738. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3739. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3740. "\\(--?"
  3741. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3742. "Regular expression to match a timestamp time or time range.
  3743. After a match, the following groups carry important information:
  3744. 0 the full match
  3745. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3746. 2 the first time, range or not
  3747. 4 the second time, if it is a range.")
  3748. (defconst org-startup-options
  3749. '(("fold" org-startup-folded t)
  3750. ("overview" org-startup-folded t)
  3751. ("nofold" org-startup-folded nil)
  3752. ("showall" org-startup-folded nil)
  3753. ("showeverything" org-startup-folded showeverything)
  3754. ("content" org-startup-folded content)
  3755. ("indent" org-startup-indented t)
  3756. ("noindent" org-startup-indented nil)
  3757. ("hidestars" org-hide-leading-stars t)
  3758. ("showstars" org-hide-leading-stars nil)
  3759. ("odd" org-odd-levels-only t)
  3760. ("oddeven" org-odd-levels-only nil)
  3761. ("align" org-startup-align-all-tables t)
  3762. ("noalign" org-startup-align-all-tables nil)
  3763. ("inlineimages" org-startup-with-inline-images t)
  3764. ("noinlineimages" org-startup-with-inline-images nil)
  3765. ("customtime" org-display-custom-times t)
  3766. ("logdone" org-log-done time)
  3767. ("lognotedone" org-log-done note)
  3768. ("nologdone" org-log-done nil)
  3769. ("lognoteclock-out" org-log-note-clock-out t)
  3770. ("nolognoteclock-out" org-log-note-clock-out nil)
  3771. ("logrepeat" org-log-repeat state)
  3772. ("lognoterepeat" org-log-repeat note)
  3773. ("nologrepeat" org-log-repeat nil)
  3774. ("logreschedule" org-log-reschedule time)
  3775. ("lognotereschedule" org-log-reschedule note)
  3776. ("nologreschedule" org-log-reschedule nil)
  3777. ("logredeadline" org-log-redeadline time)
  3778. ("lognoteredeadline" org-log-redeadline note)
  3779. ("nologredeadline" org-log-redeadline nil)
  3780. ("logrefile" org-log-refile time)
  3781. ("lognoterefile" org-log-refile note)
  3782. ("nologrefile" org-log-refile nil)
  3783. ("fninline" org-footnote-define-inline t)
  3784. ("nofninline" org-footnote-define-inline nil)
  3785. ("fnlocal" org-footnote-section nil)
  3786. ("fnauto" org-footnote-auto-label t)
  3787. ("fnprompt" org-footnote-auto-label nil)
  3788. ("fnconfirm" org-footnote-auto-label confirm)
  3789. ("fnplain" org-footnote-auto-label plain)
  3790. ("fnadjust" org-footnote-auto-adjust t)
  3791. ("nofnadjust" org-footnote-auto-adjust nil)
  3792. ("constcgs" constants-unit-system cgs)
  3793. ("constSI" constants-unit-system SI)
  3794. ("noptag" org-tag-persistent-alist nil)
  3795. ("hideblocks" org-hide-block-startup t)
  3796. ("nohideblocks" org-hide-block-startup nil)
  3797. ("beamer" org-startup-with-beamer-mode t)
  3798. ("entitiespretty" org-pretty-entities t)
  3799. ("entitiesplain" org-pretty-entities nil))
  3800. "Variable associated with STARTUP options for org-mode.
  3801. Each element is a list of three items: The startup options as written
  3802. in the #+STARTUP line, the corresponding variable, and the value to
  3803. set this variable to if the option is found. An optional forth element PUSH
  3804. means to push this value onto the list in the variable.")
  3805. (defun org-set-regexps-and-options ()
  3806. "Precompute regular expressions for current buffer."
  3807. (when (org-mode-p)
  3808. (org-set-local 'org-todo-kwd-alist nil)
  3809. (org-set-local 'org-todo-key-alist nil)
  3810. (org-set-local 'org-todo-key-trigger nil)
  3811. (org-set-local 'org-todo-keywords-1 nil)
  3812. (org-set-local 'org-done-keywords nil)
  3813. (org-set-local 'org-todo-heads nil)
  3814. (org-set-local 'org-todo-sets nil)
  3815. (org-set-local 'org-todo-log-states nil)
  3816. (org-set-local 'org-file-properties nil)
  3817. (org-set-local 'org-file-tags nil)
  3818. (let ((re (org-make-options-regexp
  3819. '("CATEGORY" "TODO" "COLUMNS"
  3820. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3821. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3822. "OPTIONS")
  3823. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3824. (splitre "[ \t]+")
  3825. (scripts org-use-sub-superscripts)
  3826. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3827. tail sep kws1 prio props ftags drawers beamer-p
  3828. ext-setup-or-nil setup-contents (start 0))
  3829. (save-excursion
  3830. (save-restriction
  3831. (widen)
  3832. (goto-char (point-min))
  3833. (while (or (and ext-setup-or-nil
  3834. (string-match re ext-setup-or-nil start)
  3835. (setq start (match-end 0)))
  3836. (and (setq ext-setup-or-nil nil start 0)
  3837. (re-search-forward re nil t)))
  3838. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3839. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3840. (if (stringp value) (setq value (org-trim value)))
  3841. (cond
  3842. ((equal key "CATEGORY")
  3843. (setq cat value))
  3844. ((member key '("SEQ_TODO" "TODO"))
  3845. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3846. ((equal key "TYP_TODO")
  3847. (push (cons 'type (org-split-string value splitre)) kwds))
  3848. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3849. ;; general TODO-like setup
  3850. (push (cons (intern (downcase (match-string 1 key)))
  3851. (org-split-string value splitre)) kwds))
  3852. ((equal key "TAGS")
  3853. (setq tags (append tags (if tags '("\\n") nil)
  3854. (org-split-string value splitre))))
  3855. ((equal key "COLUMNS")
  3856. (org-set-local 'org-columns-default-format value))
  3857. ((equal key "LINK")
  3858. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3859. (push (cons (match-string 1 value)
  3860. (org-trim (match-string 2 value)))
  3861. links)))
  3862. ((equal key "PRIORITIES")
  3863. (setq prio (org-split-string value " +")))
  3864. ((equal key "PROPERTY")
  3865. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3866. (push (cons (match-string 1 value) (match-string 2 value))
  3867. props)))
  3868. ((equal key "FILETAGS")
  3869. (when (string-match "\\S-" value)
  3870. (setq ftags
  3871. (append
  3872. ftags
  3873. (apply 'append
  3874. (mapcar (lambda (x) (org-split-string x ":"))
  3875. (org-split-string value)))))))
  3876. ((equal key "DRAWERS")
  3877. (setq drawers (org-split-string value splitre)))
  3878. ((equal key "CONSTANTS")
  3879. (setq const (append const (org-split-string value splitre))))
  3880. ((equal key "STARTUP")
  3881. (let ((opts (org-split-string value splitre))
  3882. l var val)
  3883. (while (setq l (pop opts))
  3884. (when (setq l (assoc l org-startup-options))
  3885. (setq var (nth 1 l) val (nth 2 l))
  3886. (if (not (nth 3 l))
  3887. (set (make-local-variable var) val)
  3888. (if (not (listp (symbol-value var)))
  3889. (set (make-local-variable var) nil))
  3890. (set (make-local-variable var) (symbol-value var))
  3891. (add-to-list var val))))))
  3892. ((equal key "ARCHIVE")
  3893. (setq arch value)
  3894. (remove-text-properties 0 (length arch)
  3895. '(face t fontified t) arch))
  3896. ((equal key "LATEX_CLASS")
  3897. (setq beamer-p (equal value "beamer")))
  3898. ((equal key "OPTIONS")
  3899. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3900. (setq scripts (read (match-string 2 value)))))
  3901. ((equal key "SETUPFILE")
  3902. (setq setup-contents (org-file-contents
  3903. (expand-file-name
  3904. (org-remove-double-quotes value))
  3905. 'noerror))
  3906. (if (not ext-setup-or-nil)
  3907. (setq ext-setup-or-nil setup-contents start 0)
  3908. (setq ext-setup-or-nil
  3909. (concat (substring ext-setup-or-nil 0 start)
  3910. "\n" setup-contents "\n"
  3911. (substring ext-setup-or-nil start)))))
  3912. ))))
  3913. (org-set-local 'org-use-sub-superscripts scripts)
  3914. (when cat
  3915. (org-set-local 'org-category (intern cat))
  3916. (push (cons "CATEGORY" cat) props))
  3917. (when prio
  3918. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3919. (setq prio (mapcar 'string-to-char prio))
  3920. (org-set-local 'org-highest-priority (nth 0 prio))
  3921. (org-set-local 'org-lowest-priority (nth 1 prio))
  3922. (org-set-local 'org-default-priority (nth 2 prio)))
  3923. (and props (org-set-local 'org-file-properties (nreverse props)))
  3924. (and ftags (org-set-local 'org-file-tags
  3925. (mapcar 'org-add-prop-inherited ftags)))
  3926. (and drawers (org-set-local 'org-drawers drawers))
  3927. (and arch (org-set-local 'org-archive-location arch))
  3928. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3929. ;; Process the TODO keywords
  3930. (unless kwds
  3931. ;; Use the global values as if they had been given locally.
  3932. (setq kwds (default-value 'org-todo-keywords))
  3933. (if (stringp (car kwds))
  3934. (setq kwds (list (cons org-todo-interpretation
  3935. (default-value 'org-todo-keywords)))))
  3936. (setq kwds (reverse kwds)))
  3937. (setq kwds (nreverse kwds))
  3938. (let (inter kws kw)
  3939. (while (setq kws (pop kwds))
  3940. (let ((kws (or
  3941. (run-hook-with-args-until-success
  3942. 'org-todo-setup-filter-hook kws)
  3943. kws)))
  3944. (setq inter (pop kws) sep (member "|" kws)
  3945. kws0 (delete "|" (copy-sequence kws))
  3946. kwsa nil
  3947. kws1 (mapcar
  3948. (lambda (x)
  3949. ;; 1 2
  3950. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3951. (progn
  3952. (setq kw (match-string 1 x)
  3953. key (and (match-end 2) (match-string 2 x))
  3954. log (org-extract-log-state-settings x))
  3955. (push (cons kw (and key (string-to-char key))) kwsa)
  3956. (and log (push log org-todo-log-states))
  3957. kw)
  3958. (error "Invalid TODO keyword %s" x)))
  3959. kws0)
  3960. kwsa (if kwsa (append '((:startgroup))
  3961. (nreverse kwsa)
  3962. '((:endgroup))))
  3963. hw (car kws1)
  3964. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3965. tail (list inter hw (car dws) (org-last dws))))
  3966. (add-to-list 'org-todo-heads hw 'append)
  3967. (push kws1 org-todo-sets)
  3968. (setq org-done-keywords (append org-done-keywords dws nil))
  3969. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3970. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3971. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3972. (setq org-todo-sets (nreverse org-todo-sets)
  3973. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3974. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3975. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3976. ;; Process the constants
  3977. (when const
  3978. (let (e cst)
  3979. (while (setq e (pop const))
  3980. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3981. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3982. (setq org-table-formula-constants-local cst)))
  3983. ;; Process the tags.
  3984. (when tags
  3985. (let (e tgs)
  3986. (while (setq e (pop tags))
  3987. (cond
  3988. ((equal e "{") (push '(:startgroup) tgs))
  3989. ((equal e "}") (push '(:endgroup) tgs))
  3990. ((equal e "\\n") (push '(:newline) tgs))
  3991. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3992. (push (cons (match-string 1 e)
  3993. (string-to-char (match-string 2 e)))
  3994. tgs))
  3995. (t (push (list e) tgs))))
  3996. (org-set-local 'org-tag-alist nil)
  3997. (while (setq e (pop tgs))
  3998. (or (and (stringp (car e))
  3999. (assoc (car e) org-tag-alist))
  4000. (push e org-tag-alist)))))
  4001. ;; Compute the regular expressions and other local variables
  4002. (if (not org-done-keywords)
  4003. (setq org-done-keywords (and org-todo-keywords-1
  4004. (list (org-last org-todo-keywords-1)))))
  4005. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4006. (length org-scheduled-string)
  4007. (length org-clock-string)
  4008. (length org-closed-string)))
  4009. org-drawer-regexp
  4010. (concat "^[ \t]*:\\("
  4011. (mapconcat 'regexp-quote org-drawers "\\|")
  4012. "\\):[ \t]*$")
  4013. org-not-done-keywords
  4014. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4015. org-todo-regexp
  4016. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4017. "\\|") "\\)\\>")
  4018. org-not-done-regexp
  4019. (concat "\\<\\("
  4020. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4021. "\\)\\>")
  4022. org-not-done-heading-regexp
  4023. (concat "^\\(\\*+\\)[ \t]+\\("
  4024. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4025. "\\)[ \t]+")
  4026. org-todo-line-regexp
  4027. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4028. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4029. "\\)[ \t]+\\)?\\(.*\\)")
  4030. org-complex-heading-regexp
  4031. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4032. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4033. "\\)[ \t]+\\)?"
  4034. "\\(?:\\(\\[#.\\]\\)[ \t]+\\)?"
  4035. "\\(.*?\\)"
  4036. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  4037. org-complex-heading-regexp-format
  4038. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4039. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4040. "\\)[ \t]+\\)?"
  4041. "\\(?:\\(\\[#.\\]\\)[ \t]+\\)?"
  4042. "\\(?:\\(?:\\[[0-9%%/]+\\]\\)[ \t]+\\)?" ; stats cookie
  4043. "\\(%s\\)[ \t]*"
  4044. "\\(?:\\[[0-9%%/]+\\]\\)?" ; stats cookie
  4045. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$"))
  4046. org-nl-done-regexp
  4047. (concat "\n\\*+[ \t]+"
  4048. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4049. "\\)" "[ \t]+")
  4050. org-todo-line-tags-regexp
  4051. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4052. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4053. "\\)[ \t]+\\)"
  4054. (org-re "\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  4055. org-looking-at-done-regexp
  4056. (concat "^" "\\(?:"
  4057. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4058. "[ \t]+")
  4059. org-deadline-regexp (concat "\\<" org-deadline-string)
  4060. org-deadline-time-regexp
  4061. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4062. org-deadline-line-regexp
  4063. (concat "\\<\\(" org-deadline-string "\\).*")
  4064. org-scheduled-regexp
  4065. (concat "\\<" org-scheduled-string)
  4066. org-scheduled-time-regexp
  4067. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4068. org-closed-time-regexp
  4069. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4070. org-keyword-time-regexp
  4071. (concat "\\<\\(" org-scheduled-string
  4072. "\\|" org-deadline-string
  4073. "\\|" org-closed-string
  4074. "\\|" org-clock-string "\\)"
  4075. " *[[<]\\([^]>]+\\)[]>]")
  4076. org-keyword-time-not-clock-regexp
  4077. (concat "\\<\\(" org-scheduled-string
  4078. "\\|" org-deadline-string
  4079. "\\|" org-closed-string
  4080. "\\)"
  4081. " *[[<]\\([^]>]+\\)[]>]")
  4082. org-maybe-keyword-time-regexp
  4083. (concat "\\(\\<\\(" org-scheduled-string
  4084. "\\|" org-deadline-string
  4085. "\\|" org-closed-string
  4086. "\\|" org-clock-string "\\)\\)?"
  4087. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4088. org-planning-or-clock-line-re
  4089. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4090. "\\|" org-deadline-string
  4091. "\\|" org-closed-string "\\|" org-clock-string
  4092. "\\)\\>\\)")
  4093. org-all-time-keywords
  4094. (mapcar (lambda (w) (substring w 0 -1))
  4095. (list org-scheduled-string org-deadline-string
  4096. org-clock-string org-closed-string))
  4097. )
  4098. (org-compute-latex-and-specials-regexp)
  4099. (org-set-font-lock-defaults))))
  4100. (defun org-file-contents (file &optional noerror)
  4101. "Return the contents of FILE, as a string."
  4102. (if (or (not file)
  4103. (not (file-readable-p file)))
  4104. (if noerror
  4105. (progn
  4106. (message "Cannot read file \"%s\"" file)
  4107. (ding) (sit-for 2)
  4108. "")
  4109. (error "Cannot read file \"%s\"" file))
  4110. (with-temp-buffer
  4111. (insert-file-contents file)
  4112. (buffer-string))))
  4113. (defun org-extract-log-state-settings (x)
  4114. "Extract the log state setting from a TODO keyword string.
  4115. This will extract info from a string like \"WAIT(w@/!)\"."
  4116. (let (kw key log1 log2)
  4117. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4118. (setq kw (match-string 1 x)
  4119. key (and (match-end 2) (match-string 2 x))
  4120. log1 (and (match-end 3) (match-string 3 x))
  4121. log2 (and (match-end 4) (match-string 4 x)))
  4122. (and (or log1 log2)
  4123. (list kw
  4124. (and log1 (if (equal log1 "!") 'time 'note))
  4125. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4126. (defun org-remove-keyword-keys (list)
  4127. "Remove a pair of parenthesis at the end of each string in LIST."
  4128. (mapcar (lambda (x)
  4129. (if (string-match "(.*)$" x)
  4130. (substring x 0 (match-beginning 0))
  4131. x))
  4132. list))
  4133. (defun org-assign-fast-keys (alist)
  4134. "Assign fast keys to a keyword-key alist.
  4135. Respect keys that are already there."
  4136. (let (new e (alt ?0))
  4137. (while (setq e (pop alist))
  4138. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4139. (cdr e)) ;; Key already assigned.
  4140. (push e new)
  4141. (let ((clist (string-to-list (downcase (car e))))
  4142. (used (append new alist)))
  4143. (when (= (car clist) ?@)
  4144. (pop clist))
  4145. (while (and clist (rassoc (car clist) used))
  4146. (pop clist))
  4147. (unless clist
  4148. (while (rassoc alt used)
  4149. (incf alt)))
  4150. (push (cons (car e) (or (car clist) alt)) new))))
  4151. (nreverse new)))
  4152. ;;; Some variables used in various places
  4153. (defvar org-window-configuration nil
  4154. "Used in various places to store a window configuration.")
  4155. (defvar org-selected-window nil
  4156. "Used in various places to store a window configuration.")
  4157. (defvar org-finish-function nil
  4158. "Function to be called when `C-c C-c' is used.
  4159. This is for getting out of special buffers like remember.")
  4160. ;; FIXME: Occasionally check by commenting these, to make sure
  4161. ;; no other functions uses these, forgetting to let-bind them.
  4162. (defvar entry)
  4163. (defvar last-state)
  4164. (defvar date)
  4165. ;; Defined somewhere in this file, but used before definition.
  4166. (defvar org-entities) ;; defined in org-entities.el
  4167. (defvar org-struct-menu)
  4168. (defvar org-org-menu)
  4169. (defvar org-tbl-menu)
  4170. ;;;; Define the Org-mode
  4171. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4172. (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"))
  4173. ;; We use a before-change function to check if a table might need
  4174. ;; an update.
  4175. (defvar org-table-may-need-update t
  4176. "Indicates that a table might need an update.
  4177. This variable is set by `org-before-change-function'.
  4178. `org-table-align' sets it back to nil.")
  4179. (defun org-before-change-function (beg end)
  4180. "Every change indicates that a table might need an update."
  4181. (setq org-table-may-need-update t))
  4182. (defvar org-mode-map)
  4183. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4184. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4185. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4186. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4187. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4188. (defvar org-table-buffer-is-an nil)
  4189. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4190. ;; some places -- e.g. see the macro org-with-limited-levels.
  4191. ;;
  4192. ;; In Org buffers, the value of `outline-regexp' is that of
  4193. ;; `org-outline-regexp'. The only function still directly relying on
  4194. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4195. ;; job when `orgstruct-mode' is active.
  4196. (defvar org-outline-regexp "\\*+ "
  4197. "Regexp to match Org headlines.")
  4198. (defconst org-outline-regexp-bol "^\\*+ "
  4199. "Regexp to match Org headlines.
  4200. This is similar to `org-outline-regexp' but additionally makes
  4201. sure that we are at the beginning of the line.")
  4202. ;;;###autoload
  4203. (define-derived-mode org-mode outline-mode "Org"
  4204. "Outline-based notes management and organizer, alias
  4205. \"Carsten's outline-mode for keeping track of everything.\"
  4206. Org-mode develops organizational tasks around a NOTES file which
  4207. contains information about projects as plain text. Org-mode is
  4208. implemented on top of outline-mode, which is ideal to keep the content
  4209. of large files well structured. It supports ToDo items, deadlines and
  4210. time stamps, which magically appear in the diary listing of the Emacs
  4211. calendar. Tables are easily created with a built-in table editor.
  4212. Plain text URL-like links connect to websites, emails (VM), Usenet
  4213. messages (Gnus), BBDB entries, and any files related to the project.
  4214. For printing and sharing of notes, an Org-mode file (or a part of it)
  4215. can be exported as a structured ASCII or HTML file.
  4216. The following commands are available:
  4217. \\{org-mode-map}"
  4218. ;; Get rid of Outline menus, they are not needed
  4219. ;; Need to do this here because define-derived-mode sets up
  4220. ;; the keymap so late. Still, it is a waste to call this each time
  4221. ;; we switch another buffer into org-mode.
  4222. (if (featurep 'xemacs)
  4223. (when (boundp 'outline-mode-menu-heading)
  4224. ;; Assume this is Greg's port, it uses easymenu
  4225. (easy-menu-remove outline-mode-menu-heading)
  4226. (easy-menu-remove outline-mode-menu-show)
  4227. (easy-menu-remove outline-mode-menu-hide))
  4228. (define-key org-mode-map [menu-bar headings] 'undefined)
  4229. (define-key org-mode-map [menu-bar hide] 'undefined)
  4230. (define-key org-mode-map [menu-bar show] 'undefined))
  4231. (org-load-modules-maybe)
  4232. (easy-menu-add org-org-menu)
  4233. (easy-menu-add org-tbl-menu)
  4234. (org-install-agenda-files-menu)
  4235. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4236. (add-to-invisibility-spec '(org-cwidth))
  4237. (add-to-invisibility-spec '(org-hide-block . t))
  4238. (when (featurep 'xemacs)
  4239. (org-set-local 'line-move-ignore-invisible t))
  4240. (org-set-local 'outline-regexp org-outline-regexp)
  4241. (org-set-local 'outline-level 'org-outline-level)
  4242. (when (and org-ellipsis
  4243. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4244. (fboundp 'make-glyph-code))
  4245. (unless org-display-table
  4246. (setq org-display-table (make-display-table)))
  4247. (set-display-table-slot
  4248. org-display-table 4
  4249. (vconcat (mapcar
  4250. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4251. org-ellipsis)))
  4252. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4253. (setq buffer-display-table org-display-table))
  4254. (org-set-regexps-and-options)
  4255. (when (and org-tag-faces (not org-tags-special-faces-re))
  4256. ;; tag faces set outside customize.... force initialization.
  4257. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4258. ;; Calc embedded
  4259. (org-set-local 'calc-embedded-open-mode "# ")
  4260. (modify-syntax-entry ?@ "w")
  4261. (if org-startup-truncated (setq truncate-lines t))
  4262. (org-set-local 'font-lock-unfontify-region-function
  4263. 'org-unfontify-region)
  4264. ;; Activate before-change-function
  4265. (org-set-local 'org-table-may-need-update t)
  4266. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4267. 'local)
  4268. ;; Check for running clock before killing a buffer
  4269. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4270. ;; Paragraphs and auto-filling
  4271. (org-set-autofill-regexps)
  4272. (setq indent-line-function 'org-indent-line-function)
  4273. (org-update-radio-target-regexp)
  4274. ;; Beginning/end of defun
  4275. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4276. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4277. ;; Next error for sparse trees
  4278. (org-set-local 'next-error-function 'org-occur-next-match)
  4279. ;; Make sure dependence stuff works reliably, even for users who set it
  4280. ;; too late :-(
  4281. (if org-enforce-todo-dependencies
  4282. (add-hook 'org-blocker-hook
  4283. 'org-block-todo-from-children-or-siblings-or-parent)
  4284. (remove-hook 'org-blocker-hook
  4285. 'org-block-todo-from-children-or-siblings-or-parent))
  4286. (if org-enforce-todo-checkbox-dependencies
  4287. (add-hook 'org-blocker-hook
  4288. 'org-block-todo-from-checkboxes)
  4289. (remove-hook 'org-blocker-hook
  4290. 'org-block-todo-from-checkboxes))
  4291. ;; Comment characters
  4292. (org-set-local 'comment-start "#")
  4293. (org-set-local 'comment-padding " ")
  4294. ;; Align options lines
  4295. (org-set-local
  4296. 'align-mode-rules-list
  4297. '((org-in-buffer-settings
  4298. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4299. (modes . '(org-mode)))))
  4300. ;; Imenu
  4301. (org-set-local 'imenu-create-index-function
  4302. 'org-imenu-get-tree)
  4303. ;; Make isearch reveal context
  4304. (if (or (featurep 'xemacs)
  4305. (not (boundp 'outline-isearch-open-invisible-function)))
  4306. ;; Emacs 21 and XEmacs make use of the hook
  4307. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4308. ;; Emacs 22 deals with this through a special variable
  4309. (org-set-local 'outline-isearch-open-invisible-function
  4310. (lambda (&rest ignore) (org-show-context 'isearch))))
  4311. ;; Turn on org-beamer-mode?
  4312. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4313. ;; Setup the pcomplete hooks
  4314. (set (make-local-variable 'pcomplete-command-completion-function)
  4315. 'org-pcomplete-initial)
  4316. (set (make-local-variable 'pcomplete-command-name-function)
  4317. 'org-command-at-point)
  4318. (set (make-local-variable 'pcomplete-default-completion-function)
  4319. 'ignore)
  4320. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4321. 'org-parse-arguments)
  4322. (set (make-local-variable 'pcomplete-termination-string) "")
  4323. (set (make-local-variable 'face-remapping-alist)
  4324. '((default org-default)))
  4325. ;; If empty file that did not turn on org-mode automatically, make it to.
  4326. (if (and org-insert-mode-line-in-empty-file
  4327. (org-called-interactively-p 'any)
  4328. (= (point-min) (point-max)))
  4329. (insert "# -*- mode: org -*-\n\n"))
  4330. (unless org-inhibit-startup
  4331. (when org-startup-align-all-tables
  4332. (let ((bmp (buffer-modified-p)))
  4333. (org-table-map-tables 'org-table-align 'quietly)
  4334. (set-buffer-modified-p bmp)))
  4335. (when org-startup-with-inline-images
  4336. (org-display-inline-images))
  4337. (when org-startup-indented
  4338. (require 'org-indent)
  4339. (org-indent-mode 1))
  4340. (unless org-inhibit-startup-visibility-stuff
  4341. (org-set-startup-visibility))))
  4342. (when (fboundp 'abbrev-table-put)
  4343. (abbrev-table-put org-mode-abbrev-table
  4344. :parents (list text-mode-abbrev-table)))
  4345. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4346. (defun org-current-time ()
  4347. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4348. (if (> (car org-time-stamp-rounding-minutes) 1)
  4349. (let ((r (car org-time-stamp-rounding-minutes))
  4350. (time (decode-time)))
  4351. (apply 'encode-time
  4352. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4353. (nthcdr 2 time))))
  4354. (current-time)))
  4355. (defun org-today ()
  4356. "Return today date, considering `org-extend-today-until'."
  4357. (time-to-days
  4358. (time-subtract (current-time)
  4359. (list 0 (* 3600 org-extend-today-until) 0))))
  4360. ;;;; Font-Lock stuff, including the activators
  4361. (defvar org-mouse-map (make-sparse-keymap))
  4362. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4363. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4364. (when org-mouse-1-follows-link
  4365. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4366. (when org-tab-follows-link
  4367. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4368. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4369. (require 'font-lock)
  4370. (defconst org-non-link-chars "]\t\n\r<>")
  4371. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4372. "shell" "elisp" "doi" "message"))
  4373. (defvar org-link-types-re nil
  4374. "Matches a link that has a url-like prefix like \"http:\"")
  4375. (defvar org-link-re-with-space nil
  4376. "Matches a link with spaces, optional angular brackets around it.")
  4377. (defvar org-link-re-with-space2 nil
  4378. "Matches a link with spaces, optional angular brackets around it.")
  4379. (defvar org-link-re-with-space3 nil
  4380. "Matches a link with spaces, only for internal part in bracket links.")
  4381. (defvar org-angle-link-re nil
  4382. "Matches link with angular brackets, spaces are allowed.")
  4383. (defvar org-plain-link-re nil
  4384. "Matches plain link, without spaces.")
  4385. (defvar org-bracket-link-regexp nil
  4386. "Matches a link in double brackets.")
  4387. (defvar org-bracket-link-analytic-regexp nil
  4388. "Regular expression used to analyze links.
  4389. Here is what the match groups contain after a match:
  4390. 1: http:
  4391. 2: http
  4392. 3: path
  4393. 4: [desc]
  4394. 5: desc")
  4395. (defvar org-bracket-link-analytic-regexp++ nil
  4396. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4397. (defvar org-any-link-re nil
  4398. "Regular expression matching any link.")
  4399. (defcustom org-match-sexp-depth 3
  4400. "Number of stacked braces for sub/superscript matching.
  4401. This has to be set before loading org.el to be effective."
  4402. :group 'org-export-translation ; ??????????????????????????/
  4403. :type 'integer)
  4404. (defun org-create-multibrace-regexp (left right n)
  4405. "Create a regular expression which will match a balanced sexp.
  4406. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4407. as single character strings.
  4408. The regexp returned will match the entire expression including the
  4409. delimiters. It will also define a single group which contains the
  4410. match except for the outermost delimiters. The maximum depth of
  4411. stacked delimiters is N. Escaping delimiters is not possible."
  4412. (let* ((nothing (concat "[^" left right "]*?"))
  4413. (or "\\|")
  4414. (re nothing)
  4415. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4416. (while (> n 1)
  4417. (setq n (1- n)
  4418. re (concat re or next)
  4419. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4420. (concat left "\\(" re "\\)" right)))
  4421. (defvar org-match-substring-regexp
  4422. (concat
  4423. "\\([^\\]\\)\\([_^]\\)\\("
  4424. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4425. "\\|"
  4426. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4427. "\\|"
  4428. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4429. "The regular expression matching a sub- or superscript.")
  4430. (defvar org-match-substring-with-braces-regexp
  4431. (concat
  4432. "\\([^\\]\\)\\([_^]\\)\\("
  4433. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4434. "\\)")
  4435. "The regular expression matching a sub- or superscript, forcing braces.")
  4436. (defun org-make-link-regexps ()
  4437. "Update the link regular expressions.
  4438. This should be called after the variable `org-link-types' has changed."
  4439. (setq org-link-types-re
  4440. (concat
  4441. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4442. org-link-re-with-space
  4443. (concat
  4444. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4445. "\\([^" org-non-link-chars " ]"
  4446. "[^" org-non-link-chars "]*"
  4447. "[^" org-non-link-chars " ]\\)>?")
  4448. org-link-re-with-space2
  4449. (concat
  4450. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4451. "\\([^" org-non-link-chars " ]"
  4452. "[^\t\n\r]*"
  4453. "[^" org-non-link-chars " ]\\)>?")
  4454. org-link-re-with-space3
  4455. (concat
  4456. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4457. "\\([^" org-non-link-chars " ]"
  4458. "[^\t\n\r]*\\)")
  4459. org-angle-link-re
  4460. (concat
  4461. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4462. "\\([^" org-non-link-chars " ]"
  4463. "[^" org-non-link-chars "]*"
  4464. "\\)>")
  4465. org-plain-link-re
  4466. (concat
  4467. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4468. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4469. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4470. org-bracket-link-regexp
  4471. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4472. org-bracket-link-analytic-regexp
  4473. (concat
  4474. "\\[\\["
  4475. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4476. "\\([^]]+\\)"
  4477. "\\]"
  4478. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4479. "\\]")
  4480. org-bracket-link-analytic-regexp++
  4481. (concat
  4482. "\\[\\["
  4483. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4484. "\\([^]]+\\)"
  4485. "\\]"
  4486. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4487. "\\]")
  4488. org-any-link-re
  4489. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4490. org-angle-link-re "\\)\\|\\("
  4491. org-plain-link-re "\\)")))
  4492. (org-make-link-regexps)
  4493. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4494. "Regular expression for fast time stamp matching.")
  4495. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?\\)[]>]"
  4496. "Regular expression for fast time stamp matching.")
  4497. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4498. "Regular expression matching time strings for analysis.
  4499. This one does not require the space after the date, so it can be used
  4500. on a string that terminates immediately after the date.")
  4501. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4502. "Regular expression matching time strings for analysis.")
  4503. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4504. "Regular expression matching time stamps, with groups.")
  4505. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4506. "Regular expression matching time stamps (also [..]), with groups.")
  4507. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4508. "Regular expression matching a time stamp range.")
  4509. (defconst org-tr-regexp-both
  4510. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4511. "Regular expression matching a time stamp range.")
  4512. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4513. org-ts-regexp "\\)?")
  4514. "Regular expression matching a time stamp or time stamp range.")
  4515. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4516. org-ts-regexp-both "\\)?")
  4517. "Regular expression matching a time stamp or time stamp range.
  4518. The time stamps may be either active or inactive.")
  4519. (defvar org-emph-face nil)
  4520. (defun org-do-emphasis-faces (limit)
  4521. "Run through the buffer and add overlays to emphasised strings."
  4522. (let (rtn a)
  4523. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4524. (if (not (= (char-after (match-beginning 3))
  4525. (char-after (match-beginning 4))))
  4526. (progn
  4527. (setq rtn t)
  4528. (setq a (assoc (match-string 3) org-emphasis-alist))
  4529. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4530. 'face
  4531. (nth 1 a))
  4532. (and (nth 4 a)
  4533. (org-remove-flyspell-overlays-in
  4534. (match-beginning 0) (match-end 0)))
  4535. (add-text-properties (match-beginning 2) (match-end 2)
  4536. '(font-lock-multiline t org-emphasis t))
  4537. (when org-hide-emphasis-markers
  4538. (add-text-properties (match-end 4) (match-beginning 5)
  4539. '(invisible org-link))
  4540. (add-text-properties (match-beginning 3) (match-end 3)
  4541. '(invisible org-link)))))
  4542. (backward-char 1))
  4543. rtn))
  4544. (defun org-emphasize (&optional char)
  4545. "Insert or change an emphasis, i.e. a font like bold or italic.
  4546. If there is an active region, change that region to a new emphasis.
  4547. If there is no region, just insert the marker characters and position
  4548. the cursor between them.
  4549. CHAR should be either the marker character, or the first character of the
  4550. HTML tag associated with that emphasis. If CHAR is a space, the means
  4551. to remove the emphasis of the selected region.
  4552. If char is not given (for example in an interactive call) it
  4553. will be prompted for."
  4554. (interactive)
  4555. (let ((eal org-emphasis-alist) e det
  4556. (erc org-emphasis-regexp-components)
  4557. (prompt "")
  4558. (string "") beg end move tag c s)
  4559. (if (org-region-active-p)
  4560. (setq beg (region-beginning) end (region-end)
  4561. string (buffer-substring beg end))
  4562. (setq move t))
  4563. (while (setq e (pop eal))
  4564. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4565. c (aref tag 0))
  4566. (push (cons c (string-to-char (car e))) det)
  4567. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4568. (substring tag 1)))))
  4569. (setq det (nreverse det))
  4570. (unless char
  4571. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4572. (setq char (read-char-exclusive)))
  4573. (setq char (or (cdr (assoc char det)) char))
  4574. (if (equal char ?\ )
  4575. (setq s "" move nil)
  4576. (unless (assoc (char-to-string char) org-emphasis-alist)
  4577. (error "No such emphasis marker: \"%c\"" char))
  4578. (setq s (char-to-string char)))
  4579. (while (and (> (length string) 1)
  4580. (equal (substring string 0 1) (substring string -1))
  4581. (assoc (substring string 0 1) org-emphasis-alist))
  4582. (setq string (substring string 1 -1)))
  4583. (setq string (concat s string s))
  4584. (if beg (delete-region beg end))
  4585. (unless (or (bolp)
  4586. (string-match (concat "[" (nth 0 erc) "\n]")
  4587. (char-to-string (char-before (point)))))
  4588. (insert " "))
  4589. (unless (or (eobp)
  4590. (string-match (concat "[" (nth 1 erc) "\n]")
  4591. (char-to-string (char-after (point)))))
  4592. (insert " ") (backward-char 1))
  4593. (insert string)
  4594. (and move (backward-char 1))))
  4595. (defconst org-nonsticky-props
  4596. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4597. (defsubst org-rear-nonsticky-at (pos)
  4598. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4599. (defun org-activate-plain-links (limit)
  4600. "Run through the buffer and add overlays to links."
  4601. (catch 'exit
  4602. (let (f)
  4603. (if (re-search-forward org-plain-link-re limit t)
  4604. (progn
  4605. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4606. (setq f (get-text-property (match-beginning 0) 'face))
  4607. (if (or (eq f 'org-tag)
  4608. (and (listp f) (memq 'org-tag f)))
  4609. nil
  4610. (add-text-properties (match-beginning 0) (match-end 0)
  4611. (list 'mouse-face 'highlight
  4612. 'face 'org-link
  4613. 'keymap org-mouse-map))
  4614. (org-rear-nonsticky-at (match-end 0)))
  4615. t)))))
  4616. (defun org-activate-code (limit)
  4617. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4618. (progn
  4619. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4620. (remove-text-properties (match-beginning 0) (match-end 0)
  4621. '(display t invisible t intangible t))
  4622. t)))
  4623. (defcustom org-src-fontify-natively nil
  4624. "When non-nil, fontify code in code blocks."
  4625. :type 'boolean
  4626. :group 'org-appearance
  4627. :group 'org-babel)
  4628. (defun org-fontify-meta-lines-and-blocks (limit)
  4629. (condition-case nil
  4630. (org-fontify-meta-lines-and-blocks-1 limit)
  4631. (error (message "org-mode fontification error"))))
  4632. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4633. "Fontify #+ lines and blocks, in the correct ways."
  4634. (let ((case-fold-search t))
  4635. (if (re-search-forward
  4636. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4637. limit t)
  4638. (let ((beg (match-beginning 0))
  4639. (block-start (match-end 0))
  4640. (block-end nil)
  4641. (lang (match-string 7))
  4642. (beg1 (line-beginning-position 2))
  4643. (dc1 (downcase (match-string 2)))
  4644. (dc3 (downcase (match-string 3)))
  4645. end end1 quoting block-type ovl)
  4646. (cond
  4647. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4648. ;; a single line of backend-specific content
  4649. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4650. (remove-text-properties (match-beginning 0) (match-end 0)
  4651. '(display t invisible t intangible t))
  4652. (add-text-properties (match-beginning 1) (match-end 3)
  4653. '(font-lock-fontified t face org-meta-line))
  4654. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4655. '(font-lock-fontified t face org-block))
  4656. ; for backend-specific code
  4657. t)
  4658. ((and (match-end 4) (equal dc3 "begin"))
  4659. ;; Truly a block
  4660. (setq block-type (downcase (match-string 5))
  4661. quoting (member block-type org-protecting-blocks))
  4662. (when (re-search-forward
  4663. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4664. nil t) ;; on purpose, we look further than LIMIT
  4665. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4666. (setq block-end (match-beginning 0))
  4667. (when quoting
  4668. (remove-text-properties beg end
  4669. '(display t invisible t intangible t)))
  4670. (add-text-properties
  4671. beg end
  4672. '(font-lock-fontified t font-lock-multiline t))
  4673. (add-text-properties beg beg1 '(face org-meta-line))
  4674. (add-text-properties end1 (min (point-max) (1+ end))
  4675. '(face org-meta-line)) ; for end_src
  4676. (cond
  4677. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4678. (org-src-font-lock-fontify-block lang block-start block-end)
  4679. ;; remove old background overlays
  4680. (mapc (lambda (ov)
  4681. (if (eq (overlay-get ov 'face) 'org-block-background)
  4682. (delete-overlay ov)))
  4683. (overlays-at (/ (+ beg1 block-end) 2)))
  4684. ;; add a background overlay
  4685. (setq ovl (make-overlay beg1 block-end))
  4686. (overlay-put ovl 'face 'org-block-background)
  4687. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4688. (quoting
  4689. (add-text-properties beg1 (min (point-max) (1+ end1))
  4690. '(face org-block))) ; end of source block
  4691. ((not org-fontify-quote-and-verse-blocks))
  4692. ((string= block-type "quote")
  4693. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4694. ((string= block-type "verse")
  4695. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4696. (add-text-properties beg beg1 '(face org-block-begin-line))
  4697. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4698. t))
  4699. ((member dc1 '("title:" "author:" "email:" "date:"))
  4700. (add-text-properties
  4701. beg (match-end 3)
  4702. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4703. '(font-lock-fontified t invisible t)
  4704. '(font-lock-fontified t face org-document-info-keyword)))
  4705. (add-text-properties
  4706. (match-beginning 6) (match-end 6)
  4707. (if (string-equal dc1 "title:")
  4708. '(font-lock-fontified t face org-document-title)
  4709. '(font-lock-fontified t face org-document-info))))
  4710. ((not (member (char-after beg) '(?\ ?\t)))
  4711. ;; just any other in-buffer setting, but not indented
  4712. (add-text-properties
  4713. beg (1+ (match-end 0))
  4714. '(font-lock-fontified t face org-meta-line))
  4715. t)
  4716. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4717. "orgtbl:" "tblfm:" "tblname:" "result:"
  4718. "results:" "source:" "srcname:" "call:"
  4719. "data:" "header:" "headers:"))
  4720. (and (match-end 4) (equal dc3 "attr")))
  4721. (add-text-properties
  4722. beg (match-end 0)
  4723. '(font-lock-fontified t face org-meta-line))
  4724. t)
  4725. ((member dc3 '(" " ""))
  4726. (add-text-properties
  4727. beg (match-end 0)
  4728. '(font-lock-fontified t face font-lock-comment-face)))
  4729. (t nil))))))
  4730. (defun org-activate-angle-links (limit)
  4731. "Run through the buffer and add overlays to links."
  4732. (if (re-search-forward org-angle-link-re limit t)
  4733. (progn
  4734. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4735. (add-text-properties (match-beginning 0) (match-end 0)
  4736. (list 'mouse-face 'highlight
  4737. 'keymap org-mouse-map))
  4738. (org-rear-nonsticky-at (match-end 0))
  4739. t)))
  4740. (defun org-activate-footnote-links (limit)
  4741. "Run through the buffer and add overlays to footnotes."
  4742. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4743. (when fn
  4744. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4745. (org-remove-flyspell-overlays-in beg end)
  4746. (add-text-properties beg end
  4747. (list 'mouse-face 'highlight
  4748. 'keymap org-mouse-map
  4749. 'help-echo
  4750. (if (= (point-at-bol) beg)
  4751. "Footnote definition"
  4752. "Footnote reference")
  4753. 'font-lock-fontified t
  4754. 'font-lock-multiline t
  4755. 'face 'org-footnote))))))
  4756. (defun org-activate-bracket-links (limit)
  4757. "Run through the buffer and add overlays to bracketed links."
  4758. (if (re-search-forward org-bracket-link-regexp limit t)
  4759. (let* ((help (concat "LINK: "
  4760. (org-match-string-no-properties 1)))
  4761. ;; FIXME: above we should remove the escapes.
  4762. ;; but that requires another match, protecting match data,
  4763. ;; a lot of overhead for font-lock.
  4764. (ip (org-maybe-intangible
  4765. (list 'invisible 'org-link
  4766. 'keymap org-mouse-map 'mouse-face 'highlight
  4767. 'font-lock-multiline t 'help-echo help)))
  4768. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4769. 'font-lock-multiline t 'help-echo help)))
  4770. ;; We need to remove the invisible property here. Table narrowing
  4771. ;; may have made some of this invisible.
  4772. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4773. (remove-text-properties (match-beginning 0) (match-end 0)
  4774. '(invisible nil))
  4775. (if (match-end 3)
  4776. (progn
  4777. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4778. (org-rear-nonsticky-at (match-beginning 3))
  4779. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4780. (org-rear-nonsticky-at (match-end 3))
  4781. (add-text-properties (match-end 3) (match-end 0) ip)
  4782. (org-rear-nonsticky-at (match-end 0)))
  4783. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4784. (org-rear-nonsticky-at (match-beginning 1))
  4785. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4786. (org-rear-nonsticky-at (match-end 1))
  4787. (add-text-properties (match-end 1) (match-end 0) ip)
  4788. (org-rear-nonsticky-at (match-end 0)))
  4789. t)))
  4790. (defun org-activate-dates (limit)
  4791. "Run through the buffer and add overlays to dates."
  4792. (if (re-search-forward org-tsr-regexp-both limit t)
  4793. (progn
  4794. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4795. (add-text-properties (match-beginning 0) (match-end 0)
  4796. (list 'mouse-face 'highlight
  4797. 'keymap org-mouse-map))
  4798. (org-rear-nonsticky-at (match-end 0))
  4799. (when org-display-custom-times
  4800. (if (match-end 3)
  4801. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4802. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4803. t)))
  4804. (defvar org-target-link-regexp nil
  4805. "Regular expression matching radio targets in plain text.")
  4806. (make-variable-buffer-local 'org-target-link-regexp)
  4807. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4808. "Regular expression matching a link target.")
  4809. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4810. "Regular expression matching a radio target.")
  4811. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4812. "Regular expression matching any target.")
  4813. (defun org-activate-target-links (limit)
  4814. "Run through the buffer and add overlays to target matches."
  4815. (when org-target-link-regexp
  4816. (let ((case-fold-search t))
  4817. (if (re-search-forward org-target-link-regexp limit t)
  4818. (progn
  4819. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4820. (add-text-properties (match-beginning 0) (match-end 0)
  4821. (list 'mouse-face 'highlight
  4822. 'keymap org-mouse-map
  4823. 'help-echo "Radio target link"
  4824. 'org-linked-text t))
  4825. (org-rear-nonsticky-at (match-end 0))
  4826. t)))))
  4827. (defun org-update-radio-target-regexp ()
  4828. "Find all radio targets in this file and update the regular expression."
  4829. (interactive)
  4830. (when (memq 'radio org-activate-links)
  4831. (setq org-target-link-regexp
  4832. (org-make-target-link-regexp (org-all-targets 'radio)))
  4833. (org-restart-font-lock)))
  4834. (defun org-hide-wide-columns (limit)
  4835. (let (s e)
  4836. (setq s (text-property-any (point) (or limit (point-max))
  4837. 'org-cwidth t))
  4838. (when s
  4839. (setq e (next-single-property-change s 'org-cwidth))
  4840. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4841. (goto-char e)
  4842. t)))
  4843. (defvar org-latex-and-specials-regexp nil
  4844. "Regular expression for highlighting export special stuff.")
  4845. (defvar org-match-substring-regexp)
  4846. (defvar org-match-substring-with-braces-regexp)
  4847. ;; This should be with the exporter code, but we also use if for font-locking
  4848. (defconst org-export-html-special-string-regexps
  4849. '(("\\\\-" . "&shy;")
  4850. ("---\\([^-]\\)" . "&mdash;\\1")
  4851. ("--\\([^-]\\)" . "&ndash;\\1")
  4852. ("\\.\\.\\." . "&hellip;"))
  4853. "Regular expressions for special string conversion.")
  4854. (defun org-compute-latex-and-specials-regexp ()
  4855. "Compute regular expression for stuff treated specially by exporters."
  4856. (if (not org-highlight-latex-fragments-and-specials)
  4857. (org-set-local 'org-latex-and-specials-regexp nil)
  4858. (require 'org-exp)
  4859. (let*
  4860. ((matchers (plist-get org-format-latex-options :matchers))
  4861. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4862. org-latex-regexps)))
  4863. (org-export-allow-BIND nil)
  4864. (options (org-combine-plists (org-default-export-plist)
  4865. (org-infile-export-plist)))
  4866. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4867. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4868. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4869. (org-export-html-expand (plist-get options :expand-quoted-html))
  4870. (org-export-with-special-strings (plist-get options :special-strings))
  4871. (re-sub
  4872. (cond
  4873. ((equal org-export-with-sub-superscripts '{})
  4874. (list org-match-substring-with-braces-regexp))
  4875. (org-export-with-sub-superscripts
  4876. (list org-match-substring-regexp))
  4877. (t nil)))
  4878. (re-latex
  4879. (if org-export-with-LaTeX-fragments
  4880. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4881. (re-macros
  4882. (if org-export-with-TeX-macros
  4883. (list (concat "\\\\"
  4884. (regexp-opt
  4885. (append
  4886. (delq nil
  4887. (mapcar 'car-safe
  4888. (append org-entities-user
  4889. org-entities)))
  4890. (if (boundp 'org-latex-entities)
  4891. (mapcar (lambda (x)
  4892. (or (car-safe x) x))
  4893. org-latex-entities)
  4894. nil))
  4895. 'words))) ; FIXME
  4896. ))
  4897. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4898. (re-special (if org-export-with-special-strings
  4899. (mapcar (lambda (x) (car x))
  4900. org-export-html-special-string-regexps)))
  4901. (re-rest
  4902. (delq nil
  4903. (list
  4904. (if org-export-html-expand "@<[^>\n]+>")
  4905. ))))
  4906. (org-set-local
  4907. 'org-latex-and-specials-regexp
  4908. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4909. re-rest) "\\|")))))
  4910. (defun org-do-latex-and-special-faces (limit)
  4911. "Run through the buffer and add overlays to links."
  4912. (when org-latex-and-specials-regexp
  4913. (let (rtn d)
  4914. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4915. limit t))
  4916. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4917. 'face))
  4918. '(org-code org-verbatim underline)))
  4919. (progn
  4920. (setq rtn t
  4921. d (cond ((member (char-after (1+ (match-beginning 0)))
  4922. '(?_ ?^)) 1)
  4923. (t 0)))
  4924. (font-lock-prepend-text-property
  4925. (+ d (match-beginning 0)) (match-end 0)
  4926. 'face 'org-latex-and-export-specials)
  4927. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4928. '(font-lock-multiline t)))))
  4929. rtn)))
  4930. (defun org-restart-font-lock ()
  4931. "Restart `font-lock-mode', to force refontification."
  4932. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4933. (font-lock-mode -1)
  4934. (font-lock-mode 1)))
  4935. (defun org-all-targets (&optional radio)
  4936. "Return a list of all targets in this file.
  4937. With optional argument RADIO, only find radio targets."
  4938. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4939. rtn)
  4940. (save-excursion
  4941. (goto-char (point-min))
  4942. (while (re-search-forward re nil t)
  4943. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4944. rtn)))
  4945. (defun org-make-target-link-regexp (targets)
  4946. "Make regular expression matching all strings in TARGETS.
  4947. The regular expression finds the targets also if there is a line break
  4948. between words."
  4949. (and targets
  4950. (concat
  4951. "\\<\\("
  4952. (mapconcat
  4953. (lambda (x)
  4954. (setq x (regexp-quote x))
  4955. (while (string-match " +" x)
  4956. (setq x (replace-match "\\s-+" t t x)))
  4957. x)
  4958. targets
  4959. "\\|")
  4960. "\\)\\>")))
  4961. (defun org-activate-tags (limit)
  4962. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4963. (progn
  4964. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4965. (add-text-properties (match-beginning 1) (match-end 1)
  4966. (list 'mouse-face 'highlight
  4967. 'keymap org-mouse-map))
  4968. (org-rear-nonsticky-at (match-end 1))
  4969. t)))
  4970. (defun org-outline-level ()
  4971. "Compute the outline level of the heading at point.
  4972. This function assumes that the cursor is at the beginning of a line matched
  4973. by `outline-regexp'. Otherwise it returns garbage.
  4974. If this is called at a normal headline, the level is the number of stars.
  4975. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  4976. (save-excursion
  4977. (looking-at org-outline-regexp)
  4978. (1- (- (match-end 0) (match-beginning 0)))))
  4979. (defvar org-font-lock-keywords nil)
  4980. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4981. "Regular expression matching a property line.")
  4982. (defvar org-font-lock-hook nil
  4983. "Functions to be called for special font lock stuff.")
  4984. (defvar org-font-lock-set-keywords-hook nil
  4985. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4986. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4987. it is installed to be used by font lock. This can be useful if something
  4988. needs to be inserted at a specific position in the font-lock sequence.")
  4989. (defun org-font-lock-hook (limit)
  4990. (run-hook-with-args 'org-font-lock-hook limit))
  4991. (defun org-set-font-lock-defaults ()
  4992. (let* ((em org-fontify-emphasized-text)
  4993. (lk org-activate-links)
  4994. (org-font-lock-extra-keywords
  4995. (list
  4996. ;; Call the hook
  4997. '(org-font-lock-hook)
  4998. ;; Headlines
  4999. `(,(if org-fontify-whole-heading-line
  5000. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5001. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5002. (1 (org-get-level-face 1))
  5003. (2 (org-get-level-face 2))
  5004. (3 (org-get-level-face 3)))
  5005. ;; Table lines
  5006. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5007. (1 'org-table t))
  5008. ;; Table internals
  5009. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5010. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5011. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5012. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5013. ;; Drawers
  5014. (list org-drawer-regexp '(0 'org-special-keyword t))
  5015. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5016. ;; Properties
  5017. (list org-property-re
  5018. '(1 'org-special-keyword t)
  5019. '(3 'org-property-value t))
  5020. ;; Links
  5021. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5022. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5023. (if (memq 'plain lk) '(org-activate-plain-links))
  5024. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5025. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5026. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5027. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5028. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5029. '(org-hide-wide-columns (0 nil append))
  5030. ;; TODO lines
  5031. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  5032. '(1 (org-get-todo-face 1) t))
  5033. ;; DONE
  5034. (if org-fontify-done-headline
  5035. (list (concat "^[*]+ +\\<\\("
  5036. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5037. "\\)\\(.*\\)")
  5038. '(2 'org-headline-done t))
  5039. nil)
  5040. ;; Priorities
  5041. '(org-font-lock-add-priority-faces)
  5042. ;; Tags
  5043. '(org-font-lock-add-tag-faces)
  5044. ;; Special keywords
  5045. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5046. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5047. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5048. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5049. ;; Emphasis
  5050. (if em
  5051. (if (featurep 'xemacs)
  5052. '(org-do-emphasis-faces (0 nil append))
  5053. '(org-do-emphasis-faces)))
  5054. ;; Checkboxes
  5055. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5056. 1 'org-checkbox prepend)
  5057. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5058. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5059. (0 (org-get-checkbox-statistics-face) t)))
  5060. ;; Description list items
  5061. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5062. 1 'bold prepend)
  5063. ;; ARCHIVEd headings
  5064. (list (concat
  5065. org-outline-regexp-bol
  5066. "\\(.*:" org-archive-tag ":.*\\)")
  5067. '(1 'org-archived prepend))
  5068. ;; Specials
  5069. '(org-do-latex-and-special-faces)
  5070. '(org-fontify-entities)
  5071. '(org-raise-scripts)
  5072. ;; Code
  5073. '(org-activate-code (1 'org-code t))
  5074. ;; COMMENT
  5075. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5076. "\\|" org-quote-string "\\)\\>")
  5077. '(1 'org-special-keyword t))
  5078. '("^#.*" (0 'font-lock-comment-face t))
  5079. ;; Blocks and meta lines
  5080. '(org-fontify-meta-lines-and-blocks)
  5081. )))
  5082. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5083. (run-hooks 'org-font-lock-set-keywords-hook)
  5084. ;; Now set the full font-lock-keywords
  5085. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5086. (org-set-local 'font-lock-defaults
  5087. '(org-font-lock-keywords t nil nil backward-paragraph))
  5088. (kill-local-variable 'font-lock-keywords) nil))
  5089. (defun org-toggle-pretty-entities ()
  5090. "Toggle the composition display of entities as UTF8 characters."
  5091. (interactive)
  5092. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5093. (org-restart-font-lock)
  5094. (if org-pretty-entities
  5095. (message "Entities are displayed as UTF8 characers")
  5096. (save-restriction
  5097. (widen)
  5098. (org-decompose-region (point-min) (point-max))
  5099. (message "Entities are displayed plain"))))
  5100. (defun org-fontify-entities (limit)
  5101. "Find an entity to fontify."
  5102. (let (ee)
  5103. (when org-pretty-entities
  5104. (catch 'match
  5105. (while (re-search-forward
  5106. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  5107. limit t)
  5108. (if (and (not (org-in-indented-comment-line))
  5109. (setq ee (org-entity-get (match-string 1)))
  5110. (= (length (nth 6 ee)) 1))
  5111. (progn
  5112. (add-text-properties
  5113. (match-beginning 0) (match-end 1)
  5114. (list 'font-lock-fontified t))
  5115. (compose-region (match-beginning 0) (match-end 1)
  5116. (nth 6 ee) nil)
  5117. (backward-char 1)
  5118. (throw 'match t))))
  5119. nil))))
  5120. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5121. "Fontify string S like in Org-mode."
  5122. (with-temp-buffer
  5123. (insert s)
  5124. (let ((org-odd-levels-only odd-levels))
  5125. (org-mode)
  5126. (font-lock-fontify-buffer)
  5127. (buffer-string))))
  5128. (defvar org-m nil)
  5129. (defvar org-l nil)
  5130. (defvar org-f nil)
  5131. (defun org-get-level-face (n)
  5132. "Get the right face for match N in font-lock matching of headlines."
  5133. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5134. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5135. (if org-cycle-level-faces
  5136. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5137. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5138. (cond
  5139. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5140. ((eq n 2) org-f)
  5141. (t (if org-level-color-stars-only nil org-f))))
  5142. (defun org-get-todo-face (kwd)
  5143. "Get the right face for a TODO keyword KWD.
  5144. If KWD is a number, get the corresponding match group."
  5145. (if (numberp kwd) (setq kwd (match-string kwd)))
  5146. (or (org-face-from-face-or-color
  5147. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5148. (and (member kwd org-done-keywords) 'org-done)
  5149. 'org-todo))
  5150. (defun org-face-from-face-or-color (context inherit face-or-color)
  5151. "Create a face list that inherits INHERIT, but sets the foreground color.
  5152. When FACE-OR-COLOR is not a string, just return it."
  5153. (if (stringp face-or-color)
  5154. (list :inherit inherit
  5155. (cdr (assoc context org-faces-easy-properties))
  5156. face-or-color)
  5157. face-or-color))
  5158. (defun org-font-lock-add-tag-faces (limit)
  5159. "Add the special tag faces."
  5160. (when (and org-tag-faces org-tags-special-faces-re)
  5161. (while (re-search-forward org-tags-special-faces-re limit t)
  5162. (add-text-properties (match-beginning 1) (match-end 1)
  5163. (list 'face (org-get-tag-face 1)
  5164. 'font-lock-fontified t))
  5165. (backward-char 1))))
  5166. (defun org-font-lock-add-priority-faces (limit)
  5167. "Add the special priority faces."
  5168. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5169. (add-text-properties
  5170. (match-beginning 0) (match-end 0)
  5171. (list 'face (or (org-face-from-face-or-color
  5172. 'priority 'org-special-keyword
  5173. (cdr (assoc (char-after (match-beginning 1))
  5174. org-priority-faces)))
  5175. 'org-special-keyword)
  5176. 'font-lock-fontified t))))
  5177. (defun org-get-tag-face (kwd)
  5178. "Get the right face for a TODO keyword KWD.
  5179. If KWD is a number, get the corresponding match group."
  5180. (if (numberp kwd) (setq kwd (match-string kwd)))
  5181. (or (org-face-from-face-or-color
  5182. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5183. 'org-tag))
  5184. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5185. "Remove fontification and activation overlays from links."
  5186. (font-lock-default-unfontify-region beg end)
  5187. (let* ((buffer-undo-list t)
  5188. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5189. (inhibit-modification-hooks t)
  5190. deactivate-mark buffer-file-name buffer-file-truename)
  5191. (org-decompose-region beg end)
  5192. (remove-text-properties beg end
  5193. '(mouse-face t keymap t org-linked-text t
  5194. invisible t intangible t
  5195. org-no-flyspell t org-emphasis t))
  5196. (org-remove-font-lock-display-properties beg end)))
  5197. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5198. ((raise 0.3) (height 0.7))
  5199. ((raise -0.5))
  5200. ((raise 0.5)))
  5201. "Display properties for showing superscripts and subscripts.")
  5202. (defun org-remove-font-lock-display-properties (beg end)
  5203. "Remove specific display properties that have been added by font lock.
  5204. The will remove the raise properties that are used to show superscripts
  5205. and subscripts."
  5206. (let (next prop)
  5207. (while (< beg end)
  5208. (setq next (next-single-property-change beg 'display nil end)
  5209. prop (get-text-property beg 'display))
  5210. (if (member prop org-script-display)
  5211. (put-text-property beg next 'display nil))
  5212. (setq beg next))))
  5213. (defun org-raise-scripts (limit)
  5214. "Add raise properties to sub/superscripts."
  5215. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5216. (if (re-search-forward
  5217. (if (eq org-use-sub-superscripts t)
  5218. org-match-substring-regexp
  5219. org-match-substring-with-braces-regexp)
  5220. limit t)
  5221. (let* ((pos (point)) table-p comment-p
  5222. (mpos (match-beginning 3))
  5223. (emph-p (get-text-property mpos 'org-emphasis))
  5224. (link-p (get-text-property mpos 'mouse-face))
  5225. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5226. (goto-char (point-at-bol))
  5227. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5228. comment-p (org-looking-at-p "[ \t]*#"))
  5229. (goto-char pos)
  5230. ;; FIXME: Should we go back one character here, for a_b^c
  5231. ;; (goto-char (1- pos)) ;????????????????????
  5232. (if (or comment-p emph-p link-p keyw-p)
  5233. t
  5234. (put-text-property (match-beginning 3) (match-end 0)
  5235. 'display
  5236. (if (equal (char-after (match-beginning 2)) ?^)
  5237. (nth (if table-p 3 1) org-script-display)
  5238. (nth (if table-p 2 0) org-script-display)))
  5239. (add-text-properties (match-beginning 2) (match-end 2)
  5240. (list 'invisible t
  5241. 'org-dwidth t 'org-dwidth-n 1))
  5242. (if (and (eq (char-after (match-beginning 3)) ?{)
  5243. (eq (char-before (match-end 3)) ?}))
  5244. (progn
  5245. (add-text-properties
  5246. (match-beginning 3) (1+ (match-beginning 3))
  5247. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5248. (add-text-properties
  5249. (1- (match-end 3)) (match-end 3)
  5250. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5251. t)))))
  5252. ;;;; Visibility cycling, including org-goto and indirect buffer
  5253. ;;; Cycling
  5254. (defvar org-cycle-global-status nil)
  5255. (make-variable-buffer-local 'org-cycle-global-status)
  5256. (defvar org-cycle-subtree-status nil)
  5257. (make-variable-buffer-local 'org-cycle-subtree-status)
  5258. ;;;###autoload
  5259. (defvar org-inlinetask-min-level)
  5260. (defun org-cycle (&optional arg)
  5261. "TAB-action and visibility cycling for Org-mode.
  5262. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5263. is outline visibility cycling, but it also invokes other actions
  5264. in special contexts.
  5265. - When this function is called with a prefix argument, rotate the entire
  5266. buffer through 3 states (global cycling)
  5267. 1. OVERVIEW: Show only top-level headlines.
  5268. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5269. 3. SHOW ALL: Show everything.
  5270. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5271. determined by the variable `org-startup-folded', and by any VISIBILITY
  5272. properties in the buffer.
  5273. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5274. including any drawers.
  5275. - When inside a table, re-align the table and move to the next field.
  5276. - When point is at the beginning of a headline, rotate the subtree started
  5277. by this line through 3 different states (local cycling)
  5278. 1. FOLDED: Only the main headline is shown.
  5279. 2. CHILDREN: The main headline and the direct children are shown.
  5280. From this state, you can move to one of the children
  5281. and zoom in further.
  5282. 3. SUBTREE: Show the entire subtree, including body text.
  5283. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5284. - When point is at the beginning of an empty headline and the variable
  5285. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5286. of the headline by demoting and promoting it to likely levels. This
  5287. speeds up creation document structure by pressing TAB once or several
  5288. times right after creating a new headline.
  5289. - When there is a numeric prefix, go up to a heading with level ARG, do
  5290. a `show-subtree' and return to the previous cursor position. If ARG
  5291. is negative, go up that many levels.
  5292. - When point is not at the beginning of a headline, execute the global
  5293. binding for TAB, which is re-indenting the line. See the option
  5294. `org-cycle-emulate-tab' for details.
  5295. - Special case: if point is at the beginning of the buffer and there is
  5296. no headline in line 1, this function will act as if called with prefix arg
  5297. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5298. But only if also the variable `org-cycle-global-at-bob' is t."
  5299. (interactive "P")
  5300. (org-load-modules-maybe)
  5301. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5302. (and org-cycle-level-after-item/entry-creation
  5303. (or (org-cycle-level)
  5304. (org-cycle-item-indentation))))
  5305. (let* ((limit-level
  5306. (or org-cycle-max-level
  5307. (and (boundp 'org-inlinetask-min-level)
  5308. org-inlinetask-min-level
  5309. (1- org-inlinetask-min-level))))
  5310. (nstars (and limit-level
  5311. (if org-odd-levels-only
  5312. (and limit-level (1- (* limit-level 2)))
  5313. limit-level)))
  5314. (org-outline-regexp
  5315. (if (not (org-mode-p))
  5316. outline-regexp
  5317. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5318. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5319. (not (looking-at org-outline-regexp))))
  5320. (org-cycle-hook
  5321. (if bob-special
  5322. (delq 'org-optimize-window-after-visibility-change
  5323. (copy-sequence org-cycle-hook))
  5324. org-cycle-hook))
  5325. (pos (point)))
  5326. (if (or bob-special (equal arg '(4)))
  5327. ;; special case: use global cycling
  5328. (setq arg t))
  5329. (cond
  5330. ((equal arg '(16))
  5331. (setq last-command 'dummy)
  5332. (org-set-startup-visibility)
  5333. (message "Startup visibility, plus VISIBILITY properties"))
  5334. ((equal arg '(64))
  5335. (show-all)
  5336. (message "Entire buffer visible, including drawers"))
  5337. ;; Table: enter it or move to the next field.
  5338. ((org-at-table-p 'any)
  5339. (if (org-at-table.el-p)
  5340. (message "Use C-c ' to edit table.el tables")
  5341. (if arg (org-table-edit-field t)
  5342. (org-table-justify-field-maybe)
  5343. (call-interactively 'org-table-next-field))))
  5344. ((run-hook-with-args-until-success
  5345. 'org-tab-after-check-for-table-hook))
  5346. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5347. ((eq arg t) (org-cycle-internal-global))
  5348. ;; Drawers: delegate to `org-flag-drawer'.
  5349. ((and org-drawers org-drawer-regexp
  5350. (save-excursion
  5351. (beginning-of-line 1)
  5352. (looking-at org-drawer-regexp)))
  5353. (org-flag-drawer ; toggle block visibility
  5354. (not (get-char-property (match-end 0) 'invisible))))
  5355. ;; Show-subtree, ARG levels up from here.
  5356. ((integerp arg)
  5357. (save-excursion
  5358. (org-back-to-heading)
  5359. (outline-up-heading (if (< arg 0) (- arg)
  5360. (- (funcall outline-level) arg)))
  5361. (org-show-subtree)))
  5362. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5363. ((and (featurep 'org-inlinetask)
  5364. (org-inlinetask-at-task-p)
  5365. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5366. (org-inlinetask-toggle-visibility))
  5367. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5368. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5369. (save-excursion (beginning-of-line 1)
  5370. (looking-at org-outline-regexp)))
  5371. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5372. (org-cycle-internal-local))
  5373. ;; From there: TAB emulation and template completion.
  5374. (buffer-read-only (org-back-to-heading))
  5375. ((run-hook-with-args-until-success
  5376. 'org-tab-after-check-for-cycling-hook))
  5377. ((org-try-structure-completion))
  5378. ((org-try-cdlatex-tab))
  5379. ((run-hook-with-args-until-success
  5380. 'org-tab-before-tab-emulation-hook))
  5381. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5382. (or (not (bolp))
  5383. (not (looking-at org-outline-regexp))))
  5384. (call-interactively (global-key-binding "\t")))
  5385. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5386. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5387. (or (and (eq org-cycle-emulate-tab 'white)
  5388. (= (match-end 0) (point-at-eol)))
  5389. (and (eq org-cycle-emulate-tab 'whitestart)
  5390. (>= (match-end 0) pos))))
  5391. t
  5392. (eq org-cycle-emulate-tab t))
  5393. (call-interactively (global-key-binding "\t")))
  5394. (t (save-excursion
  5395. (org-back-to-heading)
  5396. (org-cycle)))))))
  5397. (defun org-cycle-internal-global ()
  5398. "Do the global cycling action."
  5399. (cond
  5400. ((and (eq last-command this-command)
  5401. (eq org-cycle-global-status 'overview))
  5402. ;; We just created the overview - now do table of contents
  5403. ;; This can be slow in very large buffers, so indicate action
  5404. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5405. (message "CONTENTS...")
  5406. (org-content)
  5407. (message "CONTENTS...done")
  5408. (setq org-cycle-global-status 'contents)
  5409. (run-hook-with-args 'org-cycle-hook 'contents))
  5410. ((and (eq last-command this-command)
  5411. (eq org-cycle-global-status 'contents))
  5412. ;; We just showed the table of contents - now show everything
  5413. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5414. (show-all)
  5415. (message "SHOW ALL")
  5416. (setq org-cycle-global-status 'all)
  5417. (run-hook-with-args 'org-cycle-hook 'all))
  5418. (t
  5419. ;; Default action: go to overview
  5420. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5421. (org-overview)
  5422. (message "OVERVIEW")
  5423. (setq org-cycle-global-status 'overview)
  5424. (run-hook-with-args 'org-cycle-hook 'overview))))
  5425. (defun org-cycle-internal-local ()
  5426. "Do the local cycling action."
  5427. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5428. ;; First, determine end of headline (EOH), end of subtree or item
  5429. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5430. (save-excursion
  5431. (if (org-at-item-p)
  5432. (progn
  5433. (beginning-of-line)
  5434. (setq struct (org-list-struct))
  5435. (setq eoh (point-at-eol))
  5436. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5437. (setq has-children (org-list-has-child-p (point) struct)))
  5438. (org-back-to-heading)
  5439. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5440. (setq eos (save-excursion
  5441. (org-end-of-subtree t)
  5442. (unless (eobp)
  5443. (skip-chars-forward " \t\n"))
  5444. (if (eobp) (point) (1- (point)))))
  5445. (setq has-children
  5446. (or (save-excursion
  5447. (let ((level (funcall outline-level)))
  5448. (outline-next-heading)
  5449. (and (org-at-heading-p t)
  5450. (> (funcall outline-level) level))))
  5451. (save-excursion
  5452. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5453. ;; Determine end invisible part of buffer (EOL)
  5454. (beginning-of-line 2)
  5455. ;; XEmacs doesn't have `next-single-char-property-change'
  5456. (if (featurep 'xemacs)
  5457. (while (and (not (eobp)) ;; this is like `next-line'
  5458. (get-char-property (1- (point)) 'invisible))
  5459. (beginning-of-line 2))
  5460. (while (and (not (eobp)) ;; this is like `next-line'
  5461. (get-char-property (1- (point)) 'invisible))
  5462. (goto-char (next-single-char-property-change (point) 'invisible))
  5463. (and (eolp) (beginning-of-line 2))))
  5464. (setq eol (point)))
  5465. ;; Find out what to do next and set `this-command'
  5466. (cond
  5467. ((= eos eoh)
  5468. ;; Nothing is hidden behind this heading
  5469. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5470. (message "EMPTY ENTRY")
  5471. (setq org-cycle-subtree-status nil)
  5472. (save-excursion
  5473. (goto-char eos)
  5474. (outline-next-heading)
  5475. (if (outline-invisible-p) (org-flag-heading nil))))
  5476. ((and (or (>= eol eos)
  5477. (not (string-match "\\S-" (buffer-substring eol eos))))
  5478. (or has-children
  5479. (not (setq children-skipped
  5480. org-cycle-skip-children-state-if-no-children))))
  5481. ;; Entire subtree is hidden in one line: children view
  5482. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5483. (if (org-at-item-p)
  5484. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5485. (org-show-entry)
  5486. (show-children)
  5487. ;; Fold every list in subtree to top-level items.
  5488. (when (eq org-cycle-include-plain-lists 'integrate)
  5489. (save-excursion
  5490. (org-back-to-heading)
  5491. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5492. (beginning-of-line 1)
  5493. (let* ((struct (org-list-struct))
  5494. (prevs (org-list-prevs-alist struct))
  5495. (end (org-list-get-bottom-point struct)))
  5496. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5497. (org-list-get-all-items (point) struct prevs))
  5498. (goto-char end))))))
  5499. (message "CHILDREN")
  5500. (save-excursion
  5501. (goto-char eos)
  5502. (outline-next-heading)
  5503. (if (outline-invisible-p) (org-flag-heading nil)))
  5504. (setq org-cycle-subtree-status 'children)
  5505. (run-hook-with-args 'org-cycle-hook 'children))
  5506. ((or children-skipped
  5507. (and (eq last-command this-command)
  5508. (eq org-cycle-subtree-status 'children)))
  5509. ;; We just showed the children, or no children are there,
  5510. ;; now show everything.
  5511. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5512. (outline-flag-region eoh eos nil)
  5513. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5514. (setq org-cycle-subtree-status 'subtree)
  5515. (run-hook-with-args 'org-cycle-hook 'subtree))
  5516. (t
  5517. ;; Default action: hide the subtree.
  5518. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5519. (outline-flag-region eoh eos t)
  5520. (message "FOLDED")
  5521. (setq org-cycle-subtree-status 'folded)
  5522. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5523. ;;;###autoload
  5524. (defun org-global-cycle (&optional arg)
  5525. "Cycle the global visibility. For details see `org-cycle'.
  5526. With \\[universal-argument] prefix arg, switch to startup visibility.
  5527. With a numeric prefix, show all headlines up to that level."
  5528. (interactive "P")
  5529. (let ((org-cycle-include-plain-lists
  5530. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5531. (cond
  5532. ((integerp arg)
  5533. (show-all)
  5534. (hide-sublevels arg)
  5535. (setq org-cycle-global-status 'contents))
  5536. ((equal arg '(4))
  5537. (org-set-startup-visibility)
  5538. (message "Startup visibility, plus VISIBILITY properties."))
  5539. (t
  5540. (org-cycle '(4))))))
  5541. (defun org-set-startup-visibility ()
  5542. "Set the visibility required by startup options and properties."
  5543. (cond
  5544. ((eq org-startup-folded t)
  5545. (org-cycle '(4)))
  5546. ((eq org-startup-folded 'content)
  5547. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5548. (org-cycle '(4)) (org-cycle '(4)))))
  5549. (unless (eq org-startup-folded 'showeverything)
  5550. (if org-hide-block-startup (org-hide-block-all))
  5551. (org-set-visibility-according-to-property 'no-cleanup)
  5552. (org-cycle-hide-archived-subtrees 'all)
  5553. (org-cycle-hide-drawers 'all)
  5554. (org-cycle-show-empty-lines t)))
  5555. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5556. "Switch subtree visibilities according to :VISIBILITY: property."
  5557. (interactive)
  5558. (let (org-show-entry-below state)
  5559. (save-excursion
  5560. (goto-char (point-min))
  5561. (while (re-search-forward
  5562. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5563. nil t)
  5564. (setq state (match-string 1))
  5565. (save-excursion
  5566. (org-back-to-heading t)
  5567. (hide-subtree)
  5568. (org-reveal)
  5569. (cond
  5570. ((equal state '("fold" "folded"))
  5571. (hide-subtree))
  5572. ((equal state "children")
  5573. (org-show-hidden-entry)
  5574. (show-children))
  5575. ((equal state "content")
  5576. (save-excursion
  5577. (save-restriction
  5578. (org-narrow-to-subtree)
  5579. (org-content))))
  5580. ((member state '("all" "showall"))
  5581. (show-subtree)))))
  5582. (unless no-cleanup
  5583. (org-cycle-hide-archived-subtrees 'all)
  5584. (org-cycle-hide-drawers 'all)
  5585. (org-cycle-show-empty-lines 'all)))))
  5586. ;; This function uses outline-regexp instead of the more fundamental
  5587. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5588. ;; buffers, where outline-regexp is needed.
  5589. (defun org-overview ()
  5590. "Switch to overview mode, showing only top-level headlines.
  5591. Really, this shows all headlines with level equal or greater than the level
  5592. of the first headline in the buffer. This is important, because if the
  5593. first headline is not level one, then (hide-sublevels 1) gives confusing
  5594. results."
  5595. (interactive)
  5596. (let ((level (save-excursion
  5597. (goto-char (point-min))
  5598. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5599. (progn
  5600. (goto-char (match-beginning 0))
  5601. (funcall outline-level))))))
  5602. (and level (hide-sublevels level))))
  5603. (defun org-content (&optional arg)
  5604. "Show all headlines in the buffer, like a table of contents.
  5605. With numerical argument N, show content up to level N."
  5606. (interactive "P")
  5607. (save-excursion
  5608. ;; Visit all headings and show their offspring
  5609. (and (integerp arg) (org-overview))
  5610. (goto-char (point-max))
  5611. (catch 'exit
  5612. (while (and (progn (condition-case nil
  5613. (outline-previous-visible-heading 1)
  5614. (error (goto-char (point-min))))
  5615. t)
  5616. (looking-at org-outline-regexp))
  5617. (if (integerp arg)
  5618. (show-children (1- arg))
  5619. (show-branches))
  5620. (if (bobp) (throw 'exit nil))))))
  5621. (defun org-optimize-window-after-visibility-change (state)
  5622. "Adjust the window after a change in outline visibility.
  5623. This function is the default value of the hook `org-cycle-hook'."
  5624. (when (get-buffer-window (current-buffer))
  5625. (cond
  5626. ((eq state 'content) nil)
  5627. ((eq state 'all) nil)
  5628. ((eq state 'folded) nil)
  5629. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5630. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5631. (defun org-remove-empty-overlays-at (pos)
  5632. "Remove outline overlays that do not contain non-white stuff."
  5633. (mapc
  5634. (lambda (o)
  5635. (and (eq 'outline (overlay-get o 'invisible))
  5636. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5637. (overlay-end o))))
  5638. (delete-overlay o)))
  5639. (overlays-at pos)))
  5640. (defun org-clean-visibility-after-subtree-move ()
  5641. "Fix visibility issues after moving a subtree."
  5642. ;; First, find a reasonable region to look at:
  5643. ;; Start two siblings above, end three below
  5644. (let* ((beg (save-excursion
  5645. (and (org-get-last-sibling)
  5646. (org-get-last-sibling))
  5647. (point)))
  5648. (end (save-excursion
  5649. (and (org-get-next-sibling)
  5650. (org-get-next-sibling)
  5651. (org-get-next-sibling))
  5652. (if (org-at-heading-p)
  5653. (point-at-eol)
  5654. (point))))
  5655. (level (looking-at "\\*+"))
  5656. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5657. (save-excursion
  5658. (save-restriction
  5659. (narrow-to-region beg end)
  5660. (when re
  5661. ;; Properly fold already folded siblings
  5662. (goto-char (point-min))
  5663. (while (re-search-forward re nil t)
  5664. (if (and (not (outline-invisible-p))
  5665. (save-excursion
  5666. (goto-char (point-at-eol)) (outline-invisible-p)))
  5667. (hide-entry))))
  5668. (org-cycle-show-empty-lines 'overview)
  5669. (org-cycle-hide-drawers 'overview)))))
  5670. (defun org-cycle-show-empty-lines (state)
  5671. "Show empty lines above all visible headlines.
  5672. The region to be covered depends on STATE when called through
  5673. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5674. entire buffer covered. Note that an empty line is only shown if there
  5675. are at least `org-cycle-separator-lines' empty lines before the headline."
  5676. (when (not (= org-cycle-separator-lines 0))
  5677. (save-excursion
  5678. (let* ((n (abs org-cycle-separator-lines))
  5679. (re (cond
  5680. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5681. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5682. (t (let ((ns (number-to-string (- n 2))))
  5683. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5684. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5685. beg end b e)
  5686. (cond
  5687. ((memq state '(overview contents t))
  5688. (setq beg (point-min) end (point-max)))
  5689. ((memq state '(children folded))
  5690. (setq beg (point) end (progn (org-end-of-subtree t t)
  5691. (beginning-of-line 2)
  5692. (point)))))
  5693. (when beg
  5694. (goto-char beg)
  5695. (while (re-search-forward re end t)
  5696. (unless (get-char-property (match-end 1) 'invisible)
  5697. (setq e (match-end 1))
  5698. (if (< org-cycle-separator-lines 0)
  5699. (setq b (save-excursion
  5700. (goto-char (match-beginning 0))
  5701. (org-back-over-empty-lines)
  5702. (if (save-excursion
  5703. (goto-char (max (point-min) (1- (point))))
  5704. (org-on-heading-p))
  5705. (1- (point))
  5706. (point))))
  5707. (setq b (match-beginning 1)))
  5708. (outline-flag-region b e nil)))))))
  5709. ;; Never hide empty lines at the end of the file.
  5710. (save-excursion
  5711. (goto-char (point-max))
  5712. (outline-previous-heading)
  5713. (outline-end-of-heading)
  5714. (if (and (looking-at "[ \t\n]+")
  5715. (= (match-end 0) (point-max)))
  5716. (outline-flag-region (point) (match-end 0) nil))))
  5717. (defun org-show-empty-lines-in-parent ()
  5718. "Move to the parent and re-show empty lines before visible headlines."
  5719. (save-excursion
  5720. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5721. (org-cycle-show-empty-lines context))))
  5722. (defun org-files-list ()
  5723. "Return `org-agenda-files' list, plus all open org-mode files.
  5724. This is useful for operations that need to scan all of a user's
  5725. open and agenda-wise Org files."
  5726. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5727. (dolist (buf (buffer-list))
  5728. (with-current-buffer buf
  5729. (if (and (org-mode-p) (buffer-file-name))
  5730. (let ((file (expand-file-name (buffer-file-name))))
  5731. (unless (member file files)
  5732. (push file files))))))
  5733. files))
  5734. (defsubst org-entry-beginning-position ()
  5735. "Return the beginning position of the current entry."
  5736. (save-excursion (outline-back-to-heading t) (point)))
  5737. (defsubst org-entry-end-position ()
  5738. "Return the end position of the current entry."
  5739. (save-excursion (outline-next-heading) (point)))
  5740. (defun org-cycle-hide-drawers (state)
  5741. "Re-hide all drawers after a visibility state change."
  5742. (when (and (org-mode-p)
  5743. (not (memq state '(overview folded contents))))
  5744. (save-excursion
  5745. (let* ((globalp (memq state '(contents all)))
  5746. (beg (if globalp (point-min) (point)))
  5747. (end (if globalp (point-max)
  5748. (if (eq state 'children)
  5749. (save-excursion (outline-next-heading) (point))
  5750. (org-end-of-subtree t)))))
  5751. (goto-char beg)
  5752. (while (re-search-forward org-drawer-regexp end t)
  5753. (org-flag-drawer t))))))
  5754. (defun org-flag-drawer (flag)
  5755. (save-excursion
  5756. (beginning-of-line 1)
  5757. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5758. (let ((b (match-end 0)))
  5759. (if (re-search-forward
  5760. "^[ \t]*:END:"
  5761. (save-excursion (outline-next-heading) (point)) t)
  5762. (outline-flag-region b (point-at-eol) flag)
  5763. (error ":END: line missing at position %s" b))))))
  5764. (defun org-subtree-end-visible-p ()
  5765. "Is the end of the current subtree visible?"
  5766. (pos-visible-in-window-p
  5767. (save-excursion (org-end-of-subtree t) (point))))
  5768. (defun org-first-headline-recenter (&optional N)
  5769. "Move cursor to the first headline and recenter the headline.
  5770. Optional argument N means put the headline into the Nth line of the window."
  5771. (goto-char (point-min))
  5772. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5773. (beginning-of-line)
  5774. (recenter (prefix-numeric-value N))))
  5775. ;;; Saving and restoring visibility
  5776. (defun org-outline-overlay-data (&optional use-markers)
  5777. "Return a list of the locations of all outline overlays.
  5778. These are overlays with the `invisible' property value `outline'.
  5779. The return value is a list of cons cells, with start and stop
  5780. positions for each overlay.
  5781. If USE-MARKERS is set, return the positions as markers."
  5782. (let (beg end)
  5783. (save-excursion
  5784. (save-restriction
  5785. (widen)
  5786. (delq nil
  5787. (mapcar (lambda (o)
  5788. (when (eq (overlay-get o 'invisible) 'outline)
  5789. (setq beg (overlay-start o)
  5790. end (overlay-end o))
  5791. (and beg end (> end beg)
  5792. (if use-markers
  5793. (cons (move-marker (make-marker) beg)
  5794. (move-marker (make-marker) end))
  5795. (cons beg end)))))
  5796. (overlays-in (point-min) (point-max))))))))
  5797. (defun org-set-outline-overlay-data (data)
  5798. "Create visibility overlays for all positions in DATA.
  5799. DATA should have been made by `org-outline-overlay-data'."
  5800. (let (o)
  5801. (save-excursion
  5802. (save-restriction
  5803. (widen)
  5804. (show-all)
  5805. (mapc (lambda (c)
  5806. (setq o (make-overlay (car c) (cdr c)))
  5807. (overlay-put o 'invisible 'outline))
  5808. data)))))
  5809. ;;; Folding of blocks
  5810. (defconst org-block-regexp
  5811. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  5812. "Regular expression for hiding blocks.")
  5813. (defvar org-hide-block-overlays nil
  5814. "Overlays hiding blocks.")
  5815. (make-variable-buffer-local 'org-hide-block-overlays)
  5816. (defun org-block-map (function &optional start end)
  5817. "Call FUNCTION at the head of all source blocks in the current buffer.
  5818. Optional arguments START and END can be used to limit the range."
  5819. (let ((start (or start (point-min)))
  5820. (end (or end (point-max))))
  5821. (save-excursion
  5822. (goto-char start)
  5823. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5824. (save-excursion
  5825. (save-match-data
  5826. (goto-char (match-beginning 0))
  5827. (funcall function)))))))
  5828. (defun org-hide-block-toggle-all ()
  5829. "Toggle the visibility of all blocks in the current buffer."
  5830. (org-block-map #'org-hide-block-toggle))
  5831. (defun org-hide-block-all ()
  5832. "Fold all blocks in the current buffer."
  5833. (interactive)
  5834. (org-show-block-all)
  5835. (org-block-map #'org-hide-block-toggle-maybe))
  5836. (defun org-show-block-all ()
  5837. "Unfold all blocks in the current buffer."
  5838. (interactive)
  5839. (mapc 'delete-overlay org-hide-block-overlays)
  5840. (setq org-hide-block-overlays nil))
  5841. (defun org-hide-block-toggle-maybe ()
  5842. "Toggle visibility of block at point."
  5843. (interactive)
  5844. (let ((case-fold-search t))
  5845. (if (save-excursion
  5846. (beginning-of-line 1)
  5847. (looking-at org-block-regexp))
  5848. (progn (org-hide-block-toggle)
  5849. t) ;; to signal that we took action
  5850. nil))) ;; to signal that we did not
  5851. (defun org-hide-block-toggle (&optional force)
  5852. "Toggle the visibility of the current block."
  5853. (interactive)
  5854. (save-excursion
  5855. (beginning-of-line)
  5856. (if (re-search-forward org-block-regexp nil t)
  5857. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5858. (end (match-end 0)) ;; end of entire body
  5859. ov)
  5860. (if (memq t (mapcar (lambda (overlay)
  5861. (eq (overlay-get overlay 'invisible)
  5862. 'org-hide-block))
  5863. (overlays-at start)))
  5864. (if (or (not force) (eq force 'off))
  5865. (mapc (lambda (ov)
  5866. (when (member ov org-hide-block-overlays)
  5867. (setq org-hide-block-overlays
  5868. (delq ov org-hide-block-overlays)))
  5869. (when (eq (overlay-get ov 'invisible)
  5870. 'org-hide-block)
  5871. (delete-overlay ov)))
  5872. (overlays-at start)))
  5873. (setq ov (make-overlay start end))
  5874. (overlay-put ov 'invisible 'org-hide-block)
  5875. ;; make the block accessible to isearch
  5876. (overlay-put
  5877. ov 'isearch-open-invisible
  5878. (lambda (ov)
  5879. (when (member ov org-hide-block-overlays)
  5880. (setq org-hide-block-overlays
  5881. (delq ov org-hide-block-overlays)))
  5882. (when (eq (overlay-get ov 'invisible)
  5883. 'org-hide-block)
  5884. (delete-overlay ov))))
  5885. (push ov org-hide-block-overlays)))
  5886. (error "Not looking at a source block"))))
  5887. ;; org-tab-after-check-for-cycling-hook
  5888. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5889. ;; Remove overlays when changing major mode
  5890. (add-hook 'org-mode-hook
  5891. (lambda () (org-add-hook 'change-major-mode-hook
  5892. 'org-show-block-all 'append 'local)))
  5893. ;;; Org-goto
  5894. (defvar org-goto-window-configuration nil)
  5895. (defvar org-goto-marker nil)
  5896. (defvar org-goto-map
  5897. (let ((map (make-sparse-keymap)))
  5898. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5899. (while (setq cmd (pop cmds))
  5900. (substitute-key-definition cmd cmd map global-map)))
  5901. (suppress-keymap map)
  5902. (org-defkey map "\C-m" 'org-goto-ret)
  5903. (org-defkey map [(return)] 'org-goto-ret)
  5904. (org-defkey map [(left)] 'org-goto-left)
  5905. (org-defkey map [(right)] 'org-goto-right)
  5906. (org-defkey map [(control ?g)] 'org-goto-quit)
  5907. (org-defkey map "\C-i" 'org-cycle)
  5908. (org-defkey map [(tab)] 'org-cycle)
  5909. (org-defkey map [(down)] 'outline-next-visible-heading)
  5910. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5911. (if org-goto-auto-isearch
  5912. (if (fboundp 'define-key-after)
  5913. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5914. nil)
  5915. (org-defkey map "q" 'org-goto-quit)
  5916. (org-defkey map "n" 'outline-next-visible-heading)
  5917. (org-defkey map "p" 'outline-previous-visible-heading)
  5918. (org-defkey map "f" 'outline-forward-same-level)
  5919. (org-defkey map "b" 'outline-backward-same-level)
  5920. (org-defkey map "u" 'outline-up-heading))
  5921. (org-defkey map "/" 'org-occur)
  5922. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5923. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5924. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5925. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5926. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5927. map))
  5928. (defconst org-goto-help
  5929. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5930. RET=jump to location [Q]uit and return to previous location
  5931. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5932. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5933. ;; FIXME: Docstring does not mention both interfaces
  5934. (defun org-goto (&optional alternative-interface)
  5935. "Look up a different location in the current file, keeping current visibility.
  5936. When you want look-up or go to a different location in a document, the
  5937. fastest way is often to fold the entire buffer and then dive into the tree.
  5938. This method has the disadvantage, that the previous location will be folded,
  5939. which may not be what you want.
  5940. This command works around this by showing a copy of the current buffer
  5941. in an indirect buffer, in overview mode. You can dive into the tree in
  5942. that copy, use org-occur and incremental search to find a location.
  5943. When pressing RET or `Q', the command returns to the original buffer in
  5944. which the visibility is still unchanged. After RET is will also jump to
  5945. the location selected in the indirect buffer and expose the headline
  5946. hierarchy above."
  5947. (interactive "P")
  5948. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5949. (org-refile-use-outline-path t)
  5950. (org-refile-target-verify-function nil)
  5951. (interface
  5952. (if (not alternative-interface)
  5953. org-goto-interface
  5954. (if (eq org-goto-interface 'outline)
  5955. 'outline-path-completion
  5956. 'outline)))
  5957. (org-goto-start-pos (point))
  5958. (selected-point
  5959. (if (eq interface 'outline)
  5960. (car (org-get-location (current-buffer) org-goto-help))
  5961. (let ((pa (org-refile-get-location "Goto")))
  5962. (org-refile-check-position pa)
  5963. (nth 3 pa)))))
  5964. (if selected-point
  5965. (progn
  5966. (org-mark-ring-push org-goto-start-pos)
  5967. (goto-char selected-point)
  5968. (if (or (outline-invisible-p) (org-invisible-p2))
  5969. (org-show-context 'org-goto)))
  5970. (message "Quit"))))
  5971. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5972. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5973. (defvar org-goto-local-auto-isearch-map) ; defined below
  5974. (defun org-get-location (buf help)
  5975. "Let the user select a location in the Org-mode buffer BUF.
  5976. This function uses a recursive edit. It returns the selected position
  5977. or nil."
  5978. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5979. (isearch-hide-immediately nil)
  5980. (isearch-search-fun-function
  5981. (lambda () 'org-goto-local-search-headings))
  5982. (org-goto-selected-point org-goto-exit-command)
  5983. (pop-up-frames nil)
  5984. (special-display-buffer-names nil)
  5985. (special-display-regexps nil)
  5986. (special-display-function nil))
  5987. (save-excursion
  5988. (save-window-excursion
  5989. (delete-other-windows)
  5990. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5991. (org-pop-to-buffer-same-window
  5992. (condition-case nil
  5993. (make-indirect-buffer (current-buffer) "*org-goto*")
  5994. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5995. (with-output-to-temp-buffer "*Help*"
  5996. (princ help))
  5997. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5998. (setq buffer-read-only nil)
  5999. (let ((org-startup-truncated t)
  6000. (org-startup-folded nil)
  6001. (org-startup-align-all-tables nil))
  6002. (org-mode)
  6003. (org-overview))
  6004. (setq buffer-read-only t)
  6005. (if (and (boundp 'org-goto-start-pos)
  6006. (integer-or-marker-p org-goto-start-pos))
  6007. (let ((org-show-hierarchy-above t)
  6008. (org-show-siblings t)
  6009. (org-show-following-heading t))
  6010. (goto-char org-goto-start-pos)
  6011. (and (outline-invisible-p) (org-show-context)))
  6012. (goto-char (point-min)))
  6013. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6014. (message "Select location and press RET")
  6015. (use-local-map org-goto-map)
  6016. (recursive-edit)
  6017. ))
  6018. (kill-buffer "*org-goto*")
  6019. (cons org-goto-selected-point org-goto-exit-command)))
  6020. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6021. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6022. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6023. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6024. (defun org-goto-local-search-headings (string bound noerror)
  6025. "Search and make sure that any matches are in headlines."
  6026. (catch 'return
  6027. (while (if isearch-forward
  6028. (search-forward string bound noerror)
  6029. (search-backward string bound noerror))
  6030. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6031. (and (member :headline context)
  6032. (not (member :tags context))))
  6033. (throw 'return (point))))))
  6034. (defun org-goto-local-auto-isearch ()
  6035. "Start isearch."
  6036. (interactive)
  6037. (goto-char (point-min))
  6038. (let ((keys (this-command-keys)))
  6039. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6040. (isearch-mode t)
  6041. (isearch-process-search-char (string-to-char keys)))))
  6042. (defun org-goto-ret (&optional arg)
  6043. "Finish `org-goto' by going to the new location."
  6044. (interactive "P")
  6045. (setq org-goto-selected-point (point)
  6046. org-goto-exit-command 'return)
  6047. (throw 'exit nil))
  6048. (defun org-goto-left ()
  6049. "Finish `org-goto' by going to the new location."
  6050. (interactive)
  6051. (if (org-on-heading-p)
  6052. (progn
  6053. (beginning-of-line 1)
  6054. (setq org-goto-selected-point (point)
  6055. org-goto-exit-command 'left)
  6056. (throw 'exit nil))
  6057. (error "Not on a heading")))
  6058. (defun org-goto-right ()
  6059. "Finish `org-goto' by going to the new location."
  6060. (interactive)
  6061. (if (org-on-heading-p)
  6062. (progn
  6063. (setq org-goto-selected-point (point)
  6064. org-goto-exit-command 'right)
  6065. (throw 'exit nil))
  6066. (error "Not on a heading")))
  6067. (defun org-goto-quit ()
  6068. "Finish `org-goto' without cursor motion."
  6069. (interactive)
  6070. (setq org-goto-selected-point nil)
  6071. (setq org-goto-exit-command 'quit)
  6072. (throw 'exit nil))
  6073. ;;; Indirect buffer display of subtrees
  6074. (defvar org-indirect-dedicated-frame nil
  6075. "This is the frame being used for indirect tree display.")
  6076. (defvar org-last-indirect-buffer nil)
  6077. (defun org-tree-to-indirect-buffer (&optional arg)
  6078. "Create indirect buffer and narrow it to current subtree.
  6079. With numerical prefix ARG, go up to this level and then take that tree.
  6080. If ARG is negative, go up that many levels.
  6081. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6082. indirect buffer previously made with this command, to avoid proliferation of
  6083. indirect buffers. However, when you call the command with a \
  6084. \\[universal-argument] prefix, or
  6085. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6086. is kept so that you can work with several indirect buffers at the same time.
  6087. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6088. \\[universal-argument] prefix also
  6089. requests that a new frame be made for the new buffer, so that the dedicated
  6090. frame is not changed."
  6091. (interactive "P")
  6092. (let ((cbuf (current-buffer))
  6093. (cwin (selected-window))
  6094. (pos (point))
  6095. beg end level heading ibuf)
  6096. (save-excursion
  6097. (org-back-to-heading t)
  6098. (when (numberp arg)
  6099. (setq level (org-outline-level))
  6100. (if (< arg 0) (setq arg (+ level arg)))
  6101. (while (> (setq level (org-outline-level)) arg)
  6102. (outline-up-heading 1 t)))
  6103. (setq beg (point)
  6104. heading (org-get-heading))
  6105. (org-end-of-subtree t t)
  6106. (if (org-on-heading-p) (backward-char 1))
  6107. (setq end (point)))
  6108. (if (and (buffer-live-p org-last-indirect-buffer)
  6109. (not (eq org-indirect-buffer-display 'new-frame))
  6110. (not arg))
  6111. (kill-buffer org-last-indirect-buffer))
  6112. (setq ibuf (org-get-indirect-buffer cbuf)
  6113. org-last-indirect-buffer ibuf)
  6114. (cond
  6115. ((or (eq org-indirect-buffer-display 'new-frame)
  6116. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6117. (select-frame (make-frame))
  6118. (delete-other-windows)
  6119. (org-pop-to-buffer-same-window ibuf)
  6120. (org-set-frame-title heading))
  6121. ((eq org-indirect-buffer-display 'dedicated-frame)
  6122. (raise-frame
  6123. (select-frame (or (and org-indirect-dedicated-frame
  6124. (frame-live-p org-indirect-dedicated-frame)
  6125. org-indirect-dedicated-frame)
  6126. (setq org-indirect-dedicated-frame (make-frame)))))
  6127. (delete-other-windows)
  6128. (org-pop-to-buffer-same-window ibuf)
  6129. (org-set-frame-title (concat "Indirect: " heading)))
  6130. ((eq org-indirect-buffer-display 'current-window)
  6131. (org-pop-to-buffer-same-window ibuf))
  6132. ((eq org-indirect-buffer-display 'other-window)
  6133. (pop-to-buffer ibuf))
  6134. (t (error "Invalid value")))
  6135. (if (featurep 'xemacs)
  6136. (save-excursion (org-mode) (turn-on-font-lock)))
  6137. (narrow-to-region beg end)
  6138. (show-all)
  6139. (goto-char pos)
  6140. (and (window-live-p cwin) (select-window cwin))))
  6141. (defun org-get-indirect-buffer (&optional buffer)
  6142. (setq buffer (or buffer (current-buffer)))
  6143. (let ((n 1) (base (buffer-name buffer)) bname)
  6144. (while (buffer-live-p
  6145. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6146. (setq n (1+ n)))
  6147. (condition-case nil
  6148. (make-indirect-buffer buffer bname 'clone)
  6149. (error (make-indirect-buffer buffer bname)))))
  6150. (defun org-set-frame-title (title)
  6151. "Set the title of the current frame to the string TITLE."
  6152. ;; FIXME: how to name a single frame in XEmacs???
  6153. (unless (featurep 'xemacs)
  6154. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6155. ;;;; Structure editing
  6156. ;;; Inserting headlines
  6157. (defun org-previous-line-empty-p ()
  6158. (save-excursion
  6159. (and (not (bobp))
  6160. (or (beginning-of-line 0) t)
  6161. (save-match-data
  6162. (looking-at "[ \t]*$")))))
  6163. (defun org-insert-heading (&optional force-heading invisible-ok)
  6164. "Insert a new heading or item with same depth at point.
  6165. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6166. If point is at the beginning of a headline, insert a sibling before the
  6167. current headline. If point is not at the beginning, split the line,
  6168. create the new headline with the text in the current line after point
  6169. \(but see also the variable `org-M-RET-may-split-line').
  6170. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6171. This is important for non-interactive uses of the command."
  6172. (interactive "P")
  6173. (if (or (= (buffer-size) 0)
  6174. (and (not (save-excursion
  6175. (and (ignore-errors (org-back-to-heading invisible-ok))
  6176. (org-on-heading-p))))
  6177. (or force-heading (not (org-in-item-p)))))
  6178. (progn
  6179. (insert "\n* ")
  6180. (run-hooks 'org-insert-heading-hook))
  6181. (when (or force-heading (not (org-insert-item)))
  6182. (let* ((empty-line-p nil)
  6183. (level nil)
  6184. (on-heading (org-on-heading-p))
  6185. (head (save-excursion
  6186. (condition-case nil
  6187. (progn
  6188. (org-back-to-heading invisible-ok)
  6189. (when (and (not on-heading)
  6190. (featurep 'org-inlinetask)
  6191. (integerp org-inlinetask-min-level)
  6192. (>= (length (match-string 0))
  6193. org-inlinetask-min-level))
  6194. ;; Find a heading level before the inline task
  6195. (while (and (setq level (org-up-heading-safe))
  6196. (>= level org-inlinetask-min-level)))
  6197. (if (org-on-heading-p)
  6198. (org-back-to-heading invisible-ok)
  6199. (error "This should not happen")))
  6200. (setq empty-line-p (org-previous-line-empty-p))
  6201. (match-string 0))
  6202. (error "*"))))
  6203. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6204. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6205. pos hide-previous previous-pos)
  6206. (cond
  6207. ((and (org-on-heading-p) (bolp)
  6208. (or (bobp)
  6209. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6210. ;; insert before the current line
  6211. (open-line (if blank 2 1)))
  6212. ((and (bolp)
  6213. (not org-insert-heading-respect-content)
  6214. (or (bobp)
  6215. (save-excursion
  6216. (backward-char 1) (not (outline-invisible-p)))))
  6217. ;; insert right here
  6218. nil)
  6219. (t
  6220. ;; somewhere in the line
  6221. (save-excursion
  6222. (setq previous-pos (point-at-bol))
  6223. (end-of-line)
  6224. (setq hide-previous (outline-invisible-p)))
  6225. (and org-insert-heading-respect-content (org-show-subtree))
  6226. (let ((split
  6227. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6228. (save-excursion
  6229. (let ((p (point)))
  6230. (goto-char (point-at-bol))
  6231. (and (looking-at org-complex-heading-regexp)
  6232. (> p (match-beginning 4)))))))
  6233. tags pos)
  6234. (cond
  6235. (org-insert-heading-respect-content
  6236. (org-end-of-subtree nil t)
  6237. (when (featurep 'org-inlinetask)
  6238. (while (and (not (eobp))
  6239. (looking-at "\\(\\*+\\)[ \t]+")
  6240. (>= (length (match-string 1))
  6241. org-inlinetask-min-level))
  6242. (org-end-of-subtree nil t)))
  6243. (or (bolp) (newline))
  6244. (or (org-previous-line-empty-p)
  6245. (and blank (newline)))
  6246. (open-line 1))
  6247. ((org-on-heading-p)
  6248. (when hide-previous
  6249. (show-children)
  6250. (org-show-entry))
  6251. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6252. (setq tags (and (match-end 2) (match-string 2)))
  6253. (and (match-end 1)
  6254. (delete-region (match-beginning 1) (match-end 1)))
  6255. (setq pos (point-at-bol))
  6256. (or split (end-of-line 1))
  6257. (delete-horizontal-space)
  6258. (if (string-match "\\`\\*+\\'"
  6259. (buffer-substring (point-at-bol) (point)))
  6260. (insert " "))
  6261. (newline (if blank 2 1))
  6262. (when tags
  6263. (save-excursion
  6264. (goto-char pos)
  6265. (end-of-line 1)
  6266. (insert " " tags)
  6267. (org-set-tags nil 'align))))
  6268. (t
  6269. (or split (end-of-line 1))
  6270. (newline (if blank 2 1)))))))
  6271. (insert head) (just-one-space)
  6272. (setq pos (point))
  6273. (end-of-line 1)
  6274. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6275. (when (and org-insert-heading-respect-content hide-previous)
  6276. (save-excursion
  6277. (goto-char previous-pos)
  6278. (hide-subtree)))
  6279. (run-hooks 'org-insert-heading-hook)))))
  6280. (defun org-get-heading (&optional no-tags no-todo)
  6281. "Return the heading of the current entry, without the stars.
  6282. When NO-TAGS is non-nil, don't include tags.
  6283. When NO-TODO is non-nil, don't include TODO keywords."
  6284. (save-excursion
  6285. (org-back-to-heading t)
  6286. (cond
  6287. ((and no-tags no-todo)
  6288. (looking-at org-complex-heading-regexp)
  6289. (match-string 4))
  6290. (no-tags
  6291. (looking-at "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6292. (match-string 1))
  6293. (no-todo
  6294. (looking-at (concat "\\*+[ \t]+" org-todo-regexp " +"
  6295. "\\([^\n\r]*?[ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$"))
  6296. (match-string 2))
  6297. (t (looking-at "\\*+[ \t]+\\([^\r\n]*\\)")
  6298. (match-string 1)))))
  6299. (defun org-heading-components ()
  6300. "Return the components of the current heading.
  6301. This is a list with the following elements:
  6302. - the level as an integer
  6303. - the reduced level, different if `org-odd-levels-only' is set.
  6304. - the TODO keyword, or nil
  6305. - the priority character, like ?A, or nil if no priority is given
  6306. - the headline text itself, or the tags string if no headline text
  6307. - the tags string, or nil."
  6308. (save-excursion
  6309. (org-back-to-heading t)
  6310. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6311. (list (length (match-string 1))
  6312. (org-reduced-level (length (match-string 1)))
  6313. (org-match-string-no-properties 2)
  6314. (and (match-end 3) (aref (match-string 3) 2))
  6315. (org-match-string-no-properties 4)
  6316. (org-match-string-no-properties 5)))))
  6317. (defun org-get-entry ()
  6318. "Get the entry text, after heading, entire subtree."
  6319. (save-excursion
  6320. (org-back-to-heading t)
  6321. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6322. (defun org-insert-heading-after-current ()
  6323. "Insert a new heading with same level as current, after current subtree."
  6324. (interactive)
  6325. (org-back-to-heading)
  6326. (org-insert-heading)
  6327. (org-move-subtree-down)
  6328. (end-of-line 1))
  6329. (defun org-insert-heading-respect-content ()
  6330. (interactive)
  6331. (let ((org-insert-heading-respect-content t))
  6332. (org-insert-heading t)))
  6333. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6334. (interactive "P")
  6335. (let ((org-insert-heading-respect-content t))
  6336. (org-insert-todo-heading force-state t)))
  6337. (defun org-insert-todo-heading (arg &optional force-heading)
  6338. "Insert a new heading with the same level and TODO state as current heading.
  6339. If the heading has no TODO state, or if the state is DONE, use the first
  6340. state (TODO by default). Also with prefix arg, force first state."
  6341. (interactive "P")
  6342. (when (or force-heading (not (org-insert-item 'checkbox)))
  6343. (org-insert-heading force-heading)
  6344. (save-excursion
  6345. (org-back-to-heading)
  6346. (outline-previous-heading)
  6347. (looking-at org-todo-line-regexp))
  6348. (let*
  6349. ((new-mark-x
  6350. (if (or arg
  6351. (not (match-beginning 2))
  6352. (member (match-string 2) org-done-keywords))
  6353. (car org-todo-keywords-1)
  6354. (match-string 2)))
  6355. (new-mark
  6356. (or
  6357. (run-hook-with-args-until-success
  6358. 'org-todo-get-default-hook new-mark-x nil)
  6359. new-mark-x)))
  6360. (beginning-of-line 1)
  6361. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6362. (if org-treat-insert-todo-heading-as-state-change
  6363. (org-todo new-mark)
  6364. (insert new-mark " "))))
  6365. (when org-provide-todo-statistics
  6366. (org-update-parent-todo-statistics))))
  6367. (defun org-insert-subheading (arg)
  6368. "Insert a new subheading and demote it.
  6369. Works for outline headings and for plain lists alike."
  6370. (interactive "P")
  6371. (org-insert-heading arg)
  6372. (cond
  6373. ((org-on-heading-p) (org-do-demote))
  6374. ((org-at-item-p) (org-indent-item))))
  6375. (defun org-insert-todo-subheading (arg)
  6376. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6377. Works for outline headings and for plain lists alike."
  6378. (interactive "P")
  6379. (org-insert-todo-heading arg)
  6380. (cond
  6381. ((org-on-heading-p) (org-do-demote))
  6382. ((org-at-item-p) (org-indent-item))))
  6383. ;;; Promotion and Demotion
  6384. (defvar org-after-demote-entry-hook nil
  6385. "Hook run after an entry has been demoted.
  6386. The cursor will be at the beginning of the entry.
  6387. When a subtree is being demoted, the hook will be called for each node.")
  6388. (defvar org-after-promote-entry-hook nil
  6389. "Hook run after an entry has been promoted.
  6390. The cursor will be at the beginning of the entry.
  6391. When a subtree is being promoted, the hook will be called for each node.")
  6392. (defun org-promote-subtree ()
  6393. "Promote the entire subtree.
  6394. See also `org-promote'."
  6395. (interactive)
  6396. (save-excursion
  6397. (org-with-limited-levels (org-map-tree 'org-promote)))
  6398. (org-fix-position-after-promote))
  6399. (defun org-demote-subtree ()
  6400. "Demote the entire subtree. See `org-demote'.
  6401. See also `org-promote'."
  6402. (interactive)
  6403. (save-excursion
  6404. (org-with-limited-levels (org-map-tree 'org-demote)))
  6405. (org-fix-position-after-promote))
  6406. (defun org-do-promote ()
  6407. "Promote the current heading higher up the tree.
  6408. If the region is active in `transient-mark-mode', promote all headings
  6409. in the region."
  6410. (interactive)
  6411. (save-excursion
  6412. (if (org-region-active-p)
  6413. (org-map-region 'org-promote (region-beginning) (region-end))
  6414. (org-promote)))
  6415. (org-fix-position-after-promote))
  6416. (defun org-do-demote ()
  6417. "Demote the current heading lower down the tree.
  6418. If the region is active in `transient-mark-mode', demote all headings
  6419. in the region."
  6420. (interactive)
  6421. (save-excursion
  6422. (if (org-region-active-p)
  6423. (org-map-region 'org-demote (region-beginning) (region-end))
  6424. (org-demote)))
  6425. (org-fix-position-after-promote))
  6426. (defun org-fix-position-after-promote ()
  6427. "Make sure that after pro/demotion cursor position is right."
  6428. (let ((pos (point)))
  6429. (when (save-excursion
  6430. (beginning-of-line 1)
  6431. (looking-at org-todo-line-regexp)
  6432. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6433. (cond ((eobp) (insert " "))
  6434. ((eolp) (insert " "))
  6435. ((equal (char-after) ?\ ) (forward-char 1))))))
  6436. (defun org-current-level ()
  6437. "Return the level of the current entry, or nil if before the first headline.
  6438. The level is the number of stars at the beginning of the headline."
  6439. (save-excursion
  6440. (org-with-limited-levels
  6441. (ignore-errors
  6442. (org-back-to-heading t)
  6443. (funcall outline-level)))))
  6444. (defun org-get-previous-line-level ()
  6445. "Return the outline depth of the last headline before the current line.
  6446. Returns 0 for the first headline in the buffer, and nil if before the
  6447. first headline."
  6448. (let ((current-level (org-current-level))
  6449. (prev-level (when (> (line-number-at-pos) 1)
  6450. (save-excursion
  6451. (beginning-of-line 0)
  6452. (org-current-level)))))
  6453. (cond ((null current-level) nil) ; Before first headline
  6454. ((null prev-level) 0) ; At first headline
  6455. (prev-level))))
  6456. (defun org-reduced-level (l)
  6457. "Compute the effective level of a heading.
  6458. This takes into account the setting of `org-odd-levels-only'."
  6459. (cond
  6460. ((zerop l) 0)
  6461. (org-odd-levels-only (1+ (floor (/ l 2))))
  6462. (t l)))
  6463. (defun org-level-increment ()
  6464. "Return the number of stars that will be added or removed at a
  6465. time to headlines when structure editing, based on the value of
  6466. `org-odd-levels-only'."
  6467. (if org-odd-levels-only 2 1))
  6468. (defun org-get-valid-level (level &optional change)
  6469. "Rectify a level change under the influence of `org-odd-levels-only'
  6470. LEVEL is a current level, CHANGE is by how much the level should be
  6471. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6472. even level numbers will become the next higher odd number."
  6473. (if org-odd-levels-only
  6474. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6475. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6476. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6477. (max 1 (+ level (or change 0)))))
  6478. (if (boundp 'define-obsolete-function-alias)
  6479. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6480. (define-obsolete-function-alias 'org-get-legal-level
  6481. 'org-get-valid-level)
  6482. (define-obsolete-function-alias 'org-get-legal-level
  6483. 'org-get-valid-level "23.1")))
  6484. (defun org-promote ()
  6485. "Promote the current heading higher up the tree.
  6486. If the region is active in `transient-mark-mode', promote all headings
  6487. in the region."
  6488. (org-back-to-heading t)
  6489. (let* ((level (save-match-data (funcall outline-level)))
  6490. (after-change-functions (remove 'flyspell-after-change-function
  6491. after-change-functions))
  6492. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6493. (diff (abs (- level (length up-head) -1))))
  6494. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6495. (replace-match up-head nil t)
  6496. ;; Fixup tag positioning
  6497. (and org-auto-align-tags (org-set-tags nil t))
  6498. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6499. (run-hooks 'org-after-promote-entry-hook)))
  6500. (defun org-demote ()
  6501. "Demote the current heading lower down the tree.
  6502. If the region is active in `transient-mark-mode', demote all headings
  6503. in the region."
  6504. (org-back-to-heading t)
  6505. (let* ((level (save-match-data (funcall outline-level)))
  6506. (after-change-functions (remove 'flyspell-after-change-function
  6507. after-change-functions))
  6508. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6509. (diff (abs (- level (length down-head) -1))))
  6510. (replace-match down-head nil t)
  6511. ;; Fixup tag positioning
  6512. (and org-auto-align-tags (org-set-tags nil t))
  6513. (if org-adapt-indentation (org-fixup-indentation diff))
  6514. (run-hooks 'org-after-demote-entry-hook)))
  6515. (defun org-cycle-level ()
  6516. "Cycle the level of an empty headline through possible states.
  6517. This goes first to child, then to parent, level, then up the hierarchy.
  6518. After top level, it switches back to sibling level."
  6519. (interactive)
  6520. (let ((org-adapt-indentation nil))
  6521. (when (org-point-at-end-of-empty-headline)
  6522. (setq this-command 'org-cycle-level) ; Only needed for caching
  6523. (let ((cur-level (org-current-level))
  6524. (prev-level (org-get-previous-line-level)))
  6525. (cond
  6526. ;; If first headline in file, promote to top-level.
  6527. ((= prev-level 0)
  6528. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6529. do (org-do-promote)))
  6530. ;; If same level as prev, demote one.
  6531. ((= prev-level cur-level)
  6532. (org-do-demote))
  6533. ;; If parent is top-level, promote to top level if not already.
  6534. ((= prev-level 1)
  6535. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6536. do (org-do-promote)))
  6537. ;; If top-level, return to prev-level.
  6538. ((= cur-level 1)
  6539. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6540. do (org-do-demote)))
  6541. ;; If less than prev-level, promote one.
  6542. ((< cur-level prev-level)
  6543. (org-do-promote))
  6544. ;; If deeper than prev-level, promote until higher than
  6545. ;; prev-level.
  6546. ((> cur-level prev-level)
  6547. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6548. do (org-do-promote))))
  6549. t))))
  6550. (defun org-map-tree (fun)
  6551. "Call FUN for every heading underneath the current one."
  6552. (org-back-to-heading)
  6553. (let ((level (funcall outline-level)))
  6554. (save-excursion
  6555. (funcall fun)
  6556. (while (and (progn
  6557. (outline-next-heading)
  6558. (> (funcall outline-level) level))
  6559. (not (eobp)))
  6560. (funcall fun)))))
  6561. (defun org-map-region (fun beg end)
  6562. "Call FUN for every heading between BEG and END."
  6563. (let ((org-ignore-region t))
  6564. (save-excursion
  6565. (setq end (copy-marker end))
  6566. (goto-char beg)
  6567. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6568. (< (point) end))
  6569. (funcall fun))
  6570. (while (and (progn
  6571. (outline-next-heading)
  6572. (< (point) end))
  6573. (not (eobp)))
  6574. (funcall fun)))))
  6575. (defvar org-property-end-re) ; silence byte-compiler
  6576. (defun org-fixup-indentation (diff)
  6577. "Change the indentation in the current entry by DIFF.
  6578. However, if any line in the current entry has no indentation, or if it
  6579. would end up with no indentation after the change, nothing at all is done."
  6580. (save-excursion
  6581. (let* ((end (save-excursion (outline-next-heading)
  6582. (point-marker)))
  6583. ;; FIXME we should use `org-end-of-meta-data-and-drawers'
  6584. ;; here but it matches misplaced :END:...
  6585. (drawer-end (save-excursion
  6586. (and (re-search-forward
  6587. org-property-end-re end t)
  6588. (match-end 0))))
  6589. (prohibit (if (> diff 0)
  6590. "^\\S-"
  6591. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6592. col)
  6593. (while (re-search-forward
  6594. (concat "\\(" (regexp-opt org-all-time-keywords)
  6595. "\\|" "^[ \t]*" org-tsr-regexp-both "*$"
  6596. "\\|" "^[ \t]*:[a-zA-Z][a-zA-Z0-9_]*:.*$"
  6597. "\\)") (or drawer-end end) t)
  6598. (beginning-of-line)
  6599. (when (looking-at "^[ \t]+")
  6600. (goto-char (match-end 0))
  6601. (setq col (current-column))
  6602. (if (< diff 0) (replace-match ""))
  6603. (org-indent-to-column (+ diff col))
  6604. (if drawer-end (setq drawer-end (+ diff drawer-end))))
  6605. (end-of-line))
  6606. (unless (save-excursion (end-of-line 1)
  6607. (re-search-forward prohibit end t))
  6608. (while (and (< (point) end)
  6609. (re-search-forward "^[ \t]+" end t))
  6610. (goto-char (match-end 0))
  6611. (setq col (current-column))
  6612. (if (< diff 0) (replace-match ""))
  6613. (org-indent-to-column (+ diff col))))
  6614. (move-marker end nil))))
  6615. (defun org-convert-to-odd-levels ()
  6616. "Convert an org-mode file with all levels allowed to one with odd levels.
  6617. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6618. level 5 etc."
  6619. (interactive)
  6620. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6621. (let ((outline-level 'org-outline-level)
  6622. (org-odd-levels-only nil) n)
  6623. (save-excursion
  6624. (goto-char (point-min))
  6625. (while (re-search-forward "^\\*\\*+ " nil t)
  6626. (setq n (- (length (match-string 0)) 2))
  6627. (while (>= (setq n (1- n)) 0)
  6628. (org-demote))
  6629. (end-of-line 1))))))
  6630. (defun org-convert-to-oddeven-levels ()
  6631. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6632. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6633. file contains a section with an even level, conversion would
  6634. destroy the structure of the file. An error is signaled in this
  6635. case."
  6636. (interactive)
  6637. (goto-char (point-min))
  6638. ;; First check if there are no even levels
  6639. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6640. (org-show-context t)
  6641. (error "Not all levels are odd in this file. Conversion not possible"))
  6642. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6643. (let ((outline-regexp org-outline-regexp)
  6644. (outline-level 'org-outline-level)
  6645. (org-odd-levels-only nil) n)
  6646. (save-excursion
  6647. (goto-char (point-min))
  6648. (while (re-search-forward "^\\*\\*+ " nil t)
  6649. (setq n (/ (1- (length (match-string 0))) 2))
  6650. (while (>= (setq n (1- n)) 0)
  6651. (org-promote))
  6652. (end-of-line 1))))))
  6653. (defun org-tr-level (n)
  6654. "Make N odd if required."
  6655. (if org-odd-levels-only (1+ (/ n 2)) n))
  6656. ;;; Vertical tree motion, cutting and pasting of subtrees
  6657. (defun org-move-subtree-up (&optional arg)
  6658. "Move the current subtree up past ARG headlines of the same level."
  6659. (interactive "p")
  6660. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6661. (defun org-move-subtree-down (&optional arg)
  6662. "Move the current subtree down past ARG headlines of the same level."
  6663. (interactive "p")
  6664. (setq arg (prefix-numeric-value arg))
  6665. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6666. 'org-get-last-sibling))
  6667. (ins-point (make-marker))
  6668. (cnt (abs arg))
  6669. (col (current-column))
  6670. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6671. ;; Select the tree
  6672. (org-back-to-heading)
  6673. (setq beg0 (point))
  6674. (save-excursion
  6675. (setq ne-beg (org-back-over-empty-lines))
  6676. (setq beg (point)))
  6677. (save-match-data
  6678. (save-excursion (outline-end-of-heading)
  6679. (setq folded (outline-invisible-p)))
  6680. (outline-end-of-subtree))
  6681. (outline-next-heading)
  6682. (setq ne-end (org-back-over-empty-lines))
  6683. (setq end (point))
  6684. (goto-char beg0)
  6685. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6686. ;; include less whitespace
  6687. (save-excursion
  6688. (goto-char beg)
  6689. (forward-line (- ne-beg ne-end))
  6690. (setq beg (point))))
  6691. ;; Find insertion point, with error handling
  6692. (while (> cnt 0)
  6693. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6694. (progn (goto-char beg0)
  6695. (error "Cannot move past superior level or buffer limit")))
  6696. (setq cnt (1- cnt)))
  6697. (if (> arg 0)
  6698. ;; Moving forward - still need to move over subtree
  6699. (progn (org-end-of-subtree t t)
  6700. (save-excursion
  6701. (org-back-over-empty-lines)
  6702. (or (bolp) (newline)))))
  6703. (setq ne-ins (org-back-over-empty-lines))
  6704. (move-marker ins-point (point))
  6705. (setq txt (buffer-substring beg end))
  6706. (org-save-markers-in-region beg end)
  6707. (delete-region beg end)
  6708. (org-remove-empty-overlays-at beg)
  6709. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6710. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6711. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6712. (let ((bbb (point)))
  6713. (insert-before-markers txt)
  6714. (org-reinstall-markers-in-region bbb)
  6715. (move-marker ins-point bbb))
  6716. (or (bolp) (insert "\n"))
  6717. (setq ins-end (point))
  6718. (goto-char ins-point)
  6719. (org-skip-whitespace)
  6720. (when (and (< arg 0)
  6721. (org-first-sibling-p)
  6722. (> ne-ins ne-beg))
  6723. ;; Move whitespace back to beginning
  6724. (save-excursion
  6725. (goto-char ins-end)
  6726. (let ((kill-whole-line t))
  6727. (kill-line (- ne-ins ne-beg)) (point)))
  6728. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6729. (move-marker ins-point nil)
  6730. (if folded
  6731. (hide-subtree)
  6732. (org-show-entry)
  6733. (show-children)
  6734. (org-cycle-hide-drawers 'children))
  6735. (org-clean-visibility-after-subtree-move)
  6736. ;; move back to the initial column we were at
  6737. (move-to-column col)))
  6738. (defvar org-subtree-clip ""
  6739. "Clipboard for cut and paste of subtrees.
  6740. This is actually only a copy of the kill, because we use the normal kill
  6741. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6742. (defvar org-subtree-clip-folded nil
  6743. "Was the last copied subtree folded?
  6744. This is used to fold the tree back after pasting.")
  6745. (defun org-cut-subtree (&optional n)
  6746. "Cut the current subtree into the clipboard.
  6747. With prefix arg N, cut this many sequential subtrees.
  6748. This is a short-hand for marking the subtree and then cutting it."
  6749. (interactive "p")
  6750. (org-copy-subtree n 'cut))
  6751. (defun org-copy-subtree (&optional n cut force-store-markers)
  6752. "Cut the current subtree into the clipboard.
  6753. With prefix arg N, cut this many sequential subtrees.
  6754. This is a short-hand for marking the subtree and then copying it.
  6755. If CUT is non-nil, actually cut the subtree.
  6756. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6757. of some markers in the region, even if CUT is non-nil. This is
  6758. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6759. (interactive "p")
  6760. (let (beg end folded (beg0 (point)))
  6761. (if (org-called-interactively-p 'any)
  6762. (org-back-to-heading nil) ; take what looks like a subtree
  6763. (org-back-to-heading t)) ; take what is really there
  6764. (org-back-over-empty-lines)
  6765. (setq beg (point))
  6766. (skip-chars-forward " \t\r\n")
  6767. (save-match-data
  6768. (save-excursion (outline-end-of-heading)
  6769. (setq folded (outline-invisible-p)))
  6770. (condition-case nil
  6771. (org-forward-same-level (1- n) t)
  6772. (error nil))
  6773. (org-end-of-subtree t t))
  6774. (org-back-over-empty-lines)
  6775. (setq end (point))
  6776. (goto-char beg0)
  6777. (when (> end beg)
  6778. (setq org-subtree-clip-folded folded)
  6779. (when (or cut force-store-markers)
  6780. (org-save-markers-in-region beg end))
  6781. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6782. (setq org-subtree-clip (current-kill 0))
  6783. (message "%s: Subtree(s) with %d characters"
  6784. (if cut "Cut" "Copied")
  6785. (length org-subtree-clip)))))
  6786. (defun org-paste-subtree (&optional level tree for-yank)
  6787. "Paste the clipboard as a subtree, with modification of headline level.
  6788. The entire subtree is promoted or demoted in order to match a new headline
  6789. level.
  6790. If the cursor is at the beginning of a headline, the same level as
  6791. that headline is used to paste the tree
  6792. If not, the new level is derived from the *visible* headings
  6793. before and after the insertion point, and taken to be the inferior headline
  6794. level of the two. So if the previous visible heading is level 3 and the
  6795. next is level 4 (or vice versa), level 4 will be used for insertion.
  6796. This makes sure that the subtree remains an independent subtree and does
  6797. not swallow low level entries.
  6798. You can also force a different level, either by using a numeric prefix
  6799. argument, or by inserting the heading marker by hand. For example, if the
  6800. cursor is after \"*****\", then the tree will be shifted to level 5.
  6801. If optional TREE is given, use this text instead of the kill ring.
  6802. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6803. move back over whitespace before inserting, and move point to the end of
  6804. the inserted text when done."
  6805. (interactive "P")
  6806. (setq tree (or tree (and kill-ring (current-kill 0))))
  6807. (unless (org-kill-is-subtree-p tree)
  6808. (error "%s"
  6809. (substitute-command-keys
  6810. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6811. (org-with-limited-levels
  6812. (let* ((visp (not (outline-invisible-p)))
  6813. (txt tree)
  6814. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6815. (old-level (if (string-match org-outline-regexp-bol txt)
  6816. (- (match-end 0) (match-beginning 0) 1)
  6817. -1))
  6818. (force-level (cond (level (prefix-numeric-value level))
  6819. ((and (looking-at "[ \t]*$")
  6820. (string-match
  6821. "^\\*+$" (buffer-substring
  6822. (point-at-bol) (point))))
  6823. (- (match-end 1) (match-beginning 1)))
  6824. ((and (bolp)
  6825. (looking-at org-outline-regexp))
  6826. (- (match-end 0) (point) 1))
  6827. (t nil)))
  6828. (previous-level (save-excursion
  6829. (condition-case nil
  6830. (progn
  6831. (outline-previous-visible-heading 1)
  6832. (if (looking-at ^re_)
  6833. (- (match-end 0) (match-beginning 0) 1)
  6834. 1))
  6835. (error 1))))
  6836. (next-level (save-excursion
  6837. (condition-case nil
  6838. (progn
  6839. (or (looking-at org-outline-regexp)
  6840. (outline-next-visible-heading 1))
  6841. (if (looking-at ^re_)
  6842. (- (match-end 0) (match-beginning 0) 1)
  6843. 1))
  6844. (error 1))))
  6845. (new-level (or force-level (max previous-level next-level)))
  6846. (shift (if (or (= old-level -1)
  6847. (= new-level -1)
  6848. (= old-level new-level))
  6849. 0
  6850. (- new-level old-level)))
  6851. (delta (if (> shift 0) -1 1))
  6852. (func (if (> shift 0) 'org-demote 'org-promote))
  6853. (org-odd-levels-only nil)
  6854. beg end newend)
  6855. ;; Remove the forced level indicator
  6856. (if force-level
  6857. (delete-region (point-at-bol) (point)))
  6858. ;; Paste
  6859. (beginning-of-line (if (bolp) 1 2))
  6860. (unless for-yank (org-back-over-empty-lines))
  6861. (setq beg (point))
  6862. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6863. (insert-before-markers txt)
  6864. (unless (string-match "\n\\'" txt) (insert "\n"))
  6865. (setq newend (point))
  6866. (org-reinstall-markers-in-region beg)
  6867. (setq end (point))
  6868. (goto-char beg)
  6869. (skip-chars-forward " \t\n\r")
  6870. (setq beg (point))
  6871. (if (and (outline-invisible-p) visp)
  6872. (save-excursion (outline-show-heading)))
  6873. ;; Shift if necessary
  6874. (unless (= shift 0)
  6875. (save-restriction
  6876. (narrow-to-region beg end)
  6877. (while (not (= shift 0))
  6878. (org-map-region func (point-min) (point-max))
  6879. (setq shift (+ delta shift)))
  6880. (goto-char (point-min))
  6881. (setq newend (point-max))))
  6882. (when (or (org-called-interactively-p 'interactive) for-yank)
  6883. (message "Clipboard pasted as level %d subtree" new-level))
  6884. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6885. kill-ring
  6886. (eq org-subtree-clip (current-kill 0))
  6887. org-subtree-clip-folded)
  6888. ;; The tree was folded before it was killed/copied
  6889. (hide-subtree))
  6890. (and for-yank (goto-char newend)))))
  6891. (defun org-kill-is-subtree-p (&optional txt)
  6892. "Check if the current kill is an outline subtree, or a set of trees.
  6893. Returns nil if kill does not start with a headline, or if the first
  6894. headline level is not the largest headline level in the tree.
  6895. So this will actually accept several entries of equal levels as well,
  6896. which is OK for `org-paste-subtree'.
  6897. If optional TXT is given, check this string instead of the current kill."
  6898. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6899. (re (org-get-limited-outline-regexp))
  6900. (^re (concat "^" re))
  6901. (start-level (and kill
  6902. (string-match
  6903. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  6904. kill)
  6905. (- (match-end 2) (match-beginning 2) 1)))
  6906. (start (1+ (or (match-beginning 2) -1))))
  6907. (if (not start-level)
  6908. (progn
  6909. nil) ;; does not even start with a heading
  6910. (catch 'exit
  6911. (while (setq start (string-match ^re kill (1+ start)))
  6912. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6913. (throw 'exit nil)))
  6914. t))))
  6915. (defvar org-markers-to-move nil
  6916. "Markers that should be moved with a cut-and-paste operation.
  6917. Those markers are stored together with their positions relative to
  6918. the start of the region.")
  6919. (defun org-save-markers-in-region (beg end)
  6920. "Check markers in region.
  6921. If these markers are between BEG and END, record their position relative
  6922. to BEG, so that after moving the block of text, we can put the markers back
  6923. into place.
  6924. This function gets called just before an entry or tree gets cut from the
  6925. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6926. called immediately, to move the markers with the entries."
  6927. (setq org-markers-to-move nil)
  6928. (when (featurep 'org-clock)
  6929. (org-clock-save-markers-for-cut-and-paste beg end))
  6930. (when (featurep 'org-agenda)
  6931. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6932. (defun org-check-and-save-marker (marker beg end)
  6933. "Check if MARKER is between BEG and END.
  6934. If yes, remember the marker and the distance to BEG."
  6935. (when (and (marker-buffer marker)
  6936. (equal (marker-buffer marker) (current-buffer)))
  6937. (if (and (>= marker beg) (< marker end))
  6938. (push (cons marker (- marker beg)) org-markers-to-move))))
  6939. (defun org-reinstall-markers-in-region (beg)
  6940. "Move all remembered markers to their position relative to BEG."
  6941. (mapc (lambda (x)
  6942. (move-marker (car x) (+ beg (cdr x))))
  6943. org-markers-to-move)
  6944. (setq org-markers-to-move nil))
  6945. (defun org-narrow-to-subtree ()
  6946. "Narrow buffer to the current subtree."
  6947. (interactive)
  6948. (save-excursion
  6949. (save-match-data
  6950. (org-with-limited-levels
  6951. (narrow-to-region
  6952. (progn (org-back-to-heading t) (point))
  6953. (progn (org-end-of-subtree t t)
  6954. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6955. (point)))))))
  6956. (defun org-narrow-to-block ()
  6957. "Narrow buffer to the current block."
  6958. (interactive)
  6959. (let* ((case-fold-search t)
  6960. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  6961. "^[ \t]*#\\+end_.*")))
  6962. (if blockp
  6963. (narrow-to-region (car blockp) (cdr blockp))
  6964. (error "Not in a block"))))
  6965. (eval-when-compile
  6966. (defvar org-property-drawer-re))
  6967. (defvar org-property-start-re) ;; defined below
  6968. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6969. "Clone the task (subtree) at point N times.
  6970. The clones will be inserted as siblings.
  6971. In interactive use, the user will be prompted for the number of
  6972. clones to be produced, and for a time SHIFT, which may be a
  6973. repeater as used in time stamps, for example `+3d'.
  6974. When a valid repeater is given and the entry contains any time
  6975. stamps, the clones will become a sequence in time, with time
  6976. stamps in the subtree shifted for each clone produced. If SHIFT
  6977. is nil or the empty string, time stamps will be left alone. The
  6978. ID property of the original subtree is removed.
  6979. If the original subtree did contain time stamps with a repeater,
  6980. the following will happen:
  6981. - the repeater will be removed in each clone
  6982. - an additional clone will be produced, with the current, unshifted
  6983. date(s) in the entry.
  6984. - the original entry will be placed *after* all the clones, with
  6985. repeater intact.
  6986. - the start days in the repeater in the original entry will be shifted
  6987. to past the last clone.
  6988. I this way you can spell out a number of instances of a repeating task,
  6989. and still retain the repeater to cover future instances of the task."
  6990. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6991. (let (beg end template task idprop
  6992. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6993. (if (not (and (integerp n) (> n 0)))
  6994. (error "Invalid number of replications %s" n))
  6995. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6996. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6997. shift)))
  6998. (error "Invalid shift specification %s" shift))
  6999. (when doshift
  7000. (setq shift-n (string-to-number (match-string 1 shift))
  7001. shift-what (cdr (assoc (match-string 2 shift)
  7002. '(("d" . day) ("w" . week)
  7003. ("m" . month) ("y" . year))))))
  7004. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7005. (setq nmin 1 nmax n)
  7006. (org-back-to-heading t)
  7007. (setq beg (point))
  7008. (setq idprop (org-entry-get nil "ID"))
  7009. (org-end-of-subtree t t)
  7010. (or (bolp) (insert "\n"))
  7011. (setq end (point))
  7012. (setq template (buffer-substring beg end))
  7013. ;; Remove clocks and empty drawers
  7014. (with-temp-buffer
  7015. (insert template)
  7016. (goto-char (point-min))
  7017. (while (re-search-forward
  7018. "^[ \t]*CLOCK:.*$" (save-excursion (org-end-of-subtree t t)) t)
  7019. (replace-match "")
  7020. (kill-whole-line))
  7021. (goto-char (point-min))
  7022. (while (re-search-forward
  7023. (concat "^[ \t]*:" (regexp-opt org-drawers) ":[ \t]*$") nil t)
  7024. (mapc (lambda(d) (org-remove-empty-drawer-at d (point))) org-drawers))
  7025. (setq template (buffer-substring (point-min) (point-max))))
  7026. (when (and doshift
  7027. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  7028. (delete-region beg end)
  7029. (setq end beg)
  7030. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7031. (goto-char end)
  7032. (loop for n from nmin to nmax do
  7033. ;; prepare clone
  7034. (with-temp-buffer
  7035. (insert template)
  7036. (org-mode)
  7037. (goto-char (point-min))
  7038. (and idprop (if org-clone-delete-id
  7039. (org-entry-delete nil "ID")
  7040. (org-id-get-create t)))
  7041. (while (re-search-forward org-property-start-re nil t)
  7042. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  7043. (goto-char (point-min))
  7044. (when doshift
  7045. (while (re-search-forward org-ts-regexp-both nil t)
  7046. (org-timestamp-change (* n shift-n) shift-what))
  7047. (unless (= n n-no-remove)
  7048. (goto-char (point-min))
  7049. (while (re-search-forward org-ts-regexp nil t)
  7050. (save-excursion
  7051. (goto-char (match-beginning 0))
  7052. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  7053. (delete-region (match-beginning 1) (match-end 1)))))))
  7054. (setq task (buffer-string)))
  7055. (insert task))
  7056. (goto-char beg)))
  7057. ;;; Outline Sorting
  7058. (defun org-sort (with-case)
  7059. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7060. Optional argument WITH-CASE means sort case-sensitively.
  7061. With a double prefix argument, also remove duplicate entries."
  7062. (interactive "P")
  7063. (cond
  7064. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7065. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7066. (t
  7067. (org-call-with-arg 'org-sort-entries with-case))))
  7068. (defun org-sort-remove-invisible (s)
  7069. (remove-text-properties 0 (length s) org-rm-props s)
  7070. (while (string-match org-bracket-link-regexp s)
  7071. (setq s (replace-match (if (match-end 2)
  7072. (match-string 3 s)
  7073. (match-string 1 s)) t t s)))
  7074. s)
  7075. (defvar org-priority-regexp) ; defined later in the file
  7076. (defvar org-after-sorting-entries-or-items-hook nil
  7077. "Hook that is run after a bunch of entries or items have been sorted.
  7078. When children are sorted, the cursor is in the parent line when this
  7079. hook gets called. When a region or a plain list is sorted, the cursor
  7080. will be in the first entry of the sorted region/list.")
  7081. (defun org-sort-entries
  7082. (&optional with-case sorting-type getkey-func compare-func property)
  7083. "Sort entries on a certain level of an outline tree.
  7084. If there is an active region, the entries in the region are sorted.
  7085. Else, if the cursor is before the first entry, sort the top-level items.
  7086. Else, the children of the entry at point are sorted.
  7087. Sorting can be alphabetically, numerically, by date/time as given by
  7088. a time stamp, by a property or by priority.
  7089. The command prompts for the sorting type unless it has been given to the
  7090. function through the SORTING-TYPE argument, which needs to be a character,
  7091. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7092. precise meaning of each character:
  7093. n Numerically, by converting the beginning of the entry/item to a number.
  7094. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7095. t By date/time, either the first active time stamp in the entry, or, if
  7096. none exist, by the first inactive one.
  7097. s By the scheduled date/time.
  7098. d By deadline date/time.
  7099. c By creation time, which is assumed to be the first inactive time stamp
  7100. at the beginning of a line.
  7101. p By priority according to the cookie.
  7102. r By the value of a property.
  7103. Capital letters will reverse the sort order.
  7104. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7105. called with point at the beginning of the record. It must return either
  7106. a string or a number that should serve as the sorting key for that record.
  7107. Comparing entries ignores case by default. However, with an optional argument
  7108. WITH-CASE, the sorting considers case as well."
  7109. (interactive "P")
  7110. (let ((case-func (if with-case 'identity 'downcase))
  7111. start beg end stars re re2
  7112. txt what tmp)
  7113. ;; Find beginning and end of region to sort
  7114. (cond
  7115. ((org-region-active-p)
  7116. ;; we will sort the region
  7117. (setq end (region-end)
  7118. what "region")
  7119. (goto-char (region-beginning))
  7120. (if (not (org-on-heading-p)) (outline-next-heading))
  7121. (setq start (point)))
  7122. ((or (org-on-heading-p)
  7123. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7124. ;; we will sort the children of the current headline
  7125. (org-back-to-heading)
  7126. (setq start (point)
  7127. end (progn (org-end-of-subtree t t)
  7128. (or (bolp) (insert "\n"))
  7129. (org-back-over-empty-lines)
  7130. (point))
  7131. what "children")
  7132. (goto-char start)
  7133. (show-subtree)
  7134. (outline-next-heading))
  7135. (t
  7136. ;; we will sort the top-level entries in this file
  7137. (goto-char (point-min))
  7138. (or (org-on-heading-p) (outline-next-heading))
  7139. (setq start (point))
  7140. (goto-char (point-max))
  7141. (beginning-of-line 1)
  7142. (when (looking-at ".*?\\S-")
  7143. ;; File ends in a non-white line
  7144. (end-of-line 1)
  7145. (insert "\n"))
  7146. (setq end (point-max))
  7147. (setq what "top-level")
  7148. (goto-char start)
  7149. (show-all)))
  7150. (setq beg (point))
  7151. (if (>= beg end) (error "Nothing to sort"))
  7152. (looking-at "\\(\\*+\\)")
  7153. (setq stars (match-string 1)
  7154. re (concat "^" (regexp-quote stars) " +")
  7155. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7156. txt (buffer-substring beg end))
  7157. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7158. (if (and (not (equal stars "*")) (string-match re2 txt))
  7159. (error "Region to sort contains a level above the first entry"))
  7160. (unless sorting-type
  7161. (message
  7162. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7163. [t]ime [s]cheduled [d]eadline [c]reated
  7164. A/N/T/S/D/C/P/O/F means reversed:"
  7165. what)
  7166. (setq sorting-type (read-char-exclusive))
  7167. (and (= (downcase sorting-type) ?f)
  7168. (setq getkey-func
  7169. (org-icompleting-read "Sort using function: "
  7170. obarray 'fboundp t nil nil))
  7171. (setq getkey-func (intern getkey-func)))
  7172. (and (= (downcase sorting-type) ?r)
  7173. (setq property
  7174. (org-icompleting-read "Property: "
  7175. (mapcar 'list (org-buffer-property-keys t))
  7176. nil t))))
  7177. (message "Sorting entries...")
  7178. (save-restriction
  7179. (narrow-to-region start end)
  7180. (let ((dcst (downcase sorting-type))
  7181. (case-fold-search nil)
  7182. (now (current-time)))
  7183. (sort-subr
  7184. (/= dcst sorting-type)
  7185. ;; This function moves to the beginning character of the "record" to
  7186. ;; be sorted.
  7187. (lambda nil
  7188. (if (re-search-forward re nil t)
  7189. (goto-char (match-beginning 0))
  7190. (goto-char (point-max))))
  7191. ;; This function moves to the last character of the "record" being
  7192. ;; sorted.
  7193. (lambda nil
  7194. (save-match-data
  7195. (condition-case nil
  7196. (outline-forward-same-level 1)
  7197. (error
  7198. (goto-char (point-max))))))
  7199. ;; This function returns the value that gets sorted against.
  7200. (lambda nil
  7201. (cond
  7202. ((= dcst ?n)
  7203. (if (looking-at org-complex-heading-regexp)
  7204. (string-to-number (match-string 4))
  7205. nil))
  7206. ((= dcst ?a)
  7207. (if (looking-at org-complex-heading-regexp)
  7208. (funcall case-func (match-string 4))
  7209. nil))
  7210. ((= dcst ?t)
  7211. (let ((end (save-excursion (outline-next-heading) (point))))
  7212. (if (or (re-search-forward org-ts-regexp end t)
  7213. (re-search-forward org-ts-regexp-both end t))
  7214. (org-time-string-to-seconds (match-string 0))
  7215. (org-float-time now))))
  7216. ((= dcst ?c)
  7217. (let ((end (save-excursion (outline-next-heading) (point))))
  7218. (if (re-search-forward
  7219. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7220. end t)
  7221. (org-time-string-to-seconds (match-string 0))
  7222. (org-float-time now))))
  7223. ((= dcst ?s)
  7224. (let ((end (save-excursion (outline-next-heading) (point))))
  7225. (if (re-search-forward org-scheduled-time-regexp end t)
  7226. (org-time-string-to-seconds (match-string 1))
  7227. (org-float-time now))))
  7228. ((= dcst ?d)
  7229. (let ((end (save-excursion (outline-next-heading) (point))))
  7230. (if (re-search-forward org-deadline-time-regexp end t)
  7231. (org-time-string-to-seconds (match-string 1))
  7232. (org-float-time now))))
  7233. ((= dcst ?p)
  7234. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7235. (string-to-char (match-string 2))
  7236. org-default-priority))
  7237. ((= dcst ?r)
  7238. (or (org-entry-get nil property) ""))
  7239. ((= dcst ?o)
  7240. (if (looking-at org-complex-heading-regexp)
  7241. (- 9999 (length (member (match-string 2)
  7242. org-todo-keywords-1)))))
  7243. ((= dcst ?f)
  7244. (if getkey-func
  7245. (progn
  7246. (setq tmp (funcall getkey-func))
  7247. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7248. tmp)
  7249. (error "Invalid key function `%s'" getkey-func)))
  7250. (t (error "Invalid sorting type `%c'" sorting-type))))
  7251. nil
  7252. (cond
  7253. ((= dcst ?a) 'string<)
  7254. ((= dcst ?f) compare-func)
  7255. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7256. (t nil)))))
  7257. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7258. (message "Sorting entries...done")))
  7259. (defun org-do-sort (table what &optional with-case sorting-type)
  7260. "Sort TABLE of WHAT according to SORTING-TYPE.
  7261. The user will be prompted for the SORTING-TYPE if the call to this
  7262. function does not specify it. WHAT is only for the prompt, to indicate
  7263. what is being sorted. The sorting key will be extracted from
  7264. the car of the elements of the table.
  7265. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7266. (unless sorting-type
  7267. (message
  7268. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7269. what)
  7270. (setq sorting-type (read-char-exclusive)))
  7271. (let ((dcst (downcase sorting-type))
  7272. extractfun comparefun)
  7273. ;; Define the appropriate functions
  7274. (cond
  7275. ((= dcst ?n)
  7276. (setq extractfun 'string-to-number
  7277. comparefun (if (= dcst sorting-type) '< '>)))
  7278. ((= dcst ?a)
  7279. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7280. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7281. comparefun (if (= dcst sorting-type)
  7282. 'string<
  7283. (lambda (a b) (and (not (string< a b))
  7284. (not (string= a b)))))))
  7285. ((= dcst ?t)
  7286. (setq extractfun
  7287. (lambda (x)
  7288. (if (or (string-match org-ts-regexp x)
  7289. (string-match org-ts-regexp-both x))
  7290. (org-float-time
  7291. (org-time-string-to-time (match-string 0 x)))
  7292. 0))
  7293. comparefun (if (= dcst sorting-type) '< '>)))
  7294. (t (error "Invalid sorting type `%c'" sorting-type)))
  7295. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7296. table)
  7297. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7298. ;;; The orgstruct minor mode
  7299. ;; Define a minor mode which can be used in other modes in order to
  7300. ;; integrate the org-mode structure editing commands.
  7301. ;; This is really a hack, because the org-mode structure commands use
  7302. ;; keys which normally belong to the major mode. Here is how it
  7303. ;; works: The minor mode defines all the keys necessary to operate the
  7304. ;; structure commands, but wraps the commands into a function which
  7305. ;; tests if the cursor is currently at a headline or a plain list
  7306. ;; item. If that is the case, the structure command is used,
  7307. ;; temporarily setting many Org-mode variables like regular
  7308. ;; expressions for filling etc. However, when any of those keys is
  7309. ;; used at a different location, function uses `key-binding' to look
  7310. ;; up if the key has an associated command in another currently active
  7311. ;; keymap (minor modes, major mode, global), and executes that
  7312. ;; command. There might be problems if any of the keys is otherwise
  7313. ;; used as a prefix key.
  7314. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7315. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7316. ;; addresses this by checking explicitly for both bindings.
  7317. (defvar orgstruct-mode-map (make-sparse-keymap)
  7318. "Keymap for the minor `orgstruct-mode'.")
  7319. (defvar org-local-vars nil
  7320. "List of local variables, for use by `orgstruct-mode'.")
  7321. ;;;###autoload
  7322. (define-minor-mode orgstruct-mode
  7323. "Toggle the minor mode `orgstruct-mode'.
  7324. This mode is for using Org-mode structure commands in other
  7325. modes. The following keys behave as if Org-mode were active, if
  7326. the cursor is on a headline, or on a plain list item (both as
  7327. defined by Org-mode).
  7328. M-up Move entry/item up
  7329. M-down Move entry/item down
  7330. M-left Promote
  7331. M-right Demote
  7332. M-S-up Move entry/item up
  7333. M-S-down Move entry/item down
  7334. M-S-left Promote subtree
  7335. M-S-right Demote subtree
  7336. M-q Fill paragraph and items like in Org-mode
  7337. C-c ^ Sort entries
  7338. C-c - Cycle list bullet
  7339. TAB Cycle item visibility
  7340. M-RET Insert new heading/item
  7341. S-M-RET Insert new TODO heading / Checkbox item
  7342. C-c C-c Set tags / toggle checkbox"
  7343. nil " OrgStruct" nil
  7344. (org-load-modules-maybe)
  7345. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7346. ;;;###autoload
  7347. (defun turn-on-orgstruct ()
  7348. "Unconditionally turn on `orgstruct-mode'."
  7349. (orgstruct-mode 1))
  7350. (defun orgstruct++-mode (&optional arg)
  7351. "Toggle `orgstruct-mode', the enhanced version of it.
  7352. In addition to setting orgstruct-mode, this also exports all indentation
  7353. and autofilling variables from org-mode into the buffer. It will also
  7354. recognize item context in multiline items.
  7355. Note that turning off orgstruct-mode will *not* remove the
  7356. indentation/paragraph settings. This can only be done by refreshing the
  7357. major mode, for example with \\[normal-mode]."
  7358. (interactive "P")
  7359. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7360. (if (< arg 1)
  7361. (orgstruct-mode -1)
  7362. (orgstruct-mode 1)
  7363. (let (var val)
  7364. (mapc
  7365. (lambda (x)
  7366. (when (string-match
  7367. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7368. (symbol-name (car x)))
  7369. (setq var (car x) val (nth 1 x))
  7370. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7371. org-local-vars)
  7372. (org-set-local 'orgstruct-is-++ t))))
  7373. (defvar orgstruct-is-++ nil
  7374. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7375. (make-variable-buffer-local 'orgstruct-is-++)
  7376. ;;;###autoload
  7377. (defun turn-on-orgstruct++ ()
  7378. "Unconditionally turn on `orgstruct++-mode'."
  7379. (orgstruct++-mode 1))
  7380. (defun orgstruct-error ()
  7381. "Error when there is no default binding for a structure key."
  7382. (interactive)
  7383. (error "This key has no function outside structure elements"))
  7384. (defun orgstruct-setup ()
  7385. "Setup orgstruct keymaps."
  7386. (let ((nfunc 0)
  7387. (bindings
  7388. (list
  7389. '([(meta up)] org-metaup)
  7390. '([(meta down)] org-metadown)
  7391. '([(meta left)] org-metaleft)
  7392. '([(meta right)] org-metaright)
  7393. '([(meta shift up)] org-shiftmetaup)
  7394. '([(meta shift down)] org-shiftmetadown)
  7395. '([(meta shift left)] org-shiftmetaleft)
  7396. '([(meta shift right)] org-shiftmetaright)
  7397. '([?\e (up)] org-metaup)
  7398. '([?\e (down)] org-metadown)
  7399. '([?\e (left)] org-metaleft)
  7400. '([?\e (right)] org-metaright)
  7401. '([?\e (shift up)] org-shiftmetaup)
  7402. '([?\e (shift down)] org-shiftmetadown)
  7403. '([?\e (shift left)] org-shiftmetaleft)
  7404. '([?\e (shift right)] org-shiftmetaright)
  7405. '([(shift up)] org-shiftup)
  7406. '([(shift down)] org-shiftdown)
  7407. '([(shift left)] org-shiftleft)
  7408. '([(shift right)] org-shiftright)
  7409. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7410. '("\M-q" fill-paragraph)
  7411. '("\C-c^" org-sort)
  7412. '("\C-c-" org-cycle-list-bullet)))
  7413. elt key fun cmd)
  7414. (while (setq elt (pop bindings))
  7415. (setq nfunc (1+ nfunc))
  7416. (setq key (org-key (car elt))
  7417. fun (nth 1 elt)
  7418. cmd (orgstruct-make-binding fun nfunc key))
  7419. (org-defkey orgstruct-mode-map key cmd))
  7420. ;; Special treatment needed for TAB and RET
  7421. (org-defkey orgstruct-mode-map [(tab)]
  7422. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7423. (org-defkey orgstruct-mode-map "\C-i"
  7424. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7425. (org-defkey orgstruct-mode-map "\M-\C-m"
  7426. (orgstruct-make-binding 'org-insert-heading 105
  7427. "\M-\C-m" [(meta return)]))
  7428. (org-defkey orgstruct-mode-map [(meta return)]
  7429. (orgstruct-make-binding 'org-insert-heading 106
  7430. [(meta return)] "\M-\C-m"))
  7431. (org-defkey orgstruct-mode-map [(shift meta return)]
  7432. (orgstruct-make-binding 'org-insert-todo-heading 107
  7433. [(meta return)] "\M-\C-m"))
  7434. (org-defkey orgstruct-mode-map "\e\C-m"
  7435. (orgstruct-make-binding 'org-insert-heading 108
  7436. "\e\C-m" [?\e (return)]))
  7437. (org-defkey orgstruct-mode-map [?\e (return)]
  7438. (orgstruct-make-binding 'org-insert-heading 109
  7439. [?\e (return)] "\e\C-m"))
  7440. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7441. (orgstruct-make-binding 'org-insert-todo-heading 110
  7442. [?\e (return)] "\e\C-m"))
  7443. (unless org-local-vars
  7444. (setq org-local-vars (org-get-local-variables)))
  7445. t))
  7446. (defun orgstruct-make-binding (fun n &rest keys)
  7447. "Create a function for binding in the structure minor mode.
  7448. FUN is the command to call inside a table. N is used to create a unique
  7449. command name. KEYS are keys that should be checked in for a command
  7450. to execute outside of tables."
  7451. (eval
  7452. (list 'defun
  7453. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7454. '(arg)
  7455. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7456. "Outside of structure, run the binding of `"
  7457. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7458. "'.")
  7459. '(interactive "p")
  7460. (list 'if
  7461. `(org-context-p 'headline 'item
  7462. (and orgstruct-is-++
  7463. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7464. 'item-body))
  7465. (list 'org-run-like-in-org-mode (list 'quote fun))
  7466. (list 'let '(orgstruct-mode)
  7467. (list 'call-interactively
  7468. (append '(or)
  7469. (mapcar (lambda (k)
  7470. (list 'key-binding k))
  7471. keys)
  7472. '('orgstruct-error))))))))
  7473. (defun org-context-p (&rest contexts)
  7474. "Check if local context is any of CONTEXTS.
  7475. Possible values in the list of contexts are `table', `headline', and `item'."
  7476. (let ((pos (point)))
  7477. (goto-char (point-at-bol))
  7478. (prog1 (or (and (memq 'table contexts)
  7479. (looking-at "[ \t]*|"))
  7480. (and (memq 'headline contexts)
  7481. (looking-at org-outline-regexp))
  7482. (and (memq 'item contexts)
  7483. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7484. (and (memq 'item-body contexts)
  7485. (org-in-item-p)))
  7486. (goto-char pos))))
  7487. (defun org-get-local-variables ()
  7488. "Return a list of all local variables in an org-mode buffer."
  7489. (let (varlist)
  7490. (with-current-buffer (get-buffer-create "*Org tmp*")
  7491. (erase-buffer)
  7492. (org-mode)
  7493. (setq varlist (buffer-local-variables)))
  7494. (kill-buffer "*Org tmp*")
  7495. (delq nil
  7496. (mapcar
  7497. (lambda (x)
  7498. (setq x
  7499. (if (symbolp x)
  7500. (list x)
  7501. (list (car x) (list 'quote (cdr x)))))
  7502. (if (string-match
  7503. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7504. (symbol-name (car x)))
  7505. x nil))
  7506. varlist))))
  7507. (defun org-clone-local-variables (from-buffer &optional regexp)
  7508. "Clone local variables from FROM-BUFFER.
  7509. Optional argument REGEXP selects variables to clone."
  7510. (mapc
  7511. (lambda (pair)
  7512. (and (symbolp (car pair))
  7513. (or (null regexp)
  7514. (string-match regexp (symbol-name (car pair))))
  7515. (set (make-local-variable (car pair))
  7516. (cdr pair))))
  7517. (buffer-local-variables from-buffer)))
  7518. ;;;###autoload
  7519. (defun org-run-like-in-org-mode (cmd)
  7520. "Run a command, pretending that the current buffer is in Org-mode.
  7521. This will temporarily bind local variables that are typically bound in
  7522. Org-mode to the values they have in Org-mode, and then interactively
  7523. call CMD."
  7524. (org-load-modules-maybe)
  7525. (unless org-local-vars
  7526. (setq org-local-vars (org-get-local-variables)))
  7527. (eval (list 'let org-local-vars
  7528. (list 'call-interactively (list 'quote cmd)))))
  7529. ;;;; Archiving
  7530. (defun org-get-category (&optional pos force-refresh)
  7531. "Get the category applying to position POS."
  7532. (if force-refresh (org-refresh-category-properties))
  7533. (let ((pos (or pos (point))))
  7534. (or (get-text-property pos 'org-category)
  7535. (progn (org-refresh-category-properties)
  7536. (get-text-property pos 'org-category)))))
  7537. (defun org-refresh-category-properties ()
  7538. "Refresh category text properties in the buffer."
  7539. (let ((def-cat (cond
  7540. ((null org-category)
  7541. (if buffer-file-name
  7542. (file-name-sans-extension
  7543. (file-name-nondirectory buffer-file-name))
  7544. "???"))
  7545. ((symbolp org-category) (symbol-name org-category))
  7546. (t org-category)))
  7547. beg end cat pos optionp)
  7548. (org-unmodified
  7549. (save-excursion
  7550. (save-restriction
  7551. (widen)
  7552. (goto-char (point-min))
  7553. (put-text-property (point) (point-max) 'org-category def-cat)
  7554. (while (re-search-forward
  7555. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7556. (setq pos (match-end 0)
  7557. optionp (equal (char-after (match-beginning 0)) ?#)
  7558. cat (org-trim (match-string 2)))
  7559. (if optionp
  7560. (setq beg (point-at-bol) end (point-max))
  7561. (org-back-to-heading t)
  7562. (setq beg (point) end (org-end-of-subtree t t)))
  7563. (put-text-property beg end 'org-category cat)
  7564. (put-text-property beg end 'org-category-position beg)
  7565. (goto-char pos)))))))
  7566. ;;;; Link Stuff
  7567. ;;; Link abbreviations
  7568. (defun org-link-expand-abbrev (link)
  7569. "Apply replacements as defined in `org-link-abbrev-alist."
  7570. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7571. (let* ((key (match-string 1 link))
  7572. (as (or (assoc key org-link-abbrev-alist-local)
  7573. (assoc key org-link-abbrev-alist)))
  7574. (tag (and (match-end 2) (match-string 3 link)))
  7575. rpl)
  7576. (if (not as)
  7577. link
  7578. (setq rpl (cdr as))
  7579. (cond
  7580. ((symbolp rpl) (funcall rpl tag))
  7581. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7582. ((string-match "%h" rpl)
  7583. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7584. (t (concat rpl tag)))))
  7585. link))
  7586. ;;; Storing and inserting links
  7587. (defvar org-insert-link-history nil
  7588. "Minibuffer history for links inserted with `org-insert-link'.")
  7589. (defvar org-stored-links nil
  7590. "Contains the links stored with `org-store-link'.")
  7591. (defvar org-store-link-plist nil
  7592. "Plist with info about the most recently link created with `org-store-link'.")
  7593. (defvar org-link-protocols nil
  7594. "Link protocols added to Org-mode using `org-add-link-type'.")
  7595. (defvar org-store-link-functions nil
  7596. "List of functions that are called to create and store a link.
  7597. Each function will be called in turn until one returns a non-nil
  7598. value. Each function should check if it is responsible for creating
  7599. this link (for example by looking at the major mode).
  7600. If not, it must exit and return nil.
  7601. If yes, it should return a non-nil value after a calling
  7602. `org-store-link-props' with a list of properties and values.
  7603. Special properties are:
  7604. :type The link prefix, like \"http\". This must be given.
  7605. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7606. This is obligatory as well.
  7607. :description Optional default description for the second pair
  7608. of brackets in an Org-mode link. The user can still change
  7609. this when inserting this link into an Org-mode buffer.
  7610. In addition to these, any additional properties can be specified
  7611. and then used in remember templates.")
  7612. (defun org-add-link-type (type &optional follow export)
  7613. "Add TYPE to the list of `org-link-types'.
  7614. Re-compute all regular expressions depending on `org-link-types'
  7615. FOLLOW and EXPORT are two functions.
  7616. FOLLOW should take the link path as the single argument and do whatever
  7617. is necessary to follow the link, for example find a file or display
  7618. a mail message.
  7619. EXPORT should format the link path for export to one of the export formats.
  7620. It should be a function accepting three arguments:
  7621. path the path of the link, the text after the prefix (like \"http:\")
  7622. desc the description of the link, if any, or a description added by
  7623. org-export-normalize-links if there is none
  7624. format the export format, a symbol like `html' or `latex' or `ascii'..
  7625. The function may use the FORMAT information to return different values
  7626. depending on the format. The return value will be put literally into
  7627. the exported file. If the return value is nil, this means Org should
  7628. do what it normally does with links which do not have EXPORT defined.
  7629. Org-mode has a built-in default for exporting links. If you are happy with
  7630. this default, there is no need to define an export function for the link
  7631. type. For a simple example of an export function, see `org-bbdb.el'."
  7632. (add-to-list 'org-link-types type t)
  7633. (org-make-link-regexps)
  7634. (if (assoc type org-link-protocols)
  7635. (setcdr (assoc type org-link-protocols) (list follow export))
  7636. (push (list type follow export) org-link-protocols)))
  7637. (defvar org-agenda-buffer-name)
  7638. ;;;###autoload
  7639. (defun org-store-link (arg)
  7640. "\\<org-mode-map>Store an org-link to the current location.
  7641. This link is added to `org-stored-links' and can later be inserted
  7642. into an org-buffer with \\[org-insert-link].
  7643. For some link types, a prefix arg is interpreted:
  7644. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7645. For file links, arg negates `org-context-in-file-links'."
  7646. (interactive "P")
  7647. (org-load-modules-maybe)
  7648. (setq org-store-link-plist nil) ; reset
  7649. (org-with-limited-levels
  7650. (let (link cpltxt desc description search txt custom-id agenda-link)
  7651. (cond
  7652. ((run-hook-with-args-until-success 'org-store-link-functions)
  7653. (setq link (plist-get org-store-link-plist :link)
  7654. desc (or (plist-get org-store-link-plist :description) link)))
  7655. ((equal (buffer-name) "*Org Edit Src Example*")
  7656. (let (label gc)
  7657. (while (or (not label)
  7658. (save-excursion
  7659. (save-restriction
  7660. (widen)
  7661. (goto-char (point-min))
  7662. (re-search-forward
  7663. (regexp-quote (format org-coderef-label-format label))
  7664. nil t))))
  7665. (when label (message "Label exists already") (sit-for 2))
  7666. (setq label (read-string "Code line label: " label)))
  7667. (end-of-line 1)
  7668. (setq link (format org-coderef-label-format label))
  7669. (setq gc (- 79 (length link)))
  7670. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7671. (insert link)
  7672. (setq link (concat "(" label ")") desc nil)))
  7673. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7674. ;; We are in the agenda, link to referenced location
  7675. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7676. (get-text-property (point) 'org-marker))))
  7677. (when m
  7678. (org-with-point-at m
  7679. (setq agenda-link
  7680. (if (org-called-interactively-p 'any)
  7681. (call-interactively 'org-store-link)
  7682. (org-store-link nil)))))))
  7683. ((eq major-mode 'calendar-mode)
  7684. (let ((cd (calendar-cursor-to-date)))
  7685. (setq link
  7686. (format-time-string
  7687. (car org-time-stamp-formats)
  7688. (apply 'encode-time
  7689. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7690. nil nil nil))))
  7691. (org-store-link-props :type "calendar" :date cd)))
  7692. ((eq major-mode 'w3-mode)
  7693. (setq cpltxt (if (and (buffer-name)
  7694. (not (string-match "Untitled" (buffer-name))))
  7695. (buffer-name)
  7696. (url-view-url t))
  7697. link (org-make-link (url-view-url t)))
  7698. (org-store-link-props :type "w3" :url (url-view-url t)))
  7699. ((eq major-mode 'w3m-mode)
  7700. (setq cpltxt (or w3m-current-title w3m-current-url)
  7701. link (org-make-link w3m-current-url))
  7702. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7703. ((setq search (run-hook-with-args-until-success
  7704. 'org-create-file-search-functions))
  7705. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7706. "::" search))
  7707. (setq cpltxt (or description link)))
  7708. ((eq major-mode 'image-mode)
  7709. (setq cpltxt (concat "file:"
  7710. (abbreviate-file-name buffer-file-name))
  7711. link (org-make-link cpltxt))
  7712. (org-store-link-props :type "image" :file buffer-file-name))
  7713. ((eq major-mode 'dired-mode)
  7714. ;; link to the file in the current line
  7715. (let ((file (dired-get-filename nil t)))
  7716. (setq file (if file
  7717. (abbreviate-file-name
  7718. (expand-file-name (dired-get-filename nil t)))
  7719. ;; otherwise, no file so use current directory.
  7720. default-directory))
  7721. (setq cpltxt (concat "file:" file)
  7722. link (org-make-link cpltxt))))
  7723. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7724. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7725. (cond
  7726. ((org-in-regexp "<<\\(.*?\\)>>")
  7727. (setq cpltxt
  7728. (concat "file:"
  7729. (abbreviate-file-name
  7730. (buffer-file-name (buffer-base-buffer)))
  7731. "::" (match-string 1))
  7732. link (org-make-link cpltxt)))
  7733. ((and (featurep 'org-id)
  7734. (or (eq org-link-to-org-use-id t)
  7735. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7736. (org-called-interactively-p 'any))
  7737. (and (eq org-link-to-org-use-id
  7738. 'create-if-interactive-and-no-custom-id)
  7739. (org-called-interactively-p 'any)
  7740. (not custom-id))
  7741. (and org-link-to-org-use-id
  7742. (org-entry-get nil "ID"))))
  7743. ;; We can make a link using the ID.
  7744. (setq link (condition-case nil
  7745. (prog1 (org-id-store-link)
  7746. (setq desc (plist-get org-store-link-plist
  7747. :description)))
  7748. (error
  7749. ;; probably before first headline, link to file only
  7750. (concat "file:"
  7751. (abbreviate-file-name
  7752. (buffer-file-name (buffer-base-buffer))))))))
  7753. (t
  7754. ;; Just link to current headline
  7755. (setq cpltxt (concat "file:"
  7756. (abbreviate-file-name
  7757. (buffer-file-name (buffer-base-buffer)))))
  7758. ;; Add a context search string
  7759. (when (org-xor org-context-in-file-links arg)
  7760. (setq txt (cond
  7761. ((org-on-heading-p) nil)
  7762. ((org-region-active-p)
  7763. (buffer-substring (region-beginning) (region-end)))
  7764. (t nil)))
  7765. (when (or (null txt) (string-match "\\S-" txt))
  7766. (setq cpltxt
  7767. (concat cpltxt "::"
  7768. (condition-case nil
  7769. (org-make-org-heading-search-string txt)
  7770. (error "")))
  7771. desc (or (nth 4 (ignore-errors
  7772. (org-heading-components))) "NONE"))))
  7773. (if (string-match "::\\'" cpltxt)
  7774. (setq cpltxt (substring cpltxt 0 -2)))
  7775. (setq link (org-make-link cpltxt)))))
  7776. ((buffer-file-name (buffer-base-buffer))
  7777. ;; Just link to this file here.
  7778. (setq cpltxt (concat "file:"
  7779. (abbreviate-file-name
  7780. (buffer-file-name (buffer-base-buffer)))))
  7781. ;; Add a context string
  7782. (when (org-xor org-context-in-file-links arg)
  7783. (setq txt (if (org-region-active-p)
  7784. (buffer-substring (region-beginning) (region-end))
  7785. (buffer-substring (point-at-bol) (point-at-eol))))
  7786. ;; Only use search option if there is some text.
  7787. (when (string-match "\\S-" txt)
  7788. (setq cpltxt
  7789. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7790. desc "NONE")))
  7791. (setq link (org-make-link cpltxt)))
  7792. ((org-called-interactively-p 'interactive)
  7793. (error "Cannot link to a buffer which is not visiting a file"))
  7794. (t (setq link nil)))
  7795. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7796. (setq link (or link cpltxt)
  7797. desc (or desc cpltxt))
  7798. (if (equal desc "NONE") (setq desc nil))
  7799. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7800. (progn
  7801. (setq org-stored-links
  7802. (cons (list link desc) org-stored-links))
  7803. (message "Stored: %s" (or desc link))
  7804. (when custom-id
  7805. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7806. "::#" custom-id))
  7807. (setq org-stored-links
  7808. (cons (list link desc) org-stored-links))))
  7809. (or agenda-link (and link (org-make-link-string link desc)))))))
  7810. (defun org-store-link-props (&rest plist)
  7811. "Store link properties, extract names and addresses."
  7812. (let (x adr)
  7813. (when (setq x (plist-get plist :from))
  7814. (setq adr (mail-extract-address-components x))
  7815. (setq plist (plist-put plist :fromname (car adr)))
  7816. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7817. (when (setq x (plist-get plist :to))
  7818. (setq adr (mail-extract-address-components x))
  7819. (setq plist (plist-put plist :toname (car adr)))
  7820. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7821. (let ((from (plist-get plist :from))
  7822. (to (plist-get plist :to)))
  7823. (when (and from to org-from-is-user-regexp)
  7824. (setq plist
  7825. (plist-put plist :fromto
  7826. (if (string-match org-from-is-user-regexp from)
  7827. (concat "to %t")
  7828. (concat "from %f"))))))
  7829. (setq org-store-link-plist plist))
  7830. (defun org-add-link-props (&rest plist)
  7831. "Add these properties to the link property list."
  7832. (let (key value)
  7833. (while plist
  7834. (setq key (pop plist) value (pop plist))
  7835. (setq org-store-link-plist
  7836. (plist-put org-store-link-plist key value)))))
  7837. (defun org-email-link-description (&optional fmt)
  7838. "Return the description part of an email link.
  7839. This takes information from `org-store-link-plist' and formats it
  7840. according to FMT (default from `org-email-link-description-format')."
  7841. (setq fmt (or fmt org-email-link-description-format))
  7842. (let* ((p org-store-link-plist)
  7843. (to (plist-get p :toaddress))
  7844. (from (plist-get p :fromaddress))
  7845. (table
  7846. (list
  7847. (cons "%c" (plist-get p :fromto))
  7848. (cons "%F" (plist-get p :from))
  7849. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7850. (cons "%T" (plist-get p :to))
  7851. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7852. (cons "%s" (plist-get p :subject))
  7853. (cons "%d" (plist-get p :date))
  7854. (cons "%m" (plist-get p :message-id)))))
  7855. (when (string-match "%c" fmt)
  7856. ;; Check if the user wrote this message
  7857. (if (and org-from-is-user-regexp from to
  7858. (save-match-data (string-match org-from-is-user-regexp from)))
  7859. (setq fmt (replace-match "to %t" t t fmt))
  7860. (setq fmt (replace-match "from %f" t t fmt))))
  7861. (org-replace-escapes fmt table)))
  7862. (defun org-make-org-heading-search-string (&optional string heading)
  7863. "Make search string for STRING or current headline."
  7864. (interactive)
  7865. (let ((s (or string (org-get-heading)))
  7866. (lines org-context-in-file-links))
  7867. (unless (and string (not heading))
  7868. ;; We are using a headline, clean up garbage in there.
  7869. (if (string-match org-todo-regexp s)
  7870. (setq s (replace-match "" t t s)))
  7871. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7872. (setq s (replace-match "" t t s)))
  7873. (setq s (org-trim s))
  7874. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7875. org-comment-string "\\)") s)
  7876. (setq s (replace-match "" t t s)))
  7877. (while (string-match org-ts-regexp s)
  7878. (setq s (replace-match "" t t s))))
  7879. (or string (setq s (concat "*" s))) ; Add * for headlines
  7880. (when (and string (integerp lines) (> lines 0))
  7881. (let ((slines (org-split-string s "\n")))
  7882. (when (< lines (length slines))
  7883. (setq s (mapconcat
  7884. 'identity
  7885. (reverse (nthcdr (- (length slines) lines)
  7886. (reverse slines))) "\n")))))
  7887. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7888. (defun org-make-link (&rest strings)
  7889. "Concatenate STRINGS."
  7890. (apply 'concat strings))
  7891. (defun org-make-link-string (link &optional description)
  7892. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7893. (unless (string-match "\\S-" link)
  7894. (error "Empty link"))
  7895. (when (and description
  7896. (stringp description)
  7897. (not (string-match "\\S-" description)))
  7898. (setq description nil))
  7899. (when (stringp description)
  7900. ;; Remove brackets from the description, they are fatal.
  7901. (while (string-match "\\[" description)
  7902. (setq description (replace-match "{" t t description)))
  7903. (while (string-match "\\]" description)
  7904. (setq description (replace-match "}" t t description))))
  7905. (when (equal link description)
  7906. ;; No description needed, it is identical
  7907. (setq description nil))
  7908. (when (and (not description)
  7909. (not (string-match (org-image-file-name-regexp) link))
  7910. (not (equal link (org-link-escape link))))
  7911. (setq description (org-extract-attributes link)))
  7912. (setq link
  7913. (cond ((string-match (org-image-file-name-regexp) link) link)
  7914. ((string-match org-link-types-re link)
  7915. (concat (match-string 1 link)
  7916. (org-link-escape (substring link (match-end 1)))))
  7917. (t (org-link-escape link))))
  7918. (concat "[[" link "]"
  7919. (if description (concat "[" description "]") "")
  7920. "]"))
  7921. (defconst org-link-escape-chars
  7922. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  7923. "List of characters that should be escaped in link.
  7924. This is the list that is used for internal purposes.")
  7925. (defvar org-url-encoding-use-url-hexify nil)
  7926. (defconst org-link-escape-chars-browser
  7927. '(?\ )
  7928. "List of escapes for characters that are problematic in links.
  7929. This is the list that is used before handing over to the browser.")
  7930. (defun org-link-escape (text &optional table merge)
  7931. "Return percent escaped representation of TEXT.
  7932. TEXT is a string with the text to escape.
  7933. Optional argument TABLE is a list with characters that should be
  7934. escaped. When nil, `org-link-escape-chars' is used.
  7935. If optional argument MERGE is set, merge TABLE into
  7936. `org-link-escape-chars'."
  7937. (if (and org-url-encoding-use-url-hexify (not table))
  7938. (url-hexify-string text)
  7939. (cond
  7940. ((and table merge)
  7941. (mapc (lambda (defchr)
  7942. (unless (member defchr table)
  7943. (setq table (cons defchr table)))) org-link-escape-chars))
  7944. ((null table)
  7945. (setq table org-link-escape-chars)))
  7946. (mapconcat
  7947. (lambda (char)
  7948. (if (or (member char table)
  7949. (< char 32) (= char 37) (> char 126))
  7950. (mapconcat (lambda (sequence-element)
  7951. (format "%%%.2X" sequence-element))
  7952. (or (encode-coding-char char 'utf-8)
  7953. (error "Unable to percent escape character: %s"
  7954. (char-to-string char))) "")
  7955. (char-to-string char))) text "")))
  7956. (defun org-link-unescape (str)
  7957. "Unhex hexified unicode strings as returned from the JavaScript function
  7958. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  7959. (unless (and (null str) (string= "" str))
  7960. (let ((pos 0) (case-fold-search t) unhexed)
  7961. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  7962. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  7963. (setq str (replace-match unhexed t t str))
  7964. (setq pos (+ pos (length unhexed))))))
  7965. str)
  7966. (defun org-link-unescape-compound (hex)
  7967. "Unhexify unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  7968. Note: this function also decodes single byte encodings like
  7969. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  7970. (save-match-data
  7971. (let* ((bytes (cdr (split-string hex "%")))
  7972. (ret "")
  7973. (eat 0)
  7974. (sum 0))
  7975. (while bytes
  7976. (let* ((val (string-to-number (pop bytes) 16))
  7977. (shift-xor
  7978. (if (= 0 eat)
  7979. (cond
  7980. ((>= val 252) (cons 6 252))
  7981. ((>= val 248) (cons 5 248))
  7982. ((>= val 240) (cons 4 240))
  7983. ((>= val 224) (cons 3 224))
  7984. ((>= val 192) (cons 2 192))
  7985. (t (cons 0 0)))
  7986. (cons 6 128))))
  7987. (if (>= val 192) (setq eat (car shift-xor)))
  7988. (setq val (logxor val (cdr shift-xor)))
  7989. (setq sum (+ (lsh sum (car shift-xor)) val))
  7990. (if (> eat 0) (setq eat (- eat 1)))
  7991. (cond
  7992. ((= 0 eat) ;multi byte
  7993. (setq ret (concat ret (org-char-to-string sum)))
  7994. (setq sum 0))
  7995. ((not bytes) ; single byte(s)
  7996. (setq ret (org-link-unescape-single-byte-sequence hex))))
  7997. )) ;; end (while bytes
  7998. ret )))
  7999. (defun org-link-unescape-single-byte-sequence (hex)
  8000. "Unhexify hex-encoded single byte character sequences."
  8001. (mapconcat (lambda (byte)
  8002. (char-to-string (string-to-number byte 16)))
  8003. (cdr (split-string hex "%")) ""))
  8004. (defun org-xor (a b)
  8005. "Exclusive or."
  8006. (if a (not b) b))
  8007. (defun org-fixup-message-id-for-http (s)
  8008. "Replace special characters in a message id, so it can be used in an http query."
  8009. (when (string-match "%" s)
  8010. (setq s (mapconcat (lambda (c)
  8011. (if (eq c ?%)
  8012. "%25"
  8013. (char-to-string c)))
  8014. s "")))
  8015. (while (string-match "<" s)
  8016. (setq s (replace-match "%3C" t t s)))
  8017. (while (string-match ">" s)
  8018. (setq s (replace-match "%3E" t t s)))
  8019. (while (string-match "@" s)
  8020. (setq s (replace-match "%40" t t s)))
  8021. s)
  8022. ;;;###autoload
  8023. (defun org-insert-link-global ()
  8024. "Insert a link like Org-mode does.
  8025. This command can be called in any mode to insert a link in Org-mode syntax."
  8026. (interactive)
  8027. (org-load-modules-maybe)
  8028. (org-run-like-in-org-mode 'org-insert-link))
  8029. (defun org-insert-link (&optional complete-file link-location)
  8030. "Insert a link. At the prompt, enter the link.
  8031. Completion can be used to insert any of the link protocol prefixes like
  8032. http or ftp in use.
  8033. The history can be used to select a link previously stored with
  8034. `org-store-link'. When the empty string is entered (i.e. if you just
  8035. press RET at the prompt), the link defaults to the most recently
  8036. stored link. As SPC triggers completion in the minibuffer, you need to
  8037. use M-SPC or C-q SPC to force the insertion of a space character.
  8038. You will also be prompted for a description, and if one is given, it will
  8039. be displayed in the buffer instead of the link.
  8040. If there is already a link at point, this command will allow you to edit link
  8041. and description parts.
  8042. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8043. be selected using completion. The path to the file will be relative to the
  8044. current directory if the file is in the current directory or a subdirectory.
  8045. Otherwise, the link will be the absolute path as completed in the minibuffer
  8046. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8047. option `org-link-file-path-type'.
  8048. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8049. the current directory or below.
  8050. With three \\[universal-argument] prefixes, negate the meaning of
  8051. `org-keep-stored-link-after-insertion'.
  8052. If `org-make-link-description-function' is non-nil, this function will be
  8053. called with the link target, and the result will be the default
  8054. link description.
  8055. If the LINK-LOCATION parameter is non-nil, this value will be
  8056. used as the link location instead of reading one interactively."
  8057. (interactive "P")
  8058. (let* ((wcf (current-window-configuration))
  8059. (region (if (org-region-active-p)
  8060. (buffer-substring (region-beginning) (region-end))))
  8061. (remove (and region (list (region-beginning) (region-end))))
  8062. (desc region)
  8063. tmphist ; byte-compile incorrectly complains about this
  8064. (link link-location)
  8065. entry file all-prefixes)
  8066. (cond
  8067. (link-location) ; specified by arg, just use it.
  8068. ((org-in-regexp org-bracket-link-regexp 1)
  8069. ;; We do have a link at point, and we are going to edit it.
  8070. (setq remove (list (match-beginning 0) (match-end 0)))
  8071. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8072. (setq link (read-string "Link: "
  8073. (org-link-unescape
  8074. (org-match-string-no-properties 1)))))
  8075. ((or (org-in-regexp org-angle-link-re)
  8076. (org-in-regexp org-plain-link-re))
  8077. ;; Convert to bracket link
  8078. (setq remove (list (match-beginning 0) (match-end 0))
  8079. link (read-string "Link: "
  8080. (org-remove-angle-brackets (match-string 0)))))
  8081. ((member complete-file '((4) (16)))
  8082. ;; Completing read for file names.
  8083. (setq link (org-file-complete-link complete-file)))
  8084. (t
  8085. ;; Read link, with completion for stored links.
  8086. (with-output-to-temp-buffer "*Org Links*"
  8087. (princ "Insert a link.
  8088. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8089. (when org-stored-links
  8090. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8091. (princ (mapconcat
  8092. (lambda (x)
  8093. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8094. (reverse org-stored-links) "\n"))))
  8095. (let ((cw (selected-window)))
  8096. (select-window (get-buffer-window "*Org Links*" 'visible))
  8097. (with-current-buffer "*Org Links*" (setq truncate-lines) t)
  8098. (unless (pos-visible-in-window-p (point-max))
  8099. (org-fit-window-to-buffer))
  8100. (and (window-live-p cw) (select-window cw)))
  8101. ;; Fake a link history, containing the stored links.
  8102. (setq tmphist (append (mapcar 'car org-stored-links)
  8103. org-insert-link-history))
  8104. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8105. (mapcar 'car org-link-abbrev-alist)
  8106. org-link-types))
  8107. (unwind-protect
  8108. (progn
  8109. (setq link
  8110. (let ((org-completion-use-ido nil)
  8111. (org-completion-use-iswitchb nil))
  8112. (org-completing-read
  8113. "Link: "
  8114. (append
  8115. (mapcar (lambda (x) (list (concat x ":")))
  8116. all-prefixes)
  8117. (mapcar 'car org-stored-links))
  8118. nil nil nil
  8119. 'tmphist
  8120. (car (car org-stored-links)))))
  8121. (if (not (string-match "\\S-" link))
  8122. (error "No link selected"))
  8123. (if (or (member link all-prefixes)
  8124. (and (equal ":" (substring link -1))
  8125. (member (substring link 0 -1) all-prefixes)
  8126. (setq link (substring link 0 -1))))
  8127. (setq link (org-link-try-special-completion link))))
  8128. (set-window-configuration wcf)
  8129. (kill-buffer "*Org Links*"))
  8130. (setq entry (assoc link org-stored-links))
  8131. (or entry (push link org-insert-link-history))
  8132. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8133. (not org-keep-stored-link-after-insertion))
  8134. (setq org-stored-links (delq (assoc link org-stored-links)
  8135. org-stored-links)))
  8136. (setq desc (or desc (nth 1 entry)))))
  8137. (if (string-match org-plain-link-re link)
  8138. ;; URL-like link, normalize the use of angular brackets.
  8139. (setq link (org-make-link (org-remove-angle-brackets link))))
  8140. ;; Check if we are linking to the current file with a search option
  8141. ;; If yes, simplify the link by using only the search option.
  8142. (when (and buffer-file-name
  8143. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8144. (let* ((path (match-string 1 link))
  8145. (case-fold-search nil)
  8146. (search (match-string 2 link)))
  8147. (save-match-data
  8148. (if (equal (file-truename buffer-file-name) (file-truename path))
  8149. ;; We are linking to this same file, with a search option
  8150. (setq link search)))))
  8151. ;; Check if we can/should use a relative path. If yes, simplify the link
  8152. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8153. (let* ((type (match-string 1 link))
  8154. (path (match-string 2 link))
  8155. (origpath path)
  8156. (case-fold-search nil))
  8157. (cond
  8158. ((or (eq org-link-file-path-type 'absolute)
  8159. (equal complete-file '(16)))
  8160. (setq path (abbreviate-file-name (expand-file-name path))))
  8161. ((eq org-link-file-path-type 'noabbrev)
  8162. (setq path (expand-file-name path)))
  8163. ((eq org-link-file-path-type 'relative)
  8164. (setq path (file-relative-name path)))
  8165. (t
  8166. (save-match-data
  8167. (if (string-match (concat "^" (regexp-quote
  8168. (expand-file-name
  8169. (file-name-as-directory
  8170. default-directory))))
  8171. (expand-file-name path))
  8172. ;; We are linking a file with relative path name.
  8173. (setq path (substring (expand-file-name path)
  8174. (match-end 0)))
  8175. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8176. (setq link (concat type path))
  8177. (if (equal desc origpath)
  8178. (setq desc path))))
  8179. (if org-make-link-description-function
  8180. (setq desc (funcall org-make-link-description-function link desc)))
  8181. (setq desc (read-string "Description: " desc))
  8182. (unless (string-match "\\S-" desc) (setq desc nil))
  8183. (if remove (apply 'delete-region remove))
  8184. (insert (org-make-link-string link desc))))
  8185. (defun org-link-try-special-completion (type)
  8186. "If there is completion support for link type TYPE, offer it."
  8187. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8188. (if (functionp fun)
  8189. (funcall fun)
  8190. (read-string "Link (no completion support): " (concat type ":")))))
  8191. (defun org-file-complete-link (&optional arg)
  8192. "Create a file link using completion."
  8193. (let (file link)
  8194. (setq file (read-file-name "File: "))
  8195. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8196. (pwd1 (file-name-as-directory (abbreviate-file-name
  8197. (expand-file-name ".")))))
  8198. (cond
  8199. ((equal arg '(16))
  8200. (setq link (org-make-link
  8201. "file:"
  8202. (abbreviate-file-name (expand-file-name file)))))
  8203. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8204. (setq link (org-make-link "file:" (match-string 1 file))))
  8205. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8206. (expand-file-name file))
  8207. (setq link (org-make-link
  8208. "file:" (match-string 1 (expand-file-name file)))))
  8209. (t (setq link (org-make-link "file:" file)))))
  8210. link))
  8211. (defun org-completing-read (&rest args)
  8212. "Completing-read with SPACE being a normal character."
  8213. (let ((minibuffer-local-completion-map
  8214. (copy-keymap minibuffer-local-completion-map)))
  8215. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8216. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8217. (apply 'org-icompleting-read args)))
  8218. (defun org-completing-read-no-i (&rest args)
  8219. (let (org-completion-use-ido org-completion-use-iswitchb)
  8220. (apply 'org-completing-read args)))
  8221. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8222. "Use iswitch as a completing-read replacement to choose from choices.
  8223. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8224. from."
  8225. (let* ((iswitchb-use-virtual-buffers nil)
  8226. (iswitchb-make-buflist-hook
  8227. (lambda ()
  8228. (setq iswitchb-temp-buflist choices))))
  8229. (iswitchb-read-buffer prompt)))
  8230. (defun org-icompleting-read (&rest args)
  8231. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8232. (org-without-partial-completion
  8233. (if (and org-completion-use-ido
  8234. (fboundp 'ido-completing-read)
  8235. (boundp 'ido-mode) ido-mode
  8236. (listp (second args)))
  8237. (let ((ido-enter-matching-directory nil))
  8238. (apply 'ido-completing-read (concat (car args))
  8239. (if (consp (car (nth 1 args)))
  8240. (mapcar 'car (nth 1 args))
  8241. (nth 1 args))
  8242. (cddr args)))
  8243. (if (and org-completion-use-iswitchb
  8244. (boundp 'iswitchb-mode) iswitchb-mode
  8245. (listp (second args)))
  8246. (apply 'org-iswitchb-completing-read (concat (car args))
  8247. (if (consp (car (nth 1 args)))
  8248. (mapcar 'car (nth 1 args))
  8249. (nth 1 args))
  8250. (cddr args))
  8251. (apply 'completing-read args)))))
  8252. (defun org-extract-attributes (s)
  8253. "Extract the attributes cookie from a string and set as text property."
  8254. (let (a attr (start 0) key value)
  8255. (save-match-data
  8256. (when (string-match "{{\\([^}]+\\)}}$" s)
  8257. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8258. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8259. (setq key (match-string 1 a) value (match-string 2 a)
  8260. start (match-end 0)
  8261. attr (plist-put attr (intern key) value))))
  8262. (org-add-props s nil 'org-attr attr))
  8263. s))
  8264. (defun org-extract-attributes-from-string (tag)
  8265. (let (key value attr)
  8266. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8267. (setq key (match-string 1 tag) value (match-string 2 tag)
  8268. tag (replace-match "" t t tag)
  8269. attr (plist-put attr (intern key) value)))
  8270. (cons tag attr)))
  8271. (defun org-attributes-to-string (plist)
  8272. "Format a property list into an HTML attribute list."
  8273. (let ((s "") key value)
  8274. (while plist
  8275. (setq key (pop plist) value (pop plist))
  8276. (and value
  8277. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8278. s))
  8279. ;;; Opening/following a link
  8280. (defvar org-link-search-failed nil)
  8281. (defvar org-open-link-functions nil
  8282. "Hook for functions finding a plain text link.
  8283. These functions must take a single argument, the link content.
  8284. They will be called for links that look like [[link text][description]]
  8285. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8286. like a filename (e.g. \"./blue.png\").
  8287. These functions will be called *before* Org attempts to resolve the
  8288. link by doing text searches in the current buffer - so if you want a
  8289. link \"[[target]]\" to still find \"<<target>>\", your function should
  8290. handle this as a special case.
  8291. When the function does handle the link, it must return a non-nil value.
  8292. If it decides that it is not responsible for this link, it must return
  8293. nil to indicate that that Org-mode can continue with other options
  8294. like exact and fuzzy text search.")
  8295. (defun org-next-link ()
  8296. "Move forward to the next link.
  8297. If the link is in hidden text, expose it."
  8298. (interactive)
  8299. (when (and org-link-search-failed (eq this-command last-command))
  8300. (goto-char (point-min))
  8301. (message "Link search wrapped back to beginning of buffer"))
  8302. (setq org-link-search-failed nil)
  8303. (let* ((pos (point))
  8304. (ct (org-context))
  8305. (a (assoc :link ct)))
  8306. (if a (goto-char (nth 2 a)))
  8307. (if (re-search-forward org-any-link-re nil t)
  8308. (progn
  8309. (goto-char (match-beginning 0))
  8310. (if (outline-invisible-p) (org-show-context)))
  8311. (goto-char pos)
  8312. (setq org-link-search-failed t)
  8313. (error "No further link found"))))
  8314. (defun org-previous-link ()
  8315. "Move backward to the previous link.
  8316. If the link is in hidden text, expose it."
  8317. (interactive)
  8318. (when (and org-link-search-failed (eq this-command last-command))
  8319. (goto-char (point-max))
  8320. (message "Link search wrapped back to end of buffer"))
  8321. (setq org-link-search-failed nil)
  8322. (let* ((pos (point))
  8323. (ct (org-context))
  8324. (a (assoc :link ct)))
  8325. (if a (goto-char (nth 1 a)))
  8326. (if (re-search-backward org-any-link-re nil t)
  8327. (progn
  8328. (goto-char (match-beginning 0))
  8329. (if (outline-invisible-p) (org-show-context)))
  8330. (goto-char pos)
  8331. (setq org-link-search-failed t)
  8332. (error "No further link found"))))
  8333. (defun org-translate-link (s)
  8334. "Translate a link string if a translation function has been defined."
  8335. (if (and org-link-translation-function
  8336. (fboundp org-link-translation-function)
  8337. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8338. (progn
  8339. (setq s (funcall org-link-translation-function
  8340. (match-string 1) (match-string 2)))
  8341. (concat (car s) ":" (cdr s)))
  8342. s))
  8343. (defun org-translate-link-from-planner (type path)
  8344. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8345. This is still an experimental function, your mileage may vary."
  8346. (cond
  8347. ((member type '("http" "https" "news" "ftp"))
  8348. ;; standard Internet links are the same.
  8349. nil)
  8350. ((and (equal type "irc") (string-match "^//" path))
  8351. ;; Planner has two / at the beginning of an irc link, we have 1.
  8352. ;; We should have zero, actually....
  8353. (setq path (substring path 1)))
  8354. ((and (equal type "lisp") (string-match "^/" path))
  8355. ;; Planner has a slash, we do not.
  8356. (setq type "elisp" path (substring path 1)))
  8357. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8358. ;; A typical message link. Planner has the id after the final slash,
  8359. ;; we separate it with a hash mark
  8360. (setq path (concat (match-string 1 path) "#"
  8361. (org-remove-angle-brackets (match-string 2 path)))))
  8362. )
  8363. (cons type path))
  8364. (defun org-find-file-at-mouse (ev)
  8365. "Open file link or URL at mouse."
  8366. (interactive "e")
  8367. (mouse-set-point ev)
  8368. (org-open-at-point 'in-emacs))
  8369. (defun org-open-at-mouse (ev)
  8370. "Open file link or URL at mouse.
  8371. See the docstring of `org-open-file' for details."
  8372. (interactive "e")
  8373. (mouse-set-point ev)
  8374. (if (eq major-mode 'org-agenda-mode)
  8375. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8376. (org-open-at-point))
  8377. (defvar org-window-config-before-follow-link nil
  8378. "The window configuration before following a link.
  8379. This is saved in case the need arises to restore it.")
  8380. (defvar org-open-link-marker (make-marker)
  8381. "Marker pointing to the location where `org-open-at-point; was called.")
  8382. ;;;###autoload
  8383. (defun org-open-at-point-global ()
  8384. "Follow a link like Org-mode does.
  8385. This command can be called in any mode to follow a link that has
  8386. Org-mode syntax."
  8387. (interactive)
  8388. (org-run-like-in-org-mode 'org-open-at-point))
  8389. ;;;###autoload
  8390. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8391. "Open a link in the string S, as if it was in Org-mode."
  8392. (interactive "sLink: \nP")
  8393. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8394. (with-temp-buffer
  8395. (let ((org-inhibit-startup t))
  8396. (org-mode)
  8397. (insert s)
  8398. (goto-char (point-min))
  8399. (when reference-buffer
  8400. (setq org-link-abbrev-alist-local
  8401. (with-current-buffer reference-buffer
  8402. org-link-abbrev-alist-local)))
  8403. (org-open-at-point arg reference-buffer)))))
  8404. (defvar org-open-at-point-functions nil
  8405. "Hook that is run when following a link at point.
  8406. Functions in this hook must return t if they identify and follow
  8407. a link at point. If they don't find anything interesting at point,
  8408. they must return nil.")
  8409. (defun org-open-at-point (&optional arg reference-buffer)
  8410. "Open link at or after point.
  8411. If there is no link at point, this function will search forward up to
  8412. the end of the current line.
  8413. Normally, files will be opened by an appropriate application. If the
  8414. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8415. With a double prefix argument, try to open outside of Emacs, in the
  8416. application the system uses for this file type."
  8417. (interactive "P")
  8418. ;; if in a code block, then open the block's results
  8419. (unless (call-interactively #'org-babel-open-src-block-result)
  8420. (org-load-modules-maybe)
  8421. (move-marker org-open-link-marker (point))
  8422. (setq org-window-config-before-follow-link (current-window-configuration))
  8423. (org-remove-occur-highlights nil nil t)
  8424. (cond
  8425. ((and (org-on-heading-p)
  8426. (not (org-in-regexp
  8427. (concat org-plain-link-re "\\|"
  8428. org-bracket-link-regexp "\\|"
  8429. org-angle-link-re "\\|"
  8430. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8431. (not (get-text-property (point) 'org-linked-text)))
  8432. (or (org-offer-links-in-entry arg)
  8433. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8434. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8435. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8436. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8437. (not (org-in-regexp org-bracket-link-regexp)))
  8438. (org-footnote-action))
  8439. (t
  8440. (let (type path link line search (pos (point)))
  8441. (catch 'match
  8442. (save-excursion
  8443. (skip-chars-forward "^]\n\r")
  8444. (when (org-in-regexp org-bracket-link-regexp 1)
  8445. (setq link (org-extract-attributes
  8446. (org-link-unescape (org-match-string-no-properties 1))))
  8447. (while (string-match " *\n *" link)
  8448. (setq link (replace-match " " t t link)))
  8449. (setq link (org-link-expand-abbrev link))
  8450. (cond
  8451. ((or (file-name-absolute-p link)
  8452. (string-match "^\\.\\.?/" link))
  8453. (setq type "file" path link))
  8454. ((string-match org-link-re-with-space3 link)
  8455. (setq type (match-string 1 link) path (match-string 2 link)))
  8456. (t (setq type "thisfile" path link)))
  8457. (throw 'match t)))
  8458. (when (get-text-property (point) 'org-linked-text)
  8459. (setq type "thisfile"
  8460. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8461. (1+ (point)) (point))
  8462. path (buffer-substring
  8463. (or (previous-single-property-change pos 'org-linked-text)
  8464. (point-min))
  8465. (or (next-single-property-change pos 'org-linked-text)
  8466. (point-max))))
  8467. (throw 'match t))
  8468. (save-excursion
  8469. (when (or (org-in-regexp org-angle-link-re)
  8470. (org-in-regexp org-plain-link-re))
  8471. (setq type (match-string 1) path (match-string 2))
  8472. (throw 'match t)))
  8473. (save-excursion
  8474. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8475. (setq type "tags"
  8476. path (match-string 1))
  8477. (while (string-match ":" path)
  8478. (setq path (replace-match "+" t t path)))
  8479. (throw 'match t)))
  8480. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8481. (setq type "tree-match"
  8482. path (match-string 1))
  8483. (throw 'match t)))
  8484. (unless path
  8485. (error "No link found"))
  8486. ;; switch back to reference buffer
  8487. ;; needed when if called in a temporary buffer through
  8488. ;; org-open-link-from-string
  8489. (with-current-buffer (or reference-buffer (current-buffer))
  8490. ;; Remove any trailing spaces in path
  8491. (if (string-match " +\\'" path)
  8492. (setq path (replace-match "" t t path)))
  8493. (if (and org-link-translation-function
  8494. (fboundp org-link-translation-function))
  8495. ;; Check if we need to translate the link
  8496. (let ((tmp (funcall org-link-translation-function type path)))
  8497. (setq type (car tmp) path (cdr tmp))))
  8498. (cond
  8499. ((assoc type org-link-protocols)
  8500. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8501. ((equal type "mailto")
  8502. (let ((cmd (car org-link-mailto-program))
  8503. (args (cdr org-link-mailto-program)) args1
  8504. (address path) (subject "") a)
  8505. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8506. (setq address (match-string 1 path)
  8507. subject (org-link-escape (match-string 2 path))))
  8508. (while args
  8509. (cond
  8510. ((not (stringp (car args))) (push (pop args) args1))
  8511. (t (setq a (pop args))
  8512. (if (string-match "%a" a)
  8513. (setq a (replace-match address t t a)))
  8514. (if (string-match "%s" a)
  8515. (setq a (replace-match subject t t a)))
  8516. (push a args1))))
  8517. (apply cmd (nreverse args1))))
  8518. ((member type '("http" "https" "ftp" "news"))
  8519. (browse-url (concat type ":" (org-link-escape
  8520. path org-link-escape-chars-browser))))
  8521. ((string= type "doi")
  8522. (browse-url (concat "http://dx.doi.org/"
  8523. (org-link-escape
  8524. path org-link-escape-chars-browser))))
  8525. ((member type '("message"))
  8526. (browse-url (concat type ":" path)))
  8527. ((string= type "tags")
  8528. (org-tags-view arg path))
  8529. ((string= type "tree-match")
  8530. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8531. ((string= type "file")
  8532. (if (string-match "::\\([0-9]+\\)\\'" path)
  8533. (setq line (string-to-number (match-string 1 path))
  8534. path (substring path 0 (match-beginning 0)))
  8535. (if (string-match "::\\(.+\\)\\'" path)
  8536. (setq search (match-string 1 path)
  8537. path (substring path 0 (match-beginning 0)))))
  8538. (if (string-match "[*?{]" (file-name-nondirectory path))
  8539. (dired path)
  8540. (org-open-file path arg line search)))
  8541. ((string= type "shell")
  8542. (let ((cmd path))
  8543. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8544. (string-match org-confirm-shell-link-not-regexp cmd))
  8545. (not org-confirm-shell-link-function)
  8546. (funcall org-confirm-shell-link-function
  8547. (format "Execute \"%s\" in shell? "
  8548. (org-add-props cmd nil
  8549. 'face 'org-warning))))
  8550. (progn
  8551. (message "Executing %s" cmd)
  8552. (shell-command cmd))
  8553. (error "Abort"))))
  8554. ((string= type "elisp")
  8555. (let ((cmd path))
  8556. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8557. (string-match org-confirm-elisp-link-not-regexp cmd))
  8558. (not org-confirm-elisp-link-function)
  8559. (funcall org-confirm-elisp-link-function
  8560. (format "Execute \"%s\" as elisp? "
  8561. (org-add-props cmd nil
  8562. 'face 'org-warning))))
  8563. (message "%s => %s" cmd
  8564. (if (equal (string-to-char cmd) ?\()
  8565. (eval (read cmd))
  8566. (call-interactively (read cmd))))
  8567. (error "Abort"))))
  8568. ((and (string= type "thisfile")
  8569. (run-hook-with-args-until-success
  8570. 'org-open-link-functions path)))
  8571. ((string= type "thisfile")
  8572. (if arg
  8573. (switch-to-buffer-other-window
  8574. (org-get-buffer-for-internal-link (current-buffer)))
  8575. (org-mark-ring-push))
  8576. (let ((cmd `(org-link-search
  8577. ,path
  8578. ,(cond ((equal arg '(4)) ''occur)
  8579. ((equal arg '(16)) ''org-occur)
  8580. (t nil))
  8581. ,pos)))
  8582. (condition-case nil (eval cmd)
  8583. (error (progn (widen) (eval cmd))))))
  8584. (t
  8585. (browse-url-at-point)))))))
  8586. (move-marker org-open-link-marker nil)
  8587. (run-hook-with-args 'org-follow-link-hook)))
  8588. (defun org-offer-links-in-entry (&optional nth zero)
  8589. "Offer links in the current entry and follow the selected link.
  8590. If there is only one link, follow it immediately as well.
  8591. If NTH is an integer, immediately pick the NTH link found.
  8592. If ZERO is a string, check also this string for a link, and if
  8593. there is one, offer it as link number zero."
  8594. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8595. "\\(" org-angle-link-re "\\)\\|"
  8596. "\\(" org-plain-link-re "\\)"))
  8597. (cnt ?0)
  8598. (in-emacs (if (integerp nth) nil nth))
  8599. have-zero end links link c)
  8600. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8601. (push (match-string 0 zero) links)
  8602. (setq cnt (1- cnt) have-zero t))
  8603. (save-excursion
  8604. (org-back-to-heading t)
  8605. (setq end (save-excursion (outline-next-heading) (point)))
  8606. (while (re-search-forward re end t)
  8607. (push (match-string 0) links))
  8608. (setq links (org-uniquify (reverse links))))
  8609. (cond
  8610. ((null links)
  8611. (message "No links"))
  8612. ((equal (length links) 1)
  8613. (setq link (list (car links))))
  8614. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8615. (setq link (nth (if have-zero nth (1- nth)) links)))
  8616. (t ; we have to select a link
  8617. (save-excursion
  8618. (save-window-excursion
  8619. (delete-other-windows)
  8620. (with-output-to-temp-buffer "*Select Link*"
  8621. (mapc (lambda (l)
  8622. (if (not (string-match org-bracket-link-regexp l))
  8623. (princ (format "[%c] %s\n" (incf cnt)
  8624. (org-remove-angle-brackets l)))
  8625. (if (match-end 3)
  8626. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8627. (match-string 3 l) (match-string 1 l)))
  8628. (princ (format "[%c] %s\n" (incf cnt)
  8629. (match-string 1 l))))))
  8630. links))
  8631. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8632. (message "Select link to open, RET to open all:")
  8633. (setq c (read-char-exclusive))
  8634. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8635. (when (equal c ?q) (error "Abort"))
  8636. (if (equal c ?\C-m)
  8637. (setq link links)
  8638. (setq nth (- c ?0))
  8639. (if have-zero (setq nth (1+ nth)))
  8640. (unless (and (integerp nth) (>= (length links) nth))
  8641. (error "Invalid link selection"))
  8642. (setq link (list (nth (1- nth) links))))))
  8643. (if link
  8644. (let ((buf (current-buffer)))
  8645. (dolist (l link)
  8646. (org-open-link-from-string l in-emacs buf))
  8647. t)
  8648. nil)))
  8649. ;; Add special file links that specify the way of opening
  8650. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8651. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8652. (defun org-open-file-with-system (path)
  8653. "Open file at PATH using the system way of opening it."
  8654. (org-open-file path 'system))
  8655. (defun org-open-file-with-emacs (path)
  8656. "Open file at PATH in Emacs."
  8657. (org-open-file path 'emacs))
  8658. (defun org-remove-file-link-modifiers ()
  8659. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8660. (goto-char (point-min))
  8661. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8662. (org-if-unprotected
  8663. (replace-match "file:" t t))))
  8664. (eval-after-load "org-exp"
  8665. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8666. 'org-remove-file-link-modifiers))
  8667. ;;;; Time estimates
  8668. (defun org-get-effort (&optional pom)
  8669. "Get the effort estimate for the current entry."
  8670. (org-entry-get pom org-effort-property))
  8671. ;;; File search
  8672. (defvar org-create-file-search-functions nil
  8673. "List of functions to construct the right search string for a file link.
  8674. These functions are called in turn with point at the location to
  8675. which the link should point.
  8676. A function in the hook should first test if it would like to
  8677. handle this file type, for example by checking the `major-mode'
  8678. or the file extension. If it decides not to handle this file, it
  8679. should just return nil to give other functions a chance. If it
  8680. does handle the file, it must return the search string to be used
  8681. when following the link. The search string will be part of the
  8682. file link, given after a double colon, and `org-open-at-point'
  8683. will automatically search for it. If special measures must be
  8684. taken to make the search successful, another function should be
  8685. added to the companion hook `org-execute-file-search-functions',
  8686. which see.
  8687. A function in this hook may also use `setq' to set the variable
  8688. `description' to provide a suggestion for the descriptive text to
  8689. be used for this link when it gets inserted into an Org-mode
  8690. buffer with \\[org-insert-link].")
  8691. (defvar org-execute-file-search-functions nil
  8692. "List of functions to execute a file search triggered by a link.
  8693. Functions added to this hook must accept a single argument, the
  8694. search string that was part of the file link, the part after the
  8695. double colon. The function must first check if it would like to
  8696. handle this search, for example by checking the `major-mode' or
  8697. the file extension. If it decides not to handle this search, it
  8698. should just return nil to give other functions a chance. If it
  8699. does handle the search, it must return a non-nil value to keep
  8700. other functions from trying.
  8701. Each function can access the current prefix argument through the
  8702. variable `current-prefix-argument'. Note that a single prefix is
  8703. used to force opening a link in Emacs, so it may be good to only
  8704. use a numeric or double prefix to guide the search function.
  8705. In case this is needed, a function in this hook can also restore
  8706. the window configuration before `org-open-at-point' was called using:
  8707. (set-window-configuration org-window-config-before-follow-link)")
  8708. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8709. (defun org-link-search (s &optional type avoid-pos)
  8710. "Search for a link search option.
  8711. If S is surrounded by forward slashes, it is interpreted as a
  8712. regular expression. In org-mode files, this will create an `org-occur'
  8713. sparse tree. In ordinary files, `occur' will be used to list matches.
  8714. If the current buffer is in `dired-mode', grep will be used to search
  8715. in all files. If AVOID-POS is given, ignore matches near that position."
  8716. (let ((case-fold-search t)
  8717. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8718. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8719. (append '(("") (" ") ("\t") ("\n"))
  8720. org-emphasis-alist)
  8721. "\\|") "\\)"))
  8722. (pos (point))
  8723. (pre nil) (post nil)
  8724. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8725. (cond
  8726. ;; First check if there are any special search functions
  8727. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8728. ;; Now try the builtin stuff
  8729. ((and (equal (string-to-char s0) ?#)
  8730. (> (length s0) 1)
  8731. (save-excursion
  8732. (goto-char (point-min))
  8733. (and
  8734. (re-search-forward
  8735. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8736. (setq type 'dedicated
  8737. pos (match-beginning 0))))
  8738. ;; There is an exact target for this
  8739. (goto-char pos)
  8740. (org-back-to-heading t)))
  8741. ((save-excursion
  8742. (goto-char (point-min))
  8743. (and
  8744. (re-search-forward
  8745. (concat "<<" (regexp-quote s0) ">>") nil t)
  8746. (setq type 'dedicated
  8747. pos (match-beginning 0))))
  8748. ;; There is an exact target for this
  8749. (goto-char pos))
  8750. ((and (string-match "^(\\(.*\\))$" s0)
  8751. (save-excursion
  8752. (goto-char (point-min))
  8753. (and
  8754. (re-search-forward
  8755. (concat "[^[]" (regexp-quote
  8756. (format org-coderef-label-format
  8757. (match-string 1 s0))))
  8758. nil t)
  8759. (setq type 'dedicated
  8760. pos (1+ (match-beginning 0))))))
  8761. ;; There is a coderef target for this
  8762. (goto-char pos))
  8763. ((string-match "^/\\(.*\\)/$" s)
  8764. ;; A regular expression
  8765. (cond
  8766. ((org-mode-p)
  8767. (org-occur (match-string 1 s)))
  8768. ;;((eq major-mode 'dired-mode)
  8769. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8770. (t (org-do-occur (match-string 1 s)))))
  8771. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8772. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8773. (goto-char (point-min))
  8774. (cond
  8775. ((let (case-fold-search)
  8776. (re-search-forward (format org-complex-heading-regexp-format
  8777. (regexp-quote s))
  8778. nil t))
  8779. ;; OK, found a match
  8780. (setq type 'dedicated)
  8781. (goto-char (match-beginning 0)))
  8782. ((and (not org-link-search-inhibit-query)
  8783. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8784. (y-or-n-p "No match - create this as a new heading? "))
  8785. (goto-char (point-max))
  8786. (or (bolp) (newline))
  8787. (insert "* " s "\n")
  8788. (beginning-of-line 0))
  8789. (t
  8790. (goto-char pos)
  8791. (error "No match"))))
  8792. (t
  8793. ;; A normal search string
  8794. (when (equal (string-to-char s) ?*)
  8795. ;; Anchor on headlines, post may include tags.
  8796. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8797. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8798. s (substring s 1)))
  8799. (remove-text-properties
  8800. 0 (length s)
  8801. '(face nil mouse-face nil keymap nil fontified nil) s)
  8802. ;; Make a series of regular expressions to find a match
  8803. (setq words (org-split-string s "[ \n\r\t]+")
  8804. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8805. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8806. "\\)" markers)
  8807. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8808. re2a (concat "[ \t\r\n]" re2a_)
  8809. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8810. re4 (concat "[^a-zA-Z_]" re4_)
  8811. re1 (concat pre re2 post)
  8812. re3 (concat pre (if pre re4_ re4) post)
  8813. re5 (concat pre ".*" re4)
  8814. re2 (concat pre re2)
  8815. re2a (concat pre (if pre re2a_ re2a))
  8816. re4 (concat pre (if pre re4_ re4))
  8817. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8818. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8819. re5 "\\)"
  8820. ))
  8821. (cond
  8822. ((eq type 'org-occur) (org-occur reall))
  8823. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8824. (t (goto-char (point-min))
  8825. (setq type 'fuzzy)
  8826. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8827. (org-search-not-self 1 re1 nil t)
  8828. (org-search-not-self 1 re2 nil t)
  8829. (org-search-not-self 1 re2a nil t)
  8830. (org-search-not-self 1 re3 nil t)
  8831. (org-search-not-self 1 re4 nil t)
  8832. (org-search-not-self 1 re5 nil t)
  8833. )
  8834. (goto-char (match-beginning 1))
  8835. (goto-char pos)
  8836. (error "No match"))))))
  8837. (and (org-mode-p) (org-show-context 'link-search))
  8838. type))
  8839. (defun org-search-not-self (group &rest args)
  8840. "Execute `re-search-forward', but only accept matches that do not
  8841. enclose the position of `org-open-link-marker'."
  8842. (let ((m org-open-link-marker))
  8843. (catch 'exit
  8844. (while (apply 're-search-forward args)
  8845. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8846. (goto-char (match-end group))
  8847. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8848. (> (match-beginning 0) (marker-position m))
  8849. (< (match-end 0) (marker-position m)))
  8850. (save-match-data
  8851. (or (not (org-in-regexp
  8852. org-bracket-link-analytic-regexp 1))
  8853. (not (match-end 4)) ; no description
  8854. (and (<= (match-beginning 4) (point))
  8855. (>= (match-end 4) (point))))))
  8856. (throw 'exit (point))))))))
  8857. (defun org-get-buffer-for-internal-link (buffer)
  8858. "Return a buffer to be used for displaying the link target of internal links."
  8859. (cond
  8860. ((not org-display-internal-link-with-indirect-buffer)
  8861. buffer)
  8862. ((string-match "(Clone)$" (buffer-name buffer))
  8863. (message "Buffer is already a clone, not making another one")
  8864. ;; we also do not modify visibility in this case
  8865. buffer)
  8866. (t ; make a new indirect buffer for displaying the link
  8867. (let* ((bn (buffer-name buffer))
  8868. (ibn (concat bn "(Clone)"))
  8869. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8870. (with-current-buffer ib (org-overview))
  8871. ib))))
  8872. (defun org-do-occur (regexp &optional cleanup)
  8873. "Call the Emacs command `occur'.
  8874. If CLEANUP is non-nil, remove the printout of the regular expression
  8875. in the *Occur* buffer. This is useful if the regex is long and not useful
  8876. to read."
  8877. (occur regexp)
  8878. (when cleanup
  8879. (let ((cwin (selected-window)) win beg end)
  8880. (when (setq win (get-buffer-window "*Occur*"))
  8881. (select-window win))
  8882. (goto-char (point-min))
  8883. (when (re-search-forward "match[a-z]+" nil t)
  8884. (setq beg (match-end 0))
  8885. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8886. (setq end (1- (match-beginning 0)))))
  8887. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8888. (goto-char (point-min))
  8889. (select-window cwin))))
  8890. ;;; The mark ring for links jumps
  8891. (defvar org-mark-ring nil
  8892. "Mark ring for positions before jumps in Org-mode.")
  8893. (defvar org-mark-ring-last-goto nil
  8894. "Last position in the mark ring used to go back.")
  8895. ;; Fill and close the ring
  8896. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8897. (loop for i from 1 to org-mark-ring-length do
  8898. (push (make-marker) org-mark-ring))
  8899. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8900. org-mark-ring)
  8901. (defun org-mark-ring-push (&optional pos buffer)
  8902. "Put the current position or POS into the mark ring and rotate it."
  8903. (interactive)
  8904. (setq pos (or pos (point)))
  8905. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8906. (move-marker (car org-mark-ring)
  8907. (or pos (point))
  8908. (or buffer (current-buffer)))
  8909. (message "%s"
  8910. (substitute-command-keys
  8911. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8912. (defun org-mark-ring-goto (&optional n)
  8913. "Jump to the previous position in the mark ring.
  8914. With prefix arg N, jump back that many stored positions. When
  8915. called several times in succession, walk through the entire ring.
  8916. Org-mode commands jumping to a different position in the current file,
  8917. or to another Org-mode file, automatically push the old position
  8918. onto the ring."
  8919. (interactive "p")
  8920. (let (p m)
  8921. (if (eq last-command this-command)
  8922. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8923. (setq p org-mark-ring))
  8924. (setq org-mark-ring-last-goto p)
  8925. (setq m (car p))
  8926. (org-pop-to-buffer-same-window (marker-buffer m))
  8927. (goto-char m)
  8928. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8929. (defun org-remove-angle-brackets (s)
  8930. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8931. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8932. s)
  8933. (defun org-add-angle-brackets (s)
  8934. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8935. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8936. s)
  8937. (defun org-remove-double-quotes (s)
  8938. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8939. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8940. s)
  8941. ;;; Following specific links
  8942. (defun org-follow-timestamp-link ()
  8943. (cond
  8944. ((org-at-date-range-p t)
  8945. (let ((org-agenda-start-on-weekday)
  8946. (t1 (match-string 1))
  8947. (t2 (match-string 2)))
  8948. (setq t1 (time-to-days (org-time-string-to-time t1))
  8949. t2 (time-to-days (org-time-string-to-time t2)))
  8950. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8951. ((org-at-timestamp-p t)
  8952. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8953. (substring (match-string 1) 0 10)))
  8954. 1))
  8955. (t (error "This should not happen"))))
  8956. ;;; Following file links
  8957. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  8958. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  8959. (declare-function mailcap-mime-info
  8960. "mailcap" (string &optional request no-decode))
  8961. (defvar org-wait nil)
  8962. (defun org-open-file (path &optional in-emacs line search)
  8963. "Open the file at PATH.
  8964. First, this expands any special file name abbreviations. Then the
  8965. configuration variable `org-file-apps' is checked if it contains an
  8966. entry for this file type, and if yes, the corresponding command is launched.
  8967. If no application is found, Emacs simply visits the file.
  8968. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8969. With a double \\[universal-argument] \\[universal-argument] \
  8970. prefix arg, Org tries to avoid opening in Emacs
  8971. and to use an external application to visit the file.
  8972. Optional LINE specifies a line to go to, optional SEARCH a string
  8973. to search for. If LINE or SEARCH is given, the file will be
  8974. opened in Emacs, unless an entry from org-file-apps that makes
  8975. use of groups in a regexp matches.
  8976. If you want to change the way frames are used when following a
  8977. link, please customize `org-link-frame-setup'.
  8978. If the file does not exist, an error is thrown."
  8979. (let* ((file (if (equal path "")
  8980. buffer-file-name
  8981. (substitute-in-file-name (expand-file-name path))))
  8982. (file-apps (append org-file-apps (org-default-apps)))
  8983. (apps (org-remove-if
  8984. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8985. (apps-dlink (org-remove-if-not
  8986. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8987. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8988. (dirp (if remp nil (file-directory-p file)))
  8989. (file (if (and dirp org-open-directory-means-index-dot-org)
  8990. (concat (file-name-as-directory file) "index.org")
  8991. file))
  8992. (a-m-a-p (assq 'auto-mode apps))
  8993. (dfile (downcase file))
  8994. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8995. (link (cond ((and (eq line nil)
  8996. (eq search nil))
  8997. file)
  8998. (line
  8999. (concat file "::" (number-to-string line)))
  9000. (search
  9001. (concat file "::" search))))
  9002. (dlink (downcase link))
  9003. (old-buffer (current-buffer))
  9004. (old-pos (point))
  9005. (old-mode major-mode)
  9006. ext cmd link-match-data)
  9007. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9008. (setq ext (match-string 1 dfile))
  9009. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9010. (setq ext (match-string 1 dfile))))
  9011. (cond
  9012. ((member in-emacs '((16) system))
  9013. (setq cmd (cdr (assoc 'system apps))))
  9014. (in-emacs (setq cmd 'emacs))
  9015. (t
  9016. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9017. (and dirp (cdr (assoc 'directory apps)))
  9018. ; first, try matching against apps-dlink
  9019. ; if we get a match here, store the match data for later
  9020. (let ((match (assoc-default dlink apps-dlink
  9021. 'string-match)))
  9022. (if match
  9023. (progn (setq link-match-data (match-data))
  9024. match)
  9025. (progn (setq in-emacs (or in-emacs line search))
  9026. nil))) ; if we have no match in apps-dlink,
  9027. ; always open the file in emacs if line or search
  9028. ; is given (for backwards compatibility)
  9029. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9030. 'string-match)
  9031. (cdr (assoc ext apps))
  9032. (cdr (assoc t apps))))))
  9033. (when (eq cmd 'system)
  9034. (setq cmd (cdr (assoc 'system apps))))
  9035. (when (eq cmd 'default)
  9036. (setq cmd (cdr (assoc t apps))))
  9037. (when (eq cmd 'mailcap)
  9038. (require 'mailcap)
  9039. (mailcap-parse-mailcaps)
  9040. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9041. (command (mailcap-mime-info mime-type)))
  9042. (if (stringp command)
  9043. (setq cmd command)
  9044. (setq cmd 'emacs))))
  9045. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9046. (not (file-exists-p file))
  9047. (not org-open-non-existing-files))
  9048. (error "No such file: %s" file))
  9049. (cond
  9050. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9051. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9052. (while (string-match "['\"]%s['\"]" cmd)
  9053. (setq cmd (replace-match "%s" t t cmd)))
  9054. (while (string-match "%s" cmd)
  9055. (setq cmd (replace-match
  9056. (save-match-data
  9057. (shell-quote-argument
  9058. (convert-standard-filename file)))
  9059. t t cmd)))
  9060. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9061. (save-match-data
  9062. (let ((match-index 1)
  9063. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9064. (set-match-data link-match-data)
  9065. (while (<= match-index number-of-groups)
  9066. (let ((regex (concat "%" (number-to-string match-index)))
  9067. (replace-with (match-string match-index dlink)))
  9068. (while (string-match regex cmd)
  9069. (setq cmd (replace-match replace-with t t cmd))))
  9070. (setq match-index (+ match-index 1)))))
  9071. (save-window-excursion
  9072. (start-process-shell-command cmd nil cmd)
  9073. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9074. ))
  9075. ((or (stringp cmd)
  9076. (eq cmd 'emacs))
  9077. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9078. (widen)
  9079. (if line (org-goto-line line)
  9080. (if search (org-link-search search))))
  9081. ((consp cmd)
  9082. (let ((file (convert-standard-filename file)))
  9083. (save-match-data
  9084. (set-match-data link-match-data)
  9085. (eval cmd))))
  9086. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9087. (and (org-mode-p) (eq old-mode 'org-mode)
  9088. (or (not (equal old-buffer (current-buffer)))
  9089. (not (equal old-pos (point))))
  9090. (org-mark-ring-push old-pos old-buffer))))
  9091. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9092. "This function returns non-nil if `entry' uses a regular
  9093. expression which should be matched against the whole link by
  9094. org-open-file.
  9095. It assumes that is the case when the entry uses a regular
  9096. expression which has at least one grouping construct and the
  9097. action is either a lisp form or a command string containing
  9098. '%1', i.e. using at least one subexpression match as a
  9099. parameter."
  9100. (let ((selector (car entry))
  9101. (action (cdr entry)))
  9102. (if (stringp selector)
  9103. (and (> (regexp-opt-depth selector) 0)
  9104. (or (and (stringp action)
  9105. (string-match "%[0-9]" action))
  9106. (consp action)))
  9107. nil)))
  9108. (defun org-default-apps ()
  9109. "Return the default applications for this operating system."
  9110. (cond
  9111. ((eq system-type 'darwin)
  9112. org-file-apps-defaults-macosx)
  9113. ((eq system-type 'windows-nt)
  9114. org-file-apps-defaults-windowsnt)
  9115. (t org-file-apps-defaults-gnu)))
  9116. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9117. "Convert extensions to regular expressions in the cars of LIST.
  9118. Also, weed out any non-string entries, because the return value is used
  9119. only for regexp matching.
  9120. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9121. point to the symbol `emacs', indicating that the file should
  9122. be opened in Emacs."
  9123. (append
  9124. (delq nil
  9125. (mapcar (lambda (x)
  9126. (if (not (stringp (car x)))
  9127. nil
  9128. (if (string-match "\\W" (car x))
  9129. x
  9130. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9131. list))
  9132. (if add-auto-mode
  9133. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9134. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9135. (defun org-file-remote-p (file)
  9136. "Test whether FILE specifies a location on a remote system.
  9137. Return non-nil if the location is indeed remote.
  9138. For example, the filename \"/user@host:/foo\" specifies a location
  9139. on the system \"/user@host:\"."
  9140. (cond ((fboundp 'file-remote-p)
  9141. (file-remote-p file))
  9142. ((fboundp 'tramp-handle-file-remote-p)
  9143. (tramp-handle-file-remote-p file))
  9144. ((and (boundp 'ange-ftp-name-format)
  9145. (string-match (car ange-ftp-name-format) file))
  9146. t)
  9147. (t nil)))
  9148. ;;;; Refiling
  9149. (defun org-get-org-file ()
  9150. "Read a filename, with default directory `org-directory'."
  9151. (let ((default (or org-default-notes-file remember-data-file)))
  9152. (read-file-name (format "File name [%s]: " default)
  9153. (file-name-as-directory org-directory)
  9154. default)))
  9155. (defun org-notes-order-reversed-p ()
  9156. "Check if the current file should receive notes in reversed order."
  9157. (cond
  9158. ((not org-reverse-note-order) nil)
  9159. ((eq t org-reverse-note-order) t)
  9160. ((not (listp org-reverse-note-order)) nil)
  9161. (t (catch 'exit
  9162. (let ((all org-reverse-note-order)
  9163. entry)
  9164. (while (setq entry (pop all))
  9165. (if (string-match (car entry) buffer-file-name)
  9166. (throw 'exit (cdr entry))))
  9167. nil)))))
  9168. (defvar org-refile-target-table nil
  9169. "The list of refile targets, created by `org-refile'.")
  9170. (defvar org-agenda-new-buffers nil
  9171. "Buffers created to visit agenda files.")
  9172. (defvar org-refile-cache nil
  9173. "Cache for refile targets.")
  9174. (defvar org-refile-markers nil
  9175. "All the markers used for caching refile locations.")
  9176. (defun org-refile-marker (pos)
  9177. "Get a new refile marker, but only if caching is in use."
  9178. (if (not org-refile-use-cache)
  9179. pos
  9180. (let ((m (make-marker)))
  9181. (move-marker m pos)
  9182. (push m org-refile-markers)
  9183. m)))
  9184. (defun org-refile-cache-clear ()
  9185. "Clear the refile cache and disable all the markers."
  9186. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9187. (setq org-refile-markers nil)
  9188. (setq org-refile-cache nil)
  9189. (message "Refile cache has been cleared"))
  9190. (defun org-refile-cache-check-set (set)
  9191. "Check if all the markers in the cache still have live buffers."
  9192. (let (marker)
  9193. (catch 'exit
  9194. (while (and set (setq marker (nth 3 (pop set))))
  9195. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9196. (when (and marker (null (marker-buffer marker)))
  9197. (message "not found") (sit-for 3)
  9198. (throw 'exit nil)))
  9199. t)))
  9200. (defun org-refile-cache-put (set &rest identifiers)
  9201. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9202. (let* ((key (sha1 (prin1-to-string identifiers)))
  9203. (entry (assoc key org-refile-cache)))
  9204. (if entry
  9205. (setcdr entry set)
  9206. (push (cons key set) org-refile-cache))))
  9207. (defun org-refile-cache-get (&rest identifiers)
  9208. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9209. (cond
  9210. ((not org-refile-cache) nil)
  9211. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9212. (t
  9213. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9214. org-refile-cache))))
  9215. (and set (org-refile-cache-check-set set) set)))))
  9216. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9217. "Produce a table with refile targets."
  9218. (let ((case-fold-search nil)
  9219. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9220. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9221. targets tgs txt re files f desc descre fast-path-p level pos0)
  9222. (message "Getting targets...")
  9223. (with-current-buffer (or default-buffer (current-buffer))
  9224. (while (setq entry (pop entries))
  9225. (setq files (car entry) desc (cdr entry))
  9226. (setq fast-path-p nil)
  9227. (cond
  9228. ((null files) (setq files (list (current-buffer))))
  9229. ((eq files 'org-agenda-files)
  9230. (setq files (org-agenda-files 'unrestricted)))
  9231. ((and (symbolp files) (fboundp files))
  9232. (setq files (funcall files)))
  9233. ((and (symbolp files) (boundp files))
  9234. (setq files (symbol-value files))))
  9235. (if (stringp files) (setq files (list files)))
  9236. (cond
  9237. ((eq (car desc) :tag)
  9238. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9239. ((eq (car desc) :todo)
  9240. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9241. ((eq (car desc) :regexp)
  9242. (setq descre (cdr desc)))
  9243. ((eq (car desc) :level)
  9244. (setq descre (concat "^\\*\\{" (number-to-string
  9245. (if org-odd-levels-only
  9246. (1- (* 2 (cdr desc)))
  9247. (cdr desc)))
  9248. "\\}[ \t]")))
  9249. ((eq (car desc) :maxlevel)
  9250. (setq fast-path-p t)
  9251. (setq descre (concat "^\\*\\{1," (number-to-string
  9252. (if org-odd-levels-only
  9253. (1- (* 2 (cdr desc)))
  9254. (cdr desc)))
  9255. "\\}[ \t]")))
  9256. (t (error "Bad refiling target description %s" desc)))
  9257. (while (setq f (pop files))
  9258. (with-current-buffer
  9259. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9260. (or
  9261. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9262. (progn
  9263. (if (bufferp f) (setq f (buffer-file-name
  9264. (buffer-base-buffer f))))
  9265. (setq f (and f (expand-file-name f)))
  9266. (if (eq org-refile-use-outline-path 'file)
  9267. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9268. (save-excursion
  9269. (save-restriction
  9270. (widen)
  9271. (goto-char (point-min))
  9272. (while (re-search-forward descre nil t)
  9273. (goto-char (setq pos0 (point-at-bol)))
  9274. (catch 'next
  9275. (when org-refile-target-verify-function
  9276. (save-match-data
  9277. (or (funcall org-refile-target-verify-function)
  9278. (throw 'next t))))
  9279. (when (and (looking-at org-complex-heading-regexp)
  9280. (not (member (match-string 4) excluded-entries)))
  9281. (setq level (org-reduced-level
  9282. (- (match-end 1) (match-beginning 1)))
  9283. txt (org-link-display-format (match-string 4))
  9284. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9285. re (format org-complex-heading-regexp-format
  9286. (regexp-quote (match-string 4))))
  9287. (when org-refile-use-outline-path
  9288. (setq txt (mapconcat
  9289. 'org-protect-slash
  9290. (append
  9291. (if (eq org-refile-use-outline-path
  9292. 'file)
  9293. (list (file-name-nondirectory
  9294. (buffer-file-name
  9295. (buffer-base-buffer))))
  9296. (if (eq org-refile-use-outline-path
  9297. 'full-file-path)
  9298. (list (buffer-file-name
  9299. (buffer-base-buffer)))))
  9300. (org-get-outline-path fast-path-p
  9301. level txt)
  9302. (list txt))
  9303. "/")))
  9304. (push (list txt f re (org-refile-marker (point)))
  9305. tgs)))
  9306. (when (= (point) pos0)
  9307. ;; verification function has not moved point
  9308. (goto-char (point-at-eol))))))))
  9309. (when org-refile-use-cache
  9310. (org-refile-cache-put tgs (buffer-file-name) descre))
  9311. (setq targets (append tgs targets))
  9312. ))))
  9313. (message "Getting targets...done")
  9314. (nreverse targets)))
  9315. (defun org-protect-slash (s)
  9316. (while (string-match "/" s)
  9317. (setq s (replace-match "\\" t t s)))
  9318. s)
  9319. (defvar org-olpa (make-vector 20 nil))
  9320. (defun org-get-outline-path (&optional fastp level heading)
  9321. "Return the outline path to the current entry, as a list.
  9322. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9323. routine which makes outline path derivations for an entire file,
  9324. avoiding backtracing. Refile target collection makes use of that."
  9325. (if fastp
  9326. (progn
  9327. (if (> level 19)
  9328. (error "Outline path failure, more than 19 levels"))
  9329. (loop for i from level upto 19 do
  9330. (aset org-olpa i nil))
  9331. (prog1
  9332. (delq nil (append org-olpa nil))
  9333. (aset org-olpa level heading)))
  9334. (let (rtn case-fold-search)
  9335. (save-excursion
  9336. (save-restriction
  9337. (widen)
  9338. (while (org-up-heading-safe)
  9339. (when (looking-at org-complex-heading-regexp)
  9340. (push (org-match-string-no-properties 4) rtn)))
  9341. rtn)))))
  9342. (defun org-format-outline-path (path &optional width prefix)
  9343. "Format the outline path PATH for display.
  9344. Width is the maximum number of characters that is available.
  9345. Prefix is a prefix to be included in the returned string,
  9346. such as the file name."
  9347. (setq width (or width 79))
  9348. (if prefix (setq width (- width (length prefix))))
  9349. (if (not path)
  9350. (or prefix "")
  9351. (let* ((nsteps (length path))
  9352. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9353. (maxwidth (if (<= total-width width)
  9354. 10000 ;; everything fits
  9355. ;; we need to shorten the level headings
  9356. (/ (- width nsteps) nsteps)))
  9357. (org-odd-levels-only nil)
  9358. (n 0)
  9359. (total (1+ (length prefix))))
  9360. (setq maxwidth (max maxwidth 10))
  9361. (concat prefix
  9362. (mapconcat
  9363. (lambda (h)
  9364. (setq n (1+ n))
  9365. (if (and (= n nsteps) (< maxwidth 10000))
  9366. (setq maxwidth (- total-width total)))
  9367. (if (< (length h) maxwidth)
  9368. (progn (setq total (+ total (length h) 1)) h)
  9369. (setq h (substring h 0 (- maxwidth 2))
  9370. total (+ total maxwidth 1))
  9371. (if (string-match "[ \t]+\\'" h)
  9372. (setq h (substring h 0 (match-beginning 0))))
  9373. (setq h (concat h "..")))
  9374. (org-add-props h nil 'face
  9375. (nth (% (1- n) org-n-level-faces)
  9376. org-level-faces))
  9377. h)
  9378. path "/")))))
  9379. (defun org-display-outline-path (&optional file current)
  9380. "Display the current outline path in the echo area."
  9381. (interactive "P")
  9382. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9383. (case-fold-search nil)
  9384. (path (and (org-mode-p) (org-get-outline-path))))
  9385. (if current (setq path (append path
  9386. (save-excursion
  9387. (org-back-to-heading t)
  9388. (if (looking-at org-complex-heading-regexp)
  9389. (list (match-string 4)))))))
  9390. (message "%s"
  9391. (org-format-outline-path
  9392. path
  9393. (1- (frame-width))
  9394. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9395. (defvar org-refile-history nil
  9396. "History for refiling operations.")
  9397. (defvar org-after-refile-insert-hook nil
  9398. "Hook run after `org-refile' has inserted its stuff at the new location.
  9399. Note that this is still *before* the stuff will be removed from
  9400. the *old* location.")
  9401. (defvar org-capture-last-stored-marker)
  9402. (defun org-refile (&optional goto default-buffer rfloc)
  9403. "Move the entry or entries at point to another heading.
  9404. The list of target headings is compiled using the information in
  9405. `org-refile-targets', which see.
  9406. At the target location, the entry is filed as a subitem of the target
  9407. heading. Depending on `org-reverse-note-order', the new subitem will
  9408. either be the first or the last subitem.
  9409. If there is an active region, all entries in that region will be moved.
  9410. However, the region must fulfill the requirement that the first heading
  9411. is the first one sets the top-level of the moved text - at most siblings
  9412. below it are allowed.
  9413. With prefix arg GOTO, the command will only visit the target location
  9414. and not actually move anything.
  9415. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9416. go to the location where the last refiling operation has put the subtree.
  9417. With a prefix argument of `2', refile to the running clock.
  9418. RFLOC can be a refile location obtained in a different way.
  9419. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9420. If you are using target caching (see `org-refile-use-cache'),
  9421. You have to clear the target cache in order to find new targets.
  9422. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9423. prefix argument (`C-u C-u C-u C-c C-w')."
  9424. (interactive "P")
  9425. (if (member goto '(0 (64)))
  9426. (org-refile-cache-clear)
  9427. (let* ((cbuf (current-buffer))
  9428. (regionp (org-region-active-p))
  9429. (region-start (and regionp (region-beginning)))
  9430. (region-end (and regionp (region-end)))
  9431. (region-length (and regionp (- region-end region-start)))
  9432. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9433. pos it nbuf file re level reversed)
  9434. (setq last-command nil)
  9435. (when regionp
  9436. (goto-char region-start)
  9437. (or (bolp) (goto-char (point-at-bol)))
  9438. (setq region-start (point))
  9439. (unless (or (org-kill-is-subtree-p
  9440. (buffer-substring region-start region-end))
  9441. (prog1 org-refile-active-region-within-subtree
  9442. (org-toggle-heading)))
  9443. (error "The region is not a (sequence of) subtree(s)")))
  9444. (if (equal goto '(16))
  9445. (org-refile-goto-last-stored)
  9446. (when (or
  9447. (and (equal goto 2)
  9448. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9449. (prog1
  9450. (setq it (list (or org-clock-heading "running clock")
  9451. (buffer-file-name
  9452. (marker-buffer org-clock-hd-marker))
  9453. ""
  9454. (marker-position org-clock-hd-marker)))
  9455. (setq goto nil)))
  9456. (setq it (or rfloc
  9457. (save-excursion
  9458. (unless goto (org-back-to-heading t))
  9459. (org-refile-get-location
  9460. (cond (goto "Goto")
  9461. (regionp "Refile region to")
  9462. (t "Refile subtree to")) default-buffer
  9463. org-refile-allow-creating-parent-nodes)))))
  9464. (setq file (nth 1 it)
  9465. re (nth 2 it)
  9466. pos (nth 3 it))
  9467. (if (and (not goto)
  9468. pos
  9469. (equal (buffer-file-name) file)
  9470. (if regionp
  9471. (and (>= pos region-start)
  9472. (<= pos region-end))
  9473. (and (>= pos (point))
  9474. (< pos (save-excursion
  9475. (org-end-of-subtree t t))))))
  9476. (error "Cannot refile to position inside the tree or region"))
  9477. (setq nbuf (or (find-buffer-visiting file)
  9478. (find-file-noselect file)))
  9479. (if goto
  9480. (progn
  9481. (org-pop-to-buffer-same-window nbuf)
  9482. (goto-char pos)
  9483. (org-show-context 'org-goto))
  9484. (if regionp
  9485. (progn
  9486. (org-kill-new (buffer-substring region-start region-end))
  9487. (org-save-markers-in-region region-start region-end))
  9488. (org-copy-subtree 1 nil t))
  9489. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9490. (find-file-noselect file)))
  9491. (setq reversed (org-notes-order-reversed-p))
  9492. (save-excursion
  9493. (save-restriction
  9494. (widen)
  9495. (if pos
  9496. (progn
  9497. (goto-char pos)
  9498. (looking-at org-outline-regexp)
  9499. (setq level (org-get-valid-level (funcall outline-level) 1))
  9500. (goto-char
  9501. (if reversed
  9502. (or (outline-next-heading) (point-max))
  9503. (or (save-excursion (org-get-next-sibling))
  9504. (org-end-of-subtree t t)
  9505. (point-max)))))
  9506. (setq level 1)
  9507. (if (not reversed)
  9508. (goto-char (point-max))
  9509. (goto-char (point-min))
  9510. (or (outline-next-heading) (goto-char (point-max)))))
  9511. (if (not (bolp)) (newline))
  9512. (org-paste-subtree level)
  9513. (when org-log-refile
  9514. (org-add-log-setup 'refile nil nil 'findpos
  9515. org-log-refile)
  9516. (unless (eq org-log-refile 'note)
  9517. (save-excursion (org-add-log-note))))
  9518. (and org-auto-align-tags (org-set-tags nil t))
  9519. (bookmark-set "org-refile-last-stored")
  9520. ;; If we are refiling for capture, make sure that the
  9521. ;; last-capture pointers point here
  9522. (when (org-bound-and-true-p org-refile-for-capture)
  9523. (bookmark-set "org-capture-last-stored-marker")
  9524. (move-marker org-capture-last-stored-marker (point)))
  9525. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9526. (run-hooks 'org-after-refile-insert-hook))))
  9527. (if regionp
  9528. (delete-region (point) (+ (point) region-length))
  9529. (org-cut-subtree))
  9530. (when (featurep 'org-inlinetask)
  9531. (org-inlinetask-remove-END-maybe))
  9532. (setq org-markers-to-move nil)
  9533. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9534. (defun org-refile-goto-last-stored ()
  9535. "Go to the location where the last refile was stored."
  9536. (interactive)
  9537. (bookmark-jump "org-refile-last-stored")
  9538. (message "This is the location of the last refile"))
  9539. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9540. "Prompt the user for a refile location, using PROMPT.
  9541. PROMPT should not be suffixed with a colon and a space, because
  9542. this function appends the default value from
  9543. `org-refile-history' automatically, if that is not empty."
  9544. (let ((org-refile-targets org-refile-targets)
  9545. (org-refile-use-outline-path org-refile-use-outline-path)
  9546. excluded-entries)
  9547. (when (and (eq major-mode 'org-mode)
  9548. (not org-refile-use-cache))
  9549. (org-map-tree
  9550. (lambda()
  9551. (setq excluded-entries
  9552. (append excluded-entries (list (org-get-heading t t)))))))
  9553. (setq org-refile-target-table
  9554. (org-refile-get-targets default-buffer excluded-entries)))
  9555. (unless org-refile-target-table
  9556. (error "No refile targets"))
  9557. (let* ((prompt (concat prompt
  9558. (and (car org-refile-history)
  9559. (concat " (default " (car org-refile-history) ")"))
  9560. ": "))
  9561. (cbuf (current-buffer))
  9562. (partial-completion-mode nil)
  9563. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9564. (cfunc (if (and org-refile-use-outline-path
  9565. org-outline-path-complete-in-steps)
  9566. 'org-olpath-completing-read
  9567. 'org-icompleting-read))
  9568. (extra (if org-refile-use-outline-path "/" ""))
  9569. (filename (and cfn (expand-file-name cfn)))
  9570. (tbl (mapcar
  9571. (lambda (x)
  9572. (if (and (not (member org-refile-use-outline-path
  9573. '(file full-file-path)))
  9574. (not (equal filename (nth 1 x))))
  9575. (cons (concat (car x) extra " ("
  9576. (file-name-nondirectory (nth 1 x)) ")")
  9577. (cdr x))
  9578. (cons (concat (car x) extra) (cdr x))))
  9579. org-refile-target-table))
  9580. (completion-ignore-case t)
  9581. pa answ parent-target child parent old-hist)
  9582. (setq old-hist org-refile-history)
  9583. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9584. nil 'org-refile-history (car org-refile-history)))
  9585. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9586. (org-refile-check-position pa)
  9587. (if pa
  9588. (progn
  9589. (when (or (not org-refile-history)
  9590. (not (eq old-hist org-refile-history))
  9591. (not (equal (car pa) (car org-refile-history))))
  9592. (setq org-refile-history
  9593. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9594. org-refile-history
  9595. (cdr org-refile-history))))
  9596. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9597. (pop org-refile-history)))
  9598. pa)
  9599. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9600. (progn
  9601. (setq parent (match-string 1 answ)
  9602. child (match-string 2 answ))
  9603. (setq parent-target (or (assoc parent tbl)
  9604. (assoc (concat parent "/") tbl)))
  9605. (when (and parent-target
  9606. (or (eq new-nodes t)
  9607. (and (eq new-nodes 'confirm)
  9608. (y-or-n-p (format "Create new node \"%s\"? "
  9609. child)))))
  9610. (org-refile-new-child parent-target child)))
  9611. (error "Invalid target location")))))
  9612. (defun org-refile-check-position (refile-pointer)
  9613. "Check if the refile pointer matches the readline to which it points."
  9614. (let* ((file (nth 1 refile-pointer))
  9615. (re (nth 2 refile-pointer))
  9616. (pos (nth 3 refile-pointer))
  9617. buffer)
  9618. (when (org-string-nw-p re)
  9619. (setq buffer (if (markerp pos)
  9620. (marker-buffer pos)
  9621. (or (find-buffer-visiting file)
  9622. (find-file-noselect file))))
  9623. (with-current-buffer buffer
  9624. (save-excursion
  9625. (save-restriction
  9626. (widen)
  9627. (goto-char pos)
  9628. (beginning-of-line 1)
  9629. (unless (org-looking-at-p re)
  9630. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9631. (defun org-refile-new-child (parent-target child)
  9632. "Use refile target PARENT-TARGET to add new CHILD below it."
  9633. (unless parent-target
  9634. (error "Cannot find parent for new node"))
  9635. (let ((file (nth 1 parent-target))
  9636. (pos (nth 3 parent-target))
  9637. level)
  9638. (with-current-buffer (or (find-buffer-visiting file)
  9639. (find-file-noselect file))
  9640. (save-excursion
  9641. (save-restriction
  9642. (widen)
  9643. (if pos
  9644. (goto-char pos)
  9645. (goto-char (point-max))
  9646. (if (not (bolp)) (newline)))
  9647. (when (looking-at org-outline-regexp)
  9648. (setq level (funcall outline-level))
  9649. (org-end-of-subtree t t))
  9650. (org-back-over-empty-lines)
  9651. (insert "\n" (make-string
  9652. (if pos (org-get-valid-level level 1) 1) ?*)
  9653. " " child "\n")
  9654. (beginning-of-line 0)
  9655. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9656. (defun org-olpath-completing-read (prompt collection &rest args)
  9657. "Read an outline path like a file name."
  9658. (let ((thetable collection)
  9659. (org-completion-use-ido nil) ; does not work with ido.
  9660. (org-completion-use-iswitchb nil)) ; or iswitchb
  9661. (apply
  9662. 'org-icompleting-read prompt
  9663. (lambda (string predicate &optional flag)
  9664. (let (rtn r f (l (length string)))
  9665. (cond
  9666. ((eq flag nil)
  9667. ;; try completion
  9668. (try-completion string thetable))
  9669. ((eq flag t)
  9670. ;; all-completions
  9671. (setq rtn (all-completions string thetable predicate))
  9672. (mapcar
  9673. (lambda (x)
  9674. (setq r (substring x l))
  9675. (if (string-match " ([^)]*)$" x)
  9676. (setq f (match-string 0 x))
  9677. (setq f ""))
  9678. (if (string-match "/" r)
  9679. (concat string (substring r 0 (match-end 0)) f)
  9680. x))
  9681. rtn))
  9682. ((eq flag 'lambda)
  9683. ;; exact match?
  9684. (assoc string thetable)))
  9685. ))
  9686. args)))
  9687. ;;;; Dynamic blocks
  9688. (defun org-find-dblock (name)
  9689. "Find the first dynamic block with name NAME in the buffer.
  9690. If not found, stay at current position and return nil."
  9691. (let (pos)
  9692. (save-excursion
  9693. (goto-char (point-min))
  9694. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9695. nil t)
  9696. (match-beginning 0))))
  9697. (if pos (goto-char pos))
  9698. pos))
  9699. (defconst org-dblock-start-re
  9700. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9701. "Matches the start line of a dynamic block, with parameters.")
  9702. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9703. "Matches the end of a dynamic block.")
  9704. (defun org-create-dblock (plist)
  9705. "Create a dynamic block section, with parameters taken from PLIST.
  9706. PLIST must contain a :name entry which is used as name of the block."
  9707. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9708. (end-of-line 1)
  9709. (newline))
  9710. (let ((col (current-column))
  9711. (name (plist-get plist :name)))
  9712. (insert "#+BEGIN: " name)
  9713. (while plist
  9714. (if (eq (car plist) :name)
  9715. (setq plist (cddr plist))
  9716. (insert " " (prin1-to-string (pop plist)))))
  9717. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9718. (beginning-of-line -2)))
  9719. (defun org-prepare-dblock ()
  9720. "Prepare dynamic block for refresh.
  9721. This empties the block, puts the cursor at the insert position and returns
  9722. the property list including an extra property :name with the block name."
  9723. (unless (looking-at org-dblock-start-re)
  9724. (error "Not at a dynamic block"))
  9725. (let* ((begdel (1+ (match-end 0)))
  9726. (name (org-no-properties (match-string 1)))
  9727. (params (append (list :name name)
  9728. (read (concat "(" (match-string 3) ")")))))
  9729. (save-excursion
  9730. (beginning-of-line 1)
  9731. (skip-chars-forward " \t")
  9732. (setq params (plist-put params :indentation-column (current-column))))
  9733. (unless (re-search-forward org-dblock-end-re nil t)
  9734. (error "Dynamic block not terminated"))
  9735. (setq params
  9736. (append params
  9737. (list :content (buffer-substring
  9738. begdel (match-beginning 0)))))
  9739. (delete-region begdel (match-beginning 0))
  9740. (goto-char begdel)
  9741. (open-line 1)
  9742. params))
  9743. (defun org-map-dblocks (&optional command)
  9744. "Apply COMMAND to all dynamic blocks in the current buffer.
  9745. If COMMAND is not given, use `org-update-dblock'."
  9746. (let ((cmd (or command 'org-update-dblock)))
  9747. (save-excursion
  9748. (goto-char (point-min))
  9749. (while (re-search-forward org-dblock-start-re nil t)
  9750. (goto-char (match-beginning 0))
  9751. (save-excursion
  9752. (condition-case nil
  9753. (funcall cmd)
  9754. (error (message "Error during update of dynamic block"))))
  9755. (unless (re-search-forward org-dblock-end-re nil t)
  9756. (error "Dynamic block not terminated"))))))
  9757. (defun org-dblock-update (&optional arg)
  9758. "User command for updating dynamic blocks.
  9759. Update the dynamic block at point. With prefix ARG, update all dynamic
  9760. blocks in the buffer."
  9761. (interactive "P")
  9762. (if arg
  9763. (org-update-all-dblocks)
  9764. (or (looking-at org-dblock-start-re)
  9765. (org-beginning-of-dblock))
  9766. (org-update-dblock)))
  9767. (defun org-update-dblock ()
  9768. "Update the dynamic block at point.
  9769. This means to empty the block, parse for parameters and then call
  9770. the correct writing function."
  9771. (interactive)
  9772. (save-window-excursion
  9773. (let* ((pos (point))
  9774. (line (org-current-line))
  9775. (params (org-prepare-dblock))
  9776. (name (plist-get params :name))
  9777. (indent (plist-get params :indentation-column))
  9778. (cmd (intern (concat "org-dblock-write:" name))))
  9779. (message "Updating dynamic block `%s' at line %d..." name line)
  9780. (funcall cmd params)
  9781. (message "Updating dynamic block `%s' at line %d...done" name line)
  9782. (goto-char pos)
  9783. (when (and indent (> indent 0))
  9784. (setq indent (make-string indent ?\ ))
  9785. (save-excursion
  9786. (org-beginning-of-dblock)
  9787. (forward-line 1)
  9788. (while (not (looking-at org-dblock-end-re))
  9789. (insert indent)
  9790. (beginning-of-line 2))
  9791. (when (looking-at org-dblock-end-re)
  9792. (and (looking-at "[ \t]+")
  9793. (replace-match ""))
  9794. (insert indent)))))))
  9795. (defun org-beginning-of-dblock ()
  9796. "Find the beginning of the dynamic block at point.
  9797. Error if there is no such block at point."
  9798. (let ((pos (point))
  9799. beg)
  9800. (end-of-line 1)
  9801. (if (and (re-search-backward org-dblock-start-re nil t)
  9802. (setq beg (match-beginning 0))
  9803. (re-search-forward org-dblock-end-re nil t)
  9804. (> (match-end 0) pos))
  9805. (goto-char beg)
  9806. (goto-char pos)
  9807. (error "Not in a dynamic block"))))
  9808. (defun org-update-all-dblocks ()
  9809. "Update all dynamic blocks in the buffer.
  9810. This function can be used in a hook."
  9811. (interactive)
  9812. (when (org-mode-p)
  9813. (org-map-dblocks 'org-update-dblock)))
  9814. ;;;; Completion
  9815. (defconst org-additional-option-like-keywords
  9816. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9817. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9818. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9819. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9820. "BEGIN:" "END:"
  9821. "ORGTBL" "TBLFM:" "TBLNAME:"
  9822. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9823. "BEGIN_QUOTE" "END_QUOTE"
  9824. "BEGIN_VERSE" "END_VERSE"
  9825. "BEGIN_CENTER" "END_CENTER"
  9826. "BEGIN_SRC" "END_SRC"
  9827. "BEGIN_RESULT" "END_RESULT"
  9828. "SOURCE:" "SRCNAME:" "FUNCTION:"
  9829. "RESULTS:" "DATA:"
  9830. "HEADER:" "HEADERS:"
  9831. "BABEL:"
  9832. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9833. "CAPTION:" "LABEL:"
  9834. "SETUPFILE:"
  9835. "INCLUDE:"
  9836. "BIND:"
  9837. "MACRO:"))
  9838. (defcustom org-structure-template-alist
  9839. '(
  9840. ("s" "#+begin_src ?\n\n#+end_src"
  9841. "<src lang=\"?\">\n\n</src>")
  9842. ("e" "#+begin_example\n?\n#+end_example"
  9843. "<example>\n?\n</example>")
  9844. ("q" "#+begin_quote\n?\n#+end_quote"
  9845. "<quote>\n?\n</quote>")
  9846. ("v" "#+begin_verse\n?\n#+end_verse"
  9847. "<verse>\n?\n/verse>")
  9848. ("c" "#+begin_center\n?\n#+end_center"
  9849. "<center>\n?\n/center>")
  9850. ("l" "#+begin_latex\n?\n#+end_latex"
  9851. "<literal style=\"latex\">\n?\n</literal>")
  9852. ("L" "#+latex: "
  9853. "<literal style=\"latex\">?</literal>")
  9854. ("h" "#+begin_html\n?\n#+end_html"
  9855. "<literal style=\"html\">\n?\n</literal>")
  9856. ("H" "#+html: "
  9857. "<literal style=\"html\">?</literal>")
  9858. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9859. ("A" "#+ascii: ")
  9860. ("i" "#+index: ?"
  9861. "#+index: ?")
  9862. ("I" "#+include %file ?"
  9863. "<include file=%file markup=\"?\">")
  9864. )
  9865. "Structure completion elements.
  9866. This is a list of abbreviation keys and values. The value gets inserted
  9867. if you type `<' followed by the key and then press the completion key,
  9868. usually `M-TAB'. %file will be replaced by a file name after prompting
  9869. for the file using completion. The cursor will be placed at the position
  9870. of the `?` in the template.
  9871. There are two templates for each key, the first uses the original Org syntax,
  9872. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9873. the default when the /org-mtags.el/ module has been loaded. See also the
  9874. variable `org-mtags-prefer-muse-templates'.
  9875. This is an experimental feature, it is undecided if it is going to stay in."
  9876. :group 'org-completion
  9877. :type '(repeat
  9878. (string :tag "Key")
  9879. (string :tag "Template")
  9880. (string :tag "Muse Template")))
  9881. (defun org-try-structure-completion ()
  9882. "Try to complete a structure template before point.
  9883. This looks for strings like \"<e\" on an otherwise empty line and
  9884. expands them."
  9885. (let ((l (buffer-substring (point-at-bol) (point)))
  9886. a)
  9887. (when (and (looking-at "[ \t]*$")
  9888. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  9889. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9890. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9891. (match-beginning 1)) a)
  9892. t)))
  9893. (defun org-complete-expand-structure-template (start cell)
  9894. "Expand a structure template."
  9895. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9896. (rpl (nth (if musep 2 1) cell))
  9897. (ind ""))
  9898. (delete-region start (point))
  9899. (when (string-match "\\`#\\+" rpl)
  9900. (cond
  9901. ((bolp))
  9902. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9903. (setq ind (buffer-substring (point-at-bol) (point))))
  9904. (t (newline))))
  9905. (setq start (point))
  9906. (if (string-match "%file" rpl)
  9907. (setq rpl (replace-match
  9908. (concat
  9909. "\""
  9910. (save-match-data
  9911. (abbreviate-file-name (read-file-name "Include file: ")))
  9912. "\"")
  9913. t t rpl)))
  9914. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9915. (concat "\n" ind)))
  9916. (insert rpl)
  9917. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9918. ;;;; TODO, DEADLINE, Comments
  9919. (defun org-toggle-comment ()
  9920. "Change the COMMENT state of an entry."
  9921. (interactive)
  9922. (save-excursion
  9923. (org-back-to-heading)
  9924. (let (case-fold-search)
  9925. (if (looking-at (concat org-outline-regexp
  9926. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9927. (replace-match "" t t nil 1)
  9928. (if (looking-at org-outline-regexp)
  9929. (progn
  9930. (goto-char (match-end 0))
  9931. (insert org-comment-string " ")))))))
  9932. (defvar org-last-todo-state-is-todo nil
  9933. "This is non-nil when the last TODO state change led to a TODO state.
  9934. If the last change removed the TODO tag or switched to DONE, then
  9935. this is nil.")
  9936. (defvar org-setting-tags nil) ; dynamically skipped
  9937. (defvar org-todo-setup-filter-hook nil
  9938. "Hook for functions that pre-filter todo specs.
  9939. Each function takes a todo spec and returns either nil or the spec
  9940. transformed into canonical form." )
  9941. (defvar org-todo-get-default-hook nil
  9942. "Hook for functions that get a default item for todo.
  9943. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9944. nil or a string to be used for the todo mark." )
  9945. (defvar org-agenda-headline-snapshot-before-repeat)
  9946. (defun org-current-effective-time ()
  9947. "Return current time adjusted for `org-extend-today-until' variable"
  9948. (let* ((ct (org-current-time))
  9949. (dct (decode-time ct))
  9950. (ct1
  9951. (if (and org-use-effective-time
  9952. (< (nth 2 dct) org-extend-today-until))
  9953. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  9954. ct)))
  9955. ct1))
  9956. (defun org-todo-yesterday (&optional arg)
  9957. "Like `org-todo' but the time of change will be 23:59 of yesterday"
  9958. (interactive "P")
  9959. (let* ((hour (third (decode-time
  9960. (org-current-time))))
  9961. (org-extend-today-until (1+ hour)))
  9962. (org-todo arg)))
  9963. (defun org-todo (&optional arg)
  9964. "Change the TODO state of an item.
  9965. The state of an item is given by a keyword at the start of the heading,
  9966. like
  9967. *** TODO Write paper
  9968. *** DONE Call mom
  9969. The different keywords are specified in the variable `org-todo-keywords'.
  9970. By default the available states are \"TODO\" and \"DONE\".
  9971. So for this example: when the item starts with TODO, it is changed to DONE.
  9972. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9973. DONE are present, add TODO at the beginning of the heading.
  9974. With \\[universal-argument] prefix arg, use completion to determine the new \
  9975. state.
  9976. With numeric prefix arg, switch to that state.
  9977. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9978. keywords (nextset).
  9979. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9980. For calling through lisp, arg is also interpreted in the following way:
  9981. 'none -> empty state
  9982. \"\"(empty string) -> switch to empty state
  9983. 'done -> switch to DONE
  9984. 'nextset -> switch to the next set of keywords
  9985. 'previousset -> switch to the previous set of keywords
  9986. \"WAITING\" -> switch to the specified keyword, but only if it
  9987. really is a member of `org-todo-keywords'."
  9988. (interactive "P")
  9989. (if (equal arg '(16)) (setq arg 'nextset))
  9990. (let ((org-blocker-hook org-blocker-hook)
  9991. (case-fold-search nil))
  9992. (when (equal arg '(64))
  9993. (setq arg nil org-blocker-hook nil))
  9994. (when (and org-blocker-hook
  9995. (or org-inhibit-blocking
  9996. (org-entry-get nil "NOBLOCKING")))
  9997. (setq org-blocker-hook nil))
  9998. (save-excursion
  9999. (catch 'exit
  10000. (org-back-to-heading t)
  10001. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10002. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  10003. (looking-at " *"))
  10004. (let* ((match-data (match-data))
  10005. (startpos (point-at-bol))
  10006. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10007. (org-log-done org-log-done)
  10008. (org-log-repeat org-log-repeat)
  10009. (org-todo-log-states org-todo-log-states)
  10010. (this (match-string 1))
  10011. (hl-pos (match-beginning 0))
  10012. (head (org-get-todo-sequence-head this))
  10013. (ass (assoc head org-todo-kwd-alist))
  10014. (interpret (nth 1 ass))
  10015. (done-word (nth 3 ass))
  10016. (final-done-word (nth 4 ass))
  10017. (last-state (or this ""))
  10018. (completion-ignore-case t)
  10019. (member (member this org-todo-keywords-1))
  10020. (tail (cdr member))
  10021. (state (cond
  10022. ((and org-todo-key-trigger
  10023. (or (and (equal arg '(4))
  10024. (eq org-use-fast-todo-selection 'prefix))
  10025. (and (not arg) org-use-fast-todo-selection
  10026. (not (eq org-use-fast-todo-selection
  10027. 'prefix)))))
  10028. ;; Use fast selection
  10029. (org-fast-todo-selection))
  10030. ((and (equal arg '(4))
  10031. (or (not org-use-fast-todo-selection)
  10032. (not org-todo-key-trigger)))
  10033. ;; Read a state with completion
  10034. (org-icompleting-read
  10035. "State: " (mapcar (lambda(x) (list x))
  10036. org-todo-keywords-1)
  10037. nil t))
  10038. ((eq arg 'right)
  10039. (if this
  10040. (if tail (car tail) nil)
  10041. (car org-todo-keywords-1)))
  10042. ((eq arg 'left)
  10043. (if (equal member org-todo-keywords-1)
  10044. nil
  10045. (if this
  10046. (nth (- (length org-todo-keywords-1)
  10047. (length tail) 2)
  10048. org-todo-keywords-1)
  10049. (org-last org-todo-keywords-1))))
  10050. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10051. (setq arg nil))) ; hack to fall back to cycling
  10052. (arg
  10053. ;; user or caller requests a specific state
  10054. (cond
  10055. ((equal arg "") nil)
  10056. ((eq arg 'none) nil)
  10057. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10058. ((eq arg 'nextset)
  10059. (or (car (cdr (member head org-todo-heads)))
  10060. (car org-todo-heads)))
  10061. ((eq arg 'previousset)
  10062. (let ((org-todo-heads (reverse org-todo-heads)))
  10063. (or (car (cdr (member head org-todo-heads)))
  10064. (car org-todo-heads))))
  10065. ((car (member arg org-todo-keywords-1)))
  10066. ((stringp arg)
  10067. (error "State `%s' not valid in this file" arg))
  10068. ((nth (1- (prefix-numeric-value arg))
  10069. org-todo-keywords-1))))
  10070. ((null member) (or head (car org-todo-keywords-1)))
  10071. ((equal this final-done-word) nil) ;; -> make empty
  10072. ((null tail) nil) ;; -> first entry
  10073. ((memq interpret '(type priority))
  10074. (if (eq this-command last-command)
  10075. (car tail)
  10076. (if (> (length tail) 0)
  10077. (or done-word (car org-done-keywords))
  10078. nil)))
  10079. (t
  10080. (car tail))))
  10081. (state (or
  10082. (run-hook-with-args-until-success
  10083. 'org-todo-get-default-hook state last-state)
  10084. state))
  10085. (next (if state (concat " " state " ") " "))
  10086. (change-plist (list :type 'todo-state-change :from this :to state
  10087. :position startpos))
  10088. dolog now-done-p)
  10089. (when org-blocker-hook
  10090. (setq org-last-todo-state-is-todo
  10091. (not (member this org-done-keywords)))
  10092. (unless (save-excursion
  10093. (save-match-data
  10094. (org-with-wide-buffer
  10095. (run-hook-with-args-until-failure
  10096. 'org-blocker-hook change-plist))))
  10097. (if (org-called-interactively-p 'interactive)
  10098. (error "TODO state change from %s to %s blocked" this state)
  10099. ;; fail silently
  10100. (message "TODO state change from %s to %s blocked" this state)
  10101. (throw 'exit nil))))
  10102. (store-match-data match-data)
  10103. (replace-match next t t)
  10104. (unless (pos-visible-in-window-p hl-pos)
  10105. (message "TODO state changed to %s" (org-trim next)))
  10106. (unless head
  10107. (setq head (org-get-todo-sequence-head state)
  10108. ass (assoc head org-todo-kwd-alist)
  10109. interpret (nth 1 ass)
  10110. done-word (nth 3 ass)
  10111. final-done-word (nth 4 ass)))
  10112. (when (memq arg '(nextset previousset))
  10113. (message "Keyword-Set %d/%d: %s"
  10114. (- (length org-todo-sets) -1
  10115. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10116. (length org-todo-sets)
  10117. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10118. (setq org-last-todo-state-is-todo
  10119. (not (member state org-done-keywords)))
  10120. (setq now-done-p (and (member state org-done-keywords)
  10121. (not (member this org-done-keywords))))
  10122. (and logging (org-local-logging logging))
  10123. (when (and (or org-todo-log-states org-log-done)
  10124. (not (eq org-inhibit-logging t))
  10125. (not (memq arg '(nextset previousset))))
  10126. ;; we need to look at recording a time and note
  10127. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10128. (nth 2 (assoc this org-todo-log-states))))
  10129. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10130. (setq dolog 'time))
  10131. (when (and state
  10132. (member state org-not-done-keywords)
  10133. (not (member this org-not-done-keywords)))
  10134. ;; This is now a todo state and was not one before
  10135. ;; If there was a CLOSED time stamp, get rid of it.
  10136. (org-add-planning-info nil nil 'closed))
  10137. (when (and now-done-p org-log-done)
  10138. ;; It is now done, and it was not done before
  10139. (org-add-planning-info 'closed (org-current-effective-time))
  10140. (if (and (not dolog) (eq 'note org-log-done))
  10141. (org-add-log-setup 'done state this 'findpos 'note)))
  10142. (when (and state dolog)
  10143. ;; This is a non-nil state, and we need to log it
  10144. (org-add-log-setup 'state state this 'findpos dolog)))
  10145. ;; Fixup tag positioning
  10146. (org-todo-trigger-tag-changes state)
  10147. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10148. (when org-provide-todo-statistics
  10149. (org-update-parent-todo-statistics))
  10150. (run-hooks 'org-after-todo-state-change-hook)
  10151. (if (and arg (not (member state org-done-keywords)))
  10152. (setq head (org-get-todo-sequence-head state)))
  10153. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10154. ;; Do we need to trigger a repeat?
  10155. (when now-done-p
  10156. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10157. ;; This is for the agenda, take a snapshot of the headline.
  10158. (save-match-data
  10159. (setq org-agenda-headline-snapshot-before-repeat
  10160. (org-get-heading))))
  10161. (org-auto-repeat-maybe state))
  10162. ;; Fixup cursor location if close to the keyword
  10163. (if (and (outline-on-heading-p)
  10164. (not (bolp))
  10165. (save-excursion (beginning-of-line 1)
  10166. (looking-at org-todo-line-regexp))
  10167. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10168. (progn
  10169. (goto-char (or (match-end 2) (match-end 1)))
  10170. (and (looking-at " ") (just-one-space))))
  10171. (when org-trigger-hook
  10172. (save-excursion
  10173. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10174. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10175. "Block turning an entry into a TODO, using the hierarchy.
  10176. This checks whether the current task should be blocked from state
  10177. changes. Such blocking occurs when:
  10178. 1. The task has children which are not all in a completed state.
  10179. 2. A task has a parent with the property :ORDERED:, and there
  10180. are siblings prior to the current task with incomplete
  10181. status.
  10182. 3. The parent of the task is blocked because it has siblings that should
  10183. be done first, or is child of a block grandparent TODO entry."
  10184. (if (not org-enforce-todo-dependencies)
  10185. t ; if locally turned off don't block
  10186. (catch 'dont-block
  10187. ;; If this is not a todo state change, or if this entry is already DONE,
  10188. ;; do not block
  10189. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10190. (member (plist-get change-plist :from)
  10191. (cons 'done org-done-keywords))
  10192. (member (plist-get change-plist :to)
  10193. (cons 'todo org-not-done-keywords))
  10194. (not (plist-get change-plist :to)))
  10195. (throw 'dont-block t))
  10196. ;; If this task has children, and any are undone, it's blocked
  10197. (save-excursion
  10198. (org-back-to-heading t)
  10199. (let ((this-level (funcall outline-level)))
  10200. (outline-next-heading)
  10201. (let ((child-level (funcall outline-level)))
  10202. (while (and (not (eobp))
  10203. (> child-level this-level))
  10204. ;; this todo has children, check whether they are all
  10205. ;; completed
  10206. (if (and (not (org-entry-is-done-p))
  10207. (org-entry-is-todo-p))
  10208. (throw 'dont-block nil))
  10209. (outline-next-heading)
  10210. (setq child-level (funcall outline-level))))))
  10211. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10212. ;; any previous siblings are undone, it's blocked
  10213. (save-excursion
  10214. (org-back-to-heading t)
  10215. (let* ((pos (point))
  10216. (parent-pos (and (org-up-heading-safe) (point))))
  10217. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10218. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10219. (forward-line 1)
  10220. (re-search-forward org-not-done-heading-regexp pos t))
  10221. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10222. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10223. (while t
  10224. (goto-char parent-pos)
  10225. (if (not (looking-at org-not-done-heading-regexp))
  10226. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10227. (setq pos (point))
  10228. (setq parent-pos (and (org-up-heading-safe) (point)))
  10229. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10230. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10231. (forward-line 1)
  10232. (re-search-forward org-not-done-heading-regexp pos t))
  10233. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10234. (defcustom org-track-ordered-property-with-tag nil
  10235. "Should the ORDERED property also be shown as a tag?
  10236. The ORDERED property decides if an entry should require subtasks to be
  10237. completed in sequence. Since a property is not very visible, setting
  10238. this option means that toggling the ORDERED property with the command
  10239. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10240. not relevant for the behavior, but it makes things more visible.
  10241. Note that toggling the tag with tags commands will not change the property
  10242. and therefore not influence behavior!
  10243. This can be t, meaning the tag ORDERED should be used, It can also be a
  10244. string to select a different tag for this task."
  10245. :group 'org-todo
  10246. :type '(choice
  10247. (const :tag "No tracking" nil)
  10248. (const :tag "Track with ORDERED tag" t)
  10249. (string :tag "Use other tag")))
  10250. (defun org-toggle-ordered-property ()
  10251. "Toggle the ORDERED property of the current entry.
  10252. For better visibility, you can track the value of this property with a tag.
  10253. See variable `org-track-ordered-property-with-tag'."
  10254. (interactive)
  10255. (let* ((t1 org-track-ordered-property-with-tag)
  10256. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10257. (save-excursion
  10258. (org-back-to-heading)
  10259. (if (org-entry-get nil "ORDERED")
  10260. (progn
  10261. (org-delete-property "ORDERED")
  10262. (and tag (org-toggle-tag tag 'off))
  10263. (message "Subtasks can be completed in arbitrary order"))
  10264. (org-entry-put nil "ORDERED" "t")
  10265. (and tag (org-toggle-tag tag 'on))
  10266. (message "Subtasks must be completed in sequence")))))
  10267. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10268. (defun org-block-todo-from-checkboxes (change-plist)
  10269. "Block turning an entry into a TODO, using checkboxes.
  10270. This checks whether the current task should be blocked from state
  10271. changes because there are unchecked boxes in this entry."
  10272. (if (not org-enforce-todo-checkbox-dependencies)
  10273. t ; if locally turned off don't block
  10274. (catch 'dont-block
  10275. ;; If this is not a todo state change, or if this entry is already DONE,
  10276. ;; do not block
  10277. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10278. (member (plist-get change-plist :from)
  10279. (cons 'done org-done-keywords))
  10280. (member (plist-get change-plist :to)
  10281. (cons 'todo org-not-done-keywords))
  10282. (not (plist-get change-plist :to)))
  10283. (throw 'dont-block t))
  10284. ;; If this task has checkboxes that are not checked, it's blocked
  10285. (save-excursion
  10286. (org-back-to-heading t)
  10287. (let ((beg (point)) end)
  10288. (outline-next-heading)
  10289. (setq end (point))
  10290. (goto-char beg)
  10291. (if (org-list-search-forward
  10292. (concat (org-item-beginning-re)
  10293. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10294. "\\[[- ]\\]")
  10295. end t)
  10296. (progn
  10297. (if (boundp 'org-blocked-by-checkboxes)
  10298. (setq org-blocked-by-checkboxes t))
  10299. (throw 'dont-block nil)))))
  10300. t))) ; do not block
  10301. (defun org-entry-blocked-p ()
  10302. "Is the current entry blocked?"
  10303. (if (org-entry-get nil "NOBLOCKING")
  10304. nil ;; Never block this entry
  10305. (not
  10306. (run-hook-with-args-until-failure
  10307. 'org-blocker-hook
  10308. (list :type 'todo-state-change
  10309. :position (point)
  10310. :from 'todo
  10311. :to 'done)))))
  10312. (defun org-update-statistics-cookies (all)
  10313. "Update the statistics cookie, either from TODO or from checkboxes.
  10314. This should be called with the cursor in a line with a statistics cookie."
  10315. (interactive "P")
  10316. (if all
  10317. (progn
  10318. (org-update-checkbox-count 'all)
  10319. (org-map-entries 'org-update-parent-todo-statistics))
  10320. (if (not (org-on-heading-p))
  10321. (org-update-checkbox-count)
  10322. (let ((pos (move-marker (make-marker) (point)))
  10323. end l1 l2)
  10324. (ignore-errors (org-back-to-heading t))
  10325. (if (not (org-on-heading-p))
  10326. (org-update-checkbox-count)
  10327. (setq l1 (org-outline-level))
  10328. (setq end (save-excursion
  10329. (outline-next-heading)
  10330. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10331. (point)))
  10332. (if (and (save-excursion
  10333. (re-search-forward
  10334. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10335. (not (save-excursion (re-search-forward
  10336. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10337. (org-update-checkbox-count)
  10338. (if (and l2 (> l2 l1))
  10339. (progn
  10340. (goto-char end)
  10341. (org-update-parent-todo-statistics))
  10342. (goto-char pos)
  10343. (beginning-of-line 1)
  10344. (while (re-search-forward
  10345. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10346. (point-at-eol) t)
  10347. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10348. (goto-char pos)
  10349. (move-marker pos nil)))))
  10350. (defvar org-entry-property-inherited-from) ;; defined below
  10351. (defun org-update-parent-todo-statistics ()
  10352. "Update any statistics cookie in the parent of the current headline.
  10353. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10354. the entire subtree and this will travel up the hierarchy and update
  10355. statistics everywhere."
  10356. (let* ((prop (save-excursion (org-up-heading-safe)
  10357. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10358. (recursive (or (not org-hierarchical-todo-statistics)
  10359. (and prop (string-match "\\<recursive\\>" prop))))
  10360. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10361. 0))
  10362. (first t)
  10363. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10364. level ltoggle l1 new ndel
  10365. (cnt-all 0) (cnt-done 0) is-percent kwd
  10366. checkbox-beg ov ovs ove cookie-present)
  10367. (catch 'exit
  10368. (save-excursion
  10369. (beginning-of-line 1)
  10370. (setq ltoggle (funcall outline-level))
  10371. ;; Three situations are to consider:
  10372. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10373. ;; to the top-level ancestor on the headline;
  10374. ;; 2. If parent has "recursive" property, repeat up to the
  10375. ;; headline setting that property, taking inheritance into
  10376. ;; account;
  10377. ;; 3. Else, move up to direct parent and proceed only once.
  10378. (while (and (setq level (org-up-heading-safe))
  10379. (or recursive first)
  10380. (>= (point) lim))
  10381. (setq first nil cookie-present nil)
  10382. (unless (and level
  10383. (not (string-match
  10384. "\\<checkbox\\>"
  10385. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10386. "")))))
  10387. (throw 'exit nil))
  10388. (while (re-search-forward box-re (point-at-eol) t)
  10389. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10390. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10391. (save-match-data
  10392. (unless (outline-next-heading) (throw 'exit nil))
  10393. (while (and (looking-at org-complex-heading-regexp)
  10394. (> (setq l1 (length (match-string 1))) level))
  10395. (setq kwd (and (or recursive (= l1 ltoggle))
  10396. (match-string 2)))
  10397. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10398. (and (listp org-provide-todo-statistics)
  10399. (or (member kwd org-provide-todo-statistics)
  10400. (member kwd org-done-keywords))))
  10401. (setq cnt-all (1+ cnt-all))
  10402. (if (eq org-provide-todo-statistics t)
  10403. (and kwd (setq cnt-all (1+ cnt-all)))))
  10404. (and (member kwd org-done-keywords)
  10405. (setq cnt-done (1+ cnt-done)))
  10406. (outline-next-heading)))
  10407. (setq new
  10408. (if is-percent
  10409. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10410. (format "[%d/%d]" cnt-done cnt-all))
  10411. ndel (- (match-end 0) checkbox-beg))
  10412. ;; handle overlays when updating cookie from column view
  10413. (when (setq ov (car (overlays-at checkbox-beg)))
  10414. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10415. (delete-overlay ov))
  10416. (goto-char checkbox-beg)
  10417. (insert new)
  10418. (delete-region (point) (+ (point) ndel))
  10419. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10420. (when ov (move-overlay ov ovs ove)))
  10421. (when cookie-present
  10422. (run-hook-with-args 'org-after-todo-statistics-hook
  10423. cnt-done (- cnt-all cnt-done))))))
  10424. (run-hooks 'org-todo-statistics-hook)))
  10425. (defvar org-after-todo-statistics-hook nil
  10426. "Hook that is called after a TODO statistics cookie has been updated.
  10427. Each function is called with two arguments: the number of not-done entries
  10428. and the number of done entries.
  10429. For example, the following function, when added to this hook, will switch
  10430. an entry to DONE when all children are done, and back to TODO when new
  10431. entries are set to a TODO status. Note that this hook is only called
  10432. when there is a statistics cookie in the headline!
  10433. (defun org-summary-todo (n-done n-not-done)
  10434. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10435. (let (org-log-done org-log-states) ; turn off logging
  10436. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10437. ")
  10438. (defvar org-todo-statistics-hook nil
  10439. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10440. This hook runs even if there is no statistics cookie present, in which case
  10441. `org-after-todo-statistics-hook' would not run.")
  10442. (defun org-todo-trigger-tag-changes (state)
  10443. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10444. (let ((l org-todo-state-tags-triggers)
  10445. changes)
  10446. (when (or (not state) (equal state ""))
  10447. (setq changes (append changes (cdr (assoc "" l)))))
  10448. (when (and (stringp state) (> (length state) 0))
  10449. (setq changes (append changes (cdr (assoc state l)))))
  10450. (when (member state org-not-done-keywords)
  10451. (setq changes (append changes (cdr (assoc 'todo l)))))
  10452. (when (member state org-done-keywords)
  10453. (setq changes (append changes (cdr (assoc 'done l)))))
  10454. (dolist (c changes)
  10455. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10456. (defun org-local-logging (value)
  10457. "Get logging settings from a property VALUE."
  10458. (let* (words w a)
  10459. ;; directly set the variables, they are already local.
  10460. (setq org-log-done nil
  10461. org-log-repeat nil
  10462. org-todo-log-states nil)
  10463. (setq words (org-split-string value))
  10464. (while (setq w (pop words))
  10465. (cond
  10466. ((setq a (assoc w org-startup-options))
  10467. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10468. (set (nth 1 a) (nth 2 a))))
  10469. ((setq a (org-extract-log-state-settings w))
  10470. (and (member (car a) org-todo-keywords-1)
  10471. (push a org-todo-log-states)))))))
  10472. (defun org-get-todo-sequence-head (kwd)
  10473. "Return the head of the TODO sequence to which KWD belongs.
  10474. If KWD is not set, check if there is a text property remembering the
  10475. right sequence."
  10476. (let (p)
  10477. (cond
  10478. ((not kwd)
  10479. (or (get-text-property (point-at-bol) 'org-todo-head)
  10480. (progn
  10481. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10482. nil (point-at-eol)))
  10483. (get-text-property p 'org-todo-head))))
  10484. ((not (member kwd org-todo-keywords-1))
  10485. (car org-todo-keywords-1))
  10486. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10487. (defun org-fast-todo-selection ()
  10488. "Fast TODO keyword selection with single keys.
  10489. Returns the new TODO keyword, or nil if no state change should occur."
  10490. (let* ((fulltable org-todo-key-alist)
  10491. (done-keywords org-done-keywords) ;; needed for the faces.
  10492. (maxlen (apply 'max (mapcar
  10493. (lambda (x)
  10494. (if (stringp (car x)) (string-width (car x)) 0))
  10495. fulltable)))
  10496. (expert nil)
  10497. (fwidth (+ maxlen 3 1 3))
  10498. (ncol (/ (- (window-width) 4) fwidth))
  10499. tg cnt e c tbl
  10500. groups ingroup)
  10501. (save-excursion
  10502. (save-window-excursion
  10503. (if expert
  10504. (set-buffer (get-buffer-create " *Org todo*"))
  10505. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10506. (erase-buffer)
  10507. (org-set-local 'org-done-keywords done-keywords)
  10508. (setq tbl fulltable cnt 0)
  10509. (while (setq e (pop tbl))
  10510. (cond
  10511. ((equal e '(:startgroup))
  10512. (push '() groups) (setq ingroup t)
  10513. (when (not (= cnt 0))
  10514. (setq cnt 0)
  10515. (insert "\n"))
  10516. (insert "{ "))
  10517. ((equal e '(:endgroup))
  10518. (setq ingroup nil cnt 0)
  10519. (insert "}\n"))
  10520. ((equal e '(:newline))
  10521. (when (not (= cnt 0))
  10522. (setq cnt 0)
  10523. (insert "\n")
  10524. (setq e (car tbl))
  10525. (while (equal (car tbl) '(:newline))
  10526. (insert "\n")
  10527. (setq tbl (cdr tbl)))))
  10528. (t
  10529. (setq tg (car e) c (cdr e))
  10530. (if ingroup (push tg (car groups)))
  10531. (setq tg (org-add-props tg nil 'face
  10532. (org-get-todo-face tg)))
  10533. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10534. (insert "[" c "] " tg (make-string
  10535. (- fwidth 4 (length tg)) ?\ ))
  10536. (when (= (setq cnt (1+ cnt)) ncol)
  10537. (insert "\n")
  10538. (if ingroup (insert " "))
  10539. (setq cnt 0)))))
  10540. (insert "\n")
  10541. (goto-char (point-min))
  10542. (if (not expert) (org-fit-window-to-buffer))
  10543. (message "[a-z..]:Set [SPC]:clear")
  10544. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10545. (cond
  10546. ((or (= c ?\C-g)
  10547. (and (= c ?q) (not (rassoc c fulltable))))
  10548. (setq quit-flag t))
  10549. ((= c ?\ ) nil)
  10550. ((setq e (rassoc c fulltable) tg (car e))
  10551. tg)
  10552. (t (setq quit-flag t)))))))
  10553. (defun org-entry-is-todo-p ()
  10554. (member (org-get-todo-state) org-not-done-keywords))
  10555. (defun org-entry-is-done-p ()
  10556. (member (org-get-todo-state) org-done-keywords))
  10557. (defun org-get-todo-state ()
  10558. (save-excursion
  10559. (org-back-to-heading t)
  10560. (and (looking-at org-todo-line-regexp)
  10561. (match-end 2)
  10562. (match-string 2))))
  10563. (defun org-at-date-range-p (&optional inactive-ok)
  10564. "Is the cursor inside a date range?"
  10565. (interactive)
  10566. (save-excursion
  10567. (catch 'exit
  10568. (let ((pos (point)))
  10569. (skip-chars-backward "^[<\r\n")
  10570. (skip-chars-backward "<[")
  10571. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10572. (>= (match-end 0) pos)
  10573. (throw 'exit t))
  10574. (skip-chars-backward "^<[\r\n")
  10575. (skip-chars-backward "<[")
  10576. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10577. (>= (match-end 0) pos)
  10578. (throw 'exit t)))
  10579. nil)))
  10580. (defun org-get-repeat (&optional tagline)
  10581. "Check if there is a deadline/schedule with repeater in this entry."
  10582. (save-match-data
  10583. (save-excursion
  10584. (org-back-to-heading t)
  10585. (and (re-search-forward (if tagline
  10586. (concat tagline "\\s-*" org-repeat-re)
  10587. org-repeat-re)
  10588. (org-entry-end-position) t)
  10589. (match-string-no-properties 1)))))
  10590. (defvar org-last-changed-timestamp)
  10591. (defvar org-last-inserted-timestamp)
  10592. (defvar org-log-post-message)
  10593. (defvar org-log-note-purpose)
  10594. (defvar org-log-note-how)
  10595. (defvar org-log-note-extra)
  10596. (defun org-auto-repeat-maybe (done-word)
  10597. "Check if the current headline contains a repeated deadline/schedule.
  10598. If yes, set TODO state back to what it was and change the base date
  10599. of repeating deadline/scheduled time stamps to new date.
  10600. This function is run automatically after each state change to a DONE state."
  10601. ;; last-state is dynamically scoped into this function
  10602. (let* ((repeat (org-get-repeat))
  10603. (aa (assoc last-state org-todo-kwd-alist))
  10604. (interpret (nth 1 aa))
  10605. (head (nth 2 aa))
  10606. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10607. (msg "Entry repeats: ")
  10608. (org-log-done nil)
  10609. (org-todo-log-states nil)
  10610. re type n what ts time to-state)
  10611. (when repeat
  10612. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10613. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10614. org-todo-repeat-to-state))
  10615. (unless (and to-state (member to-state org-todo-keywords-1))
  10616. (setq to-state (if (eq interpret 'type) last-state head)))
  10617. (org-todo to-state)
  10618. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10619. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10620. (org-time-stamp-format t t))))
  10621. (when org-log-repeat
  10622. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10623. (memq 'org-add-log-note post-command-hook))
  10624. ;; OK, we are already setup for some record
  10625. (if (eq org-log-repeat 'note)
  10626. ;; make sure we take a note, not only a time stamp
  10627. (setq org-log-note-how 'note))
  10628. ;; Set up for taking a record
  10629. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10630. last-state
  10631. 'findpos org-log-repeat)))
  10632. (org-back-to-heading t)
  10633. (org-add-planning-info nil nil 'closed)
  10634. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10635. org-deadline-time-regexp "\\)\\|\\("
  10636. org-ts-regexp "\\)"))
  10637. (while (re-search-forward
  10638. re (save-excursion (outline-next-heading) (point)) t)
  10639. (setq type (if (match-end 1) org-scheduled-string
  10640. (if (match-end 3) org-deadline-string "Plain:"))
  10641. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10642. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10643. (setq n (string-to-number (match-string 2 ts))
  10644. what (match-string 3 ts))
  10645. (if (equal what "w") (setq n (* n 7) what "d"))
  10646. ;; Preparation, see if we need to modify the start date for the change
  10647. (when (match-end 1)
  10648. (setq time (save-match-data (org-time-string-to-time ts)))
  10649. (cond
  10650. ((equal (match-string 1 ts) ".")
  10651. ;; Shift starting date to today
  10652. (org-timestamp-change
  10653. (- (org-today) (time-to-days time))
  10654. 'day))
  10655. ((equal (match-string 1 ts) "+")
  10656. (let ((nshiftmax 10) (nshift 0))
  10657. (while (or (= nshift 0)
  10658. (<= (time-to-days time)
  10659. (time-to-days (current-time))))
  10660. (when (= (incf nshift) nshiftmax)
  10661. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10662. (error "Abort")))
  10663. (org-timestamp-change n (cdr (assoc what whata)))
  10664. (org-at-timestamp-p t)
  10665. (setq ts (match-string 1))
  10666. (setq time (save-match-data (org-time-string-to-time ts)))))
  10667. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10668. ;; rematch, so that we have everything in place for the real shift
  10669. (org-at-timestamp-p t)
  10670. (setq ts (match-string 1))
  10671. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10672. (org-timestamp-change n (cdr (assoc what whata)))
  10673. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10674. (setq org-log-post-message msg)
  10675. (message "%s" msg))))
  10676. (defun org-show-todo-tree (arg)
  10677. "Make a compact tree which shows all headlines marked with TODO.
  10678. The tree will show the lines where the regexp matches, and all higher
  10679. headlines above the match.
  10680. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10681. With a numeric prefix N, construct a sparse tree for the Nth element
  10682. of `org-todo-keywords-1'."
  10683. (interactive "P")
  10684. (let ((case-fold-search nil)
  10685. (kwd-re
  10686. (cond ((null arg) org-not-done-regexp)
  10687. ((equal arg '(4))
  10688. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10689. (mapcar 'list org-todo-keywords-1))))
  10690. (concat "\\("
  10691. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10692. "\\)\\>")))
  10693. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10694. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10695. org-todo-keywords-1)))
  10696. (t (error "Invalid prefix argument: %s" arg)))))
  10697. (message "%d TODO entries found"
  10698. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10699. (defun org-deadline (&optional remove time)
  10700. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10701. With argument REMOVE, remove any deadline from the item.
  10702. With argument TIME, set the deadline at the corresponding date. TIME
  10703. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10704. (interactive "P")
  10705. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10706. (repeater (and old-date
  10707. (string-match
  10708. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10709. old-date)
  10710. (match-string 1 old-date))))
  10711. (if remove
  10712. (progn
  10713. (when (and old-date org-log-redeadline)
  10714. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10715. org-log-redeadline))
  10716. (org-remove-timestamp-with-keyword org-deadline-string)
  10717. (message "Item no longer has a deadline."))
  10718. (org-add-planning-info 'deadline time 'closed)
  10719. (when (and old-date org-log-redeadline
  10720. (not (equal old-date
  10721. (substring org-last-inserted-timestamp 1 -1))))
  10722. (org-add-log-setup 'redeadline nil old-date 'findpos
  10723. org-log-redeadline))
  10724. (when repeater
  10725. (save-excursion
  10726. (org-back-to-heading t)
  10727. (when (re-search-forward (concat org-deadline-string " "
  10728. org-last-inserted-timestamp)
  10729. (save-excursion
  10730. (outline-next-heading) (point)) t)
  10731. (goto-char (1- (match-end 0)))
  10732. (insert " " repeater)
  10733. (setq org-last-inserted-timestamp
  10734. (concat (substring org-last-inserted-timestamp 0 -1)
  10735. " " repeater
  10736. (substring org-last-inserted-timestamp -1))))))
  10737. (message "Deadline on %s" org-last-inserted-timestamp))))
  10738. (defun org-schedule (&optional remove time)
  10739. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10740. With argument REMOVE, remove any scheduling date from the item.
  10741. With argument TIME, scheduled at the corresponding date. TIME can
  10742. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10743. (interactive "P")
  10744. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10745. (repeater (and old-date
  10746. (string-match
  10747. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10748. old-date)
  10749. (match-string 1 old-date))))
  10750. (if remove
  10751. (progn
  10752. (when (and old-date org-log-reschedule)
  10753. (org-add-log-setup 'delschedule nil old-date 'findpos
  10754. org-log-reschedule))
  10755. (org-remove-timestamp-with-keyword org-scheduled-string)
  10756. (message "Item is no longer scheduled."))
  10757. (org-add-planning-info 'scheduled time 'closed)
  10758. (when (and old-date org-log-reschedule
  10759. (not (equal old-date
  10760. (substring org-last-inserted-timestamp 1 -1))))
  10761. (org-add-log-setup 'reschedule nil old-date 'findpos
  10762. org-log-reschedule))
  10763. (when repeater
  10764. (save-excursion
  10765. (org-back-to-heading t)
  10766. (when (re-search-forward (concat org-scheduled-string " "
  10767. org-last-inserted-timestamp)
  10768. (save-excursion
  10769. (outline-next-heading) (point)) t)
  10770. (goto-char (1- (match-end 0)))
  10771. (insert " " repeater)
  10772. (setq org-last-inserted-timestamp
  10773. (concat (substring org-last-inserted-timestamp 0 -1)
  10774. " " repeater
  10775. (substring org-last-inserted-timestamp -1))))))
  10776. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10777. (defun org-get-scheduled-time (pom &optional inherit)
  10778. "Get the scheduled time as a time tuple, of a format suitable
  10779. for calling org-schedule with, or if there is no scheduling,
  10780. returns nil."
  10781. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10782. (when time
  10783. (apply 'encode-time (org-parse-time-string time)))))
  10784. (defun org-get-deadline-time (pom &optional inherit)
  10785. "Get the deadline as a time tuple, of a format suitable for
  10786. calling org-deadline with, or if there is no scheduling, returns
  10787. nil."
  10788. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10789. (when time
  10790. (apply 'encode-time (org-parse-time-string time)))))
  10791. (defun org-remove-timestamp-with-keyword (keyword)
  10792. "Remove all time stamps with KEYWORD in the current entry."
  10793. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10794. beg)
  10795. (save-excursion
  10796. (org-back-to-heading t)
  10797. (setq beg (point))
  10798. (outline-next-heading)
  10799. (while (re-search-backward re beg t)
  10800. (replace-match "")
  10801. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10802. (equal (char-before) ?\ ))
  10803. (backward-delete-char 1)
  10804. (if (string-match "^[ \t]*$" (buffer-substring
  10805. (point-at-bol) (point-at-eol)))
  10806. (delete-region (point-at-bol)
  10807. (min (point-max) (1+ (point-at-eol))))))))))
  10808. (defun org-add-planning-info (what &optional time &rest remove)
  10809. "Insert new timestamp with keyword in the line directly after the headline.
  10810. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  10811. If non is given, the user is prompted for a date.
  10812. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10813. be removed."
  10814. (interactive)
  10815. (let (org-time-was-given org-end-time-was-given ts
  10816. end default-time default-input)
  10817. (catch 'exit
  10818. (when (and (memq what '(scheduled deadline))
  10819. (or (not time)
  10820. (and (stringp time)
  10821. (string-match "^[-+]+[0-9]" time))))
  10822. ;; Try to get a default date/time from existing timestamp
  10823. (save-excursion
  10824. (org-back-to-heading t)
  10825. (setq end (save-excursion (outline-next-heading) (point)))
  10826. (when (re-search-forward (if (eq what 'scheduled)
  10827. org-scheduled-time-regexp
  10828. org-deadline-time-regexp)
  10829. end t)
  10830. (setq ts (match-string 1)
  10831. default-time
  10832. (apply 'encode-time (org-parse-time-string ts))
  10833. default-input (and ts (org-get-compact-tod ts))))))
  10834. (when what
  10835. (setq time
  10836. (if (and (stringp time)
  10837. (string-match "^[-+]+[0-9]" time))
  10838. ;; This is a relative time, set the proper date
  10839. (apply 'encode-time
  10840. (org-read-date-analyze
  10841. time default-time (decode-time default-time)))
  10842. ;; If necessary, get the time from the user
  10843. (or time (org-read-date nil 'to-time nil nil
  10844. default-time default-input)))))
  10845. (when (and org-insert-labeled-timestamps-at-point
  10846. (member what '(scheduled deadline)))
  10847. (insert
  10848. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10849. (org-insert-time-stamp time org-time-was-given
  10850. nil nil nil (list org-end-time-was-given))
  10851. (setq what nil))
  10852. (save-excursion
  10853. (save-restriction
  10854. (let (col list elt ts buffer-invisibility-spec)
  10855. (org-back-to-heading t)
  10856. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  10857. (goto-char (match-end 1))
  10858. (setq col (current-column))
  10859. (goto-char (match-end 0))
  10860. (if (eobp) (insert "\n") (forward-char 1))
  10861. (when (and (not what)
  10862. (not (looking-at
  10863. (concat "[ \t]*"
  10864. org-keyword-time-not-clock-regexp))))
  10865. ;; Nothing to add, nothing to remove...... :-)
  10866. (throw 'exit nil))
  10867. (if (and (not (looking-at org-outline-regexp))
  10868. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10869. "[^\r\n]*"))
  10870. (not (equal (match-string 1) org-clock-string)))
  10871. (narrow-to-region (match-beginning 0) (match-end 0))
  10872. (insert-before-markers "\n")
  10873. (backward-char 1)
  10874. (narrow-to-region (point) (point))
  10875. (and org-adapt-indentation (org-indent-to-column col)))
  10876. ;; Check if we have to remove something.
  10877. (setq list (cons what remove))
  10878. (while list
  10879. (setq elt (pop list))
  10880. (when (or (and (eq elt 'scheduled)
  10881. (re-search-forward org-scheduled-time-regexp nil t))
  10882. (and (eq elt 'deadline)
  10883. (re-search-forward org-deadline-time-regexp nil t))
  10884. (and (eq elt 'closed)
  10885. (re-search-forward org-closed-time-regexp nil t)))
  10886. (replace-match "")
  10887. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  10888. (and (looking-at "[ \t]+") (replace-match ""))
  10889. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10890. (when what
  10891. (insert
  10892. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10893. (cond ((eq what 'scheduled) org-scheduled-string)
  10894. ((eq what 'deadline) org-deadline-string)
  10895. ((eq what 'closed) org-closed-string))
  10896. " ")
  10897. (setq ts (org-insert-time-stamp
  10898. time
  10899. (or org-time-was-given
  10900. (and (eq what 'closed) org-log-done-with-time))
  10901. (eq what 'closed)
  10902. nil nil (list org-end-time-was-given)))
  10903. (insert
  10904. (if (not (or (bolp) (eq (char-before) ?\ )
  10905. (memq (char-after) '(32 10))
  10906. (eobp))) " " ""))
  10907. (end-of-line 1))
  10908. (goto-char (point-min))
  10909. (widen)
  10910. (if (and (looking-at "[ \t]*\n")
  10911. (equal (char-before) ?\n))
  10912. (delete-region (1- (point)) (point-at-eol)))
  10913. ts))))))
  10914. (defvar org-log-note-marker (make-marker))
  10915. (defvar org-log-note-purpose nil)
  10916. (defvar org-log-note-state nil)
  10917. (defvar org-log-note-previous-state nil)
  10918. (defvar org-log-note-how nil)
  10919. (defvar org-log-note-extra nil)
  10920. (defvar org-log-note-window-configuration nil)
  10921. (defvar org-log-note-return-to (make-marker))
  10922. (defvar org-log-note-effective-time nil
  10923. "Remembered current time so that dynamically scoped
  10924. `org-extend-today-until' affects tha timestamps in state change
  10925. log")
  10926. (defvar org-log-post-message nil
  10927. "Message to be displayed after a log note has been stored.
  10928. The auto-repeater uses this.")
  10929. (defun org-add-note ()
  10930. "Add a note to the current entry.
  10931. This is done in the same way as adding a state change note."
  10932. (interactive)
  10933. (org-add-log-setup 'note nil nil 'findpos nil))
  10934. (defvar org-property-end-re)
  10935. (defun org-add-log-setup (&optional purpose state prev-state
  10936. findpos how extra)
  10937. "Set up the post command hook to take a note.
  10938. If this is about to TODO state change, the new state is expected in STATE.
  10939. When FINDPOS is non-nil, find the correct position for the note in
  10940. the current entry. If not, assume that it can be inserted at point.
  10941. HOW is an indicator what kind of note should be created.
  10942. EXTRA is additional text that will be inserted into the notes buffer."
  10943. (let* ((org-log-into-drawer (org-log-into-drawer))
  10944. (drawer (cond ((stringp org-log-into-drawer)
  10945. org-log-into-drawer)
  10946. (org-log-into-drawer "LOGBOOK")
  10947. (t nil))))
  10948. (save-restriction
  10949. (save-excursion
  10950. (when findpos
  10951. (org-back-to-heading t)
  10952. (narrow-to-region (point) (save-excursion
  10953. (outline-next-heading) (point)))
  10954. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  10955. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10956. "[^\r\n]*\\)?"))
  10957. (goto-char (match-end 0))
  10958. (cond
  10959. (drawer
  10960. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10961. nil t)
  10962. (progn
  10963. (goto-char (match-end 0))
  10964. (or org-log-states-order-reversed
  10965. (and (re-search-forward org-property-end-re nil t)
  10966. (goto-char (1- (match-beginning 0))))))
  10967. (insert "\n:" drawer ":\n:END:")
  10968. (beginning-of-line 0)
  10969. (org-indent-line-function)
  10970. (beginning-of-line 2)
  10971. (org-indent-line-function)
  10972. (end-of-line 0)))
  10973. ((and org-log-state-notes-insert-after-drawers
  10974. (save-excursion
  10975. (forward-line) (looking-at org-drawer-regexp)))
  10976. (forward-line)
  10977. (while (looking-at org-drawer-regexp)
  10978. (goto-char (match-end 0))
  10979. (re-search-forward org-property-end-re (point-max) t)
  10980. (forward-line))
  10981. (forward-line -1)))
  10982. (unless org-log-states-order-reversed
  10983. (and (= (char-after) ?\n) (forward-char 1))
  10984. (org-skip-over-state-notes)
  10985. (skip-chars-backward " \t\n\r")))
  10986. (move-marker org-log-note-marker (point))
  10987. (setq org-log-note-purpose purpose
  10988. org-log-note-state state
  10989. org-log-note-previous-state prev-state
  10990. org-log-note-how how
  10991. org-log-note-extra extra
  10992. org-log-note-effective-time (org-current-effective-time))
  10993. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10994. (defun org-skip-over-state-notes ()
  10995. "Skip past the list of State notes in an entry."
  10996. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10997. (when (ignore-errors (goto-char (org-in-item-p)))
  10998. (let* ((struct (org-list-struct))
  10999. (prevs (org-list-prevs-alist struct)))
  11000. (while (looking-at "[ \t]*- State")
  11001. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11002. (org-list-get-item-end (point) struct)))))))
  11003. (defun org-add-log-note (&optional purpose)
  11004. "Pop up a window for taking a note, and add this note later at point."
  11005. (remove-hook 'post-command-hook 'org-add-log-note)
  11006. (setq org-log-note-window-configuration (current-window-configuration))
  11007. (delete-other-windows)
  11008. (move-marker org-log-note-return-to (point))
  11009. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11010. (goto-char org-log-note-marker)
  11011. (org-switch-to-buffer-other-window "*Org Note*")
  11012. (erase-buffer)
  11013. (if (memq org-log-note-how '(time state))
  11014. (let (current-prefix-arg) (org-store-log-note))
  11015. (let ((org-inhibit-startup t)) (org-mode))
  11016. (insert (format "# Insert note for %s.
  11017. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11018. (cond
  11019. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11020. ((eq org-log-note-purpose 'done) "closed todo item")
  11021. ((eq org-log-note-purpose 'state)
  11022. (format "state change from \"%s\" to \"%s\""
  11023. (or org-log-note-previous-state "")
  11024. (or org-log-note-state "")))
  11025. ((eq org-log-note-purpose 'reschedule)
  11026. "rescheduling")
  11027. ((eq org-log-note-purpose 'delschedule)
  11028. "no longer scheduled")
  11029. ((eq org-log-note-purpose 'redeadline)
  11030. "changing deadline")
  11031. ((eq org-log-note-purpose 'deldeadline)
  11032. "removing deadline")
  11033. ((eq org-log-note-purpose 'refile)
  11034. "refiling")
  11035. ((eq org-log-note-purpose 'note)
  11036. "this entry")
  11037. (t (error "This should not happen")))))
  11038. (if org-log-note-extra (insert org-log-note-extra))
  11039. (org-set-local 'org-finish-function 'org-store-log-note)
  11040. (run-hooks 'org-log-buffer-setup-hook)))
  11041. (defvar org-note-abort nil) ; dynamically scoped
  11042. (defun org-store-log-note ()
  11043. "Finish taking a log note, and insert it to where it belongs."
  11044. (let ((txt (buffer-string))
  11045. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11046. lines ind bul)
  11047. (kill-buffer (current-buffer))
  11048. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11049. (setq txt (replace-match "" t t txt)))
  11050. (if (string-match "\\s-+\\'" txt)
  11051. (setq txt (replace-match "" t t txt)))
  11052. (setq lines (org-split-string txt "\n"))
  11053. (when (and note (string-match "\\S-" note))
  11054. (setq note
  11055. (org-replace-escapes
  11056. note
  11057. (list (cons "%u" (user-login-name))
  11058. (cons "%U" user-full-name)
  11059. (cons "%t" (format-time-string
  11060. (org-time-stamp-format 'long 'inactive)
  11061. org-log-note-effective-time))
  11062. (cons "%T" (format-time-string
  11063. (org-time-stamp-format 'long nil)
  11064. org-log-note-effective-time))
  11065. (cons "%s" (if org-log-note-state
  11066. (concat "\"" org-log-note-state "\"")
  11067. ""))
  11068. (cons "%S" (if org-log-note-previous-state
  11069. (concat "\"" org-log-note-previous-state "\"")
  11070. "\"\"")))))
  11071. (if lines (setq note (concat note " \\\\")))
  11072. (push note lines))
  11073. (when (or current-prefix-arg org-note-abort)
  11074. (when org-log-into-drawer
  11075. (org-remove-empty-drawer-at
  11076. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11077. org-log-note-marker))
  11078. (setq lines nil))
  11079. (when lines
  11080. (with-current-buffer (marker-buffer org-log-note-marker)
  11081. (save-excursion
  11082. (goto-char org-log-note-marker)
  11083. (move-marker org-log-note-marker nil)
  11084. (end-of-line 1)
  11085. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11086. (setq ind (save-excursion
  11087. (if (ignore-errors (goto-char (org-in-item-p)))
  11088. (let ((struct (org-list-struct)))
  11089. (org-list-get-ind
  11090. (org-list-get-top-point struct) struct))
  11091. (skip-chars-backward " \r\t\n")
  11092. (cond
  11093. ((and (org-at-heading-p)
  11094. org-adapt-indentation)
  11095. (1+ (org-current-level)))
  11096. ((org-at-heading-p) 0)
  11097. (t (org-get-indentation))))))
  11098. (setq bul (org-list-bullet-string "-"))
  11099. (org-indent-line-to ind)
  11100. (insert bul (pop lines))
  11101. (let ((ind-body (+ (length bul) ind)))
  11102. (while lines
  11103. (insert "\n")
  11104. (org-indent-line-to ind-body)
  11105. (insert (pop lines))))
  11106. (message "Note stored")
  11107. (org-back-to-heading t)
  11108. (org-cycle-hide-drawers 'children)))))
  11109. (set-window-configuration org-log-note-window-configuration)
  11110. (with-current-buffer (marker-buffer org-log-note-return-to)
  11111. (goto-char org-log-note-return-to))
  11112. (move-marker org-log-note-return-to nil)
  11113. (and org-log-post-message (message "%s" org-log-post-message)))
  11114. (defun org-remove-empty-drawer-at (drawer pos)
  11115. "Remove an empty drawer DRAWER at position POS.
  11116. POS may also be a marker."
  11117. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11118. (save-excursion
  11119. (save-restriction
  11120. (widen)
  11121. (goto-char pos)
  11122. (if (org-in-regexp
  11123. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11124. (replace-match ""))))))
  11125. (defun org-sparse-tree (&optional arg)
  11126. "Create a sparse tree, prompt for the details.
  11127. This command can create sparse trees. You first need to select the type
  11128. of match used to create the tree:
  11129. t Show all TODO entries.
  11130. T Show entries with a specific TODO keyword.
  11131. m Show entries selected by a tags/property match.
  11132. p Enter a property name and its value (both with completion on existing
  11133. names/values) and show entries with that property.
  11134. r Show entries matching a regular expression (`/' can be used as well)
  11135. d Show deadlines due within `org-deadline-warning-days'.
  11136. b Show deadlines and scheduled items before a date.
  11137. a Show deadlines and scheduled items after a date."
  11138. (interactive "P")
  11139. (let (ans kwd value)
  11140. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  11141. (setq ans (read-char-exclusive))
  11142. (cond
  11143. ((equal ans ?d)
  11144. (call-interactively 'org-check-deadlines))
  11145. ((equal ans ?b)
  11146. (call-interactively 'org-check-before-date))
  11147. ((equal ans ?a)
  11148. (call-interactively 'org-check-after-date))
  11149. ((equal ans ?t)
  11150. (org-show-todo-tree nil))
  11151. ((equal ans ?T)
  11152. (org-show-todo-tree '(4)))
  11153. ((member ans '(?T ?m))
  11154. (call-interactively 'org-match-sparse-tree))
  11155. ((member ans '(?p ?P))
  11156. (setq kwd (org-icompleting-read "Property: "
  11157. (mapcar 'list (org-buffer-property-keys))))
  11158. (setq value (org-icompleting-read "Value: "
  11159. (mapcar 'list (org-property-values kwd))))
  11160. (unless (string-match "\\`{.*}\\'" value)
  11161. (setq value (concat "\"" value "\"")))
  11162. (org-match-sparse-tree arg (concat kwd "=" value)))
  11163. ((member ans '(?r ?R ?/))
  11164. (call-interactively 'org-occur))
  11165. (t (error "No such sparse tree command \"%c\"" ans)))))
  11166. (defvar org-occur-highlights nil
  11167. "List of overlays used for occur matches.")
  11168. (make-variable-buffer-local 'org-occur-highlights)
  11169. (defvar org-occur-parameters nil
  11170. "Parameters of the active org-occur calls.
  11171. This is a list, each call to org-occur pushes as cons cell,
  11172. containing the regular expression and the callback, onto the list.
  11173. The list can contain several entries if `org-occur' has been called
  11174. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11175. will only contain one set of parameters. When the highlights are
  11176. removed (for example with `C-c C-c', or with the next edit (depending
  11177. on `org-remove-highlights-with-change'), this variable is emptied
  11178. as well.")
  11179. (make-variable-buffer-local 'org-occur-parameters)
  11180. (defun org-occur (regexp &optional keep-previous callback)
  11181. "Make a compact tree which shows all matches of REGEXP.
  11182. The tree will show the lines where the regexp matches, and all higher
  11183. headlines above the match. It will also show the heading after the match,
  11184. to make sure editing the matching entry is easy.
  11185. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11186. call to `org-occur' will be kept, to allow stacking of calls to this
  11187. command.
  11188. If CALLBACK is non-nil, it is a function which is called to confirm
  11189. that the match should indeed be shown."
  11190. (interactive "sRegexp: \nP")
  11191. (when (equal regexp "")
  11192. (error "Regexp cannot be empty"))
  11193. (unless keep-previous
  11194. (org-remove-occur-highlights nil nil t))
  11195. (push (cons regexp callback) org-occur-parameters)
  11196. (let ((cnt 0))
  11197. (save-excursion
  11198. (goto-char (point-min))
  11199. (if (or (not keep-previous) ; do not want to keep
  11200. (not org-occur-highlights)) ; no previous matches
  11201. ;; hide everything
  11202. (org-overview))
  11203. (while (re-search-forward regexp nil t)
  11204. (when (or (not callback)
  11205. (save-match-data (funcall callback)))
  11206. (setq cnt (1+ cnt))
  11207. (when org-highlight-sparse-tree-matches
  11208. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11209. (org-show-context 'occur-tree))))
  11210. (when org-remove-highlights-with-change
  11211. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11212. nil 'local))
  11213. (unless org-sparse-tree-open-archived-trees
  11214. (org-hide-archived-subtrees (point-min) (point-max)))
  11215. (run-hooks 'org-occur-hook)
  11216. (if (org-called-interactively-p 'interactive)
  11217. (message "%d match(es) for regexp %s" cnt regexp))
  11218. cnt))
  11219. (defun org-occur-next-match (&optional n reset)
  11220. "Function for `next-error-function' to find sparse tree matches.
  11221. N is the number of matches to move, when negative move backwards.
  11222. RESET is entirely ignored - this function always goes back to the
  11223. starting point when no match is found."
  11224. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11225. (search-func (if (< n 0)
  11226. 'previous-single-char-property-change
  11227. 'next-single-char-property-change))
  11228. (n (abs n))
  11229. (pos (point))
  11230. p1)
  11231. (catch 'exit
  11232. (while (setq p1 (funcall search-func (point) 'org-type))
  11233. (when (equal p1 limit)
  11234. (goto-char pos)
  11235. (error "No more matches"))
  11236. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11237. (setq n (1- n))
  11238. (when (= n 0)
  11239. (goto-char p1)
  11240. (throw 'exit (point))))
  11241. (goto-char p1))
  11242. (goto-char p1)
  11243. (error "No more matches"))))
  11244. (defun org-show-context (&optional key)
  11245. "Make sure point and context are visible.
  11246. How much context is shown depends upon the variables
  11247. `org-show-hierarchy-above', `org-show-following-heading'. and
  11248. `org-show-siblings'."
  11249. (let ((heading-p (org-on-heading-p t))
  11250. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11251. (following-p (org-get-alist-option org-show-following-heading key))
  11252. (entry-p (org-get-alist-option org-show-entry-below key))
  11253. (siblings-p (org-get-alist-option org-show-siblings key)))
  11254. (catch 'exit
  11255. ;; Show heading or entry text
  11256. (if (and heading-p (not entry-p))
  11257. (org-flag-heading nil) ; only show the heading
  11258. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11259. (org-show-hidden-entry))) ; show entire entry
  11260. (when following-p
  11261. ;; Show next sibling, or heading below text
  11262. (save-excursion
  11263. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11264. (org-flag-heading nil))))
  11265. (when siblings-p (org-show-siblings))
  11266. (when hierarchy-p
  11267. ;; show all higher headings, possibly with siblings
  11268. (save-excursion
  11269. (while (and (condition-case nil
  11270. (progn (org-up-heading-all 1) t)
  11271. (error nil))
  11272. (not (bobp)))
  11273. (org-flag-heading nil)
  11274. (when siblings-p (org-show-siblings))))))))
  11275. (defvar org-reveal-start-hook nil
  11276. "Hook run before revealing a location.")
  11277. (defun org-reveal (&optional siblings)
  11278. "Show current entry, hierarchy above it, and the following headline.
  11279. This can be used to show a consistent set of context around locations
  11280. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11281. not t for the search context.
  11282. With optional argument SIBLINGS, on each level of the hierarchy all
  11283. siblings are shown. This repairs the tree structure to what it would
  11284. look like when opened with hierarchical calls to `org-cycle'.
  11285. With double optional argument \\[universal-argument] \\[universal-argument], \
  11286. go to the parent and show the
  11287. entire tree."
  11288. (interactive "P")
  11289. (run-hooks 'org-reveal-start-hook)
  11290. (let ((org-show-hierarchy-above t)
  11291. (org-show-following-heading t)
  11292. (org-show-siblings (if siblings t org-show-siblings)))
  11293. (org-show-context nil))
  11294. (when (equal siblings '(16))
  11295. (save-excursion
  11296. (when (org-up-heading-safe)
  11297. (org-show-subtree)
  11298. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11299. (defun org-highlight-new-match (beg end)
  11300. "Highlight from BEG to END and mark the highlight is an occur headline."
  11301. (let ((ov (make-overlay beg end)))
  11302. (overlay-put ov 'face 'secondary-selection)
  11303. (overlay-put ov 'org-type 'org-occur)
  11304. (push ov org-occur-highlights)))
  11305. (defun org-remove-occur-highlights (&optional beg end noremove)
  11306. "Remove the occur highlights from the buffer.
  11307. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11308. from the `before-change-functions' in the current buffer."
  11309. (interactive)
  11310. (unless org-inhibit-highlight-removal
  11311. (mapc 'delete-overlay org-occur-highlights)
  11312. (setq org-occur-highlights nil)
  11313. (setq org-occur-parameters nil)
  11314. (unless noremove
  11315. (remove-hook 'before-change-functions
  11316. 'org-remove-occur-highlights 'local))))
  11317. ;;;; Priorities
  11318. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11319. "Regular expression matching the priority indicator.")
  11320. (defvar org-remove-priority-next-time nil)
  11321. (defun org-priority-up ()
  11322. "Increase the priority of the current item."
  11323. (interactive)
  11324. (org-priority 'up))
  11325. (defun org-priority-down ()
  11326. "Decrease the priority of the current item."
  11327. (interactive)
  11328. (org-priority 'down))
  11329. (defun org-priority (&optional action)
  11330. "Change the priority of an item by ARG.
  11331. ACTION can be `set', `up', `down', or a character."
  11332. (interactive)
  11333. (unless org-enable-priority-commands
  11334. (error "Priority commands are disabled"))
  11335. (setq action (or action 'set))
  11336. (let (current new news have remove)
  11337. (save-excursion
  11338. (org-back-to-heading t)
  11339. (if (looking-at org-priority-regexp)
  11340. (setq current (string-to-char (match-string 2))
  11341. have t))
  11342. (cond
  11343. ((eq action 'remove)
  11344. (setq remove t new ?\ ))
  11345. ((or (eq action 'set)
  11346. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11347. (if (not (eq action 'set))
  11348. (setq new action)
  11349. (message "Priority %c-%c, SPC to remove: "
  11350. org-highest-priority org-lowest-priority)
  11351. (save-match-data
  11352. (setq new (read-char-exclusive))))
  11353. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11354. (= (upcase org-lowest-priority) org-lowest-priority))
  11355. (setq new (upcase new)))
  11356. (cond ((equal new ?\ ) (setq remove t))
  11357. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11358. (error "Priority must be between `%c' and `%c'"
  11359. org-highest-priority org-lowest-priority))))
  11360. ((eq action 'up)
  11361. (setq new (if have
  11362. (1- current) ; normal cycling
  11363. ;; last priority was empty
  11364. (if (eq last-command this-command)
  11365. org-lowest-priority ; wrap around empty to lowest
  11366. ;; default
  11367. (if org-priority-start-cycle-with-default
  11368. org-default-priority
  11369. (1- org-default-priority))))))
  11370. ((eq action 'down)
  11371. (setq new (if have
  11372. (1+ current) ; normal cycling
  11373. ;; last priority was empty
  11374. (if (eq last-command this-command)
  11375. org-highest-priority ; wrap around empty to highest
  11376. ;; default
  11377. (if org-priority-start-cycle-with-default
  11378. org-default-priority
  11379. (1+ org-default-priority))))))
  11380. (t (error "Invalid action")))
  11381. (if (or (< (upcase new) org-highest-priority)
  11382. (> (upcase new) org-lowest-priority))
  11383. (if (and (memq action '(up down))
  11384. (not have) (not (eq last-command this-command)))
  11385. ;; `new' is from default priority
  11386. (error
  11387. "The default can not be set, see `org-default-priority' why")
  11388. ;; normal cycling: `new' is beyond highest/lowest priority
  11389. ;; and is wrapped around to the empty priority
  11390. (setq remove t)))
  11391. (setq news (format "%c" new))
  11392. (if have
  11393. (if remove
  11394. (replace-match "" t t nil 1)
  11395. (replace-match news t t nil 2))
  11396. (if remove
  11397. (error "No priority cookie found in line")
  11398. (let ((case-fold-search nil))
  11399. (looking-at org-todo-line-regexp))
  11400. (if (match-end 2)
  11401. (progn
  11402. (goto-char (match-end 2))
  11403. (insert " [#" news "]"))
  11404. (goto-char (match-beginning 3))
  11405. (insert "[#" news "] "))))
  11406. (org-preserve-lc (org-set-tags nil 'align)))
  11407. (if remove
  11408. (message "Priority removed")
  11409. (message "Priority of current item set to %s" news))))
  11410. (defun org-get-priority (s)
  11411. "Find priority cookie and return priority."
  11412. (if (functionp org-get-priority-function)
  11413. (funcall org-get-priority-function)
  11414. (save-match-data
  11415. (if (not (string-match org-priority-regexp s))
  11416. (* 1000 (- org-lowest-priority org-default-priority))
  11417. (* 1000 (- org-lowest-priority
  11418. (string-to-char (match-string 2 s))))))))
  11419. ;;;; Tags
  11420. (defvar org-agenda-archives-mode)
  11421. (defvar org-map-continue-from nil
  11422. "Position from where mapping should continue.
  11423. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11424. (defvar org-scanner-tags nil
  11425. "The current tag list while the tags scanner is running.")
  11426. (defvar org-trust-scanner-tags nil
  11427. "Should `org-get-tags-at' use the tags for the scanner.
  11428. This is for internal dynamical scoping only.
  11429. When this is non-nil, the function `org-get-tags-at' will return the value
  11430. of `org-scanner-tags' instead of building the list by itself. This
  11431. can lead to large speed-ups when the tags scanner is used in a file with
  11432. many entries, and when the list of tags is retrieved, for example to
  11433. obtain a list of properties. Building the tags list for each entry in such
  11434. a file becomes an N^2 operation - but with this variable set, it scales
  11435. as N.")
  11436. (defun org-scan-tags (action matcher &optional todo-only)
  11437. "Scan headline tags with inheritance and produce output ACTION.
  11438. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11439. or `agenda' to produce an entry list for an agenda view. It can also be
  11440. a Lisp form or a function that should be called at each matched headline, in
  11441. this case the return value is a list of all return values from these calls.
  11442. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11443. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11444. only lines with a TODO keyword are included in the output."
  11445. (require 'org-agenda)
  11446. (let* ((re (concat "^" org-outline-regexp " *\\(\\<\\("
  11447. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11448. (org-re
  11449. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11450. (props (list 'face 'default
  11451. 'done-face 'org-agenda-done
  11452. 'undone-face 'default
  11453. 'mouse-face 'highlight
  11454. 'org-not-done-regexp org-not-done-regexp
  11455. 'org-todo-regexp org-todo-regexp
  11456. 'help-echo
  11457. (format "mouse-2 or RET jump to org file %s"
  11458. (abbreviate-file-name
  11459. (or (buffer-file-name (buffer-base-buffer))
  11460. (buffer-name (buffer-base-buffer)))))))
  11461. (case-fold-search nil)
  11462. (org-map-continue-from nil)
  11463. lspos tags tags-list
  11464. (tags-alist (list (cons 0 org-file-tags)))
  11465. (llast 0) rtn rtn1 level category i txt
  11466. todo marker entry priority)
  11467. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11468. (setq action (list 'lambda nil action)))
  11469. (save-excursion
  11470. (goto-char (point-min))
  11471. (when (eq action 'sparse-tree)
  11472. (org-overview)
  11473. (org-remove-occur-highlights))
  11474. (while (re-search-forward re nil t)
  11475. (catch :skip
  11476. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11477. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11478. (goto-char (setq lspos (match-beginning 0)))
  11479. (setq level (org-reduced-level (funcall outline-level))
  11480. category (org-get-category))
  11481. (setq i llast llast level)
  11482. ;; remove tag lists from same and sublevels
  11483. (while (>= i level)
  11484. (when (setq entry (assoc i tags-alist))
  11485. (setq tags-alist (delete entry tags-alist)))
  11486. (setq i (1- i)))
  11487. ;; add the next tags
  11488. (when tags
  11489. (setq tags (org-split-string tags ":")
  11490. tags-alist
  11491. (cons (cons level tags) tags-alist)))
  11492. ;; compile tags for current headline
  11493. (setq tags-list
  11494. (if org-use-tag-inheritance
  11495. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11496. tags)
  11497. org-scanner-tags tags-list)
  11498. (when org-use-tag-inheritance
  11499. (setcdr (car tags-alist)
  11500. (mapcar (lambda (x)
  11501. (setq x (copy-sequence x))
  11502. (org-add-prop-inherited x))
  11503. (cdar tags-alist))))
  11504. (when (and tags org-use-tag-inheritance
  11505. (or (not (eq t org-use-tag-inheritance))
  11506. org-tags-exclude-from-inheritance))
  11507. ;; selective inheritance, remove uninherited ones
  11508. (setcdr (car tags-alist)
  11509. (org-remove-uninherited-tags (cdar tags-alist))))
  11510. (when (and
  11511. ;; eval matcher only when the todo condition is OK
  11512. (and (or (not todo-only) (member todo org-not-done-keywords))
  11513. (let ((case-fold-search t)) (eval matcher)))
  11514. ;; Call the skipper, but return t if it does not skip,
  11515. ;; so that the `and' form continues evaluating
  11516. (progn
  11517. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11518. t)
  11519. ;; Check if timestamps are deselecting this entry
  11520. (or (not todo-only)
  11521. (and (member todo org-not-done-keywords)
  11522. (or (not org-agenda-tags-todo-honor-ignore-options)
  11523. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11524. ;; Extra check for the archive tag
  11525. ;; FIXME: Does the skipper already do this????
  11526. (or
  11527. (not (member org-archive-tag tags-list))
  11528. ;; we have an archive tag, should we use this anyway?
  11529. (or (not org-agenda-skip-archived-trees)
  11530. (and (eq action 'agenda) org-agenda-archives-mode))))
  11531. ;; select this headline
  11532. (cond
  11533. ((eq action 'sparse-tree)
  11534. (and org-highlight-sparse-tree-matches
  11535. (org-get-heading) (match-end 0)
  11536. (org-highlight-new-match
  11537. (match-beginning 0) (match-beginning 1)))
  11538. (org-show-context 'tags-tree))
  11539. ((eq action 'agenda)
  11540. (setq txt (org-format-agenda-item
  11541. ""
  11542. (concat
  11543. (if (eq org-tags-match-list-sublevels 'indented)
  11544. (make-string (1- level) ?.) "")
  11545. (org-get-heading))
  11546. category
  11547. tags-list
  11548. )
  11549. priority (org-get-priority txt))
  11550. (goto-char lspos)
  11551. (setq marker (org-agenda-new-marker))
  11552. (org-add-props txt props
  11553. 'org-marker marker 'org-hd-marker marker 'org-category category
  11554. 'todo-state todo
  11555. 'priority priority 'type "tagsmatch")
  11556. (push txt rtn))
  11557. ((functionp action)
  11558. (setq org-map-continue-from nil)
  11559. (save-excursion
  11560. (setq rtn1 (funcall action))
  11561. (push rtn1 rtn)))
  11562. (t (error "Invalid action")))
  11563. ;; if we are to skip sublevels, jump to end of subtree
  11564. (unless org-tags-match-list-sublevels
  11565. (org-end-of-subtree t)
  11566. (backward-char 1))))
  11567. ;; Get the correct position from where to continue
  11568. (if org-map-continue-from
  11569. (goto-char org-map-continue-from)
  11570. (and (= (point) lspos) (end-of-line 1)))))
  11571. (when (and (eq action 'sparse-tree)
  11572. (not org-sparse-tree-open-archived-trees))
  11573. (org-hide-archived-subtrees (point-min) (point-max)))
  11574. (nreverse rtn)))
  11575. (defun org-remove-uninherited-tags (tags)
  11576. "Remove all tags that are not inherited from the list TAGS."
  11577. (cond
  11578. ((eq org-use-tag-inheritance t)
  11579. (if org-tags-exclude-from-inheritance
  11580. (org-delete-all org-tags-exclude-from-inheritance tags)
  11581. tags))
  11582. ((not org-use-tag-inheritance) nil)
  11583. ((stringp org-use-tag-inheritance)
  11584. (delq nil (mapcar
  11585. (lambda (x)
  11586. (if (and (string-match org-use-tag-inheritance x)
  11587. (not (member x org-tags-exclude-from-inheritance)))
  11588. x nil))
  11589. tags)))
  11590. ((listp org-use-tag-inheritance)
  11591. (delq nil (mapcar
  11592. (lambda (x)
  11593. (if (member x org-use-tag-inheritance) x nil))
  11594. tags)))))
  11595. (defvar todo-only) ;; dynamically scoped
  11596. (defun org-match-sparse-tree (&optional todo-only match)
  11597. "Create a sparse tree according to tags string MATCH.
  11598. MATCH can contain positive and negative selection of tags, like
  11599. \"+WORK+URGENT-WITHBOSS\".
  11600. If optional argument TODO-ONLY is non-nil, only select lines that are
  11601. also TODO lines."
  11602. (interactive "P")
  11603. (org-prepare-agenda-buffers (list (current-buffer)))
  11604. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11605. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11606. (defvar org-cached-props nil)
  11607. (defun org-cached-entry-get (pom property)
  11608. (if (or (eq t org-use-property-inheritance)
  11609. (and (stringp org-use-property-inheritance)
  11610. (string-match org-use-property-inheritance property))
  11611. (and (listp org-use-property-inheritance)
  11612. (member property org-use-property-inheritance)))
  11613. ;; Caching is not possible, check it directly
  11614. (org-entry-get pom property 'inherit)
  11615. ;; Get all properties, so that we can do complicated checks easily
  11616. (cdr (assoc property (or org-cached-props
  11617. (setq org-cached-props
  11618. (org-entry-properties pom)))))))
  11619. (defun org-global-tags-completion-table (&optional files)
  11620. "Return the list of all tags in all agenda buffer/files.
  11621. Optional FILES argument is a list of files to which can be used
  11622. instead of the agenda files."
  11623. (save-excursion
  11624. (org-uniquify
  11625. (delq nil
  11626. (apply 'append
  11627. (mapcar
  11628. (lambda (file)
  11629. (set-buffer (find-file-noselect file))
  11630. (append (org-get-buffer-tags)
  11631. (mapcar (lambda (x) (if (stringp (car-safe x))
  11632. (list (car-safe x)) nil))
  11633. org-tag-alist)))
  11634. (if (and files (car files))
  11635. files
  11636. (org-agenda-files))))))))
  11637. (defun org-make-tags-matcher (match)
  11638. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11639. ;; todo-only is scoped dynamically into this function, and the function
  11640. ;; may change it if the matcher asks for it.
  11641. (unless match
  11642. ;; Get a new match request, with completion
  11643. (let ((org-last-tags-completion-table
  11644. (org-global-tags-completion-table)))
  11645. (setq match (org-completing-read-no-i
  11646. "Match: " 'org-tags-completion-function nil nil nil
  11647. 'org-tags-history))))
  11648. ;; Parse the string and create a lisp form
  11649. (let ((match0 match)
  11650. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11651. minus tag mm
  11652. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11653. orterms term orlist re-p str-p level-p level-op time-p
  11654. prop-p pn pv po gv rest)
  11655. (if (string-match "/+" match)
  11656. ;; match contains also a todo-matching request
  11657. (progn
  11658. (setq tagsmatch (substring match 0 (match-beginning 0))
  11659. todomatch (substring match (match-end 0)))
  11660. (if (string-match "^!" todomatch)
  11661. (setq todo-only t todomatch (substring todomatch 1)))
  11662. (if (string-match "^\\s-*$" todomatch)
  11663. (setq todomatch nil)))
  11664. ;; only matching tags
  11665. (setq tagsmatch match todomatch nil))
  11666. ;; Make the tags matcher
  11667. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11668. (setq tagsmatcher t)
  11669. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11670. (while (setq term (pop orterms))
  11671. (while (and (equal (substring term -1) "\\") orterms)
  11672. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11673. (while (string-match re term)
  11674. (setq rest (substring term (match-end 0))
  11675. minus (and (match-end 1)
  11676. (equal (match-string 1 term) "-"))
  11677. tag (save-match-data (replace-regexp-in-string
  11678. "\\\\-" "-"
  11679. (match-string 2 term)))
  11680. re-p (equal (string-to-char tag) ?{)
  11681. level-p (match-end 4)
  11682. prop-p (match-end 5)
  11683. mm (cond
  11684. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11685. (level-p
  11686. (setq level-op (org-op-to-function (match-string 3 term)))
  11687. `(,level-op level ,(string-to-number
  11688. (match-string 4 term))))
  11689. (prop-p
  11690. (setq pn (match-string 5 term)
  11691. po (match-string 6 term)
  11692. pv (match-string 7 term)
  11693. re-p (equal (string-to-char pv) ?{)
  11694. str-p (equal (string-to-char pv) ?\")
  11695. time-p (save-match-data
  11696. (string-match "^\"[[<].*[]>]\"$" pv))
  11697. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11698. (if time-p (setq pv (org-matcher-time pv)))
  11699. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11700. (cond
  11701. ((equal pn "CATEGORY")
  11702. (setq gv '(get-text-property (point) 'org-category)))
  11703. ((equal pn "TODO")
  11704. (setq gv 'todo))
  11705. (t
  11706. (setq gv `(org-cached-entry-get nil ,pn))))
  11707. (if re-p
  11708. (if (eq po 'org<>)
  11709. `(not (string-match ,pv (or ,gv "")))
  11710. `(string-match ,pv (or ,gv "")))
  11711. (if str-p
  11712. `(,po (or ,gv "") ,pv)
  11713. `(,po (string-to-number (or ,gv ""))
  11714. ,(string-to-number pv) ))))
  11715. (t `(member ,tag tags-list)))
  11716. mm (if minus (list 'not mm) mm)
  11717. term rest)
  11718. (push mm tagsmatcher))
  11719. (push (if (> (length tagsmatcher) 1)
  11720. (cons 'and tagsmatcher)
  11721. (car tagsmatcher))
  11722. orlist)
  11723. (setq tagsmatcher nil))
  11724. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11725. (setq tagsmatcher
  11726. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11727. ;; Make the todo matcher
  11728. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11729. (setq todomatcher t)
  11730. (setq orterms (org-split-string todomatch "|") orlist nil)
  11731. (while (setq term (pop orterms))
  11732. (while (string-match re term)
  11733. (setq minus (and (match-end 1)
  11734. (equal (match-string 1 term) "-"))
  11735. kwd (match-string 2 term)
  11736. re-p (equal (string-to-char kwd) ?{)
  11737. term (substring term (match-end 0))
  11738. mm (if re-p
  11739. `(string-match ,(substring kwd 1 -1) todo)
  11740. (list 'equal 'todo kwd))
  11741. mm (if minus (list 'not mm) mm))
  11742. (push mm todomatcher))
  11743. (push (if (> (length todomatcher) 1)
  11744. (cons 'and todomatcher)
  11745. (car todomatcher))
  11746. orlist)
  11747. (setq todomatcher nil))
  11748. (setq todomatcher (if (> (length orlist) 1)
  11749. (cons 'or orlist) (car orlist))))
  11750. ;; Return the string and lisp forms of the matcher
  11751. (setq matcher (if todomatcher
  11752. (list 'and tagsmatcher todomatcher)
  11753. tagsmatcher))
  11754. (cons match0 matcher)))
  11755. (defun org-op-to-function (op &optional stringp)
  11756. "Turn an operator into the appropriate function."
  11757. (setq op
  11758. (cond
  11759. ((equal op "<" ) '(< string< org-time<))
  11760. ((equal op ">" ) '(> org-string> org-time>))
  11761. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11762. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11763. ((member op '("=" "==")) '(= string= org-time=))
  11764. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11765. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11766. (defun org<> (a b) (not (= a b)))
  11767. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11768. (defun org-string>= (a b) (not (string< a b)))
  11769. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11770. (defun org-string<> (a b) (not (string= a b)))
  11771. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11772. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11773. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11774. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11775. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11776. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11777. (defun org-2ft (s)
  11778. "Convert S to a floating point time.
  11779. If S is already a number, just return it. If it is a string, parse
  11780. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11781. (cond
  11782. ((numberp s) s)
  11783. ((stringp s)
  11784. (condition-case nil
  11785. (float-time (apply 'encode-time (org-parse-time-string s)))
  11786. (error 0.)))
  11787. (t 0.)))
  11788. (defun org-time-today ()
  11789. "Time in seconds today at 0:00.
  11790. Returns the float number of seconds since the beginning of the
  11791. epoch to the beginning of today (00:00)."
  11792. (float-time (apply 'encode-time
  11793. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11794. (defun org-matcher-time (s)
  11795. "Interpret a time comparison value."
  11796. (save-match-data
  11797. (cond
  11798. ((string= s "<now>") (float-time))
  11799. ((string= s "<today>") (org-time-today))
  11800. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11801. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11802. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11803. (+ (org-time-today)
  11804. (* (string-to-number (match-string 1 s))
  11805. (cdr (assoc (match-string 2 s)
  11806. '(("d" . 86400.0) ("w" . 604800.0)
  11807. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11808. (t (org-2ft s)))))
  11809. (defun org-match-any-p (re list)
  11810. "Does re match any element of list?"
  11811. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11812. (delq nil list))
  11813. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11814. (defvar org-tags-overlay (make-overlay 1 1))
  11815. (org-detach-overlay org-tags-overlay)
  11816. (defun org-get-local-tags-at (&optional pos)
  11817. "Get a list of tags defined in the current headline."
  11818. (org-get-tags-at pos 'local))
  11819. (defun org-get-local-tags ()
  11820. "Get a list of tags defined in the current headline."
  11821. (org-get-tags-at nil 'local))
  11822. (defun org-get-tags-at (&optional pos local)
  11823. "Get a list of all headline tags applicable at POS.
  11824. POS defaults to point. If tags are inherited, the list contains
  11825. the targets in the same sequence as the headlines appear, i.e.
  11826. the tags of the current headline come last.
  11827. When LOCAL is non-nil, only return tags from the current headline,
  11828. ignore inherited ones."
  11829. (interactive)
  11830. (if (and org-trust-scanner-tags
  11831. (or (not pos) (equal pos (point)))
  11832. (not local))
  11833. org-scanner-tags
  11834. (let (tags ltags lastpos parent)
  11835. (save-excursion
  11836. (save-restriction
  11837. (widen)
  11838. (goto-char (or pos (point)))
  11839. (save-match-data
  11840. (catch 'done
  11841. (condition-case nil
  11842. (progn
  11843. (org-back-to-heading t)
  11844. (while (not (equal lastpos (point)))
  11845. (setq lastpos (point))
  11846. (when (looking-at
  11847. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11848. (setq ltags (org-split-string
  11849. (org-match-string-no-properties 1) ":"))
  11850. (when parent
  11851. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11852. (setq tags (append
  11853. (if parent
  11854. (org-remove-uninherited-tags ltags)
  11855. ltags)
  11856. tags)))
  11857. (or org-use-tag-inheritance (throw 'done t))
  11858. (if local (throw 'done t))
  11859. (or (org-up-heading-safe) (error nil))
  11860. (setq parent t)))
  11861. (error nil)))))
  11862. (if local
  11863. tags
  11864. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  11865. (defun org-add-prop-inherited (s)
  11866. (add-text-properties 0 (length s) '(inherited t) s)
  11867. s)
  11868. (defun org-toggle-tag (tag &optional onoff)
  11869. "Toggle the tag TAG for the current line.
  11870. If ONOFF is `on' or `off', don't toggle but set to this state."
  11871. (let (res current)
  11872. (save-excursion
  11873. (org-back-to-heading t)
  11874. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11875. (point-at-eol) t)
  11876. (progn
  11877. (setq current (match-string 1))
  11878. (replace-match ""))
  11879. (setq current ""))
  11880. (setq current (nreverse (org-split-string current ":")))
  11881. (cond
  11882. ((eq onoff 'on)
  11883. (setq res t)
  11884. (or (member tag current) (push tag current)))
  11885. ((eq onoff 'off)
  11886. (or (not (member tag current)) (setq current (delete tag current))))
  11887. (t (if (member tag current)
  11888. (setq current (delete tag current))
  11889. (setq res t)
  11890. (push tag current))))
  11891. (end-of-line 1)
  11892. (if current
  11893. (progn
  11894. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11895. (org-set-tags nil t))
  11896. (delete-horizontal-space))
  11897. (run-hooks 'org-after-tags-change-hook))
  11898. res))
  11899. (defun org-align-tags-here (to-col)
  11900. ;; Assumes that this is a headline
  11901. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11902. (beginning-of-line 1)
  11903. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11904. (< pos (match-beginning 2)))
  11905. (progn
  11906. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11907. (goto-char (match-beginning 1))
  11908. (insert " ")
  11909. (delete-region (point) (1+ (match-beginning 2)))
  11910. (setq ncol (max (1+ (current-column))
  11911. (1+ col)
  11912. (if (> to-col 0)
  11913. to-col
  11914. (- (abs to-col) tags-l))))
  11915. (setq p (point))
  11916. (insert (make-string (- ncol (current-column)) ?\ ))
  11917. (setq ncol (current-column))
  11918. (when indent-tabs-mode (tabify p (point-at-eol)))
  11919. (org-move-to-column (min ncol col) t))
  11920. (goto-char pos))))
  11921. (defun org-set-tags-command (&optional arg just-align)
  11922. "Call the set-tags command for the current entry."
  11923. (interactive "P")
  11924. (if (org-on-heading-p)
  11925. (org-set-tags arg just-align)
  11926. (save-excursion
  11927. (org-back-to-heading t)
  11928. (org-set-tags arg just-align))))
  11929. (defun org-set-tags-to (data)
  11930. "Set the tags of the current entry to DATA, replacing the current tags.
  11931. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11932. If DATA is nil or the empty string, any tags will be removed."
  11933. (interactive "sTags: ")
  11934. (setq data
  11935. (cond
  11936. ((eq data nil) "")
  11937. ((equal data "") "")
  11938. ((stringp data)
  11939. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11940. ":"))
  11941. ((listp data)
  11942. (concat ":" (mapconcat 'identity data ":") ":"))
  11943. (t nil)))
  11944. (when data
  11945. (save-excursion
  11946. (org-back-to-heading t)
  11947. (when (looking-at org-complex-heading-regexp)
  11948. (if (match-end 5)
  11949. (progn
  11950. (goto-char (match-beginning 5))
  11951. (insert data)
  11952. (delete-region (point) (point-at-eol))
  11953. (org-set-tags nil 'align))
  11954. (goto-char (point-at-eol))
  11955. (insert " " data)
  11956. (org-set-tags nil 'align)))
  11957. (beginning-of-line 1)
  11958. (if (looking-at ".*?\\([ \t]+\\)$")
  11959. (delete-region (match-beginning 1) (match-end 1))))))
  11960. (defun org-align-all-tags ()
  11961. "Align the tags i all headings."
  11962. (interactive)
  11963. (save-excursion
  11964. (or (ignore-errors (org-back-to-heading t))
  11965. (outline-next-heading))
  11966. (if (org-on-heading-p)
  11967. (org-set-tags t)
  11968. (message "No headings"))))
  11969. (defvar org-indent-indentation-per-level)
  11970. (defun org-set-tags (&optional arg just-align)
  11971. "Set the tags for the current headline.
  11972. With prefix ARG, realign all tags in headings in the current buffer."
  11973. (interactive "P")
  11974. (let* ((re org-outline-regexp-bol)
  11975. (current (org-get-tags-string))
  11976. (col (current-column))
  11977. (org-setting-tags t)
  11978. table current-tags inherited-tags ; computed below when needed
  11979. tags p0 c0 c1 rpl di tc level)
  11980. (if arg
  11981. (save-excursion
  11982. (goto-char (point-min))
  11983. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11984. (while (re-search-forward re nil t)
  11985. (org-set-tags nil t)
  11986. (end-of-line 1)))
  11987. (message "All tags realigned to column %d" org-tags-column))
  11988. (if just-align
  11989. (setq tags current)
  11990. ;; Get a new set of tags from the user
  11991. (save-excursion
  11992. (setq table (append org-tag-persistent-alist
  11993. (or org-tag-alist (org-get-buffer-tags))
  11994. (and
  11995. org-complete-tags-always-offer-all-agenda-tags
  11996. (org-global-tags-completion-table
  11997. (org-agenda-files))))
  11998. org-last-tags-completion-table table
  11999. current-tags (org-split-string current ":")
  12000. inherited-tags (nreverse
  12001. (nthcdr (length current-tags)
  12002. (nreverse (org-get-tags-at))))
  12003. tags
  12004. (if (or (eq t org-use-fast-tag-selection)
  12005. (and org-use-fast-tag-selection
  12006. (delq nil (mapcar 'cdr table))))
  12007. (org-fast-tag-selection
  12008. current-tags inherited-tags table
  12009. (if org-fast-tag-selection-include-todo
  12010. org-todo-key-alist))
  12011. (let ((org-add-colon-after-tag-completion t))
  12012. (org-trim
  12013. (org-icompleting-read "Tags: "
  12014. 'org-tags-completion-function
  12015. nil nil current 'org-tags-history))))))
  12016. (while (string-match "[-+&]+" tags)
  12017. ;; No boolean logic, just a list
  12018. (setq tags (replace-match ":" t t tags))))
  12019. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12020. (if org-tags-sort-function
  12021. (setq tags (mapconcat 'identity
  12022. (sort (org-split-string
  12023. tags (org-re "[^[:alnum:]_@#%]+"))
  12024. org-tags-sort-function) ":")))
  12025. (if (string-match "\\`[\t ]*\\'" tags)
  12026. (setq tags "")
  12027. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12028. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12029. ;; Insert new tags at the correct column
  12030. (beginning-of-line 1)
  12031. (setq level (or (and (looking-at org-outline-regexp)
  12032. (- (match-end 0) (point) 1))
  12033. 1))
  12034. (cond
  12035. ((and (equal current "") (equal tags "")))
  12036. ((re-search-forward
  12037. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12038. (point-at-eol) t)
  12039. (if (equal tags "")
  12040. (setq rpl "")
  12041. (goto-char (match-beginning 0))
  12042. (setq c0 (current-column)
  12043. ;; compute offset for the case of org-indent-mode active
  12044. di (if org-indent-mode
  12045. (* (1- org-indent-indentation-per-level) (1- level))
  12046. 0)
  12047. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12048. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12049. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12050. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12051. (replace-match rpl t t)
  12052. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12053. tags)
  12054. (t (error "Tags alignment failed")))
  12055. (org-move-to-column col)
  12056. (unless just-align
  12057. (run-hooks 'org-after-tags-change-hook)))))
  12058. (defun org-change-tag-in-region (beg end tag off)
  12059. "Add or remove TAG for each entry in the region.
  12060. This works in the agenda, and also in an org-mode buffer."
  12061. (interactive
  12062. (list (region-beginning) (region-end)
  12063. (let ((org-last-tags-completion-table
  12064. (if (org-mode-p)
  12065. (org-get-buffer-tags)
  12066. (org-global-tags-completion-table))))
  12067. (org-icompleting-read
  12068. "Tag: " 'org-tags-completion-function nil nil nil
  12069. 'org-tags-history))
  12070. (progn
  12071. (message "[s]et or [r]emove? ")
  12072. (equal (read-char-exclusive) ?r))))
  12073. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12074. (let ((agendap (equal major-mode 'org-agenda-mode))
  12075. l1 l2 m buf pos newhead (cnt 0))
  12076. (goto-char end)
  12077. (setq l2 (1- (org-current-line)))
  12078. (goto-char beg)
  12079. (setq l1 (org-current-line))
  12080. (loop for l from l1 to l2 do
  12081. (org-goto-line l)
  12082. (setq m (get-text-property (point) 'org-hd-marker))
  12083. (when (or (and (org-mode-p) (org-on-heading-p))
  12084. (and agendap m))
  12085. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12086. pos (if agendap m (point)))
  12087. (with-current-buffer buf
  12088. (save-excursion
  12089. (save-restriction
  12090. (goto-char pos)
  12091. (setq cnt (1+ cnt))
  12092. (org-toggle-tag tag (if off 'off 'on))
  12093. (setq newhead (org-get-heading)))))
  12094. (and agendap (org-agenda-change-all-lines newhead m))))
  12095. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12096. (defun org-tags-completion-function (string predicate &optional flag)
  12097. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12098. (confirm (lambda (x) (stringp (car x)))))
  12099. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12100. (setq s1 (match-string 1 string)
  12101. s2 (match-string 2 string))
  12102. (setq s1 "" s2 string))
  12103. (cond
  12104. ((eq flag nil)
  12105. ;; try completion
  12106. (setq rtn (try-completion s2 ctable confirm))
  12107. (if (stringp rtn)
  12108. (setq rtn
  12109. (concat s1 s2 (substring rtn (length s2))
  12110. (if (and org-add-colon-after-tag-completion
  12111. (assoc rtn ctable))
  12112. ":" ""))))
  12113. rtn)
  12114. ((eq flag t)
  12115. ;; all-completions
  12116. (all-completions s2 ctable confirm)
  12117. )
  12118. ((eq flag 'lambda)
  12119. ;; exact match?
  12120. (assoc s2 ctable)))
  12121. ))
  12122. (defun org-fast-tag-insert (kwd tags face &optional end)
  12123. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12124. (insert (format "%-12s" (concat kwd ":"))
  12125. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12126. (or end "")))
  12127. (defun org-fast-tag-show-exit (flag)
  12128. (save-excursion
  12129. (org-goto-line 3)
  12130. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12131. (replace-match ""))
  12132. (when flag
  12133. (end-of-line 1)
  12134. (org-move-to-column (- (window-width) 19) t)
  12135. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12136. (defun org-set-current-tags-overlay (current prefix)
  12137. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12138. (if (featurep 'xemacs)
  12139. (org-overlay-display org-tags-overlay (concat prefix s)
  12140. 'secondary-selection)
  12141. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12142. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12143. (defvar org-last-tag-selection-key nil)
  12144. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12145. "Fast tag selection with single keys.
  12146. CURRENT is the current list of tags in the headline, INHERITED is the
  12147. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12148. possibly with grouping information. TODO-TABLE is a similar table with
  12149. TODO keywords, should these have keys assigned to them.
  12150. If the keys are nil, a-z are automatically assigned.
  12151. Returns the new tags string, or nil to not change the current settings."
  12152. (let* ((fulltable (append table todo-table))
  12153. (maxlen (apply 'max (mapcar
  12154. (lambda (x)
  12155. (if (stringp (car x)) (string-width (car x)) 0))
  12156. fulltable)))
  12157. (buf (current-buffer))
  12158. (expert (eq org-fast-tag-selection-single-key 'expert))
  12159. (buffer-tags nil)
  12160. (fwidth (+ maxlen 3 1 3))
  12161. (ncol (/ (- (window-width) 4) fwidth))
  12162. (i-face 'org-done)
  12163. (c-face 'org-todo)
  12164. tg cnt e c char c1 c2 ntable tbl rtn
  12165. ov-start ov-end ov-prefix
  12166. (exit-after-next org-fast-tag-selection-single-key)
  12167. (done-keywords org-done-keywords)
  12168. groups ingroup)
  12169. (save-excursion
  12170. (beginning-of-line 1)
  12171. (if (looking-at
  12172. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12173. (setq ov-start (match-beginning 1)
  12174. ov-end (match-end 1)
  12175. ov-prefix "")
  12176. (setq ov-start (1- (point-at-eol))
  12177. ov-end (1+ ov-start))
  12178. (skip-chars-forward "^\n\r")
  12179. (setq ov-prefix
  12180. (concat
  12181. (buffer-substring (1- (point)) (point))
  12182. (if (> (current-column) org-tags-column)
  12183. " "
  12184. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12185. (move-overlay org-tags-overlay ov-start ov-end)
  12186. (save-window-excursion
  12187. (if expert
  12188. (set-buffer (get-buffer-create " *Org tags*"))
  12189. (delete-other-windows)
  12190. (split-window-vertically)
  12191. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12192. (erase-buffer)
  12193. (org-set-local 'org-done-keywords done-keywords)
  12194. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12195. (org-fast-tag-insert "Current" current c-face "\n\n")
  12196. (org-fast-tag-show-exit exit-after-next)
  12197. (org-set-current-tags-overlay current ov-prefix)
  12198. (setq tbl fulltable char ?a cnt 0)
  12199. (while (setq e (pop tbl))
  12200. (cond
  12201. ((equal (car e) :startgroup)
  12202. (push '() groups) (setq ingroup t)
  12203. (when (not (= cnt 0))
  12204. (setq cnt 0)
  12205. (insert "\n"))
  12206. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12207. ((equal (car e) :endgroup)
  12208. (setq ingroup nil cnt 0)
  12209. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12210. ((equal e '(:newline))
  12211. (when (not (= cnt 0))
  12212. (setq cnt 0)
  12213. (insert "\n")
  12214. (setq e (car tbl))
  12215. (while (equal (car tbl) '(:newline))
  12216. (insert "\n")
  12217. (setq tbl (cdr tbl)))))
  12218. (t
  12219. (setq tg (copy-sequence (car e)) c2 nil)
  12220. (if (cdr e)
  12221. (setq c (cdr e))
  12222. ;; automatically assign a character.
  12223. (setq c1 (string-to-char
  12224. (downcase (substring
  12225. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12226. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12227. (while (or (rassoc char ntable) (rassoc char table))
  12228. (setq char (1+ char)))
  12229. (setq c2 c1))
  12230. (setq c (or c2 char)))
  12231. (if ingroup (push tg (car groups)))
  12232. (setq tg (org-add-props tg nil 'face
  12233. (cond
  12234. ((not (assoc tg table))
  12235. (org-get-todo-face tg))
  12236. ((member tg current) c-face)
  12237. ((member tg inherited) i-face)
  12238. (t nil))))
  12239. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12240. (insert "[" c "] " tg (make-string
  12241. (- fwidth 4 (length tg)) ?\ ))
  12242. (push (cons tg c) ntable)
  12243. (when (= (setq cnt (1+ cnt)) ncol)
  12244. (insert "\n")
  12245. (if ingroup (insert " "))
  12246. (setq cnt 0)))))
  12247. (setq ntable (nreverse ntable))
  12248. (insert "\n")
  12249. (goto-char (point-min))
  12250. (if (not expert) (org-fit-window-to-buffer))
  12251. (setq rtn
  12252. (catch 'exit
  12253. (while t
  12254. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12255. (if (not groups) "no " "")
  12256. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12257. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12258. (setq org-last-tag-selection-key c)
  12259. (cond
  12260. ((= c ?\r) (throw 'exit t))
  12261. ((= c ?!)
  12262. (setq groups (not groups))
  12263. (goto-char (point-min))
  12264. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12265. ((= c ?\C-c)
  12266. (if (not expert)
  12267. (org-fast-tag-show-exit
  12268. (setq exit-after-next (not exit-after-next)))
  12269. (setq expert nil)
  12270. (delete-other-windows)
  12271. (set-window-buffer (split-window-vertically) " *Org tags*")
  12272. (org-switch-to-buffer-other-window " *Org tags*")
  12273. (org-fit-window-to-buffer)))
  12274. ((or (= c ?\C-g)
  12275. (and (= c ?q) (not (rassoc c ntable))))
  12276. (org-detach-overlay org-tags-overlay)
  12277. (setq quit-flag t))
  12278. ((= c ?\ )
  12279. (setq current nil)
  12280. (if exit-after-next (setq exit-after-next 'now)))
  12281. ((= c ?\t)
  12282. (condition-case nil
  12283. (setq tg (org-icompleting-read
  12284. "Tag: "
  12285. (or buffer-tags
  12286. (with-current-buffer buf
  12287. (org-get-buffer-tags)))))
  12288. (quit (setq tg "")))
  12289. (when (string-match "\\S-" tg)
  12290. (add-to-list 'buffer-tags (list tg))
  12291. (if (member tg current)
  12292. (setq current (delete tg current))
  12293. (push tg current)))
  12294. (if exit-after-next (setq exit-after-next 'now)))
  12295. ((setq e (rassoc c todo-table) tg (car e))
  12296. (with-current-buffer buf
  12297. (save-excursion (org-todo tg)))
  12298. (if exit-after-next (setq exit-after-next 'now)))
  12299. ((setq e (rassoc c ntable) tg (car e))
  12300. (if (member tg current)
  12301. (setq current (delete tg current))
  12302. (loop for g in groups do
  12303. (if (member tg g)
  12304. (mapc (lambda (x)
  12305. (setq current (delete x current)))
  12306. g)))
  12307. (push tg current))
  12308. (if exit-after-next (setq exit-after-next 'now))))
  12309. ;; Create a sorted list
  12310. (setq current
  12311. (sort current
  12312. (lambda (a b)
  12313. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12314. (if (eq exit-after-next 'now) (throw 'exit t))
  12315. (goto-char (point-min))
  12316. (beginning-of-line 2)
  12317. (delete-region (point) (point-at-eol))
  12318. (org-fast-tag-insert "Current" current c-face)
  12319. (org-set-current-tags-overlay current ov-prefix)
  12320. (while (re-search-forward
  12321. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12322. (setq tg (match-string 1))
  12323. (add-text-properties
  12324. (match-beginning 1) (match-end 1)
  12325. (list 'face
  12326. (cond
  12327. ((member tg current) c-face)
  12328. ((member tg inherited) i-face)
  12329. (t (get-text-property (match-beginning 1) 'face))))))
  12330. (goto-char (point-min)))))
  12331. (org-detach-overlay org-tags-overlay)
  12332. (if rtn
  12333. (mapconcat 'identity current ":")
  12334. nil))))
  12335. (defun org-get-tags-string ()
  12336. "Get the TAGS string in the current headline."
  12337. (unless (org-on-heading-p t)
  12338. (error "Not on a heading"))
  12339. (save-excursion
  12340. (beginning-of-line 1)
  12341. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12342. (org-match-string-no-properties 1)
  12343. "")))
  12344. (defun org-get-tags ()
  12345. "Get the list of tags specified in the current headline."
  12346. (org-split-string (org-get-tags-string) ":"))
  12347. (defun org-get-buffer-tags ()
  12348. "Get a table of all tags used in the buffer, for completion."
  12349. (let (tags)
  12350. (save-excursion
  12351. (goto-char (point-min))
  12352. (while (re-search-forward
  12353. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12354. (when (equal (char-after (point-at-bol 0)) ?*)
  12355. (mapc (lambda (x) (add-to-list 'tags x))
  12356. (org-split-string (org-match-string-no-properties 1) ":")))))
  12357. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12358. (mapcar 'list tags)))
  12359. ;;;; The mapping API
  12360. ;;;###autoload
  12361. (defun org-map-entries (func &optional match scope &rest skip)
  12362. "Call FUNC at each headline selected by MATCH in SCOPE.
  12363. FUNC is a function or a lisp form. The function will be called without
  12364. arguments, with the cursor positioned at the beginning of the headline.
  12365. The return values of all calls to the function will be collected and
  12366. returned as a list.
  12367. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12368. does not need to preserve point. After evaluation, the cursor will be
  12369. moved to the end of the line (presumably of the headline of the
  12370. processed entry) and search continues from there. Under some
  12371. circumstances, this may not produce the wanted results. For example,
  12372. if you have removed (e.g. archived) the current (sub)tree it could
  12373. mean that the next entry will be skipped entirely. In such cases, you
  12374. can specify the position from where search should continue by making
  12375. FUNC set the variable `org-map-continue-from' to the desired buffer
  12376. position.
  12377. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12378. Only headlines that are matched by this query will be considered during
  12379. the iteration. When MATCH is nil or t, all headlines will be
  12380. visited by the iteration.
  12381. SCOPE determines the scope of this command. It can be any of:
  12382. nil The current buffer, respecting the restriction if any
  12383. tree The subtree started with the entry at point
  12384. region The entries within the active region, if any
  12385. file The current buffer, without restriction
  12386. file-with-archives
  12387. The current buffer, and any archives associated with it
  12388. agenda All agenda files
  12389. agenda-with-archives
  12390. All agenda files with any archive files associated with them
  12391. \(file1 file2 ...)
  12392. If this is a list, all files in the list will be scanned
  12393. The remaining args are treated as settings for the skipping facilities of
  12394. the scanner. The following items can be given here:
  12395. archive skip trees with the archive tag.
  12396. comment skip trees with the COMMENT keyword
  12397. function or Emacs Lisp form:
  12398. will be used as value for `org-agenda-skip-function', so whenever
  12399. the function returns t, FUNC will not be called for that
  12400. entry and search will continue from the point where the
  12401. function leaves it.
  12402. If your function needs to retrieve the tags including inherited tags
  12403. at the *current* entry, you can use the value of the variable
  12404. `org-scanner-tags' which will be much faster than getting the value
  12405. with `org-get-tags-at'. If your function gets properties with
  12406. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12407. to t around the call to `org-entry-properties' to get the same speedup.
  12408. Note that if your function moves around to retrieve tags and properties at
  12409. a *different* entry, you cannot use these techniques."
  12410. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12411. (org-agenda-skip-archived-trees (memq 'archive skip))
  12412. (org-agenda-skip-comment-trees (memq 'comment skip))
  12413. (org-agenda-skip-function
  12414. (car (org-delete-all '(comment archive) skip)))
  12415. (org-tags-match-list-sublevels t)
  12416. matcher file res
  12417. org-todo-keywords-for-agenda
  12418. org-done-keywords-for-agenda
  12419. org-todo-keyword-alist-for-agenda
  12420. org-drawers-for-agenda
  12421. org-tag-alist-for-agenda)
  12422. (cond
  12423. ((eq match t) (setq matcher t))
  12424. ((eq match nil) (setq matcher t))
  12425. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12426. (save-excursion
  12427. (save-restriction
  12428. (cond ((eq scope 'tree)
  12429. (org-back-to-heading t)
  12430. (org-narrow-to-subtree)
  12431. (setq scope nil))
  12432. ((and (eq scope 'region) (org-region-active-p))
  12433. (narrow-to-region (region-beginning) (region-end))
  12434. (setq scope nil)))
  12435. (if (not scope)
  12436. (progn
  12437. (org-prepare-agenda-buffers
  12438. (list (buffer-file-name (current-buffer))))
  12439. (setq res (org-scan-tags func matcher)))
  12440. ;; Get the right scope
  12441. (cond
  12442. ((and scope (listp scope) (symbolp (car scope)))
  12443. (setq scope (eval scope)))
  12444. ((eq scope 'agenda)
  12445. (setq scope (org-agenda-files t)))
  12446. ((eq scope 'agenda-with-archives)
  12447. (setq scope (org-agenda-files t))
  12448. (setq scope (org-add-archive-files scope)))
  12449. ((eq scope 'file)
  12450. (setq scope (list (buffer-file-name))))
  12451. ((eq scope 'file-with-archives)
  12452. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12453. (org-prepare-agenda-buffers scope)
  12454. (while (setq file (pop scope))
  12455. (with-current-buffer (org-find-base-buffer-visiting file)
  12456. (save-excursion
  12457. (save-restriction
  12458. (widen)
  12459. (goto-char (point-min))
  12460. (setq res (append res (org-scan-tags func matcher))))))))))
  12461. res))
  12462. ;;;; Properties
  12463. ;;; Setting and retrieving properties
  12464. (defconst org-special-properties
  12465. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12466. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12467. "The special properties valid in Org-mode.
  12468. These are properties that are not defined in the property drawer,
  12469. but in some other way.")
  12470. (defconst org-default-properties
  12471. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12472. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12473. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12474. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12475. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12476. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12477. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12478. "Some properties that are used by Org-mode for various purposes.
  12479. Being in this list makes sure that they are offered for completion.")
  12480. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12481. "Regular expression matching the first line of a property drawer.")
  12482. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12483. "Regular expression matching the last line of a property drawer.")
  12484. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12485. "Regular expression matching the first line of a property drawer.")
  12486. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12487. "Regular expression matching the first line of a property drawer.")
  12488. (defconst org-property-drawer-re
  12489. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12490. org-property-end-re "\\)\n?")
  12491. "Matches an entire property drawer.")
  12492. (defconst org-clock-drawer-re
  12493. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12494. org-property-end-re "\\)\n?")
  12495. "Matches an entire clock drawer.")
  12496. (defsubst org-re-property (property)
  12497. "Return a regexp matching PROPERTY.
  12498. Match group 1 will be set to the value of the property."
  12499. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12500. (defun org-property-action ()
  12501. "Do an action on properties."
  12502. (interactive)
  12503. (let (c)
  12504. (org-at-property-p)
  12505. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12506. (setq c (read-char-exclusive))
  12507. (cond
  12508. ((equal c ?s)
  12509. (call-interactively 'org-set-property))
  12510. ((equal c ?d)
  12511. (call-interactively 'org-delete-property))
  12512. ((equal c ?D)
  12513. (call-interactively 'org-delete-property-globally))
  12514. ((equal c ?c)
  12515. (call-interactively 'org-compute-property-at-point))
  12516. (t (error "No such property action %c" c)))))
  12517. (defun org-set-effort (&optional value)
  12518. "Set the effort property of the current entry.
  12519. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12520. allowed value."
  12521. (interactive "P")
  12522. (if (equal value 0) (setq value 10))
  12523. (let* ((completion-ignore-case t)
  12524. (prop org-effort-property)
  12525. (cur (org-entry-get nil prop))
  12526. (allowed (org-property-get-allowed-values nil prop 'table))
  12527. (existing (mapcar 'list (org-property-values prop)))
  12528. rpl
  12529. (val (cond
  12530. ((stringp value) value)
  12531. ((and allowed (integerp value))
  12532. (or (car (nth (1- value) allowed))
  12533. (car (org-last allowed))))
  12534. (allowed
  12535. (message "Select 1-9,0, [RET%s]: %s"
  12536. (if cur (concat "=" cur) "")
  12537. (mapconcat 'car allowed " "))
  12538. (setq rpl (read-char-exclusive))
  12539. (if (equal rpl ?\r)
  12540. cur
  12541. (setq rpl (- rpl ?0))
  12542. (if (equal rpl 0) (setq rpl 10))
  12543. (if (and (> rpl 0) (<= rpl (length allowed)))
  12544. (car (nth (1- rpl) allowed))
  12545. (org-completing-read "Effort: " allowed nil))))
  12546. (t
  12547. (let (org-completion-use-ido org-completion-use-iswitchb)
  12548. (org-completing-read
  12549. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12550. (concat "[" cur "]") "")
  12551. ": ")
  12552. existing nil nil "" nil cur))))))
  12553. (unless (equal (org-entry-get nil prop) val)
  12554. (org-entry-put nil prop val))
  12555. (message "%s is now %s" prop val)))
  12556. (defun org-at-property-p ()
  12557. "Is cursor inside a property drawer?"
  12558. (save-excursion
  12559. (beginning-of-line 1)
  12560. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12561. (save-match-data ;; Used by calling procedures
  12562. (let ((p (point))
  12563. (range (unless (org-before-first-heading-p)
  12564. (org-get-property-block))))
  12565. (and range (<= (car range) p) (< p (cdr range))))))))
  12566. (defun org-get-property-block (&optional beg end force)
  12567. "Return the (beg . end) range of the body of the property drawer.
  12568. BEG and END can be beginning and end of subtree, if not given
  12569. they will be found.
  12570. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12571. (catch 'exit
  12572. (save-excursion
  12573. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12574. (end (or end (progn (outline-next-heading) (point)))))
  12575. (goto-char beg)
  12576. (if (re-search-forward org-property-start-re end t)
  12577. (setq beg (1+ (match-end 0)))
  12578. (if force
  12579. (save-excursion
  12580. (org-insert-property-drawer)
  12581. (setq end (progn (outline-next-heading) (point))))
  12582. (throw 'exit nil))
  12583. (goto-char beg)
  12584. (if (re-search-forward org-property-start-re end t)
  12585. (setq beg (1+ (match-end 0)))))
  12586. (if (re-search-forward org-property-end-re end t)
  12587. (setq end (match-beginning 0))
  12588. (or force (throw 'exit nil))
  12589. (goto-char beg)
  12590. (setq end beg)
  12591. (org-indent-line-function)
  12592. (insert ":END:\n"))
  12593. (cons beg end)))))
  12594. (defun org-entry-properties (&optional pom which specific)
  12595. "Get all properties of the entry at point-or-marker POM.
  12596. This includes the TODO keyword, the tags, time strings for deadline,
  12597. scheduled, and clocking, and any additional properties defined in the
  12598. entry. The return value is an alist, keys may occur multiple times
  12599. if the property key was used several times.
  12600. POM may also be nil, in which case the current entry is used.
  12601. If WHICH is nil or `all', get all properties. If WHICH is
  12602. `special' or `standard', only get that subclass. If WHICH
  12603. is a string only get exactly this property. SPECIFIC can be a string, the
  12604. specific property we are interested in. Specifying it can speed
  12605. things up because then unnecessary parsing is avoided."
  12606. (setq which (or which 'all))
  12607. (org-with-point-at pom
  12608. (let ((clockstr (substring org-clock-string 0 -1))
  12609. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12610. (case-fold-search nil)
  12611. beg end range props sum-props key key1 value string clocksum)
  12612. (save-excursion
  12613. (when (condition-case nil
  12614. (and (org-mode-p) (org-back-to-heading t))
  12615. (error nil))
  12616. (setq beg (point))
  12617. (setq sum-props (get-text-property (point) 'org-summaries))
  12618. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12619. (outline-next-heading)
  12620. (setq end (point))
  12621. (when (memq which '(all special))
  12622. ;; Get the special properties, like TODO and tags
  12623. (goto-char beg)
  12624. (when (and (or (not specific) (string= specific "TODO"))
  12625. (looking-at org-todo-line-regexp) (match-end 2))
  12626. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12627. (when (and (or (not specific) (string= specific "PRIORITY"))
  12628. (looking-at org-priority-regexp))
  12629. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12630. (when (or (not specific) (string= specific "FILE"))
  12631. (push (cons "FILE" buffer-file-name) props))
  12632. (when (and (or (not specific) (string= specific "TAGS"))
  12633. (setq value (org-get-tags-string))
  12634. (string-match "\\S-" value))
  12635. (push (cons "TAGS" value) props))
  12636. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12637. (setq value (org-get-tags-at)))
  12638. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12639. ":"))
  12640. props))
  12641. (when (or (not specific) (string= specific "BLOCKED"))
  12642. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12643. (when (or (not specific)
  12644. (member specific
  12645. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12646. "TIMESTAMP" "TIMESTAMP_IA")))
  12647. (catch 'match
  12648. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12649. (setq key (if (match-end 1)
  12650. (substring (org-match-string-no-properties 1)
  12651. 0 -1))
  12652. string (if (equal key clockstr)
  12653. (org-no-properties
  12654. (org-trim
  12655. (buffer-substring
  12656. (match-beginning 3) (goto-char
  12657. (point-at-eol)))))
  12658. (substring (org-match-string-no-properties 3)
  12659. 1 -1)))
  12660. ;; Get the correct property name from the key. This is
  12661. ;; necessary if the user has configured time keywords.
  12662. (setq key1 (concat key ":"))
  12663. (cond
  12664. ((not key)
  12665. (setq key
  12666. (if (= (char-after (match-beginning 3)) ?\[)
  12667. "TIMESTAMP_IA" "TIMESTAMP")))
  12668. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12669. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12670. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12671. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12672. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12673. (progn
  12674. (push (cons key string) props)
  12675. ;; no need to search further if match is found
  12676. (throw 'match t))
  12677. (when (or (equal key "CLOCK") (not (assoc key props)))
  12678. (push (cons key string) props))))))
  12679. )
  12680. (when (memq which '(all standard))
  12681. ;; Get the standard properties, like :PROP: ...
  12682. (setq range (org-get-property-block beg end))
  12683. (when range
  12684. (goto-char (car range))
  12685. (while (re-search-forward
  12686. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12687. (cdr range) t)
  12688. (setq key (org-match-string-no-properties 1)
  12689. value (org-trim (or (org-match-string-no-properties 2) "")))
  12690. (unless (member key excluded)
  12691. (push (cons key (or value "")) props)))))
  12692. (if clocksum
  12693. (push (cons "CLOCKSUM"
  12694. (org-columns-number-to-string (/ (float clocksum) 60.)
  12695. 'add_times))
  12696. props))
  12697. (unless (assoc "CATEGORY" props)
  12698. (push (cons "CATEGORY" (org-get-category)) props))
  12699. (append sum-props (nreverse props)))))))
  12700. (defun org-entry-get (pom property &optional inherit literal-nil)
  12701. "Get value of PROPERTY for entry at point-or-marker POM.
  12702. If INHERIT is non-nil and the entry does not have the property,
  12703. then also check higher levels of the hierarchy.
  12704. If INHERIT is the symbol `selective', use inheritance only if the setting
  12705. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12706. If the property is present but empty, the return value is the empty string.
  12707. If the property is not present at all, nil is returned.
  12708. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12709. do not interpret it as the list atom nil. This is used for inheritance
  12710. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12711. (org-with-point-at pom
  12712. (if (and inherit (if (eq inherit 'selective)
  12713. (org-property-inherit-p property)
  12714. t))
  12715. (org-entry-get-with-inheritance property literal-nil)
  12716. (if (member property org-special-properties)
  12717. ;; We need a special property. Use `org-entry-properties' to
  12718. ;; retrieve it, but specify the wanted property
  12719. (cdr (assoc property (org-entry-properties nil 'special property)))
  12720. (let ((range (unless (org-before-first-heading-p)
  12721. (org-get-property-block))))
  12722. (if (and range
  12723. (goto-char (car range))
  12724. (re-search-forward
  12725. (org-re-property property)
  12726. (cdr range) t))
  12727. ;; Found the property, return it.
  12728. (if (match-end 1)
  12729. (if literal-nil
  12730. (org-match-string-no-properties 1)
  12731. (org-not-nil (org-match-string-no-properties 1)))
  12732. "")))))))
  12733. (defun org-property-or-variable-value (var &optional inherit)
  12734. "Check if there is a property fixing the value of VAR.
  12735. If yes, return this value. If not, return the current value of the variable."
  12736. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12737. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12738. (read prop)
  12739. (symbol-value var))))
  12740. (defun org-entry-delete (pom property)
  12741. "Delete the property PROPERTY from entry at point-or-marker POM."
  12742. (org-with-point-at pom
  12743. (if (member property org-special-properties)
  12744. nil ; cannot delete these properties.
  12745. (let ((range (org-get-property-block)))
  12746. (if (and range
  12747. (goto-char (car range))
  12748. (re-search-forward
  12749. (org-re-property property)
  12750. (cdr range) t))
  12751. (progn
  12752. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12753. t)
  12754. nil)))))
  12755. ;; Multi-values properties are properties that contain multiple values
  12756. ;; These values are assumed to be single words, separated by whitespace.
  12757. (defun org-entry-add-to-multivalued-property (pom property value)
  12758. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12759. (let* ((old (org-entry-get pom property))
  12760. (values (and old (org-split-string old "[ \t]"))))
  12761. (setq value (org-entry-protect-space value))
  12762. (unless (member value values)
  12763. (setq values (cons value values))
  12764. (org-entry-put pom property
  12765. (mapconcat 'identity values " ")))))
  12766. (defun org-entry-remove-from-multivalued-property (pom property value)
  12767. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12768. (let* ((old (org-entry-get pom property))
  12769. (values (and old (org-split-string old "[ \t]"))))
  12770. (setq value (org-entry-protect-space value))
  12771. (when (member value values)
  12772. (setq values (delete value values))
  12773. (org-entry-put pom property
  12774. (mapconcat 'identity values " ")))))
  12775. (defun org-entry-member-in-multivalued-property (pom property value)
  12776. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12777. (let* ((old (org-entry-get pom property))
  12778. (values (and old (org-split-string old "[ \t]"))))
  12779. (setq value (org-entry-protect-space value))
  12780. (member value values)))
  12781. (defun org-entry-get-multivalued-property (pom property)
  12782. "Return a list of values in a multivalued property."
  12783. (let* ((value (org-entry-get pom property))
  12784. (values (and value (org-split-string value "[ \t]"))))
  12785. (mapcar 'org-entry-restore-space values)))
  12786. (defun org-entry-put-multivalued-property (pom property &rest values)
  12787. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12788. VALUES should be a list of strings. Spaces will be protected."
  12789. (org-entry-put pom property
  12790. (mapconcat 'org-entry-protect-space values " "))
  12791. (let* ((value (org-entry-get pom property))
  12792. (values (and value (org-split-string value "[ \t]"))))
  12793. (mapcar 'org-entry-restore-space values)))
  12794. (defun org-entry-protect-space (s)
  12795. "Protect spaces and newline in string S."
  12796. (while (string-match " " s)
  12797. (setq s (replace-match "%20" t t s)))
  12798. (while (string-match "\n" s)
  12799. (setq s (replace-match "%0A" t t s)))
  12800. s)
  12801. (defun org-entry-restore-space (s)
  12802. "Restore spaces and newline in string S."
  12803. (while (string-match "%20" s)
  12804. (setq s (replace-match " " t t s)))
  12805. (while (string-match "%0A" s)
  12806. (setq s (replace-match "\n" t t s)))
  12807. s)
  12808. (defvar org-entry-property-inherited-from (make-marker)
  12809. "Marker pointing to the entry from where a property was inherited.
  12810. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12811. location of the entry where the inheritance search matched. If there was
  12812. no match, the marker will point nowhere.
  12813. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12814. is set.")
  12815. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12816. "Get entry property, and search higher levels if not present.
  12817. The search will stop at the first ancestor which has the property defined.
  12818. If the value found is \"nil\", return nil to show that the property
  12819. should be considered as undefined (this is the meaning of nil here).
  12820. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12821. (move-marker org-entry-property-inherited-from nil)
  12822. (let (tmp)
  12823. (unless (org-before-first-heading-p)
  12824. (save-excursion
  12825. (save-restriction
  12826. (widen)
  12827. (catch 'ex
  12828. (while t
  12829. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12830. (org-back-to-heading t)
  12831. (move-marker org-entry-property-inherited-from (point))
  12832. (throw 'ex tmp))
  12833. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12834. (setq tmp (or tmp
  12835. (cdr (assoc property org-file-properties))
  12836. (cdr (assoc property org-global-properties))
  12837. (cdr (assoc property org-global-properties-fixed))))
  12838. (if literal-nil tmp (org-not-nil tmp))))
  12839. (defvar org-property-changed-functions nil
  12840. "Hook called when the value of a property has changed.
  12841. Each hook function should accept two arguments, the name of the property
  12842. and the new value.")
  12843. (defun org-entry-put (pom property value)
  12844. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12845. (org-with-point-at pom
  12846. (org-back-to-heading t)
  12847. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12848. range)
  12849. (cond
  12850. ((equal property "TODO")
  12851. (when (and (stringp value) (string-match "\\S-" value)
  12852. (not (member value org-todo-keywords-1)))
  12853. (error "\"%s\" is not a valid TODO state" value))
  12854. (if (or (not value)
  12855. (not (string-match "\\S-" value)))
  12856. (setq value 'none))
  12857. (org-todo value)
  12858. (org-set-tags nil 'align))
  12859. ((equal property "PRIORITY")
  12860. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12861. (string-to-char value) ?\ ))
  12862. (org-set-tags nil 'align))
  12863. ((equal property "SCHEDULED")
  12864. (if (re-search-forward org-scheduled-time-regexp end t)
  12865. (cond
  12866. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12867. ((eq value 'later) (org-timestamp-change 1 'day))
  12868. (t (call-interactively 'org-schedule)))
  12869. (call-interactively 'org-schedule)))
  12870. ((equal property "DEADLINE")
  12871. (if (re-search-forward org-deadline-time-regexp end t)
  12872. (cond
  12873. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12874. ((eq value 'later) (org-timestamp-change 1 'day))
  12875. (t (call-interactively 'org-deadline)))
  12876. (call-interactively 'org-deadline)))
  12877. ((member property org-special-properties)
  12878. (error "The %s property can not yet be set with `org-entry-put'"
  12879. property))
  12880. (t ; a non-special property
  12881. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12882. (setq range (org-get-property-block beg end 'force))
  12883. (goto-char (car range))
  12884. (if (re-search-forward
  12885. (org-re-property property) (cdr range) t)
  12886. (progn
  12887. (delete-region (match-beginning 0) (match-end 0))
  12888. (goto-char (match-beginning 0)))
  12889. (goto-char (cdr range))
  12890. (insert "\n")
  12891. (backward-char 1)
  12892. (org-indent-line-function))
  12893. (insert ":" property ":")
  12894. (and value (insert " " value))
  12895. (org-indent-line-function)))))
  12896. (run-hook-with-args 'org-property-changed-functions property value)))
  12897. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12898. "Get all property keys in the current buffer.
  12899. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12900. like tags and TODO state.
  12901. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12902. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  12903. and others.
  12904. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12905. formats in the current buffer."
  12906. (let (rtn range cfmt s p)
  12907. (save-excursion
  12908. (save-restriction
  12909. (widen)
  12910. (goto-char (point-min))
  12911. (while (re-search-forward org-property-start-re nil t)
  12912. (setq range (org-get-property-block))
  12913. (goto-char (car range))
  12914. (while (re-search-forward
  12915. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12916. (cdr range) t)
  12917. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12918. (outline-next-heading))))
  12919. (when include-specials
  12920. (setq rtn (append org-special-properties rtn)))
  12921. (when include-defaults
  12922. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12923. (add-to-list 'rtn org-effort-property))
  12924. (when include-columns
  12925. (save-excursion
  12926. (save-restriction
  12927. (widen)
  12928. (goto-char (point-min))
  12929. (while (re-search-forward
  12930. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12931. nil t)
  12932. (setq cfmt (match-string 2) s 0)
  12933. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12934. cfmt s)
  12935. (setq s (match-end 0)
  12936. p (match-string 1 cfmt))
  12937. (unless (or (equal p "ITEM")
  12938. (member p org-special-properties))
  12939. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12940. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12941. (defun org-property-values (key)
  12942. "Return a list of all values of property KEY in the current buffer."
  12943. (save-excursion
  12944. (save-restriction
  12945. (widen)
  12946. (goto-char (point-min))
  12947. (let ((re (org-re-property key))
  12948. values)
  12949. (while (re-search-forward re nil t)
  12950. (add-to-list 'values (org-trim (match-string 1))))
  12951. (delete "" values)))))
  12952. (defun org-insert-property-drawer ()
  12953. "Insert a property drawer into the current entry."
  12954. (interactive)
  12955. (org-back-to-heading t)
  12956. (looking-at org-outline-regexp)
  12957. (let ((indent (if org-adapt-indentation
  12958. (- (match-end 0)(match-beginning 0))
  12959. 0))
  12960. (beg (point))
  12961. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12962. end hiddenp)
  12963. (outline-next-heading)
  12964. (setq end (point))
  12965. (goto-char beg)
  12966. (while (re-search-forward re end t))
  12967. (setq hiddenp (outline-invisible-p))
  12968. (end-of-line 1)
  12969. (and (equal (char-after) ?\n) (forward-char 1))
  12970. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12971. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12972. ;; just skip this line
  12973. (beginning-of-line 2)
  12974. ;; Drawer start, find the end
  12975. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12976. (beginning-of-line 1)))
  12977. (org-skip-over-state-notes)
  12978. (skip-chars-backward " \t\n\r")
  12979. (if (eq (char-before) ?*) (forward-char 1))
  12980. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12981. (beginning-of-line 0)
  12982. (org-indent-to-column indent)
  12983. (beginning-of-line 2)
  12984. (org-indent-to-column indent)
  12985. (beginning-of-line 0)
  12986. (if hiddenp
  12987. (save-excursion
  12988. (org-back-to-heading t)
  12989. (hide-entry))
  12990. (org-flag-drawer t))))
  12991. (defvar org-property-set-functions-alist nil
  12992. "Property set function alist.
  12993. Each entry should have the following format:
  12994. (PROPERTY . READ-FUNCTION)
  12995. The read function will be called with the same argument as
  12996. `org-completing-read'.")
  12997. (defun org-set-property-function (property)
  12998. "Get the function that should be used to set PROPERTY.
  12999. This is computed according to `org-property-set-functions-alist'."
  13000. (or (cdr (assoc property org-property-set-functions-alist))
  13001. 'org-completing-read))
  13002. (defun org-read-property-value (property)
  13003. "Read PROPERTY value from user."
  13004. (let* ((completion-ignore-case t)
  13005. (allowed (org-property-get-allowed-values nil property 'table))
  13006. (cur (org-entry-get nil property))
  13007. (prompt (concat property " value"
  13008. (if (and cur (string-match "\\S-" cur))
  13009. (concat " [" cur "]") "") ": "))
  13010. (set-function (org-set-property-function property))
  13011. (val (if allowed
  13012. (funcall set-function prompt allowed nil
  13013. (not (get-text-property 0 'org-unrestricted
  13014. (caar allowed))))
  13015. (let (org-completion-use-ido org-completion-use-iswitchb)
  13016. (funcall set-function prompt
  13017. (mapcar 'list (org-property-values property))
  13018. nil nil "" nil cur)))))
  13019. (if (equal val "")
  13020. cur
  13021. val)))
  13022. (defvar org-last-set-property nil)
  13023. (defun org-read-property-name ()
  13024. "Read a property name."
  13025. (let* ((completion-ignore-case t)
  13026. (keys (org-buffer-property-keys nil t t))
  13027. (default-prop (or (save-excursion
  13028. (save-match-data
  13029. (beginning-of-line)
  13030. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13031. (null (string= (match-string 1) "END"))
  13032. (match-string 1))))
  13033. org-last-set-property))
  13034. (property (org-icompleting-read
  13035. (concat "Property"
  13036. (if default-prop (concat " [" default-prop "]") "")
  13037. ": ")
  13038. (mapcar 'list keys)
  13039. nil nil nil nil
  13040. default-prop
  13041. )))
  13042. (if (member property keys)
  13043. property
  13044. (or (cdr (assoc (downcase property)
  13045. (mapcar (lambda (x) (cons (downcase x) x))
  13046. keys)))
  13047. property))))
  13048. (defun org-set-property (property value)
  13049. "In the current entry, set PROPERTY to VALUE.
  13050. When called interactively, this will prompt for a property name, offering
  13051. completion on existing and default properties. And then it will prompt
  13052. for a value, offering completion either on allowed values (via an inherited
  13053. xxx_ALL property) or on existing values in other instances of this property
  13054. in the current file."
  13055. (interactive (list nil nil))
  13056. (let* ((property (or property (org-read-property-name)))
  13057. (value (or value (org-read-property-value property)))
  13058. (fn (assoc property org-properties-postprocess-alist)))
  13059. (setq org-last-set-property property)
  13060. ;; Possibly postprocess the inserted value:
  13061. (when fn (setq value (funcall (cadr fn) value)))
  13062. (unless (equal (org-entry-get nil property) value)
  13063. (org-entry-put nil property value))))
  13064. (defun org-delete-property (property)
  13065. "In the current entry, delete PROPERTY."
  13066. (interactive
  13067. (let* ((completion-ignore-case t)
  13068. (prop (org-icompleting-read "Property: "
  13069. (org-entry-properties nil 'standard))))
  13070. (list prop)))
  13071. (message "Property %s %s" property
  13072. (if (org-entry-delete nil property)
  13073. "deleted"
  13074. "was not present in the entry")))
  13075. (defun org-delete-property-globally (property)
  13076. "Remove PROPERTY globally, from all entries."
  13077. (interactive
  13078. (let* ((completion-ignore-case t)
  13079. (prop (org-icompleting-read
  13080. "Globally remove property: "
  13081. (mapcar 'list (org-buffer-property-keys)))))
  13082. (list prop)))
  13083. (save-excursion
  13084. (save-restriction
  13085. (widen)
  13086. (goto-char (point-min))
  13087. (let ((cnt 0))
  13088. (while (re-search-forward
  13089. (org-re-property property)
  13090. nil t)
  13091. (setq cnt (1+ cnt))
  13092. (replace-match ""))
  13093. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13094. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13095. (defun org-compute-property-at-point ()
  13096. "Compute the property at point.
  13097. This looks for an enclosing column format, extracts the operator and
  13098. then applies it to the property in the column format's scope."
  13099. (interactive)
  13100. (unless (org-at-property-p)
  13101. (error "Not at a property"))
  13102. (let ((prop (org-match-string-no-properties 2)))
  13103. (org-columns-get-format-and-top-level)
  13104. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13105. (error "No operator defined for property %s" prop))
  13106. (org-columns-compute prop)))
  13107. (defvar org-property-allowed-value-functions nil
  13108. "Hook for functions supplying allowed values for a specific property.
  13109. The functions must take a single argument, the name of the property, and
  13110. return a flat list of allowed values. If \":ETC\" is one of
  13111. the values, this means that these values are intended as defaults for
  13112. completion, but that other values should be allowed too.
  13113. The functions must return nil if they are not responsible for this
  13114. property.")
  13115. (defun org-property-get-allowed-values (pom property &optional table)
  13116. "Get allowed values for the property PROPERTY.
  13117. When TABLE is non-nil, return an alist that can directly be used for
  13118. completion."
  13119. (let (vals)
  13120. (cond
  13121. ((equal property "TODO")
  13122. (setq vals (org-with-point-at pom
  13123. (append org-todo-keywords-1 '("")))))
  13124. ((equal property "PRIORITY")
  13125. (let ((n org-lowest-priority))
  13126. (while (>= n org-highest-priority)
  13127. (push (char-to-string n) vals)
  13128. (setq n (1- n)))))
  13129. ((member property org-special-properties))
  13130. ((setq vals (run-hook-with-args-until-success
  13131. 'org-property-allowed-value-functions property)))
  13132. (t
  13133. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13134. (when (and vals (string-match "\\S-" vals))
  13135. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13136. (setq vals (mapcar (lambda (x)
  13137. (cond ((stringp x) x)
  13138. ((numberp x) (number-to-string x))
  13139. ((symbolp x) (symbol-name x))
  13140. (t "???")))
  13141. vals)))))
  13142. (when (member ":ETC" vals)
  13143. (setq vals (remove ":ETC" vals))
  13144. (org-add-props (car vals) '(org-unrestricted t)))
  13145. (if table (mapcar 'list vals) vals)))
  13146. (defun org-property-previous-allowed-value (&optional previous)
  13147. "Switch to the next allowed value for this property."
  13148. (interactive)
  13149. (org-property-next-allowed-value t))
  13150. (defun org-property-next-allowed-value (&optional previous)
  13151. "Switch to the next allowed value for this property."
  13152. (interactive)
  13153. (unless (org-at-property-p)
  13154. (error "Not at a property"))
  13155. (let* ((key (match-string 2))
  13156. (value (match-string 3))
  13157. (allowed (or (org-property-get-allowed-values (point) key)
  13158. (and (member value '("[ ]" "[-]" "[X]"))
  13159. '("[ ]" "[X]"))))
  13160. nval)
  13161. (unless allowed
  13162. (error "Allowed values for this property have not been defined"))
  13163. (if previous (setq allowed (reverse allowed)))
  13164. (if (member value allowed)
  13165. (setq nval (car (cdr (member value allowed)))))
  13166. (setq nval (or nval (car allowed)))
  13167. (if (equal nval value)
  13168. (error "Only one allowed value for this property"))
  13169. (org-at-property-p)
  13170. (replace-match (concat " :" key ": " nval) t t)
  13171. (org-indent-line-function)
  13172. (beginning-of-line 1)
  13173. (skip-chars-forward " \t")
  13174. (run-hook-with-args 'org-property-changed-functions key nval)))
  13175. (defun org-find-olp (path &optional this-buffer)
  13176. "Return a marker pointing to the entry at outline path OLP.
  13177. If anything goes wrong, throw an error.
  13178. You can wrap this call to catch the error like this:
  13179. (condition-case msg
  13180. (org-mobile-locate-entry (match-string 4))
  13181. (error (nth 1 msg)))
  13182. The return value will then be either a string with the error message,
  13183. or a marker if everything is OK.
  13184. If THIS-BUFFER is set, the outline path does not contain a file,
  13185. only headings."
  13186. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13187. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13188. (level 1)
  13189. (lmin 1)
  13190. (lmax 1)
  13191. limit re end found pos heading cnt flevel)
  13192. (unless buffer (error "File not found :%s" file))
  13193. (with-current-buffer buffer
  13194. (save-excursion
  13195. (save-restriction
  13196. (widen)
  13197. (setq limit (point-max))
  13198. (goto-char (point-min))
  13199. (while (setq heading (pop path))
  13200. (setq re (format org-complex-heading-regexp-format
  13201. (regexp-quote heading)))
  13202. (setq cnt 0 pos (point))
  13203. (while (re-search-forward re end t)
  13204. (setq level (- (match-end 1) (match-beginning 1)))
  13205. (if (and (>= level lmin) (<= level lmax))
  13206. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13207. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13208. lmax heading))
  13209. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13210. lmax heading))
  13211. (goto-char found)
  13212. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13213. (setq end (save-excursion (org-end-of-subtree t t))))
  13214. (when (org-on-heading-p)
  13215. (move-marker (make-marker) (point))))))))
  13216. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13217. "Find node HEADING in BUFFER.
  13218. Return a marker to the heading if it was found, or nil if not.
  13219. If POS-ONLY is set, return just the position instead of a marker.
  13220. The heading text must match exact, but it may have a TODO keyword,
  13221. a priority cookie and tags in the standard locations."
  13222. (with-current-buffer (or buffer (current-buffer))
  13223. (save-excursion
  13224. (save-restriction
  13225. (widen)
  13226. (goto-char (point-min))
  13227. (let (case-fold-search)
  13228. (if (re-search-forward
  13229. (format org-complex-heading-regexp-format
  13230. (regexp-quote heading)) nil t)
  13231. (if pos-only
  13232. (match-beginning 0)
  13233. (move-marker (make-marker) (match-beginning 0)))))))))
  13234. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13235. "Find Org node headline HEADING in all .org files in directory DIR.
  13236. When the target headline is found, return a marker to this location."
  13237. (let ((files (directory-files (or dir default-directory)
  13238. nil "\\`[^.#].*\\.org\\'"))
  13239. file visiting m buffer)
  13240. (catch 'found
  13241. (while (setq file (pop files))
  13242. (message "trying %s" file)
  13243. (setq visiting (org-find-base-buffer-visiting file))
  13244. (setq buffer (or visiting (find-file-noselect file)))
  13245. (setq m (org-find-exact-headline-in-buffer
  13246. heading buffer))
  13247. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13248. (and m (throw 'found m))))))
  13249. (defun org-find-entry-with-id (ident)
  13250. "Locate the entry that contains the ID property with exact value IDENT.
  13251. IDENT can be a string, a symbol or a number, this function will search for
  13252. the string representation of it.
  13253. Return the position where this entry starts, or nil if there is no such entry."
  13254. (interactive "sID: ")
  13255. (let ((id (cond
  13256. ((stringp ident) ident)
  13257. ((symbol-name ident) (symbol-name ident))
  13258. ((numberp ident) (number-to-string ident))
  13259. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13260. (case-fold-search nil))
  13261. (save-excursion
  13262. (save-restriction
  13263. (widen)
  13264. (goto-char (point-min))
  13265. (when (re-search-forward
  13266. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13267. nil t)
  13268. (org-back-to-heading t)
  13269. (point))))))
  13270. ;;;; Timestamps
  13271. (defvar org-last-changed-timestamp nil)
  13272. (defvar org-last-inserted-timestamp nil
  13273. "The last time stamp inserted with `org-insert-time-stamp'.")
  13274. (defvar org-time-was-given) ; dynamically scoped parameter
  13275. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13276. (defvar org-ts-what) ; dynamically scoped parameter
  13277. (defun org-time-stamp (arg &optional inactive)
  13278. "Prompt for a date/time and insert a time stamp.
  13279. If the user specifies a time like HH:MM, or if this command is called
  13280. with a prefix argument, the time stamp will contain date and time.
  13281. Otherwise, only the date will be included. All parts of a date not
  13282. specified by the user will be filled in from the current date/time.
  13283. So if you press just return without typing anything, the time stamp
  13284. will represent the current date/time. If there is already a timestamp
  13285. at the cursor, it will be modified."
  13286. (interactive "P")
  13287. (let* ((ts nil)
  13288. (default-time
  13289. ;; Default time is either today, or, when entering a range,
  13290. ;; the range start.
  13291. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13292. (save-excursion
  13293. (re-search-backward
  13294. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13295. (- (point) 20) t)))
  13296. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13297. (current-time)))
  13298. (default-input (and ts (org-get-compact-tod ts)))
  13299. (repeater (save-excursion
  13300. (save-match-data
  13301. (beginning-of-line)
  13302. (when (re-search-forward
  13303. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13304. (save-excursion (progn (end-of-line) (point))) t)
  13305. (match-string 0)))))
  13306. org-time-was-given org-end-time-was-given time)
  13307. (cond
  13308. ((and (org-at-timestamp-p t)
  13309. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13310. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13311. (insert "--")
  13312. (setq time (let ((this-command this-command))
  13313. (org-read-date arg 'totime nil nil
  13314. default-time default-input)))
  13315. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13316. ((org-at-timestamp-p t)
  13317. (setq time (let ((this-command this-command))
  13318. (org-read-date arg 'totime nil nil default-time default-input)))
  13319. (when (org-at-timestamp-p t) ; just to get the match data
  13320. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13321. (replace-match "")
  13322. (setq org-last-changed-timestamp
  13323. (org-insert-time-stamp
  13324. time (or org-time-was-given arg)
  13325. inactive nil nil (list org-end-time-was-given)))
  13326. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13327. (setq org-last-changed-timestamp
  13328. (concat (substring org-last-inserted-timestamp 0 -1)
  13329. " " repeater ">"))))
  13330. (message "Timestamp updated"))
  13331. (t
  13332. (setq time (let ((this-command this-command))
  13333. (org-read-date arg 'totime nil nil default-time default-input)))
  13334. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13335. nil nil (list org-end-time-was-given))))))
  13336. ;; FIXME: can we use this for something else, like computing time differences?
  13337. (defun org-get-compact-tod (s)
  13338. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13339. (let* ((t1 (match-string 1 s))
  13340. (h1 (string-to-number (match-string 2 s)))
  13341. (m1 (string-to-number (match-string 3 s)))
  13342. (t2 (and (match-end 4) (match-string 5 s)))
  13343. (h2 (and t2 (string-to-number (match-string 6 s))))
  13344. (m2 (and t2 (string-to-number (match-string 7 s))))
  13345. dh dm)
  13346. (if (not t2)
  13347. t1
  13348. (setq dh (- h2 h1) dm (- m2 m1))
  13349. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13350. (concat t1 "+" (number-to-string dh)
  13351. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13352. (defun org-time-stamp-inactive (&optional arg)
  13353. "Insert an inactive time stamp.
  13354. An inactive time stamp is enclosed in square brackets instead of angle
  13355. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13356. does not link to the calendar and cannot be changed with the S-cursor keys.
  13357. So these are more for recording a certain time/date."
  13358. (interactive "P")
  13359. (org-time-stamp arg 'inactive))
  13360. (defvar org-date-ovl (make-overlay 1 1))
  13361. (overlay-put org-date-ovl 'face 'org-warning)
  13362. (org-detach-overlay org-date-ovl)
  13363. (defvar org-ans1) ; dynamically scoped parameter
  13364. (defvar org-ans2) ; dynamically scoped parameter
  13365. (defvar org-plain-time-of-day-regexp) ; defined below
  13366. (defvar org-overriding-default-time nil) ; dynamically scoped
  13367. (defvar org-read-date-overlay nil)
  13368. (defvar org-dcst nil) ; dynamically scoped
  13369. (defvar org-read-date-history nil)
  13370. (defvar org-read-date-final-answer nil)
  13371. (defvar org-read-date-analyze-futurep nil)
  13372. (defvar org-read-date-analyze-forced-year nil)
  13373. (defun org-read-date (&optional with-time to-time from-string prompt
  13374. default-time default-input)
  13375. "Read a date, possibly a time, and make things smooth for the user.
  13376. The prompt will suggest to enter an ISO date, but you can also enter anything
  13377. which will at least partially be understood by `parse-time-string'.
  13378. Unrecognized parts of the date will default to the current day, month, year,
  13379. hour and minute. If this command is called to replace a timestamp at point,
  13380. of to enter the second timestamp of a range, the default time is taken
  13381. from the existing stamp. Furthermore, the command prefers the future,
  13382. so if you are giving a date where the year is not given, and the day-month
  13383. combination is already past in the current year, it will assume you
  13384. mean next year. For details, see the manual. A few examples:
  13385. 3-2-5 --> 2003-02-05
  13386. feb 15 --> currentyear-02-15
  13387. 2/15 --> currentyear-02-15
  13388. sep 12 9 --> 2009-09-12
  13389. 12:45 --> today 12:45
  13390. 22 sept 0:34 --> currentyear-09-22 0:34
  13391. 12 --> currentyear-currentmonth-12
  13392. Fri --> nearest Friday (today or later)
  13393. etc.
  13394. Furthermore you can specify a relative date by giving, as the *first* thing
  13395. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13396. change in days weeks, months, years.
  13397. With a single plus or minus, the date is relative to today. With a double
  13398. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13399. +4d --> four days from today
  13400. +4 --> same as above
  13401. +2w --> two weeks from today
  13402. ++5 --> five days from default date
  13403. The function understands only English month and weekday abbreviations,
  13404. but this can be configured with the variables `parse-time-months' and
  13405. `parse-time-weekdays'.
  13406. While prompting, a calendar is popped up - you can also select the
  13407. date with the mouse (button 1). The calendar shows a period of three
  13408. months. To scroll it to other months, use the keys `>' and `<'.
  13409. If you don't like the calendar, turn it off with
  13410. \(setq org-read-date-popup-calendar nil)
  13411. With optional argument TO-TIME, the date will immediately be converted
  13412. to an internal time.
  13413. With an optional argument WITH-TIME, the prompt will suggest to also
  13414. insert a time. Note that when WITH-TIME is not set, you can still
  13415. enter a time, and this function will inform the calling routine about
  13416. this change. The calling routine may then choose to change the format
  13417. used to insert the time stamp into the buffer to include the time.
  13418. With optional argument FROM-STRING, read from this string instead from
  13419. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13420. the time/date that is used for everything that is not specified by the
  13421. user."
  13422. (require 'parse-time)
  13423. (let* ((org-time-stamp-rounding-minutes
  13424. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13425. (org-dcst org-display-custom-times)
  13426. (ct (org-current-time))
  13427. (def (or org-overriding-default-time default-time ct))
  13428. (defdecode (decode-time def))
  13429. (dummy (progn
  13430. (when (< (nth 2 defdecode) org-extend-today-until)
  13431. (setcar (nthcdr 2 defdecode) -1)
  13432. (setcar (nthcdr 1 defdecode) 59)
  13433. (setq def (apply 'encode-time defdecode)
  13434. defdecode (decode-time def)))))
  13435. (calendar-frame-setup nil)
  13436. (calendar-setup nil)
  13437. (calendar-move-hook nil)
  13438. (calendar-view-diary-initially-flag nil)
  13439. (calendar-view-holidays-initially-flag nil)
  13440. (timestr (format-time-string
  13441. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13442. (prompt (concat (if prompt (concat prompt " ") "")
  13443. (format "Date+time [%s]: " timestr)))
  13444. ans (org-ans0 "") org-ans1 org-ans2 final)
  13445. (cond
  13446. (from-string (setq ans from-string))
  13447. (org-read-date-popup-calendar
  13448. (save-excursion
  13449. (save-window-excursion
  13450. (calendar)
  13451. (unwind-protect
  13452. (progn
  13453. (calendar-forward-day (- (time-to-days def)
  13454. (calendar-absolute-from-gregorian
  13455. (calendar-current-date))))
  13456. (org-eval-in-calendar nil t)
  13457. (let* ((old-map (current-local-map))
  13458. (map (copy-keymap calendar-mode-map))
  13459. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13460. (org-defkey map (kbd "RET") 'org-calendar-select)
  13461. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13462. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13463. (org-defkey minibuffer-local-map [(meta shift left)]
  13464. (lambda () (interactive)
  13465. (org-eval-in-calendar '(calendar-backward-month 1))))
  13466. (org-defkey minibuffer-local-map [(meta shift right)]
  13467. (lambda () (interactive)
  13468. (org-eval-in-calendar '(calendar-forward-month 1))))
  13469. (org-defkey minibuffer-local-map [(meta shift up)]
  13470. (lambda () (interactive)
  13471. (org-eval-in-calendar '(calendar-backward-year 1))))
  13472. (org-defkey minibuffer-local-map [(meta shift down)]
  13473. (lambda () (interactive)
  13474. (org-eval-in-calendar '(calendar-forward-year 1))))
  13475. (org-defkey minibuffer-local-map [?\e (shift left)]
  13476. (lambda () (interactive)
  13477. (org-eval-in-calendar '(calendar-backward-month 1))))
  13478. (org-defkey minibuffer-local-map [?\e (shift right)]
  13479. (lambda () (interactive)
  13480. (org-eval-in-calendar '(calendar-forward-month 1))))
  13481. (org-defkey minibuffer-local-map [?\e (shift up)]
  13482. (lambda () (interactive)
  13483. (org-eval-in-calendar '(calendar-backward-year 1))))
  13484. (org-defkey minibuffer-local-map [?\e (shift down)]
  13485. (lambda () (interactive)
  13486. (org-eval-in-calendar '(calendar-forward-year 1))))
  13487. (org-defkey minibuffer-local-map [(shift up)]
  13488. (lambda () (interactive)
  13489. (org-eval-in-calendar '(calendar-backward-week 1))))
  13490. (org-defkey minibuffer-local-map [(shift down)]
  13491. (lambda () (interactive)
  13492. (org-eval-in-calendar '(calendar-forward-week 1))))
  13493. (org-defkey minibuffer-local-map [(shift left)]
  13494. (lambda () (interactive)
  13495. (org-eval-in-calendar '(calendar-backward-day 1))))
  13496. (org-defkey minibuffer-local-map [(shift right)]
  13497. (lambda () (interactive)
  13498. (org-eval-in-calendar '(calendar-forward-day 1))))
  13499. (org-defkey minibuffer-local-map ">"
  13500. (lambda () (interactive)
  13501. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13502. (org-defkey minibuffer-local-map "<"
  13503. (lambda () (interactive)
  13504. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13505. (org-defkey minibuffer-local-map "\C-v"
  13506. (lambda () (interactive)
  13507. (org-eval-in-calendar
  13508. '(calendar-scroll-left-three-months 1))))
  13509. (org-defkey minibuffer-local-map "\M-v"
  13510. (lambda () (interactive)
  13511. (org-eval-in-calendar
  13512. '(calendar-scroll-right-three-months 1))))
  13513. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13514. (unwind-protect
  13515. (progn
  13516. (use-local-map map)
  13517. (add-hook 'post-command-hook 'org-read-date-display)
  13518. (setq org-ans0 (read-string prompt default-input
  13519. 'org-read-date-history nil))
  13520. ;; org-ans0: from prompt
  13521. ;; org-ans1: from mouse click
  13522. ;; org-ans2: from calendar motion
  13523. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13524. (remove-hook 'post-command-hook 'org-read-date-display)
  13525. (use-local-map old-map)
  13526. (when org-read-date-overlay
  13527. (delete-overlay org-read-date-overlay)
  13528. (setq org-read-date-overlay nil)))))
  13529. (bury-buffer "*Calendar*")))))
  13530. (t ; Naked prompt only
  13531. (unwind-protect
  13532. (setq ans (read-string prompt default-input
  13533. 'org-read-date-history timestr))
  13534. (when org-read-date-overlay
  13535. (delete-overlay org-read-date-overlay)
  13536. (setq org-read-date-overlay nil)))))
  13537. (setq final (org-read-date-analyze ans def defdecode))
  13538. (when org-read-date-analyze-forced-year
  13539. (message "Year was forced into %s"
  13540. (if org-read-date-force-compatible-dates
  13541. "compatible range (1970-2037)"
  13542. "range representable on this machine"))
  13543. (ding))
  13544. ;; One round trip to get rid of 34th of August and stuff like that....
  13545. (setq final (decode-time (apply 'encode-time final)))
  13546. (setq org-read-date-final-answer ans)
  13547. (if to-time
  13548. (apply 'encode-time final)
  13549. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13550. (format "%04d-%02d-%02d %02d:%02d"
  13551. (nth 5 final) (nth 4 final) (nth 3 final)
  13552. (nth 2 final) (nth 1 final))
  13553. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13554. (defvar def)
  13555. (defvar defdecode)
  13556. (defvar with-time)
  13557. (defun org-read-date-display ()
  13558. "Display the current date prompt interpretation in the minibuffer."
  13559. (when org-read-date-display-live
  13560. (when org-read-date-overlay
  13561. (delete-overlay org-read-date-overlay))
  13562. (let ((p (point)))
  13563. (end-of-line 1)
  13564. (while (not (equal (buffer-substring
  13565. (max (point-min) (- (point) 4)) (point))
  13566. " "))
  13567. (insert " "))
  13568. (goto-char p))
  13569. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13570. " " (or org-ans1 org-ans2)))
  13571. (org-end-time-was-given nil)
  13572. (f (org-read-date-analyze ans def defdecode))
  13573. (fmts (if org-dcst
  13574. org-time-stamp-custom-formats
  13575. org-time-stamp-formats))
  13576. (fmt (if (or with-time
  13577. (and (boundp 'org-time-was-given) org-time-was-given))
  13578. (cdr fmts)
  13579. (car fmts)))
  13580. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13581. (when (and org-end-time-was-given
  13582. (string-match org-plain-time-of-day-regexp txt))
  13583. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13584. org-end-time-was-given
  13585. (substring txt (match-end 0)))))
  13586. (when org-read-date-analyze-futurep
  13587. (setq txt (concat txt " (=>F)")))
  13588. (setq org-read-date-overlay
  13589. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13590. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13591. (defun org-read-date-analyze (ans def defdecode)
  13592. "Analyze the combined answer of the date prompt."
  13593. ;; FIXME: cleanup and comment
  13594. (let ((nowdecode (decode-time (current-time)))
  13595. delta deltan deltaw deltadef year month day
  13596. hour minute second wday pm h2 m2 tl wday1
  13597. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13598. (setq org-read-date-analyze-futurep nil
  13599. org-read-date-analyze-forced-year nil)
  13600. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13601. (setq ans "+0"))
  13602. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13603. (setq ans (replace-match "" t t ans)
  13604. deltan (car delta)
  13605. deltaw (nth 1 delta)
  13606. deltadef (nth 2 delta)))
  13607. ;; Check if there is an iso week date in there
  13608. ;; If yes, store the info and postpone interpreting it until the rest
  13609. ;; of the parsing is done
  13610. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13611. (setq iso-year (if (match-end 1)
  13612. (org-small-year-to-year
  13613. (string-to-number (match-string 1 ans))))
  13614. iso-weekday (if (match-end 3)
  13615. (string-to-number (match-string 3 ans)))
  13616. iso-week (string-to-number (match-string 2 ans)))
  13617. (setq ans (replace-match "" t t ans)))
  13618. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13619. (when (string-match
  13620. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13621. (setq year (if (match-end 2)
  13622. (string-to-number (match-string 2 ans))
  13623. (progn (setq kill-year t)
  13624. (string-to-number (format-time-string "%Y"))))
  13625. month (string-to-number (match-string 3 ans))
  13626. day (string-to-number (match-string 4 ans)))
  13627. (if (< year 100) (setq year (+ 2000 year)))
  13628. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13629. t nil ans)))
  13630. ;; Help matching dottet european dates
  13631. (when (string-match
  13632. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13633. (setq year (if (match-end 3)
  13634. (string-to-number (match-string 3 ans))
  13635. (progn (setq kill-year t)
  13636. (string-to-number (format-time-string "%Y"))))
  13637. day (string-to-number (match-string 1 ans))
  13638. month (string-to-number (match-string 2 ans))
  13639. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13640. t nil ans)))
  13641. ;; Help matching american dates, like 5/30 or 5/30/7
  13642. (when (string-match
  13643. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13644. (setq year (if (match-end 4)
  13645. (string-to-number (match-string 4 ans))
  13646. (progn (setq kill-year t)
  13647. (string-to-number (format-time-string "%Y"))))
  13648. month (string-to-number (match-string 1 ans))
  13649. day (string-to-number (match-string 2 ans)))
  13650. (if (< year 100) (setq year (+ 2000 year)))
  13651. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13652. t nil ans)))
  13653. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13654. ;; If there is a time with am/pm, and *no* time without it, we convert
  13655. ;; so that matching will be successful.
  13656. (loop for i from 1 to 2 do ; twice, for end time as well
  13657. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13658. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13659. (setq hour (string-to-number (match-string 1 ans))
  13660. minute (if (match-end 3)
  13661. (string-to-number (match-string 3 ans))
  13662. 0)
  13663. pm (equal ?p
  13664. (string-to-char (downcase (match-string 4 ans)))))
  13665. (if (and (= hour 12) (not pm))
  13666. (setq hour 0)
  13667. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13668. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13669. t t ans))))
  13670. ;; Check if a time range is given as a duration
  13671. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13672. (setq hour (string-to-number (match-string 1 ans))
  13673. h2 (+ hour (string-to-number (match-string 3 ans)))
  13674. minute (string-to-number (match-string 2 ans))
  13675. m2 (+ minute (if (match-end 5) (string-to-number
  13676. (match-string 5 ans))0)))
  13677. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13678. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13679. t t ans)))
  13680. ;; Check if there is a time range
  13681. (when (boundp 'org-end-time-was-given)
  13682. (setq org-time-was-given nil)
  13683. (when (and (string-match org-plain-time-of-day-regexp ans)
  13684. (match-end 8))
  13685. (setq org-end-time-was-given (match-string 8 ans))
  13686. (setq ans (concat (substring ans 0 (match-beginning 7))
  13687. (substring ans (match-end 7))))))
  13688. (setq tl (parse-time-string ans)
  13689. day (or (nth 3 tl) (nth 3 defdecode))
  13690. month (or (nth 4 tl)
  13691. (if (and org-read-date-prefer-future
  13692. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13693. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13694. (nth 4 defdecode)))
  13695. year (or (and (not kill-year) (nth 5 tl))
  13696. (if (and org-read-date-prefer-future
  13697. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13698. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13699. (nth 5 defdecode)))
  13700. hour (or (nth 2 tl) (nth 2 defdecode))
  13701. minute (or (nth 1 tl) (nth 1 defdecode))
  13702. second (or (nth 0 tl) 0)
  13703. wday (nth 6 tl))
  13704. (when (and (eq org-read-date-prefer-future 'time)
  13705. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13706. (equal day (nth 3 nowdecode))
  13707. (equal month (nth 4 nowdecode))
  13708. (equal year (nth 5 nowdecode))
  13709. (nth 2 tl)
  13710. (or (< (nth 2 tl) (nth 2 nowdecode))
  13711. (and (= (nth 2 tl) (nth 2 nowdecode))
  13712. (nth 1 tl)
  13713. (< (nth 1 tl) (nth 1 nowdecode)))))
  13714. (setq day (1+ day)
  13715. futurep t))
  13716. ;; Special date definitions below
  13717. (cond
  13718. (iso-week
  13719. ;; There was an iso week
  13720. (require 'cal-iso)
  13721. (setq futurep nil)
  13722. (setq year (or iso-year year)
  13723. day (or iso-weekday wday 1)
  13724. wday nil ; to make sure that the trigger below does not match
  13725. iso-date (calendar-gregorian-from-absolute
  13726. (calendar-absolute-from-iso
  13727. (list iso-week day year))))
  13728. ; FIXME: Should we also push ISO weeks into the future?
  13729. ; (when (and org-read-date-prefer-future
  13730. ; (not iso-year)
  13731. ; (< (calendar-absolute-from-gregorian iso-date)
  13732. ; (time-to-days (current-time))))
  13733. ; (setq year (1+ year)
  13734. ; iso-date (calendar-gregorian-from-absolute
  13735. ; (calendar-absolute-from-iso
  13736. ; (list iso-week day year)))))
  13737. (setq month (car iso-date)
  13738. year (nth 2 iso-date)
  13739. day (nth 1 iso-date)))
  13740. (deltan
  13741. (setq futurep nil)
  13742. (unless deltadef
  13743. (let ((now (decode-time (current-time))))
  13744. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13745. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13746. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13747. ((equal deltaw "m") (setq month (+ month deltan)))
  13748. ((equal deltaw "y") (setq year (+ year deltan)))))
  13749. ((and wday (not (nth 3 tl)))
  13750. (setq futurep nil)
  13751. ;; Weekday was given, but no day, so pick that day in the week
  13752. ;; on or after the derived date.
  13753. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13754. (unless (equal wday wday1)
  13755. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13756. (if (and (boundp 'org-time-was-given)
  13757. (nth 2 tl))
  13758. (setq org-time-was-given t))
  13759. (if (< year 100) (setq year (+ 2000 year)))
  13760. ;; Check of the date is representable
  13761. (if org-read-date-force-compatible-dates
  13762. (progn
  13763. (if (< year 1970)
  13764. (setq year 1970 org-read-date-analyze-forced-year t))
  13765. (if (> year 2037)
  13766. (setq year 2037 org-read-date-analyze-forced-year t)))
  13767. (condition-case nil
  13768. (ignore (encode-time second minute hour day month year))
  13769. (error
  13770. (setq year (nth 5 defdecode))
  13771. (setq org-read-date-analyze-forced-year t))))
  13772. (setq org-read-date-analyze-futurep futurep)
  13773. (list second minute hour day month year)))
  13774. (defvar parse-time-weekdays)
  13775. (defun org-read-date-get-relative (s today default)
  13776. "Check string S for special relative date string.
  13777. TODAY and DEFAULT are internal times, for today and for a default.
  13778. Return shift list (N what def-flag)
  13779. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13780. N is the number of WHATs to shift.
  13781. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13782. the DEFAULT date rather than TODAY."
  13783. (when (and
  13784. (string-match
  13785. (concat
  13786. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13787. "\\([0-9]+\\)?"
  13788. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13789. "\\([ \t]\\|$\\)") s)
  13790. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13791. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13792. (string-to-char (substring (match-string 1 s) -1))
  13793. ?+))
  13794. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13795. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13796. (what (if (match-end 3) (match-string 3 s) "d"))
  13797. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13798. (date (if rel default today))
  13799. (wday (nth 6 (decode-time date)))
  13800. delta)
  13801. (if wday1
  13802. (progn
  13803. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13804. (if (= dir ?-) (setq delta (- delta 7)))
  13805. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13806. (list delta "d" rel))
  13807. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13808. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13809. "Turn a user-specified date into the internal representation.
  13810. The internal representation needed by the calendar is (month day year).
  13811. This is a wrapper to handle the brain-dead convention in calendar that
  13812. user function argument order change dependent on argument order."
  13813. (if (boundp 'calendar-date-style)
  13814. (cond
  13815. ((eq calendar-date-style 'american)
  13816. (list arg1 arg2 arg3))
  13817. ((eq calendar-date-style 'european)
  13818. (list arg2 arg1 arg3))
  13819. ((eq calendar-date-style 'iso)
  13820. (list arg2 arg3 arg1)))
  13821. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13822. (if (org-bound-and-true-p european-calendar-style)
  13823. (list arg2 arg1 arg3)
  13824. (list arg1 arg2 arg3)))))
  13825. (defun org-eval-in-calendar (form &optional keepdate)
  13826. "Eval FORM in the calendar window and return to current window.
  13827. Also, store the cursor date in variable org-ans2."
  13828. (let ((sf (selected-frame))
  13829. (sw (selected-window)))
  13830. (select-window (get-buffer-window "*Calendar*" t))
  13831. (eval form)
  13832. (when (and (not keepdate) (calendar-cursor-to-date))
  13833. (let* ((date (calendar-cursor-to-date))
  13834. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13835. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13836. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13837. (select-window sw)
  13838. (org-select-frame-set-input-focus sf)))
  13839. (defun org-calendar-select ()
  13840. "Return to `org-read-date' with the date currently selected.
  13841. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13842. (interactive)
  13843. (when (calendar-cursor-to-date)
  13844. (let* ((date (calendar-cursor-to-date))
  13845. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13846. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13847. (if (active-minibuffer-window) (exit-minibuffer))))
  13848. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13849. "Insert a date stamp for the date given by the internal TIME.
  13850. WITH-HM means use the stamp format that includes the time of the day.
  13851. INACTIVE means use square brackets instead of angular ones, so that the
  13852. stamp will not contribute to the agenda.
  13853. PRE and POST are optional strings to be inserted before and after the
  13854. stamp.
  13855. The command returns the inserted time stamp."
  13856. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13857. stamp)
  13858. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13859. (insert-before-markers (or pre ""))
  13860. (when (listp extra)
  13861. (setq extra (car extra))
  13862. (if (and (stringp extra)
  13863. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13864. (setq extra (format "-%02d:%02d"
  13865. (string-to-number (match-string 1 extra))
  13866. (string-to-number (match-string 2 extra))))
  13867. (setq extra nil)))
  13868. (when extra
  13869. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13870. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13871. (insert-before-markers (or post ""))
  13872. (setq org-last-inserted-timestamp stamp)))
  13873. (defun org-toggle-time-stamp-overlays ()
  13874. "Toggle the use of custom time stamp formats."
  13875. (interactive)
  13876. (setq org-display-custom-times (not org-display-custom-times))
  13877. (unless org-display-custom-times
  13878. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13879. (while (setq p (next-single-property-change p 'display))
  13880. (if (and (get-text-property p 'display)
  13881. (eq (get-text-property p 'face) 'org-date))
  13882. (remove-text-properties
  13883. p (setq p (next-single-property-change p 'display))
  13884. '(display t))))
  13885. (set-buffer-modified-p bmp)))
  13886. (if (featurep 'xemacs)
  13887. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13888. (org-restart-font-lock)
  13889. (setq org-table-may-need-update t)
  13890. (if org-display-custom-times
  13891. (message "Time stamps are overlayed with custom format")
  13892. (message "Time stamp overlays removed")))
  13893. (defun org-display-custom-time (beg end)
  13894. "Overlay modified time stamp format over timestamp between BEG and END."
  13895. (let* ((ts (buffer-substring beg end))
  13896. t1 w1 with-hm tf time str w2 (off 0))
  13897. (save-match-data
  13898. (setq t1 (org-parse-time-string ts t))
  13899. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13900. (setq off (- (match-end 0) (match-beginning 0)))))
  13901. (setq end (- end off))
  13902. (setq w1 (- end beg)
  13903. with-hm (and (nth 1 t1) (nth 2 t1))
  13904. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13905. time (org-fix-decoded-time t1)
  13906. str (org-add-props
  13907. (format-time-string
  13908. (substring tf 1 -1) (apply 'encode-time time))
  13909. nil 'mouse-face 'highlight)
  13910. w2 (length str))
  13911. (if (not (= w2 w1))
  13912. (add-text-properties (1+ beg) (+ 2 beg)
  13913. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13914. (if (featurep 'xemacs)
  13915. (progn
  13916. (put-text-property beg end 'invisible t)
  13917. (put-text-property beg end 'end-glyph (make-glyph str)))
  13918. (put-text-property beg end 'display str))))
  13919. (defun org-translate-time (string)
  13920. "Translate all timestamps in STRING to custom format.
  13921. But do this only if the variable `org-display-custom-times' is set."
  13922. (when org-display-custom-times
  13923. (save-match-data
  13924. (let* ((start 0)
  13925. (re org-ts-regexp-both)
  13926. t1 with-hm inactive tf time str beg end)
  13927. (while (setq start (string-match re string start))
  13928. (setq beg (match-beginning 0)
  13929. end (match-end 0)
  13930. t1 (save-match-data
  13931. (org-parse-time-string (substring string beg end) t))
  13932. with-hm (and (nth 1 t1) (nth 2 t1))
  13933. inactive (equal (substring string beg (1+ beg)) "[")
  13934. tf (funcall (if with-hm 'cdr 'car)
  13935. org-time-stamp-custom-formats)
  13936. time (org-fix-decoded-time t1)
  13937. str (format-time-string
  13938. (concat
  13939. (if inactive "[" "<") (substring tf 1 -1)
  13940. (if inactive "]" ">"))
  13941. (apply 'encode-time time))
  13942. string (replace-match str t t string)
  13943. start (+ start (length str)))))))
  13944. string)
  13945. (defun org-fix-decoded-time (time)
  13946. "Set 0 instead of nil for the first 6 elements of time.
  13947. Don't touch the rest."
  13948. (let ((n 0))
  13949. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13950. (defun org-days-to-time (timestamp-string)
  13951. "Difference between TIMESTAMP-STRING and now in days."
  13952. (- (time-to-days (org-time-string-to-time timestamp-string))
  13953. (time-to-days (current-time))))
  13954. (defun org-deadline-close (timestamp-string &optional ndays)
  13955. "Is the time in TIMESTAMP-STRING close to the current date?"
  13956. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13957. (and (< (org-days-to-time timestamp-string) ndays)
  13958. (not (org-entry-is-done-p))))
  13959. (defun org-get-wdays (ts)
  13960. "Get the deadline lead time appropriate for timestring TS."
  13961. (cond
  13962. ((<= org-deadline-warning-days 0)
  13963. ;; 0 or negative, enforce this value no matter what
  13964. (- org-deadline-warning-days))
  13965. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13966. ;; lead time is specified.
  13967. (floor (* (string-to-number (match-string 1 ts))
  13968. (cdr (assoc (match-string 2 ts)
  13969. '(("d" . 1) ("w" . 7)
  13970. ("m" . 30.4) ("y" . 365.25)))))))
  13971. ;; go for the default.
  13972. (t org-deadline-warning-days)))
  13973. (defun org-calendar-select-mouse (ev)
  13974. "Return to `org-read-date' with the date currently selected.
  13975. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13976. (interactive "e")
  13977. (mouse-set-point ev)
  13978. (when (calendar-cursor-to-date)
  13979. (let* ((date (calendar-cursor-to-date))
  13980. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13981. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13982. (if (active-minibuffer-window) (exit-minibuffer))))
  13983. (defun org-check-deadlines (ndays)
  13984. "Check if there are any deadlines due or past due.
  13985. A deadline is considered due if it happens within `org-deadline-warning-days'
  13986. days from today's date. If the deadline appears in an entry marked DONE,
  13987. it is not shown. The prefix arg NDAYS can be used to test that many
  13988. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13989. (interactive "P")
  13990. (let* ((org-warn-days
  13991. (cond
  13992. ((equal ndays '(4)) 100000)
  13993. (ndays (prefix-numeric-value ndays))
  13994. (t (abs org-deadline-warning-days))))
  13995. (case-fold-search nil)
  13996. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13997. (callback
  13998. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13999. (message "%d deadlines past-due or due within %d days"
  14000. (org-occur regexp nil callback)
  14001. org-warn-days)))
  14002. (defun org-check-before-date (date)
  14003. "Check if there are deadlines or scheduled entries before DATE."
  14004. (interactive (list (org-read-date)))
  14005. (let ((case-fold-search nil)
  14006. (regexp (concat "\\<\\(" org-deadline-string
  14007. "\\|" org-scheduled-string
  14008. "\\) *<\\([^>]+\\)>"))
  14009. (callback
  14010. (lambda () (time-less-p
  14011. (org-time-string-to-time (match-string 2))
  14012. (org-time-string-to-time date)))))
  14013. (message "%d entries before %s"
  14014. (org-occur regexp nil callback) date)))
  14015. (defun org-check-after-date (date)
  14016. "Check if there are deadlines or scheduled entries after DATE."
  14017. (interactive (list (org-read-date)))
  14018. (let ((case-fold-search nil)
  14019. (regexp (concat "\\<\\(" org-deadline-string
  14020. "\\|" org-scheduled-string
  14021. "\\) *<\\([^>]+\\)>"))
  14022. (callback
  14023. (lambda () (not
  14024. (time-less-p
  14025. (org-time-string-to-time (match-string 2))
  14026. (org-time-string-to-time date))))))
  14027. (message "%d entries after %s"
  14028. (org-occur regexp nil callback) date)))
  14029. (defun org-evaluate-time-range (&optional to-buffer)
  14030. "Evaluate a time range by computing the difference between start and end.
  14031. Normally the result is just printed in the echo area, but with prefix arg
  14032. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14033. If the time range is actually in a table, the result is inserted into the
  14034. next column.
  14035. For time difference computation, a year is assumed to be exactly 365
  14036. days in order to avoid rounding problems."
  14037. (interactive "P")
  14038. (or
  14039. (org-clock-update-time-maybe)
  14040. (save-excursion
  14041. (unless (org-at-date-range-p t)
  14042. (goto-char (point-at-bol))
  14043. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14044. (if (not (org-at-date-range-p t))
  14045. (error "Not at a time-stamp range, and none found in current line")))
  14046. (let* ((ts1 (match-string 1))
  14047. (ts2 (match-string 2))
  14048. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14049. (match-end (match-end 0))
  14050. (time1 (org-time-string-to-time ts1))
  14051. (time2 (org-time-string-to-time ts2))
  14052. (t1 (org-float-time time1))
  14053. (t2 (org-float-time time2))
  14054. (diff (abs (- t2 t1)))
  14055. (negative (< (- t2 t1) 0))
  14056. ;; (ys (floor (* 365 24 60 60)))
  14057. (ds (* 24 60 60))
  14058. (hs (* 60 60))
  14059. (fy "%dy %dd %02d:%02d")
  14060. (fy1 "%dy %dd")
  14061. (fd "%dd %02d:%02d")
  14062. (fd1 "%dd")
  14063. (fh "%02d:%02d")
  14064. y d h m align)
  14065. (if havetime
  14066. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14067. y 0
  14068. d (floor (/ diff ds)) diff (mod diff ds)
  14069. h (floor (/ diff hs)) diff (mod diff hs)
  14070. m (floor (/ diff 60)))
  14071. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14072. y 0
  14073. d (floor (+ (/ diff ds) 0.5))
  14074. h 0 m 0))
  14075. (if (not to-buffer)
  14076. (message "%s" (org-make-tdiff-string y d h m))
  14077. (if (org-at-table-p)
  14078. (progn
  14079. (goto-char match-end)
  14080. (setq align t)
  14081. (and (looking-at " *|") (goto-char (match-end 0))))
  14082. (goto-char match-end))
  14083. (if (looking-at
  14084. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14085. (replace-match ""))
  14086. (if negative (insert " -"))
  14087. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14088. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14089. (insert " " (format fh h m))))
  14090. (if align (org-table-align))
  14091. (message "Time difference inserted")))))
  14092. (defun org-make-tdiff-string (y d h m)
  14093. (let ((fmt "")
  14094. (l nil))
  14095. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14096. l (push y l)))
  14097. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14098. l (push d l)))
  14099. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14100. l (push h l)))
  14101. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14102. l (push m l)))
  14103. (apply 'format fmt (nreverse l))))
  14104. (defun org-time-string-to-time (s)
  14105. (apply 'encode-time (org-parse-time-string s)))
  14106. (defun org-time-string-to-seconds (s)
  14107. (org-float-time (org-time-string-to-time s)))
  14108. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  14109. "Convert a time stamp to an absolute day number.
  14110. If there is a specifier for a cyclic time stamp, get the closest date to
  14111. DAYNR.
  14112. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14113. The variable date is bound by the calendar when this is called."
  14114. (cond
  14115. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14116. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14117. daynr
  14118. (+ daynr 1000)))
  14119. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14120. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14121. (time-to-days (current-time))) (match-string 0 s)
  14122. prefer show-all))
  14123. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  14124. (defun org-days-to-iso-week (days)
  14125. "Return the iso week number."
  14126. (require 'cal-iso)
  14127. (car (calendar-iso-from-absolute days)))
  14128. (defun org-small-year-to-year (year)
  14129. "Convert 2-digit years into 4-digit years.
  14130. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14131. The year 2000 cannot be abbreviated. Any year larger than 99
  14132. is returned unchanged."
  14133. (if (< year 38)
  14134. (setq year (+ 2000 year))
  14135. (if (< year 100)
  14136. (setq year (+ 1900 year))))
  14137. year)
  14138. (defun org-time-from-absolute (d)
  14139. "Return the time corresponding to date D.
  14140. D may be an absolute day number, or a calendar-type list (month day year)."
  14141. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14142. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14143. (defun org-calendar-holiday ()
  14144. "List of holidays, for Diary display in Org-mode."
  14145. (require 'holidays)
  14146. (let ((hl (funcall
  14147. (if (fboundp 'calendar-check-holidays)
  14148. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14149. (if hl (mapconcat 'identity hl "; "))))
  14150. (defun org-diary-sexp-entry (sexp entry date)
  14151. "Process a SEXP diary ENTRY for DATE."
  14152. (require 'diary-lib)
  14153. (let ((result (if calendar-debug-sexp
  14154. (let ((stack-trace-on-error t))
  14155. (eval (car (read-from-string sexp))))
  14156. (condition-case nil
  14157. (eval (car (read-from-string sexp)))
  14158. (error
  14159. (beep)
  14160. (message "Bad sexp at line %d in %s: %s"
  14161. (org-current-line)
  14162. (buffer-file-name) sexp)
  14163. (sleep-for 2))))))
  14164. (cond ((stringp result) (split-string result "; "))
  14165. ((and (consp result)
  14166. (not (consp (cdr result)))
  14167. (stringp (cdr result))) (cdr result))
  14168. ((and (consp result)
  14169. (stringp (car result))) result)
  14170. (result entry)
  14171. (t nil))))
  14172. (defun org-diary-to-ical-string (frombuf)
  14173. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14174. This uses the icalendar.el library."
  14175. (let* ((tmpdir (if (featurep 'xemacs)
  14176. (temp-directory)
  14177. temporary-file-directory))
  14178. (tmpfile (make-temp-name
  14179. (expand-file-name "orgics" tmpdir)))
  14180. buf rtn b e)
  14181. (with-current-buffer frombuf
  14182. (icalendar-export-region (point-min) (point-max) tmpfile)
  14183. (setq buf (find-buffer-visiting tmpfile))
  14184. (set-buffer buf)
  14185. (goto-char (point-min))
  14186. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14187. (setq b (match-beginning 0)))
  14188. (goto-char (point-max))
  14189. (if (re-search-backward "^END:VEVENT" nil t)
  14190. (setq e (match-end 0)))
  14191. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14192. (kill-buffer buf)
  14193. (delete-file tmpfile)
  14194. rtn))
  14195. (defun org-closest-date (start current change prefer show-all)
  14196. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14197. When PREFER is `past', return a date that is either CURRENT or past.
  14198. When PREFER is `future', return a date that is either CURRENT or future.
  14199. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14200. ;; Make the proper lists from the dates
  14201. (catch 'exit
  14202. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14203. dn dw sday cday n1 n2 n0
  14204. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14205. (setq start (org-date-to-gregorian start)
  14206. current (org-date-to-gregorian
  14207. (if show-all
  14208. current
  14209. (time-to-days (current-time))))
  14210. sday (calendar-absolute-from-gregorian start)
  14211. cday (calendar-absolute-from-gregorian current))
  14212. (if (<= cday sday) (throw 'exit sday))
  14213. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14214. (setq dn (string-to-number (match-string 1 change))
  14215. dw (cdr (assoc (match-string 2 change) a1)))
  14216. (error "Invalid change specifier: %s" change))
  14217. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14218. (cond
  14219. ((eq dw 'day)
  14220. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14221. n2 (+ n1 dn)))
  14222. ((eq dw 'year)
  14223. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14224. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14225. (setq date1 (list m d y1)
  14226. n1 (calendar-absolute-from-gregorian date1)
  14227. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14228. n2 (calendar-absolute-from-gregorian date2)))
  14229. ((eq dw 'month)
  14230. ;; approx number of month between the two dates
  14231. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14232. ;; How often does dn fit in there?
  14233. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14234. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14235. m (+ m nm)
  14236. ny (floor (/ m 12))
  14237. y (+ y ny)
  14238. m (- m (* ny 12)))
  14239. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14240. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14241. (setq m2 (+ m dn) y2 y)
  14242. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14243. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14244. (while (<= n2 cday)
  14245. (setq n1 n2 m m2 y y2)
  14246. (setq m2 (+ m dn) y2 y)
  14247. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14248. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14249. ;; Make sure n1 is the earlier date
  14250. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14251. (if show-all
  14252. (cond
  14253. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14254. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14255. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14256. (cond
  14257. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14258. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14259. (t (if (= cday n1) n1 n2)))))))
  14260. (defun org-date-to-gregorian (date)
  14261. "Turn any specification of DATE into a Gregorian date for the calendar."
  14262. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14263. ((and (listp date) (= (length date) 3)) date)
  14264. ((stringp date)
  14265. (setq date (org-parse-time-string date))
  14266. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14267. ((listp date)
  14268. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14269. (defun org-parse-time-string (s &optional nodefault)
  14270. "Parse the standard Org-mode time string.
  14271. This should be a lot faster than the normal `parse-time-string'.
  14272. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14273. hour and minute fields will be nil if not given."
  14274. (if (string-match org-ts-regexp0 s)
  14275. (list 0
  14276. (if (or (match-beginning 8) (not nodefault))
  14277. (string-to-number (or (match-string 8 s) "0")))
  14278. (if (or (match-beginning 7) (not nodefault))
  14279. (string-to-number (or (match-string 7 s) "0")))
  14280. (string-to-number (match-string 4 s))
  14281. (string-to-number (match-string 3 s))
  14282. (string-to-number (match-string 2 s))
  14283. nil nil nil)
  14284. (error "Not a standard Org-mode time string: %s" s)))
  14285. (defun org-timestamp-up (&optional arg)
  14286. "Increase the date item at the cursor by one.
  14287. If the cursor is on the year, change the year. If it is on the month,
  14288. the day or the time, change that.
  14289. With prefix ARG, change by that many units."
  14290. (interactive "p")
  14291. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14292. (defun org-timestamp-down (&optional arg)
  14293. "Decrease the date item at the cursor by one.
  14294. If the cursor is on the year, change the year. If it is on the month,
  14295. the day or the time, change that.
  14296. With prefix ARG, change by that many units."
  14297. (interactive "p")
  14298. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14299. (defun org-timestamp-up-day (&optional arg)
  14300. "Increase the date in the time stamp by one day.
  14301. With prefix ARG, change that many days."
  14302. (interactive "p")
  14303. (if (and (not (org-at-timestamp-p t))
  14304. (org-on-heading-p))
  14305. (org-todo 'up)
  14306. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14307. (defun org-timestamp-down-day (&optional arg)
  14308. "Decrease the date in the time stamp by one day.
  14309. With prefix ARG, change that many days."
  14310. (interactive "p")
  14311. (if (and (not (org-at-timestamp-p t))
  14312. (org-on-heading-p))
  14313. (org-todo 'down)
  14314. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14315. (defun org-at-timestamp-p (&optional inactive-ok)
  14316. "Determine if the cursor is in or at a timestamp."
  14317. (interactive)
  14318. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14319. (pos (point))
  14320. (ans (or (looking-at tsr)
  14321. (save-excursion
  14322. (skip-chars-backward "^[<\n\r\t")
  14323. (if (> (point) (point-min)) (backward-char 1))
  14324. (and (looking-at tsr)
  14325. (> (- (match-end 0) pos) -1))))))
  14326. (and ans
  14327. (boundp 'org-ts-what)
  14328. (setq org-ts-what
  14329. (cond
  14330. ((= pos (match-beginning 0)) 'bracket)
  14331. ;; Point is considered to be "on the bracket" whether
  14332. ;; it's really on it or right after it.
  14333. ((or (= pos (1- (match-end 0)))
  14334. (= pos (match-end 0))) 'bracket)
  14335. ((org-pos-in-match-range pos 2) 'year)
  14336. ((org-pos-in-match-range pos 3) 'month)
  14337. ((org-pos-in-match-range pos 7) 'hour)
  14338. ((org-pos-in-match-range pos 8) 'minute)
  14339. ((or (org-pos-in-match-range pos 4)
  14340. (org-pos-in-match-range pos 5)) 'day)
  14341. ((and (> pos (or (match-end 8) (match-end 5)))
  14342. (< pos (match-end 0)))
  14343. (- pos (or (match-end 8) (match-end 5))))
  14344. (t 'day))))
  14345. ans))
  14346. (defun org-toggle-timestamp-type ()
  14347. "Toggle the type (<active> or [inactive]) of a time stamp."
  14348. (interactive)
  14349. (when (org-at-timestamp-p t)
  14350. (let ((beg (match-beginning 0)) (end (match-end 0))
  14351. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14352. (save-excursion
  14353. (goto-char beg)
  14354. (while (re-search-forward "[][<>]" end t)
  14355. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14356. t t)))
  14357. (message "Timestamp is now %sactive"
  14358. (if (equal (char-after beg) ?<) "" "in")))))
  14359. (defun org-timestamp-change (n &optional what updown)
  14360. "Change the date in the time stamp at point.
  14361. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14362. `year', `minute', `second'. If WHAT is not given, the cursor position
  14363. in the timestamp determines what will be changed."
  14364. (let ((origin (point)) origin-cat
  14365. with-hm inactive
  14366. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14367. org-ts-what
  14368. extra rem
  14369. ts time time0)
  14370. (if (not (org-at-timestamp-p t))
  14371. (error "Not at a timestamp"))
  14372. (if (and (not what) (eq org-ts-what 'bracket))
  14373. (org-toggle-timestamp-type)
  14374. ;; Point isn't on brackets. Remember the part of the time-stamp
  14375. ;; the point was in. Indeed, size of time-stamps may change,
  14376. ;; but point must be kept in the same category nonetheless.
  14377. (setq origin-cat org-ts-what)
  14378. (if (and (not what) (not (eq org-ts-what 'day))
  14379. org-display-custom-times
  14380. (get-text-property (point) 'display)
  14381. (not (get-text-property (1- (point)) 'display)))
  14382. (setq org-ts-what 'day))
  14383. (setq org-ts-what (or what org-ts-what)
  14384. inactive (= (char-after (match-beginning 0)) ?\[)
  14385. ts (match-string 0))
  14386. (replace-match "")
  14387. (if (string-match
  14388. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14389. ts)
  14390. (setq extra (match-string 1 ts)))
  14391. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14392. (setq with-hm t))
  14393. (setq time0 (org-parse-time-string ts))
  14394. (when (and updown
  14395. (eq org-ts-what 'minute)
  14396. (not current-prefix-arg))
  14397. ;; This looks like s-up and s-down. Change by one rounding step.
  14398. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14399. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14400. (setcar (cdr time0) (+ (nth 1 time0)
  14401. (if (> n 0) (- rem) (- dm rem))))))
  14402. (setq time
  14403. (encode-time (or (car time0) 0)
  14404. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14405. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14406. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14407. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14408. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14409. (nthcdr 6 time0)))
  14410. (when (and (member org-ts-what '(hour minute))
  14411. extra
  14412. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14413. (setq extra (org-modify-ts-extra
  14414. extra
  14415. (if (eq org-ts-what 'hour) 2 5)
  14416. n dm)))
  14417. (when (integerp org-ts-what)
  14418. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14419. (if (eq what 'calendar)
  14420. (let ((cal-date (org-get-date-from-calendar)))
  14421. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14422. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14423. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14424. (setcar time0 (or (car time0) 0))
  14425. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14426. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14427. (setq time (apply 'encode-time time0))))
  14428. ;; Insert the new time-stamp, and ensure point stays in the same
  14429. ;; category as before (i.e. not after the last position in that
  14430. ;; category).
  14431. (let ((pos (point)))
  14432. ;; Stay before inserted string. `save-excursion' is of no use.
  14433. (setq org-last-changed-timestamp
  14434. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14435. (goto-char pos))
  14436. (save-match-data
  14437. (looking-at org-ts-regexp3)
  14438. (goto-char (cond
  14439. ;; `day' category ends before `hour' if any, or at
  14440. ;; the end of the day name.
  14441. ((eq origin-cat 'day)
  14442. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14443. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14444. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14445. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14446. ;; `year' and `month' have both fixed size: point
  14447. ;; couldn't have moved into another part.
  14448. (t origin))))
  14449. ;; Update clock if on a CLOCK line.
  14450. (org-clock-update-time-maybe)
  14451. ;; Try to recenter the calendar window, if any.
  14452. (if (and org-calendar-follow-timestamp-change
  14453. (get-buffer-window "*Calendar*" t)
  14454. (memq org-ts-what '(day month year)))
  14455. (org-recenter-calendar (time-to-days time))))))
  14456. (defun org-modify-ts-extra (s pos n dm)
  14457. "Change the different parts of the lead-time and repeat fields in timestamp."
  14458. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14459. ng h m new rem)
  14460. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14461. (cond
  14462. ((or (org-pos-in-match-range pos 2)
  14463. (org-pos-in-match-range pos 3))
  14464. (setq m (string-to-number (match-string 3 s))
  14465. h (string-to-number (match-string 2 s)))
  14466. (if (org-pos-in-match-range pos 2)
  14467. (setq h (+ h n))
  14468. (setq n (* dm (org-no-warnings (signum n))))
  14469. (when (not (= 0 (setq rem (% m dm))))
  14470. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14471. (setq m (+ m n)))
  14472. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14473. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14474. (setq h (min 24 (max 0 h)))
  14475. (setq ng 1 new (format "-%02d:%02d" h m)))
  14476. ((org-pos-in-match-range pos 6)
  14477. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14478. ((org-pos-in-match-range pos 5)
  14479. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14480. ((org-pos-in-match-range pos 9)
  14481. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14482. ((org-pos-in-match-range pos 8)
  14483. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14484. (when ng
  14485. (setq s (concat
  14486. (substring s 0 (match-beginning ng))
  14487. new
  14488. (substring s (match-end ng))))))
  14489. s))
  14490. (defun org-recenter-calendar (date)
  14491. "If the calendar is visible, recenter it to DATE."
  14492. (let* ((win (selected-window))
  14493. (cwin (get-buffer-window "*Calendar*" t))
  14494. (calendar-move-hook nil))
  14495. (when cwin
  14496. (select-window cwin)
  14497. (calendar-goto-date (if (listp date) date
  14498. (calendar-gregorian-from-absolute date)))
  14499. (select-window win))))
  14500. (defun org-goto-calendar (&optional arg)
  14501. "Go to the Emacs calendar at the current date.
  14502. If there is a time stamp in the current line, go to that date.
  14503. A prefix ARG can be used to force the current date."
  14504. (interactive "P")
  14505. (let ((tsr org-ts-regexp) diff
  14506. (calendar-move-hook nil)
  14507. (calendar-view-holidays-initially-flag nil)
  14508. (calendar-view-diary-initially-flag nil))
  14509. (if (or (org-at-timestamp-p)
  14510. (save-excursion
  14511. (beginning-of-line 1)
  14512. (looking-at (concat ".*" tsr))))
  14513. (let ((d1 (time-to-days (current-time)))
  14514. (d2 (time-to-days
  14515. (org-time-string-to-time (match-string 1)))))
  14516. (setq diff (- d2 d1))))
  14517. (calendar)
  14518. (calendar-goto-today)
  14519. (if (and diff (not arg)) (calendar-forward-day diff))))
  14520. (defun org-get-date-from-calendar ()
  14521. "Return a list (month day year) of date at point in calendar."
  14522. (with-current-buffer "*Calendar*"
  14523. (save-match-data
  14524. (calendar-cursor-to-date))))
  14525. (defun org-date-from-calendar ()
  14526. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14527. If there is already a time stamp at the cursor position, update it."
  14528. (interactive)
  14529. (if (org-at-timestamp-p t)
  14530. (org-timestamp-change 0 'calendar)
  14531. (let ((cal-date (org-get-date-from-calendar)))
  14532. (org-insert-time-stamp
  14533. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14534. (defun org-minutes-to-hh:mm-string (m)
  14535. "Compute H:MM from a number of minutes."
  14536. (let ((h (/ m 60)))
  14537. (setq m (- m (* 60 h)))
  14538. (format org-time-clocksum-format h m)))
  14539. (defun org-hh:mm-string-to-minutes (s)
  14540. "Convert a string H:MM to a number of minutes.
  14541. If the string is just a number, interpret it as minutes.
  14542. In fact, the first hh:mm or number in the string will be taken,
  14543. there can be extra stuff in the string.
  14544. If no number is found, the return value is 0."
  14545. (cond
  14546. ((integerp s) s)
  14547. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14548. (+ (* (string-to-number (match-string 1 s)) 60)
  14549. (string-to-number (match-string 2 s))))
  14550. ((string-match "\\([0-9]+\\)" s)
  14551. (string-to-number (match-string 1 s)))
  14552. (t 0)))
  14553. (defcustom org-effort-durations
  14554. `(("h" . 60)
  14555. ("d" . ,(* 60 8))
  14556. ("w" . ,(* 60 8 5))
  14557. ("m" . ,(* 60 8 5 4))
  14558. ("y" . ,(* 60 8 5 40)))
  14559. "Conversion factor to minutes for an effort modifier.
  14560. Each entry has the form (MODIFIER . MINUTES).
  14561. In an effort string, a number followed by MODIFIER is multiplied
  14562. by the specified number of MINUTES to obtain an effort in
  14563. minutes.
  14564. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14565. effort string \"2hours\" is equivalent to 120 minutes."
  14566. :group 'org-agenda
  14567. :type '(alist :key-type (string :tag "Modifier")
  14568. :value-type (number :tag "Minutes")))
  14569. (defun org-duration-string-to-minutes (s)
  14570. "Convert a duration string S to minutes.
  14571. A bare number is interpreted as minutes, modifiers can be set by
  14572. customizing `org-effort-durations' (which see).
  14573. Entries containing a colon are interpreted as H:MM by
  14574. `org-hh:mm-string-to-minutes'."
  14575. (let ((result 0)
  14576. (re (concat "\\([0-9]+\\) *\\("
  14577. (regexp-opt (mapcar 'car org-effort-durations))
  14578. "\\)")))
  14579. (while (string-match re s)
  14580. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14581. (string-to-number (match-string 1 s))))
  14582. (setq s (replace-match "" nil t s)))
  14583. (incf result (org-hh:mm-string-to-minutes s))
  14584. result))
  14585. ;;;; Files
  14586. (defun org-save-all-org-buffers ()
  14587. "Save all Org-mode buffers without user confirmation."
  14588. (interactive)
  14589. (message "Saving all Org-mode buffers...")
  14590. (save-some-buffers t 'org-mode-p)
  14591. (when (featurep 'org-id) (org-id-locations-save))
  14592. (message "Saving all Org-mode buffers... done"))
  14593. (defun org-revert-all-org-buffers ()
  14594. "Revert all Org-mode buffers.
  14595. Prompt for confirmation when there are unsaved changes.
  14596. Be sure you know what you are doing before letting this function
  14597. overwrite your changes.
  14598. This function is useful in a setup where one tracks org files
  14599. with a version control system, to revert on one machine after pulling
  14600. changes from another. I believe the procedure must be like this:
  14601. 1. M-x org-save-all-org-buffers
  14602. 2. Pull changes from the other machine, resolve conflicts
  14603. 3. M-x org-revert-all-org-buffers"
  14604. (interactive)
  14605. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14606. (error "Abort"))
  14607. (save-excursion
  14608. (save-window-excursion
  14609. (mapc
  14610. (lambda (b)
  14611. (when (and (with-current-buffer b (org-mode-p))
  14612. (with-current-buffer b buffer-file-name))
  14613. (org-pop-to-buffer-same-window b)
  14614. (revert-buffer t 'no-confirm)))
  14615. (buffer-list))
  14616. (when (and (featurep 'org-id) org-id-track-globally)
  14617. (org-id-locations-load)))))
  14618. ;;;; Agenda files
  14619. ;;;###autoload
  14620. (defun org-switchb (&optional arg)
  14621. "Switch between Org buffers.
  14622. With a prefix argument, restrict available to files.
  14623. With two prefix arguments, restrict available buffers to agenda files.
  14624. Defaults to `iswitchb' for buffer name completion.
  14625. Set `org-completion-use-ido' to make it use ido instead."
  14626. (interactive "P")
  14627. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14628. ((equal arg '(16)) (org-buffer-list 'agenda))
  14629. (t (org-buffer-list))))
  14630. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14631. (org-completion-use-ido org-completion-use-ido))
  14632. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14633. (setq org-completion-use-iswitchb t))
  14634. (org-pop-to-buffer-same-window
  14635. (org-icompleting-read "Org buffer: "
  14636. (mapcar 'list (mapcar 'buffer-name blist))
  14637. nil t))))
  14638. ;;; Define some older names previously used for this functionality
  14639. ;;;###autoload
  14640. (defalias 'org-ido-switchb 'org-switchb)
  14641. ;;;###autoload
  14642. (defalias 'org-iswitchb 'org-switchb)
  14643. (defun org-buffer-list (&optional predicate exclude-tmp)
  14644. "Return a list of Org buffers.
  14645. PREDICATE can be `export', `files' or `agenda'.
  14646. export restrict the list to Export buffers.
  14647. files restrict the list to buffers visiting Org files.
  14648. agenda restrict the list to buffers visiting agenda files.
  14649. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14650. (let* ((bfn nil)
  14651. (agenda-files (and (eq predicate 'agenda)
  14652. (mapcar 'file-truename (org-agenda-files t))))
  14653. (filter
  14654. (cond
  14655. ((eq predicate 'files)
  14656. (lambda (b) (with-current-buffer b (org-mode-p))))
  14657. ((eq predicate 'export)
  14658. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14659. ((eq predicate 'agenda)
  14660. (lambda (b)
  14661. (with-current-buffer b
  14662. (and (org-mode-p)
  14663. (setq bfn (buffer-file-name b))
  14664. (member (file-truename bfn) agenda-files)))))
  14665. (t (lambda (b) (with-current-buffer b
  14666. (or (org-mode-p)
  14667. (string-match "\*Org .*Export"
  14668. (buffer-name b)))))))))
  14669. (delq nil
  14670. (mapcar
  14671. (lambda(b)
  14672. (if (and (funcall filter b)
  14673. (or (not exclude-tmp)
  14674. (not (string-match "tmp" (buffer-name b)))))
  14675. b
  14676. nil))
  14677. (buffer-list)))))
  14678. (defun org-agenda-files (&optional unrestricted archives)
  14679. "Get the list of agenda files.
  14680. Optional UNRESTRICTED means return the full list even if a restriction
  14681. is currently in place.
  14682. When ARCHIVES is t, include all archive files that are really being
  14683. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14684. `org-agenda-archives-mode' is t."
  14685. (let ((files
  14686. (cond
  14687. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14688. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14689. ((listp org-agenda-files) org-agenda-files)
  14690. (t (error "Invalid value of `org-agenda-files'")))))
  14691. (setq files (apply 'append
  14692. (mapcar (lambda (f)
  14693. (if (file-directory-p f)
  14694. (directory-files
  14695. f t org-agenda-file-regexp)
  14696. (list f)))
  14697. files)))
  14698. (when org-agenda-skip-unavailable-files
  14699. (setq files (delq nil
  14700. (mapcar (function
  14701. (lambda (file)
  14702. (and (file-readable-p file) file)))
  14703. files))))
  14704. (when (or (eq archives t)
  14705. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14706. (setq files (org-add-archive-files files)))
  14707. files))
  14708. (defun org-agenda-file-p (&optional file)
  14709. "Return non-nil, if FILE is an agenda file.
  14710. If FILE is omitted, use the file associated with the current
  14711. buffer."
  14712. (member (or file (buffer-file-name))
  14713. (org-agenda-files t)))
  14714. (defun org-edit-agenda-file-list ()
  14715. "Edit the list of agenda files.
  14716. Depending on setup, this either uses customize to edit the variable
  14717. `org-agenda-files', or it visits the file that is holding the list. In the
  14718. latter case, the buffer is set up in a way that saving it automatically kills
  14719. the buffer and restores the previous window configuration."
  14720. (interactive)
  14721. (if (stringp org-agenda-files)
  14722. (let ((cw (current-window-configuration)))
  14723. (find-file org-agenda-files)
  14724. (org-set-local 'org-window-configuration cw)
  14725. (org-add-hook 'after-save-hook
  14726. (lambda ()
  14727. (set-window-configuration
  14728. (prog1 org-window-configuration
  14729. (kill-buffer (current-buffer))))
  14730. (org-install-agenda-files-menu)
  14731. (message "New agenda file list installed"))
  14732. nil 'local)
  14733. (message "%s" (substitute-command-keys
  14734. "Edit list and finish with \\[save-buffer]")))
  14735. (customize-variable 'org-agenda-files)))
  14736. (defun org-store-new-agenda-file-list (list)
  14737. "Set new value for the agenda file list and save it correctly."
  14738. (if (stringp org-agenda-files)
  14739. (let ((fe (org-read-agenda-file-list t)) b u)
  14740. (while (setq b (find-buffer-visiting org-agenda-files))
  14741. (kill-buffer b))
  14742. (with-temp-file org-agenda-files
  14743. (insert
  14744. (mapconcat
  14745. (lambda (f) ;; Keep un-expanded entries.
  14746. (if (setq u (assoc f fe))
  14747. (cdr u)
  14748. f))
  14749. list "\n")
  14750. "\n")))
  14751. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14752. (org-insert-mode-line-in-empty-file nil))
  14753. (setq org-agenda-files list)
  14754. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14755. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14756. "Read the list of agenda files from a file.
  14757. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14758. filenames, used by `org-store-new-agenda-file-list' to write back
  14759. un-expanded file names."
  14760. (when (file-directory-p org-agenda-files)
  14761. (error "`org-agenda-files' cannot be a single directory"))
  14762. (when (stringp org-agenda-files)
  14763. (with-temp-buffer
  14764. (insert-file-contents org-agenda-files)
  14765. (mapcar
  14766. (lambda (f)
  14767. (let ((e (expand-file-name (substitute-in-file-name f)
  14768. org-directory)))
  14769. (if pair-with-expansion
  14770. (cons e f)
  14771. e)))
  14772. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14773. ;;;###autoload
  14774. (defun org-cycle-agenda-files ()
  14775. "Cycle through the files in `org-agenda-files'.
  14776. If the current buffer visits an agenda file, find the next one in the list.
  14777. If the current buffer does not, find the first agenda file."
  14778. (interactive)
  14779. (let* ((fs (org-agenda-files t))
  14780. (files (append fs (list (car fs))))
  14781. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14782. file)
  14783. (unless files (error "No agenda files"))
  14784. (catch 'exit
  14785. (while (setq file (pop files))
  14786. (if (equal (file-truename file) tcf)
  14787. (when (car files)
  14788. (find-file (car files))
  14789. (throw 'exit t))))
  14790. (find-file (car fs)))
  14791. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  14792. (defun org-agenda-file-to-front (&optional to-end)
  14793. "Move/add the current file to the top of the agenda file list.
  14794. If the file is not present in the list, it is added to the front. If it is
  14795. present, it is moved there. With optional argument TO-END, add/move to the
  14796. end of the list."
  14797. (interactive "P")
  14798. (let ((org-agenda-skip-unavailable-files nil)
  14799. (file-alist (mapcar (lambda (x)
  14800. (cons (file-truename x) x))
  14801. (org-agenda-files t)))
  14802. (ctf (file-truename buffer-file-name))
  14803. x had)
  14804. (setq x (assoc ctf file-alist) had x)
  14805. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14806. (if to-end
  14807. (setq file-alist (append (delq x file-alist) (list x)))
  14808. (setq file-alist (cons x (delq x file-alist))))
  14809. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14810. (org-install-agenda-files-menu)
  14811. (message "File %s to %s of agenda file list"
  14812. (if had "moved" "added") (if to-end "end" "front"))))
  14813. (defun org-remove-file (&optional file)
  14814. "Remove current file from the list of files in variable `org-agenda-files'.
  14815. These are the files which are being checked for agenda entries.
  14816. Optional argument FILE means use this file instead of the current."
  14817. (interactive)
  14818. (let* ((org-agenda-skip-unavailable-files nil)
  14819. (file (or file buffer-file-name))
  14820. (true-file (file-truename file))
  14821. (afile (abbreviate-file-name file))
  14822. (files (delq nil (mapcar
  14823. (lambda (x)
  14824. (if (equal true-file
  14825. (file-truename x))
  14826. nil x))
  14827. (org-agenda-files t)))))
  14828. (if (not (= (length files) (length (org-agenda-files t))))
  14829. (progn
  14830. (org-store-new-agenda-file-list files)
  14831. (org-install-agenda-files-menu)
  14832. (message "Removed file: %s" afile))
  14833. (message "File was not in list: %s (not removed)" afile))))
  14834. (defun org-file-menu-entry (file)
  14835. (vector file (list 'find-file file) t))
  14836. (defun org-check-agenda-file (file)
  14837. "Make sure FILE exists. If not, ask user what to do."
  14838. (when (not (file-exists-p file))
  14839. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14840. (abbreviate-file-name file))
  14841. (let ((r (downcase (read-char-exclusive))))
  14842. (cond
  14843. ((equal r ?r)
  14844. (org-remove-file file)
  14845. (throw 'nextfile t))
  14846. (t (error "Abort"))))))
  14847. (defun org-get-agenda-file-buffer (file)
  14848. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14849. it to the list of buffers which might be released later."
  14850. (let ((buf (org-find-base-buffer-visiting file)))
  14851. (if buf
  14852. buf ; just return it
  14853. ;; Make a new buffer and remember it
  14854. (setq buf (find-file-noselect file))
  14855. (if buf (push buf org-agenda-new-buffers))
  14856. buf)))
  14857. (defun org-release-buffers (blist)
  14858. "Release all buffers in list, asking the user for confirmation when needed.
  14859. When a buffer is unmodified, it is just killed. When modified, it is saved
  14860. \(if the user agrees) and then killed."
  14861. (let (buf file)
  14862. (while (setq buf (pop blist))
  14863. (setq file (buffer-file-name buf))
  14864. (when (and (buffer-modified-p buf)
  14865. file
  14866. (y-or-n-p (format "Save file %s? " file)))
  14867. (with-current-buffer buf (save-buffer)))
  14868. (kill-buffer buf))))
  14869. (defun org-prepare-agenda-buffers (files)
  14870. "Create buffers for all agenda files, protect archived trees and comments."
  14871. (interactive)
  14872. (let ((pa '(:org-archived t))
  14873. (pc '(:org-comment t))
  14874. (pall '(:org-archived t :org-comment t))
  14875. (inhibit-read-only t)
  14876. (rea (concat ":" org-archive-tag ":"))
  14877. bmp file re)
  14878. (save-excursion
  14879. (save-restriction
  14880. (while (setq file (pop files))
  14881. (catch 'nextfile
  14882. (if (bufferp file)
  14883. (set-buffer file)
  14884. (org-check-agenda-file file)
  14885. (set-buffer (org-get-agenda-file-buffer file)))
  14886. (widen)
  14887. (setq bmp (buffer-modified-p))
  14888. (org-refresh-category-properties)
  14889. (setq org-todo-keywords-for-agenda
  14890. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14891. (setq org-done-keywords-for-agenda
  14892. (append org-done-keywords-for-agenda org-done-keywords))
  14893. (setq org-todo-keyword-alist-for-agenda
  14894. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14895. (setq org-drawers-for-agenda
  14896. (append org-drawers-for-agenda org-drawers))
  14897. (setq org-tag-alist-for-agenda
  14898. (append org-tag-alist-for-agenda org-tag-alist))
  14899. (save-excursion
  14900. (remove-text-properties (point-min) (point-max) pall)
  14901. (when org-agenda-skip-archived-trees
  14902. (goto-char (point-min))
  14903. (while (re-search-forward rea nil t)
  14904. (if (org-on-heading-p t)
  14905. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14906. (goto-char (point-min))
  14907. (setq re (concat org-outline-regexp-bol "+" org-comment-string "\\>"))
  14908. (while (re-search-forward re nil t)
  14909. (add-text-properties
  14910. (match-beginning 0) (org-end-of-subtree t) pc)))
  14911. (set-buffer-modified-p bmp)))))
  14912. (setq org-todo-keywords-for-agenda
  14913. (org-uniquify org-todo-keywords-for-agenda))
  14914. (setq org-todo-keyword-alist-for-agenda
  14915. (org-uniquify org-todo-keyword-alist-for-agenda)
  14916. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14917. ;;;; Embedded LaTeX
  14918. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14919. "Keymap for the minor `org-cdlatex-mode'.")
  14920. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14921. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14922. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14923. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14924. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14925. (defvar org-cdlatex-texmathp-advice-is-done nil
  14926. "Flag remembering if we have applied the advice to texmathp already.")
  14927. (define-minor-mode org-cdlatex-mode
  14928. "Toggle the minor `org-cdlatex-mode'.
  14929. This mode supports entering LaTeX environment and math in LaTeX fragments
  14930. in Org-mode.
  14931. \\{org-cdlatex-mode-map}"
  14932. nil " OCDL" nil
  14933. (when org-cdlatex-mode (require 'cdlatex))
  14934. (unless org-cdlatex-texmathp-advice-is-done
  14935. (setq org-cdlatex-texmathp-advice-is-done t)
  14936. (defadvice texmathp (around org-math-always-on activate)
  14937. "Always return t in org-mode buffers.
  14938. This is because we want to insert math symbols without dollars even outside
  14939. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14940. an embedded LaTeX fragment, let texmathp do its job.
  14941. \\[org-cdlatex-mode-map]"
  14942. (interactive)
  14943. (let (p)
  14944. (cond
  14945. ((not (org-mode-p)) ad-do-it)
  14946. ((eq this-command 'cdlatex-math-symbol)
  14947. (setq ad-return-value t
  14948. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14949. (t
  14950. (let ((p (org-inside-LaTeX-fragment-p)))
  14951. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14952. (setq ad-return-value t
  14953. texmathp-why '("Org-mode embedded math" . 0))
  14954. (if p ad-do-it)))))))))
  14955. (defun turn-on-org-cdlatex ()
  14956. "Unconditionally turn on `org-cdlatex-mode'."
  14957. (org-cdlatex-mode 1))
  14958. (defun org-inside-LaTeX-fragment-p ()
  14959. "Test if point is inside a LaTeX fragment.
  14960. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14961. sequence appearing also before point.
  14962. Even though the matchers for math are configurable, this function assumes
  14963. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14964. delimiters are skipped when they have been removed by customization.
  14965. The return value is nil, or a cons cell with the delimiter and the
  14966. position of this delimiter.
  14967. This function does a reasonably good job, but can locally be fooled by
  14968. for example currency specifications. For example it will assume being in
  14969. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14970. fragments that are properly closed, but during editing, we have to live
  14971. with the uncertainty caused by missing closing delimiters. This function
  14972. looks only before point, not after."
  14973. (catch 'exit
  14974. (let ((pos (point))
  14975. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14976. (lim (progn
  14977. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14978. (point)))
  14979. dd-on str (start 0) m re)
  14980. (goto-char pos)
  14981. (when dodollar
  14982. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14983. re (nth 1 (assoc "$" org-latex-regexps)))
  14984. (while (string-match re str start)
  14985. (cond
  14986. ((= (match-end 0) (length str))
  14987. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14988. ((= (match-end 0) (- (length str) 5))
  14989. (throw 'exit nil))
  14990. (t (setq start (match-end 0))))))
  14991. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14992. (goto-char pos)
  14993. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14994. (and (match-beginning 2) (throw 'exit nil))
  14995. ;; count $$
  14996. (while (re-search-backward "\\$\\$" lim t)
  14997. (setq dd-on (not dd-on)))
  14998. (goto-char pos)
  14999. (if dd-on (cons "$$" m))))))
  15000. (defun org-inside-latex-macro-p ()
  15001. "Is point inside a LaTeX macro or its arguments?"
  15002. (save-match-data
  15003. (org-in-regexp
  15004. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15005. (defun org-try-cdlatex-tab ()
  15006. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15007. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15008. - inside a LaTeX fragment, or
  15009. - after the first word in a line, where an abbreviation expansion could
  15010. insert a LaTeX environment."
  15011. (when org-cdlatex-mode
  15012. (cond
  15013. ((save-excursion
  15014. (skip-chars-backward "a-zA-Z0-9*")
  15015. (skip-chars-backward " \t")
  15016. (bolp))
  15017. (cdlatex-tab) t)
  15018. ((org-inside-LaTeX-fragment-p)
  15019. (cdlatex-tab) t)
  15020. (t nil))))
  15021. (defun org-cdlatex-underscore-caret (&optional arg)
  15022. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15023. Revert to the normal definition outside of these fragments."
  15024. (interactive "P")
  15025. (if (org-inside-LaTeX-fragment-p)
  15026. (call-interactively 'cdlatex-sub-superscript)
  15027. (let (org-cdlatex-mode)
  15028. (call-interactively (key-binding (vector last-input-event))))))
  15029. (defun org-cdlatex-math-modify (&optional arg)
  15030. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15031. Revert to the normal definition outside of these fragments."
  15032. (interactive "P")
  15033. (if (org-inside-LaTeX-fragment-p)
  15034. (call-interactively 'cdlatex-math-modify)
  15035. (let (org-cdlatex-mode)
  15036. (call-interactively (key-binding (vector last-input-event))))))
  15037. (defvar org-latex-fragment-image-overlays nil
  15038. "List of overlays carrying the images of latex fragments.")
  15039. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15040. (defun org-remove-latex-fragment-image-overlays ()
  15041. "Remove all overlays with LaTeX fragment images in current buffer."
  15042. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15043. (setq org-latex-fragment-image-overlays nil))
  15044. (defun org-preview-latex-fragment (&optional subtree)
  15045. "Preview the LaTeX fragment at point, or all locally or globally.
  15046. If the cursor is in a LaTeX fragment, create the image and overlay
  15047. it over the source code. If there is no fragment at point, display
  15048. all fragments in the current text, from one headline to the next. With
  15049. prefix SUBTREE, display all fragments in the current subtree. With a
  15050. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15051. the cursor is before the first headline,
  15052. display all fragments in the buffer.
  15053. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15054. (interactive "P")
  15055. (org-remove-latex-fragment-image-overlays)
  15056. (save-excursion
  15057. (save-restriction
  15058. (let (beg end at msg)
  15059. (cond
  15060. ((or (equal subtree '(16))
  15061. (not (save-excursion
  15062. (re-search-backward org-outline-regexp-bol nil t))))
  15063. (setq beg (point-min) end (point-max)
  15064. msg "Creating images for buffer...%s"))
  15065. ((equal subtree '(4))
  15066. (org-back-to-heading)
  15067. (setq beg (point) end (org-end-of-subtree t)
  15068. msg "Creating images for subtree...%s"))
  15069. (t
  15070. (if (setq at (org-inside-LaTeX-fragment-p))
  15071. (goto-char (max (point-min) (- (cdr at) 2)))
  15072. (org-back-to-heading))
  15073. (setq beg (point) end (progn (outline-next-heading) (point))
  15074. msg (if at "Creating image...%s"
  15075. "Creating images for entry...%s"))))
  15076. (message msg "")
  15077. (narrow-to-region beg end)
  15078. (goto-char beg)
  15079. (org-format-latex
  15080. (concat "ltxpng/" (file-name-sans-extension
  15081. (file-name-nondirectory
  15082. buffer-file-name)))
  15083. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15084. (message msg "done. Use `C-c C-c' to remove images.")))))
  15085. (defvar org-latex-regexps
  15086. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15087. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15088. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15089. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15090. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15091. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15092. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15093. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15094. "Regular expressions for matching embedded LaTeX.")
  15095. (defvar org-export-have-math nil) ;; dynamic scoping
  15096. (defun org-format-latex (prefix &optional dir overlays msg at
  15097. forbuffer processing-type)
  15098. "Replace LaTeX fragments with links to an image, and produce images.
  15099. Some of the options can be changed using the variable
  15100. `org-format-latex-options'."
  15101. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15102. (let* ((prefixnodir (file-name-nondirectory prefix))
  15103. (absprefix (expand-file-name prefix dir))
  15104. (todir (file-name-directory absprefix))
  15105. (opt org-format-latex-options)
  15106. (matchers (plist-get opt :matchers))
  15107. (re-list org-latex-regexps)
  15108. (org-format-latex-header-extra
  15109. (plist-get (org-infile-export-plist) :latex-header-extra))
  15110. (cnt 0) txt hash link beg end re e checkdir
  15111. executables-checked string
  15112. m n block linkfile movefile ov)
  15113. ;; Check the different regular expressions
  15114. (while (setq e (pop re-list))
  15115. (setq m (car e) re (nth 1 e) n (nth 2 e)
  15116. block (if (nth 3 e) "\n\n" ""))
  15117. (when (member m matchers)
  15118. (goto-char (point-min))
  15119. (while (re-search-forward re nil t)
  15120. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15121. (not (get-text-property (match-beginning n)
  15122. 'org-protected))
  15123. (or (not overlays)
  15124. (not (eq (get-char-property (match-beginning n)
  15125. 'org-overlay-type)
  15126. 'org-latex-overlay))))
  15127. (setq org-export-have-math t)
  15128. (cond
  15129. ((eq processing-type 'verbatim)
  15130. ;; Leave the text verbatim, just protect it
  15131. (add-text-properties (match-beginning n) (match-end n)
  15132. '(org-protected t)))
  15133. ((eq processing-type 'mathjax)
  15134. ;; Prepare for MathJax processing
  15135. (setq string (match-string n))
  15136. (if (member m '("$" "$1"))
  15137. (save-excursion
  15138. (delete-region (match-beginning n) (match-end n))
  15139. (goto-char (match-beginning n))
  15140. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15141. "\\)")
  15142. '(org-protected t))))
  15143. (add-text-properties (match-beginning n) (match-end n)
  15144. '(org-protected t))))
  15145. ((or (eq processing-type 'dvipng) t)
  15146. ;; Process to an image
  15147. (setq txt (match-string n)
  15148. beg (match-beginning n) end (match-end n)
  15149. cnt (1+ cnt))
  15150. (let (print-length print-level) ; make sure full list is printed
  15151. (setq hash (sha1 (prin1-to-string
  15152. (list org-format-latex-header
  15153. org-format-latex-header-extra
  15154. org-export-latex-default-packages-alist
  15155. org-export-latex-packages-alist
  15156. org-format-latex-options
  15157. forbuffer txt)))
  15158. linkfile (format "%s_%s.png" prefix hash)
  15159. movefile (format "%s_%s.png" absprefix hash)))
  15160. (setq link (concat block "[[file:" linkfile "]]" block))
  15161. (if msg (message msg cnt))
  15162. (goto-char beg)
  15163. (unless checkdir ; make sure the directory exists
  15164. (setq checkdir t)
  15165. (or (file-directory-p todir) (make-directory todir t)))
  15166. (unless executables-checked
  15167. (org-check-external-command
  15168. "latex" "needed to convert LaTeX fragments to images")
  15169. (org-check-external-command
  15170. "dvipng" "needed to convert LaTeX fragments to images")
  15171. (setq executables-checked t))
  15172. (unless (file-exists-p movefile)
  15173. (org-create-formula-image
  15174. txt movefile opt forbuffer))
  15175. (if overlays
  15176. (progn
  15177. (mapc (lambda (o)
  15178. (if (eq (overlay-get o 'org-overlay-type)
  15179. 'org-latex-overlay)
  15180. (delete-overlay o)))
  15181. (overlays-in beg end))
  15182. (setq ov (make-overlay beg end))
  15183. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15184. (if (featurep 'xemacs)
  15185. (progn
  15186. (overlay-put ov 'invisible t)
  15187. (overlay-put
  15188. ov 'end-glyph
  15189. (make-glyph (vector 'png :file movefile))))
  15190. (overlay-put
  15191. ov 'display
  15192. (list 'image :type 'png :file movefile :ascent 'center)))
  15193. (push ov org-latex-fragment-image-overlays)
  15194. (goto-char end))
  15195. (delete-region beg end)
  15196. (insert (org-add-props link
  15197. (list 'org-latex-src
  15198. (replace-regexp-in-string
  15199. "\"" "" txt)))))))))))))
  15200. ;; This function borrows from Ganesh Swami's latex2png.el
  15201. (defun org-create-formula-image (string tofile options buffer)
  15202. "This calls dvipng."
  15203. (require 'org-latex)
  15204. (let* ((tmpdir (if (featurep 'xemacs)
  15205. (temp-directory)
  15206. temporary-file-directory))
  15207. (texfilebase (make-temp-name
  15208. (expand-file-name "orgtex" tmpdir)))
  15209. (texfile (concat texfilebase ".tex"))
  15210. (dvifile (concat texfilebase ".dvi"))
  15211. (pngfile (concat texfilebase ".png"))
  15212. (fnh (if (featurep 'xemacs)
  15213. (font-height (get-face-font 'default))
  15214. (face-attribute 'default :height nil)))
  15215. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15216. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15217. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15218. "Black"))
  15219. (bg (or (plist-get options (if buffer :background :html-background))
  15220. "Transparent")))
  15221. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15222. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15223. (with-temp-file texfile
  15224. (insert (org-splice-latex-header
  15225. org-format-latex-header
  15226. org-export-latex-default-packages-alist
  15227. org-export-latex-packages-alist t
  15228. org-format-latex-header-extra))
  15229. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15230. (require 'org-latex)
  15231. (org-export-latex-fix-inputenc))
  15232. (let ((dir default-directory))
  15233. (condition-case nil
  15234. (progn
  15235. (cd tmpdir)
  15236. (call-process "latex" nil nil nil texfile))
  15237. (error nil))
  15238. (cd dir))
  15239. (if (not (file-exists-p dvifile))
  15240. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15241. (condition-case nil
  15242. (call-process "dvipng" nil nil nil
  15243. "-fg" fg "-bg" bg
  15244. "-D" dpi
  15245. ;;"-x" scale "-y" scale
  15246. "-T" "tight"
  15247. "-o" pngfile
  15248. dvifile)
  15249. (error nil))
  15250. (if (not (file-exists-p pngfile))
  15251. (if org-format-latex-signal-error
  15252. (error "Failed to create png file from %s" texfile)
  15253. (message "Failed to create png file from %s" texfile)
  15254. nil)
  15255. ;; Use the requested file name and clean up
  15256. (copy-file pngfile tofile 'replace)
  15257. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15258. (delete-file (concat texfilebase e)))
  15259. pngfile))))
  15260. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15261. "Fill a LaTeX header template TPL.
  15262. In the template, the following place holders will be recognized:
  15263. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15264. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15265. [PACKAGES] \\usepackage statements for PKG
  15266. [NO-PACKAGES] do not include PKG
  15267. [EXTRA] the string EXTRA
  15268. [NO-EXTRA] do not include EXTRA
  15269. For backward compatibility, if both the positive and the negative place
  15270. holder is missing, the positive one (without the \"NO-\") will be
  15271. assumed to be present at the end of the template.
  15272. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15273. EXTRA is a string.
  15274. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15275. (let (rpl (end ""))
  15276. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15277. (setq rpl (if (or (match-end 1) (not def-pkg))
  15278. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15279. tpl (replace-match rpl t t tpl))
  15280. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15281. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15282. (setq rpl (if (or (match-end 1) (not pkg))
  15283. "" (org-latex-packages-to-string pkg snippets-p t))
  15284. tpl (replace-match rpl t t tpl))
  15285. (if pkg (setq end
  15286. (concat end "\n"
  15287. (org-latex-packages-to-string pkg snippets-p)))))
  15288. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15289. (setq rpl (if (or (match-end 1) (not extra))
  15290. "" (concat extra "\n"))
  15291. tpl (replace-match rpl t t tpl))
  15292. (if (and extra (string-match "\\S-" extra))
  15293. (setq end (concat end "\n" extra))))
  15294. (if (string-match "\\S-" end)
  15295. (concat tpl "\n" end)
  15296. tpl)))
  15297. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15298. "Turn an alist of packages into a string with the \\usepackage macros."
  15299. (setq pkg (mapconcat (lambda(p)
  15300. (cond
  15301. ((stringp p) p)
  15302. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15303. (format "%% Package %s omitted" (cadr p)))
  15304. ((equal "" (car p))
  15305. (format "\\usepackage{%s}" (cadr p)))
  15306. (t
  15307. (format "\\usepackage[%s]{%s}"
  15308. (car p) (cadr p)))))
  15309. pkg
  15310. "\n"))
  15311. (if newline (concat pkg "\n") pkg))
  15312. (defun org-dvipng-color (attr)
  15313. "Return an rgb color specification for dvipng."
  15314. (apply 'format "rgb %s %s %s"
  15315. (mapcar 'org-normalize-color
  15316. (color-values (face-attribute 'default attr nil)))))
  15317. (defun org-normalize-color (value)
  15318. "Return string to be used as color value for an RGB component."
  15319. (format "%g" (/ value 65535.0)))
  15320. ;; Image display
  15321. (defvar org-inline-image-overlays nil)
  15322. (make-variable-buffer-local 'org-inline-image-overlays)
  15323. (defun org-toggle-inline-images (&optional include-linked)
  15324. "Toggle the display of inline images.
  15325. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15326. (interactive "P")
  15327. (if org-inline-image-overlays
  15328. (progn
  15329. (org-remove-inline-images)
  15330. (message "Inline image display turned off"))
  15331. (org-display-inline-images include-linked)
  15332. (if org-inline-image-overlays
  15333. (message "%d images displayed inline"
  15334. (length org-inline-image-overlays))
  15335. (message "No images to display inline"))))
  15336. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15337. "Display inline images.
  15338. Normally only links without a description part are inlined, because this
  15339. is how it will work for export. When INCLUDE-LINKED is set, also links
  15340. with a description part will be inlined. This can be nice for a quick
  15341. look at those images, but it does not reflect what exported files will look
  15342. like.
  15343. When REFRESH is set, refresh existing images between BEG and END.
  15344. This will create new image displays only if necessary.
  15345. BEG and END default to the buffer boundaries."
  15346. (interactive "P")
  15347. (unless refresh
  15348. (org-remove-inline-images)
  15349. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15350. (save-excursion
  15351. (save-restriction
  15352. (widen)
  15353. (setq beg (or beg (point-min)) end (or end (point-max)))
  15354. (goto-char (point-min))
  15355. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15356. (substring (org-image-file-name-regexp) 0 -2)
  15357. "\\)\\]" (if include-linked "" "\\]")))
  15358. old file ov img)
  15359. (while (re-search-forward re end t)
  15360. (setq old (get-char-property-and-overlay (match-beginning 1)
  15361. 'org-image-overlay))
  15362. (setq file (expand-file-name
  15363. (concat (or (match-string 3) "") (match-string 4))))
  15364. (when (file-exists-p file)
  15365. (if (and (car-safe old) refresh)
  15366. (image-refresh (overlay-get (cdr old) 'display))
  15367. (setq img (save-match-data (create-image file)))
  15368. (when img
  15369. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15370. (overlay-put ov 'display img)
  15371. (overlay-put ov 'face 'default)
  15372. (overlay-put ov 'org-image-overlay t)
  15373. (overlay-put ov 'modification-hooks
  15374. (list 'org-display-inline-modification-hook))
  15375. (push ov org-inline-image-overlays)))))))))
  15376. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15377. "Remove inline-display overlay if a corresponding region is modified."
  15378. (let ((inhibit-modification-hooks t))
  15379. (when (and ov after)
  15380. (delete ov org-inline-image-overlays)
  15381. (delete-overlay ov))))
  15382. (defun org-remove-inline-images ()
  15383. "Remove inline display of images."
  15384. (interactive)
  15385. (mapc 'delete-overlay org-inline-image-overlays)
  15386. (setq org-inline-image-overlays nil))
  15387. ;;;; Key bindings
  15388. ;; Make `C-c C-x' a prefix key
  15389. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15390. ;; TAB key with modifiers
  15391. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15392. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15393. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15394. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15395. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15396. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15397. ;; The following line is necessary under Suse GNU/Linux
  15398. (unless (featurep 'xemacs)
  15399. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15400. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15401. (define-key org-mode-map [backtab] 'org-shifttab)
  15402. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15403. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15404. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15405. ;; Cursor keys with modifiers
  15406. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15407. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15408. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15409. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15410. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15411. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15412. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15413. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15414. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15415. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15416. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15417. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15418. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15419. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15420. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15421. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15422. ;; Babel keys
  15423. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15424. (mapc (lambda (pair)
  15425. (define-key org-babel-map (car pair) (cdr pair)))
  15426. org-babel-key-bindings)
  15427. ;;; Extra keys for tty access.
  15428. ;; We only set them when really needed because otherwise the
  15429. ;; menus don't show the simple keys
  15430. (when (or org-use-extra-keys
  15431. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15432. (not window-system))
  15433. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15434. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15435. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15436. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15437. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15438. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15439. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15440. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15441. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15442. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15443. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15444. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15445. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15446. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15447. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15448. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15449. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15450. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15451. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15452. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15453. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15454. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15455. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15456. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15457. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15458. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15459. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15460. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15461. ;; All the other keys
  15462. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15463. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15464. (if (boundp 'narrow-map)
  15465. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15466. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15467. (if (boundp 'narrow-map)
  15468. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15469. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15470. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15471. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15472. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15473. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15474. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15475. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15476. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15477. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15478. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15479. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15480. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15481. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15482. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15483. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15484. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15485. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15486. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15487. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15488. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15489. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15490. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15491. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15492. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15493. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15494. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15495. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15496. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15497. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15498. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15499. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15500. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15501. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15502. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15503. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15504. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15505. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15506. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15507. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15508. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15509. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15510. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15511. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15512. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15513. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15514. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15515. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15516. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15517. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15518. (org-defkey org-mode-map "\C-m" 'org-return)
  15519. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15520. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15521. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15522. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15523. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15524. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15525. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15526. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15527. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15528. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15529. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15530. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15531. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15532. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15533. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15534. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15535. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15536. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15537. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15538. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15539. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15540. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15541. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15542. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15543. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15544. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15545. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15546. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15547. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15548. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15549. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15550. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15551. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15552. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15553. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15554. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15555. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15556. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15557. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15558. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15559. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15560. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15561. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15562. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15563. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15564. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15565. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15566. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15567. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15568. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15569. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15570. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15571. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15572. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15573. (when (featurep 'xemacs)
  15574. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15575. (defconst org-speed-commands-default
  15576. '(
  15577. ("Outline Navigation")
  15578. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15579. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15580. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15581. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15582. ("u" . (org-speed-move-safe 'outline-up-heading))
  15583. ("j" . org-goto)
  15584. ("g" . (org-refile t))
  15585. ("Outline Visibility")
  15586. ("c" . org-cycle)
  15587. ("C" . org-shifttab)
  15588. (" " . org-display-outline-path)
  15589. ("Outline Structure Editing")
  15590. ("U" . org-shiftmetaup)
  15591. ("D" . org-shiftmetadown)
  15592. ("r" . org-metaright)
  15593. ("l" . org-metaleft)
  15594. ("R" . org-shiftmetaright)
  15595. ("L" . org-shiftmetaleft)
  15596. ("i" . (progn (forward-char 1) (call-interactively
  15597. 'org-insert-heading-respect-content)))
  15598. ("^" . org-sort)
  15599. ("w" . org-refile)
  15600. ("a" . org-archive-subtree-default-with-confirmation)
  15601. ("." . org-mark-subtree)
  15602. ("Clock Commands")
  15603. ("I" . org-clock-in)
  15604. ("O" . org-clock-out)
  15605. ("Meta Data Editing")
  15606. ("t" . org-todo)
  15607. ("0" . (org-priority ?\ ))
  15608. ("1" . (org-priority ?A))
  15609. ("2" . (org-priority ?B))
  15610. ("3" . (org-priority ?C))
  15611. (";" . org-set-tags-command)
  15612. ("e" . org-set-effort)
  15613. ("Agenda Views etc")
  15614. ("v" . org-agenda)
  15615. ("/" . org-sparse-tree)
  15616. ("Misc")
  15617. ("o" . org-open-at-point)
  15618. ("?" . org-speed-command-help)
  15619. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15620. (">" . (org-agenda-remove-restriction-lock))
  15621. )
  15622. "The default speed commands.")
  15623. (defun org-print-speed-command (e)
  15624. (if (> (length (car e)) 1)
  15625. (progn
  15626. (princ "\n")
  15627. (princ (car e))
  15628. (princ "\n")
  15629. (princ (make-string (length (car e)) ?-))
  15630. (princ "\n"))
  15631. (princ (car e))
  15632. (princ " ")
  15633. (if (symbolp (cdr e))
  15634. (princ (symbol-name (cdr e)))
  15635. (prin1 (cdr e)))
  15636. (princ "\n")))
  15637. (defun org-speed-command-help ()
  15638. "Show the available speed commands."
  15639. (interactive)
  15640. (if (not org-use-speed-commands)
  15641. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15642. (with-output-to-temp-buffer "*Help*"
  15643. (princ "User-defined Speed commands\n===========================\n")
  15644. (mapc 'org-print-speed-command org-speed-commands-user)
  15645. (princ "\n")
  15646. (princ "Built-in Speed commands\n=======================\n")
  15647. (mapc 'org-print-speed-command org-speed-commands-default))
  15648. (with-current-buffer "*Help*"
  15649. (setq truncate-lines t))))
  15650. (defun org-speed-move-safe (cmd)
  15651. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15652. If not, return to the original position and throw an error."
  15653. (interactive)
  15654. (let ((pos (point)))
  15655. (call-interactively cmd)
  15656. (unless (and (bolp) (org-on-heading-p))
  15657. (goto-char pos)
  15658. (error "Boundary reached while executing %s" cmd))))
  15659. (defvar org-self-insert-command-undo-counter 0)
  15660. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15661. (defvar org-speed-command nil)
  15662. (defun org-speed-command-default-hook (keys)
  15663. "Hook for activating single-letter speed commands.
  15664. `org-speed-commands-default' specifies a minimal command set.
  15665. Use `org-speed-commands-user' for further customization."
  15666. (when (or (and (bolp) (looking-at org-outline-regexp))
  15667. (and (functionp org-use-speed-commands)
  15668. (funcall org-use-speed-commands)))
  15669. (cdr (assoc keys (append org-speed-commands-user
  15670. org-speed-commands-default)))))
  15671. (defun org-babel-speed-command-hook (keys)
  15672. "Hook for activating single-letter code block commands."
  15673. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15674. (cdr (assoc keys org-babel-key-bindings))))
  15675. (defcustom org-speed-command-hook
  15676. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15677. "Hook for activating speed commands at strategic locations.
  15678. Hook functions are called in sequence until a valid handler is
  15679. found.
  15680. Each hook takes a single argument, a user-pressed command key
  15681. which is also a `self-insert-command' from the global map.
  15682. Within the hook, examine the cursor position and the command key
  15683. and return nil or a valid handler as appropriate. Handler could
  15684. be one of an interactive command, a function, or a form.
  15685. Set `org-use-speed-commands' to non-nil value to enable this
  15686. hook. The default setting is `org-speed-command-default-hook'."
  15687. :group 'org-structure
  15688. :type 'hook)
  15689. (defun org-self-insert-command (N)
  15690. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15691. If the cursor is in a table looking at whitespace, the whitespace is
  15692. overwritten, and the table is not marked as requiring realignment."
  15693. (interactive "p")
  15694. (cond
  15695. ((and org-use-speed-commands
  15696. (setq org-speed-command
  15697. (run-hook-with-args-until-success
  15698. 'org-speed-command-hook (this-command-keys))))
  15699. (cond
  15700. ((commandp org-speed-command)
  15701. (setq this-command org-speed-command)
  15702. (call-interactively org-speed-command))
  15703. ((functionp org-speed-command)
  15704. (funcall org-speed-command))
  15705. ((and org-speed-command (listp org-speed-command))
  15706. (eval org-speed-command))
  15707. (t (let (org-use-speed-commands)
  15708. (call-interactively 'org-self-insert-command)))))
  15709. ((and
  15710. (org-table-p)
  15711. (progn
  15712. ;; check if we blank the field, and if that triggers align
  15713. (and (featurep 'org-table) org-table-auto-blank-field
  15714. (member last-command
  15715. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15716. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15717. ;; got extra space, this field does not determine column width
  15718. (let (org-table-may-need-update) (org-table-blank-field))
  15719. ;; no extra space, this field may determine column width
  15720. (org-table-blank-field)))
  15721. t)
  15722. (eq N 1)
  15723. (looking-at "[^|\n]* |"))
  15724. (let (org-table-may-need-update)
  15725. (goto-char (1- (match-end 0)))
  15726. (delete-char -1)
  15727. (goto-char (match-beginning 0))
  15728. (self-insert-command N)))
  15729. (t
  15730. (setq org-table-may-need-update t)
  15731. (self-insert-command N)
  15732. (org-fix-tags-on-the-fly)
  15733. (if org-self-insert-cluster-for-undo
  15734. (if (not (eq last-command 'org-self-insert-command))
  15735. (setq org-self-insert-command-undo-counter 1)
  15736. (if (>= org-self-insert-command-undo-counter 20)
  15737. (setq org-self-insert-command-undo-counter 1)
  15738. (and (> org-self-insert-command-undo-counter 0)
  15739. buffer-undo-list (listp buffer-undo-list)
  15740. (not (cadr buffer-undo-list)) ; remove nil entry
  15741. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15742. (setq org-self-insert-command-undo-counter
  15743. (1+ org-self-insert-command-undo-counter))))))))
  15744. (defun org-fix-tags-on-the-fly ()
  15745. (when (and (equal (char-after (point-at-bol)) ?*)
  15746. (org-on-heading-p))
  15747. (org-align-tags-here org-tags-column)))
  15748. (defun org-delete-backward-char (N)
  15749. "Like `delete-backward-char', insert whitespace at field end in tables.
  15750. When deleting backwards, in tables this function will insert whitespace in
  15751. front of the next \"|\" separator, to keep the table aligned. The table will
  15752. still be marked for re-alignment if the field did fill the entire column,
  15753. because, in this case the deletion might narrow the column."
  15754. (interactive "p")
  15755. (if (and (org-table-p)
  15756. (eq N 1)
  15757. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15758. (looking-at ".*?|"))
  15759. (let ((pos (point))
  15760. (noalign (looking-at "[^|\n\r]* |"))
  15761. (c org-table-may-need-update))
  15762. (backward-delete-char N)
  15763. (if (not overwrite-mode)
  15764. (progn
  15765. (skip-chars-forward "^|")
  15766. (insert " ")
  15767. (goto-char (1- pos))))
  15768. ;; noalign: if there were two spaces at the end, this field
  15769. ;; does not determine the width of the column.
  15770. (if noalign (setq org-table-may-need-update c)))
  15771. (backward-delete-char N)
  15772. (org-fix-tags-on-the-fly)))
  15773. (defun org-delete-char (N)
  15774. "Like `delete-char', but insert whitespace at field end in tables.
  15775. When deleting characters, in tables this function will insert whitespace in
  15776. front of the next \"|\" separator, to keep the table aligned. The table will
  15777. still be marked for re-alignment if the field did fill the entire column,
  15778. because, in this case the deletion might narrow the column."
  15779. (interactive "p")
  15780. (if (and (org-table-p)
  15781. (not (bolp))
  15782. (not (= (char-after) ?|))
  15783. (eq N 1))
  15784. (if (looking-at ".*?|")
  15785. (let ((pos (point))
  15786. (noalign (looking-at "[^|\n\r]* |"))
  15787. (c org-table-may-need-update))
  15788. (replace-match (concat
  15789. (substring (match-string 0) 1 -1)
  15790. " |"))
  15791. (goto-char pos)
  15792. ;; noalign: if there were two spaces at the end, this field
  15793. ;; does not determine the width of the column.
  15794. (if noalign (setq org-table-may-need-update c)))
  15795. (delete-char N))
  15796. (delete-char N)
  15797. (org-fix-tags-on-the-fly)))
  15798. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15799. (put 'org-self-insert-command 'delete-selection t)
  15800. (put 'orgtbl-self-insert-command 'delete-selection t)
  15801. (put 'org-delete-char 'delete-selection 'supersede)
  15802. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15803. (put 'org-yank 'delete-selection 'yank)
  15804. ;; Make `flyspell-mode' delay after some commands
  15805. (put 'org-self-insert-command 'flyspell-delayed t)
  15806. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15807. (put 'org-delete-char 'flyspell-delayed t)
  15808. (put 'org-delete-backward-char 'flyspell-delayed t)
  15809. ;; Make pabbrev-mode expand after org-mode commands
  15810. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15811. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15812. ;; How to do this: Measure non-white length of current string
  15813. ;; If equal to column width, we should realign.
  15814. (defun org-remap (map &rest commands)
  15815. "In MAP, remap the functions given in COMMANDS.
  15816. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15817. (let (new old)
  15818. (while commands
  15819. (setq old (pop commands) new (pop commands))
  15820. (if (fboundp 'command-remapping)
  15821. (org-defkey map (vector 'remap old) new)
  15822. (substitute-key-definition old new map global-map)))))
  15823. (when (eq org-enable-table-editor 'optimized)
  15824. ;; If the user wants maximum table support, we need to hijack
  15825. ;; some standard editing functions
  15826. (org-remap org-mode-map
  15827. 'self-insert-command 'org-self-insert-command
  15828. 'delete-char 'org-delete-char
  15829. 'delete-backward-char 'org-delete-backward-char)
  15830. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15831. (defvar org-ctrl-c-ctrl-c-hook nil
  15832. "Hook for functions attaching themselves to `C-c C-c'.
  15833. This can be used to add additional functionality to the C-c C-c
  15834. key which executes context-dependent commands. This hook is run
  15835. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  15836. run after the last test.
  15837. Each function will be called with no arguments. The function
  15838. must check if the context is appropriate for it to act. If yes,
  15839. it should do its thing and then return a non-nil value. If the
  15840. context is wrong, just do nothing and return nil.")
  15841. (defvar org-ctrl-c-ctrl-c-final-hook nil
  15842. "Hook for functions attaching themselves to `C-c C-c'.
  15843. This can be used to add additional functionality to the C-c C-c
  15844. key which executes context-dependent commands. This hook is run
  15845. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  15846. before the first test.
  15847. Each function will be called with no arguments. The function
  15848. must check if the context is appropriate for it to act. If yes,
  15849. it should do its thing and then return a non-nil value. If the
  15850. context is wrong, just do nothing and return nil.")
  15851. (defvar org-tab-first-hook nil
  15852. "Hook for functions to attach themselves to TAB.
  15853. See `org-ctrl-c-ctrl-c-hook' for more information.
  15854. This hook runs as the first action when TAB is pressed, even before
  15855. `org-cycle' messes around with the `outline-regexp' to cater for
  15856. inline tasks and plain list item folding.
  15857. If any function in this hook returns t, any other actions that
  15858. would have been caused by TAB (such as table field motion or visibility
  15859. cycling) will not occur.")
  15860. (defvar org-tab-after-check-for-table-hook nil
  15861. "Hook for functions to attach themselves to TAB.
  15862. See `org-ctrl-c-ctrl-c-hook' for more information.
  15863. This hook runs after it has been established that the cursor is not in a
  15864. table, but before checking if the cursor is in a headline or if global cycling
  15865. should be done.
  15866. If any function in this hook returns t, not other actions like visibility
  15867. cycling will be done.")
  15868. (defvar org-tab-after-check-for-cycling-hook nil
  15869. "Hook for functions to attach themselves to TAB.
  15870. See `org-ctrl-c-ctrl-c-hook' for more information.
  15871. This hook runs after it has been established that not table field motion and
  15872. not visibility should be done because of current context. This is probably
  15873. the place where a package like yasnippets can hook in.")
  15874. (defvar org-tab-before-tab-emulation-hook nil
  15875. "Hook for functions to attach themselves to TAB.
  15876. See `org-ctrl-c-ctrl-c-hook' for more information.
  15877. This hook runs after every other options for TAB have been exhausted, but
  15878. before indentation and \t insertion takes place.")
  15879. (defvar org-metaleft-hook nil
  15880. "Hook for functions attaching themselves to `M-left'.
  15881. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15882. (defvar org-metaright-hook nil
  15883. "Hook for functions attaching themselves to `M-right'.
  15884. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15885. (defvar org-metaup-hook nil
  15886. "Hook for functions attaching themselves to `M-up'.
  15887. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15888. (defvar org-metadown-hook nil
  15889. "Hook for functions attaching themselves to `M-down'.
  15890. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15891. (defvar org-shiftmetaleft-hook nil
  15892. "Hook for functions attaching themselves to `M-S-left'.
  15893. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15894. (defvar org-shiftmetaright-hook nil
  15895. "Hook for functions attaching themselves to `M-S-right'.
  15896. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15897. (defvar org-shiftmetaup-hook nil
  15898. "Hook for functions attaching themselves to `M-S-up'.
  15899. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15900. (defvar org-shiftmetadown-hook nil
  15901. "Hook for functions attaching themselves to `M-S-down'.
  15902. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15903. (defvar org-metareturn-hook nil
  15904. "Hook for functions attaching themselves to `M-RET'.
  15905. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15906. (defvar org-shiftup-hook nil
  15907. "Hook for functions attaching themselves to `S-up'.
  15908. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15909. (defvar org-shiftup-final-hook nil
  15910. "Hook for functions attaching themselves to `S-up'.
  15911. This one runs after all other options except shift-select have been excluded.
  15912. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15913. (defvar org-shiftdown-hook nil
  15914. "Hook for functions attaching themselves to `S-down'.
  15915. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15916. (defvar org-shiftdown-final-hook nil
  15917. "Hook for functions attaching themselves to `S-down'.
  15918. This one runs after all other options except shift-select have been excluded.
  15919. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15920. (defvar org-shiftleft-hook nil
  15921. "Hook for functions attaching themselves to `S-left'.
  15922. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15923. (defvar org-shiftleft-final-hook nil
  15924. "Hook for functions attaching themselves to `S-left'.
  15925. This one runs after all other options except shift-select have been excluded.
  15926. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15927. (defvar org-shiftright-hook nil
  15928. "Hook for functions attaching themselves to `S-right'.
  15929. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15930. (defvar org-shiftright-final-hook nil
  15931. "Hook for functions attaching themselves to `S-right'.
  15932. This one runs after all other options except shift-select have been excluded.
  15933. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15934. (defun org-modifier-cursor-error ()
  15935. "Throw an error, a modified cursor command was applied in wrong context."
  15936. (error "This command is active in special context like tables, headlines or items"))
  15937. (defun org-shiftselect-error ()
  15938. "Throw an error because Shift-Cursor command was applied in wrong context."
  15939. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15940. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15941. (error "This command works only in special context like headlines or timestamps")))
  15942. (defun org-call-for-shift-select (cmd)
  15943. (let ((this-command-keys-shift-translated t))
  15944. (call-interactively cmd)))
  15945. (defun org-shifttab (&optional arg)
  15946. "Global visibility cycling or move to previous table field.
  15947. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15948. on context.
  15949. See the individual commands for more information."
  15950. (interactive "P")
  15951. (cond
  15952. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15953. ((integerp arg)
  15954. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15955. (message "Content view to level: %d" arg)
  15956. (org-content (prefix-numeric-value arg2))
  15957. (setq org-cycle-global-status 'overview)))
  15958. (t (call-interactively 'org-global-cycle))))
  15959. (defun org-shiftmetaleft ()
  15960. "Promote subtree or delete table column.
  15961. Calls `org-promote-subtree', `org-outdent-item',
  15962. or `org-table-delete-column', depending on context.
  15963. See the individual commands for more information."
  15964. (interactive)
  15965. (cond
  15966. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15967. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15968. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15969. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15970. (t (org-modifier-cursor-error))))
  15971. (defun org-shiftmetaright ()
  15972. "Demote subtree or insert table column.
  15973. Calls `org-demote-subtree', `org-indent-item',
  15974. or `org-table-insert-column', depending on context.
  15975. See the individual commands for more information."
  15976. (interactive)
  15977. (cond
  15978. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15979. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15980. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15981. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15982. (t (org-modifier-cursor-error))))
  15983. (defun org-shiftmetaup (&optional arg)
  15984. "Move subtree up or kill table row.
  15985. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15986. `org-move-item-up' depending on context. See the individual commands
  15987. for more information."
  15988. (interactive "P")
  15989. (cond
  15990. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15991. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15992. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15993. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15994. (t (org-modifier-cursor-error))))
  15995. (defun org-shiftmetadown (&optional arg)
  15996. "Move subtree down or insert table row.
  15997. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15998. `org-move-item-down', depending on context. See the individual
  15999. commands for more information."
  16000. (interactive "P")
  16001. (cond
  16002. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16003. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16004. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16005. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16006. (t (org-modifier-cursor-error))))
  16007. (defsubst org-hidden-tree-error ()
  16008. (error
  16009. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16010. (defun org-metaleft (&optional arg)
  16011. "Promote heading or move table column to left.
  16012. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16013. With no specific context, calls the Emacs default `backward-word'.
  16014. See the individual commands for more information."
  16015. (interactive "P")
  16016. (cond
  16017. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16018. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16019. ((org-with-limited-levels
  16020. (or (org-on-heading-p)
  16021. (and (org-region-active-p)
  16022. (save-excursion
  16023. (goto-char (region-beginning))
  16024. (org-on-heading-p)))))
  16025. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16026. (call-interactively 'org-do-promote))
  16027. ;; At an inline task.
  16028. ((org-on-heading-p)
  16029. (call-interactively 'org-inlinetask-promote))
  16030. ((or (org-at-item-p)
  16031. (and (org-region-active-p)
  16032. (save-excursion
  16033. (goto-char (region-beginning))
  16034. (org-at-item-p))))
  16035. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16036. (call-interactively 'org-outdent-item))
  16037. (t (call-interactively 'backward-word))))
  16038. (defun org-metaright (&optional arg)
  16039. "Demote subtree or move table column to right.
  16040. Calls `org-do-demote' or `org-table-move-column', depending on context.
  16041. With no specific context, calls the Emacs default `forward-word'.
  16042. See the individual commands for more information."
  16043. (interactive "P")
  16044. (cond
  16045. ((run-hook-with-args-until-success 'org-metaright-hook))
  16046. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16047. ((org-with-limited-levels
  16048. (or (org-on-heading-p)
  16049. (and (org-region-active-p)
  16050. (save-excursion
  16051. (goto-char (region-beginning))
  16052. (org-on-heading-p)))))
  16053. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16054. (call-interactively 'org-do-demote))
  16055. ;; At an inline task.
  16056. ((org-on-heading-p)
  16057. (call-interactively 'org-inlinetask-demote))
  16058. ((or (org-at-item-p)
  16059. (and (org-region-active-p)
  16060. (save-excursion
  16061. (goto-char (region-beginning))
  16062. (org-at-item-p))))
  16063. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16064. (call-interactively 'org-indent-item))
  16065. (t (call-interactively 'forward-word))))
  16066. (defun org-check-for-hidden (what)
  16067. "Check if there are hidden headlines/items in the current visual line.
  16068. WHAT can be either `headlines' or `items'. If the current line is
  16069. an outline or item heading and it has a folded subtree below it,
  16070. this function returns t, nil otherwise."
  16071. (let ((re (cond
  16072. ((eq what 'headlines) org-outline-regexp-bol)
  16073. ((eq what 'items) (org-item-beginning-re))
  16074. (t (error "This should not happen"))))
  16075. beg end)
  16076. (save-excursion
  16077. (catch 'exit
  16078. (unless (org-region-active-p)
  16079. (setq beg (point-at-bol))
  16080. (beginning-of-line 2)
  16081. (while (and (not (eobp)) ;; this is like `next-line'
  16082. (get-char-property (1- (point)) 'invisible))
  16083. (beginning-of-line 2))
  16084. (setq end (point))
  16085. (goto-char beg)
  16086. (goto-char (point-at-eol))
  16087. (setq end (max end (point)))
  16088. (while (re-search-forward re end t)
  16089. (if (get-char-property (match-beginning 0) 'invisible)
  16090. (throw 'exit t))))
  16091. nil))))
  16092. (defun org-metaup (&optional arg)
  16093. "Move subtree up or move table row up.
  16094. Calls `org-move-subtree-up' or `org-table-move-row' or
  16095. `org-move-item-up', depending on context. See the individual commands
  16096. for more information."
  16097. (interactive "P")
  16098. (cond
  16099. ((run-hook-with-args-until-success 'org-metaup-hook))
  16100. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16101. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  16102. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16103. (t (transpose-lines 1) (beginning-of-line -1))))
  16104. (defun org-metadown (&optional arg)
  16105. "Move subtree down or move table row down.
  16106. Calls `org-move-subtree-down' or `org-table-move-row' or
  16107. `org-move-item-down', depending on context. See the individual
  16108. commands for more information."
  16109. (interactive "P")
  16110. (cond
  16111. ((run-hook-with-args-until-success 'org-metadown-hook))
  16112. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16113. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16114. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16115. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16116. (defun org-shiftup (&optional arg)
  16117. "Increase item in timestamp or increase priority of current headline.
  16118. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16119. depending on context. See the individual commands for more information."
  16120. (interactive "P")
  16121. (cond
  16122. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16123. ((and org-support-shift-select (org-region-active-p))
  16124. (org-call-for-shift-select 'previous-line))
  16125. ((org-at-timestamp-p t)
  16126. (call-interactively (if org-edit-timestamp-down-means-later
  16127. 'org-timestamp-down 'org-timestamp-up)))
  16128. ((and (not (eq org-support-shift-select 'always))
  16129. org-enable-priority-commands
  16130. (org-on-heading-p))
  16131. (call-interactively 'org-priority-up))
  16132. ((and (not org-support-shift-select) (org-at-item-p))
  16133. (call-interactively 'org-previous-item))
  16134. ((org-clocktable-try-shift 'up arg))
  16135. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16136. (org-support-shift-select
  16137. (org-call-for-shift-select 'previous-line))
  16138. (t (org-shiftselect-error))))
  16139. (defun org-shiftdown (&optional arg)
  16140. "Decrease item in timestamp or decrease priority of current headline.
  16141. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16142. depending on context. See the individual commands for more information."
  16143. (interactive "P")
  16144. (cond
  16145. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16146. ((and org-support-shift-select (org-region-active-p))
  16147. (org-call-for-shift-select 'next-line))
  16148. ((org-at-timestamp-p t)
  16149. (call-interactively (if org-edit-timestamp-down-means-later
  16150. 'org-timestamp-up 'org-timestamp-down)))
  16151. ((and (not (eq org-support-shift-select 'always))
  16152. org-enable-priority-commands
  16153. (org-on-heading-p))
  16154. (call-interactively 'org-priority-down))
  16155. ((and (not org-support-shift-select) (org-at-item-p))
  16156. (call-interactively 'org-next-item))
  16157. ((org-clocktable-try-shift 'down arg))
  16158. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16159. (org-support-shift-select
  16160. (org-call-for-shift-select 'next-line))
  16161. (t (org-shiftselect-error))))
  16162. (defun org-shiftright (&optional arg)
  16163. "Cycle the thing at point or in the current line, depending on context.
  16164. Depending on context, this does one of the following:
  16165. - switch a timestamp at point one day into the future
  16166. - on a headline, switch to the next TODO keyword.
  16167. - on an item, switch entire list to the next bullet type
  16168. - on a property line, switch to the next allowed value
  16169. - on a clocktable definition line, move time block into the future"
  16170. (interactive "P")
  16171. (cond
  16172. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16173. ((and org-support-shift-select (org-region-active-p))
  16174. (org-call-for-shift-select 'forward-char))
  16175. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16176. ((and (not (eq org-support-shift-select 'always))
  16177. (org-on-heading-p))
  16178. (let ((org-inhibit-logging
  16179. (not org-treat-S-cursor-todo-selection-as-state-change))
  16180. (org-inhibit-blocking
  16181. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16182. (org-call-with-arg 'org-todo 'right)))
  16183. ((or (and org-support-shift-select
  16184. (not (eq org-support-shift-select 'always))
  16185. (org-at-item-bullet-p))
  16186. (and (not org-support-shift-select) (org-at-item-p)))
  16187. (org-call-with-arg 'org-cycle-list-bullet nil))
  16188. ((and (not (eq org-support-shift-select 'always))
  16189. (org-at-property-p))
  16190. (call-interactively 'org-property-next-allowed-value))
  16191. ((org-clocktable-try-shift 'right arg))
  16192. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16193. (org-support-shift-select
  16194. (org-call-for-shift-select 'forward-char))
  16195. (t (org-shiftselect-error))))
  16196. (defun org-shiftleft (&optional arg)
  16197. "Cycle the thing at point or in the current line, depending on context.
  16198. Depending on context, this does one of the following:
  16199. - switch a timestamp at point one day into the past
  16200. - on a headline, switch to the previous TODO keyword.
  16201. - on an item, switch entire list to the previous bullet type
  16202. - on a property line, switch to the previous allowed value
  16203. - on a clocktable definition line, move time block into the past"
  16204. (interactive "P")
  16205. (cond
  16206. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16207. ((and org-support-shift-select (org-region-active-p))
  16208. (org-call-for-shift-select 'backward-char))
  16209. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16210. ((and (not (eq org-support-shift-select 'always))
  16211. (org-on-heading-p))
  16212. (let ((org-inhibit-logging
  16213. (not org-treat-S-cursor-todo-selection-as-state-change))
  16214. (org-inhibit-blocking
  16215. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16216. (org-call-with-arg 'org-todo 'left)))
  16217. ((or (and org-support-shift-select
  16218. (not (eq org-support-shift-select 'always))
  16219. (org-at-item-bullet-p))
  16220. (and (not org-support-shift-select) (org-at-item-p)))
  16221. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16222. ((and (not (eq org-support-shift-select 'always))
  16223. (org-at-property-p))
  16224. (call-interactively 'org-property-previous-allowed-value))
  16225. ((org-clocktable-try-shift 'left arg))
  16226. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16227. (org-support-shift-select
  16228. (org-call-for-shift-select 'backward-char))
  16229. (t (org-shiftselect-error))))
  16230. (defun org-shiftcontrolright ()
  16231. "Switch to next TODO set."
  16232. (interactive)
  16233. (cond
  16234. ((and org-support-shift-select (org-region-active-p))
  16235. (org-call-for-shift-select 'forward-word))
  16236. ((and (not (eq org-support-shift-select 'always))
  16237. (org-on-heading-p))
  16238. (org-call-with-arg 'org-todo 'nextset))
  16239. (org-support-shift-select
  16240. (org-call-for-shift-select 'forward-word))
  16241. (t (org-shiftselect-error))))
  16242. (defun org-shiftcontrolleft ()
  16243. "Switch to previous TODO set."
  16244. (interactive)
  16245. (cond
  16246. ((and org-support-shift-select (org-region-active-p))
  16247. (org-call-for-shift-select 'backward-word))
  16248. ((and (not (eq org-support-shift-select 'always))
  16249. (org-on-heading-p))
  16250. (org-call-with-arg 'org-todo 'previousset))
  16251. (org-support-shift-select
  16252. (org-call-for-shift-select 'backward-word))
  16253. (t (org-shiftselect-error))))
  16254. (defun org-shiftcontrolup ()
  16255. "Change timestamps synchronously up in CLOCK log lines."
  16256. (interactive)
  16257. (cond ((and (not org-support-shift-select)
  16258. (org-at-clock-log-p)
  16259. (org-at-timestamp-p t))
  16260. (org-clock-timestamps-up))
  16261. (t (org-shiftselect-error))))
  16262. (defun org-shiftcontroldown ()
  16263. "Change timestamps synchronously down in CLOCK log lines."
  16264. (interactive)
  16265. (cond ((and (not org-support-shift-select)
  16266. (org-at-clock-log-p)
  16267. (org-at-timestamp-p t))
  16268. (org-clock-timestamps-down))
  16269. (t (org-shiftselect-error))))
  16270. (defun org-ctrl-c-ret ()
  16271. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16272. (interactive)
  16273. (cond
  16274. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16275. (t (call-interactively 'org-insert-heading))))
  16276. (defun org-find-visible ()
  16277. (let ((s (point)))
  16278. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16279. (get-char-property s 'invisible)))
  16280. s))
  16281. (defun org-find-invisible ()
  16282. (let ((s (point)))
  16283. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16284. (not (get-char-property s 'invisible))))
  16285. s))
  16286. (defun org-copy-visible (beg end)
  16287. "Copy the visible parts of the region."
  16288. (interactive "r")
  16289. (let (snippets s)
  16290. (save-excursion
  16291. (save-restriction
  16292. (narrow-to-region beg end)
  16293. (setq s (goto-char (point-min)))
  16294. (while (not (= (point) (point-max)))
  16295. (goto-char (org-find-invisible))
  16296. (push (buffer-substring s (point)) snippets)
  16297. (setq s (goto-char (org-find-visible))))))
  16298. (kill-new (apply 'concat (nreverse snippets)))))
  16299. (defun org-copy-special ()
  16300. "Copy region in table or copy current subtree.
  16301. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16302. See the individual commands for more information."
  16303. (interactive)
  16304. (call-interactively
  16305. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16306. (defun org-cut-special ()
  16307. "Cut region in table or cut current subtree.
  16308. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16309. See the individual commands for more information."
  16310. (interactive)
  16311. (call-interactively
  16312. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16313. (defun org-paste-special (arg)
  16314. "Paste rectangular region into table, or past subtree relative to level.
  16315. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16316. See the individual commands for more information."
  16317. (interactive "P")
  16318. (if (org-at-table-p)
  16319. (org-table-paste-rectangle)
  16320. (org-paste-subtree arg)))
  16321. (defun org-edit-special (&optional arg)
  16322. "Call a special editor for the stuff at point.
  16323. When at a table, call the formula editor with `org-table-edit-formulas'.
  16324. When at the first line of an src example, call `org-edit-src-code'.
  16325. When in an #+include line, visit the include file. Otherwise call
  16326. `ffap' to visit the file at point."
  16327. (interactive)
  16328. ;; possibly prep session before editing source
  16329. (when arg
  16330. (let* ((info (org-babel-get-src-block-info))
  16331. (lang (nth 0 info))
  16332. (params (nth 2 info))
  16333. (session (cdr (assoc :session params))))
  16334. (when (and info session) ;; we are in a source-code block with a session
  16335. (funcall
  16336. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16337. (cond ;; proceed with `org-edit-special'
  16338. ((save-excursion
  16339. (beginning-of-line 1)
  16340. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16341. (find-file (org-trim (match-string 1))))
  16342. ((org-edit-src-code))
  16343. ((org-edit-fixed-width-region))
  16344. ((org-at-table.el-p)
  16345. (org-edit-src-code))
  16346. ((or (org-at-table-p)
  16347. (save-excursion
  16348. (beginning-of-line 1)
  16349. (looking-at "[ \t]*#\\+TBLFM:")))
  16350. (call-interactively 'org-table-edit-formulas))
  16351. (t (call-interactively 'ffap))))
  16352. (defun org-ctrl-c-ctrl-c (&optional arg)
  16353. "Set tags in headline, or update according to changed information at point.
  16354. This command does many different things, depending on context:
  16355. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16356. this is what we do.
  16357. - If the cursor is on a statistics cookie, update it.
  16358. - If the cursor is in a headline, prompt for tags and insert them
  16359. into the current line, aligned to `org-tags-column'. When called
  16360. with prefix arg, realign all tags in the current buffer.
  16361. - If the cursor is in one of the special #+KEYWORD lines, this
  16362. triggers scanning the buffer for these lines and updating the
  16363. information.
  16364. - If the cursor is inside a table, realign the table. This command
  16365. works even if the automatic table editor has been turned off.
  16366. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16367. the entire table.
  16368. - If the cursor is at a footnote reference or definition, jump to
  16369. the corresponding definition or references, respectively.
  16370. - If the cursor is a the beginning of a dynamic block, update it.
  16371. - If the current buffer is a capture buffer, close note and file it.
  16372. - If the cursor is on a <<<target>>>, update radio targets and
  16373. corresponding links in this buffer.
  16374. - If the cursor is on a numbered item in a plain list, renumber the
  16375. ordered list.
  16376. - If the cursor is on a checkbox, toggle it.
  16377. - If the cursor is on a code block, evaluate it. The variable
  16378. `org-confirm-babel-evaluate' can be used to control prompting
  16379. before code block evaluation, by default every code block
  16380. evaluation requires confirmation. Code block evaluation can be
  16381. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16382. (interactive "P")
  16383. (let ((org-enable-table-editor t))
  16384. (cond
  16385. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16386. org-occur-highlights
  16387. org-latex-fragment-image-overlays)
  16388. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16389. (org-remove-occur-highlights)
  16390. (org-remove-latex-fragment-image-overlays)
  16391. (message "Temporary highlights/overlays removed from current buffer"))
  16392. ((and (local-variable-p 'org-finish-function (current-buffer))
  16393. (fboundp org-finish-function))
  16394. (funcall org-finish-function))
  16395. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16396. ((or (looking-at org-property-start-re)
  16397. (org-at-property-p))
  16398. (call-interactively 'org-property-action))
  16399. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16400. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16401. (or (org-on-heading-p) (org-at-item-p)))
  16402. (call-interactively 'org-update-statistics-cookies))
  16403. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16404. ((org-at-table.el-p)
  16405. (message "Use C-c ' to edit table.el tables"))
  16406. ((org-at-table-p)
  16407. (org-table-maybe-eval-formula)
  16408. (if arg
  16409. (call-interactively 'org-table-recalculate)
  16410. (org-table-maybe-recalculate-line))
  16411. (call-interactively 'org-table-align)
  16412. (orgtbl-send-table 'maybe))
  16413. ((or (org-footnote-at-reference-p)
  16414. (org-footnote-at-definition-p))
  16415. (call-interactively 'org-footnote-action))
  16416. ((org-at-item-checkbox-p)
  16417. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16418. ;; list only if at top item.
  16419. (let* ((cbox (match-string 1))
  16420. (struct (org-list-struct))
  16421. (old-struct (copy-tree struct))
  16422. (parents (org-list-parents-alist struct))
  16423. (prevs (org-list-prevs-alist struct))
  16424. (orderedp (org-entry-get nil "ORDERED"))
  16425. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16426. block-item)
  16427. ;; Use a light version of `org-toggle-checkbox' to avoid
  16428. ;; computing list structure twice.
  16429. (org-list-set-checkbox (point-at-bol) struct
  16430. (cond
  16431. ((equal arg '(16)) "[-]")
  16432. ((equal arg '(4)) nil)
  16433. ((equal "[X]" cbox) "[ ]")
  16434. (t "[X]")))
  16435. (org-list-struct-fix-ind struct parents)
  16436. (org-list-struct-fix-bul struct prevs)
  16437. (setq block-item
  16438. (org-list-struct-fix-box struct parents prevs orderedp))
  16439. (when block-item
  16440. (message
  16441. "Checkboxes were removed due to unchecked box at line %d"
  16442. (org-current-line block-item)))
  16443. (org-list-struct-apply-struct struct old-struct)
  16444. (org-update-checkbox-count-maybe)
  16445. (when firstp (org-list-send-list 'maybe))))
  16446. ((org-at-item-p)
  16447. ;; Cursor at an item: repair list. Do checkbox related actions
  16448. ;; only if function was called with an argument. Send list only
  16449. ;; if at top item.
  16450. (let* ((struct (org-list-struct))
  16451. (old-struct (copy-tree struct))
  16452. (parents (org-list-parents-alist struct))
  16453. (prevs (org-list-prevs-alist struct))
  16454. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16455. (org-list-struct-fix-ind struct parents)
  16456. (org-list-struct-fix-bul struct prevs)
  16457. (when arg
  16458. (org-list-set-checkbox (point-at-bol) struct "[ ]")
  16459. (org-list-struct-fix-box struct parents prevs))
  16460. (org-list-struct-apply-struct struct old-struct)
  16461. (when arg (org-update-checkbox-count-maybe))
  16462. (when firstp (org-list-send-list 'maybe))))
  16463. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16464. ;; Dynamic block
  16465. (beginning-of-line 1)
  16466. (save-excursion (org-update-dblock)))
  16467. ((save-excursion
  16468. (beginning-of-line 1)
  16469. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16470. (cond
  16471. ((equal (match-string 1) "TBLFM")
  16472. ;; Recalculate the table before this line
  16473. (save-excursion
  16474. (beginning-of-line 1)
  16475. (skip-chars-backward " \r\n\t")
  16476. (if (org-at-table-p)
  16477. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16478. (t
  16479. (let ((org-inhibit-startup-visibility-stuff t)
  16480. (org-startup-align-all-tables nil))
  16481. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16482. (message "Local setup has been refreshed"))))
  16483. ((org-clock-update-time-maybe))
  16484. (t
  16485. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  16486. (error "C-c C-c can do nothing useful at this location"))))))
  16487. (defun org-mode-restart ()
  16488. "Restart Org-mode, to scan again for special lines.
  16489. Also updates the keyword regular expressions."
  16490. (interactive)
  16491. (org-mode)
  16492. (message "Org-mode restarted"))
  16493. (defun org-kill-note-or-show-branches ()
  16494. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16495. (interactive)
  16496. (if (not org-finish-function)
  16497. (progn
  16498. (hide-subtree)
  16499. (call-interactively 'show-branches))
  16500. (let ((org-note-abort t))
  16501. (funcall org-finish-function))))
  16502. (defun org-return (&optional indent)
  16503. "Goto next table row or insert a newline.
  16504. Calls `org-table-next-row' or `newline', depending on context.
  16505. See the individual commands for more information."
  16506. (interactive)
  16507. (cond
  16508. ((bobp) (if indent (newline-and-indent) (newline)))
  16509. ((org-at-table-p)
  16510. (org-table-justify-field-maybe)
  16511. (call-interactively 'org-table-next-row))
  16512. ;; when `newline-and-indent' is called within a list, make sure
  16513. ;; text moved stays inside the item.
  16514. ((and (org-in-item-p) indent)
  16515. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16516. (progn
  16517. (save-match-data (newline))
  16518. (org-indent-line-to (length (match-string 0))))
  16519. (let ((ind (org-get-indentation)))
  16520. (newline)
  16521. (if (org-looking-back org-list-end-re)
  16522. (org-indent-line-function)
  16523. (org-indent-line-to ind)))))
  16524. ((and org-return-follows-link
  16525. (eq (get-text-property (point) 'face) 'org-link))
  16526. (call-interactively 'org-open-at-point))
  16527. ((and (org-at-heading-p)
  16528. (looking-at
  16529. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16530. (org-show-entry)
  16531. (end-of-line 1)
  16532. (newline))
  16533. (t (if indent (newline-and-indent) (newline)))))
  16534. (defun org-return-indent ()
  16535. "Goto next table row or insert a newline and indent.
  16536. Calls `org-table-next-row' or `newline-and-indent', depending on
  16537. context. See the individual commands for more information."
  16538. (interactive)
  16539. (org-return t))
  16540. (defun org-ctrl-c-star ()
  16541. "Compute table, or change heading status of lines.
  16542. Calls `org-table-recalculate' or `org-toggle-heading',
  16543. depending on context."
  16544. (interactive)
  16545. (cond
  16546. ((org-at-table-p)
  16547. (call-interactively 'org-table-recalculate))
  16548. (t
  16549. ;; Convert all lines in region to list items
  16550. (call-interactively 'org-toggle-heading))))
  16551. (defun org-ctrl-c-minus ()
  16552. "Insert separator line in table or modify bullet status of line.
  16553. Also turns a plain line or a region of lines into list items.
  16554. Calls `org-table-insert-hline', `org-toggle-item', or
  16555. `org-cycle-list-bullet', depending on context."
  16556. (interactive)
  16557. (cond
  16558. ((org-at-table-p)
  16559. (call-interactively 'org-table-insert-hline))
  16560. ((org-region-active-p)
  16561. (call-interactively 'org-toggle-item))
  16562. ((org-in-item-p)
  16563. (call-interactively 'org-cycle-list-bullet))
  16564. (t
  16565. (call-interactively 'org-toggle-item))))
  16566. (defun org-toggle-item (arg)
  16567. "Convert headings or normal lines to items, items to normal lines.
  16568. If there is no active region, only the current line is considered.
  16569. If the first non blank line in the region is an headline, convert
  16570. all headlines to items, shifting text accordingly.
  16571. If it is an item, convert all items to normal lines.
  16572. If it is normal text, change region into an item. With a prefix
  16573. argument ARG, change each line in region into an item."
  16574. (interactive "P")
  16575. (let ((shift-text
  16576. (function
  16577. ;; Shift text in current section to IND, from point to END.
  16578. ;; The function leaves point to END line.
  16579. (lambda (ind end)
  16580. (let ((min-i 1000) (end (copy-marker end)))
  16581. ;; First determine the minimum indentation (MIN-I) of
  16582. ;; the text.
  16583. (save-excursion
  16584. (catch 'exit
  16585. (while (< (point) end)
  16586. (let ((i (org-get-indentation)))
  16587. (cond
  16588. ;; Skip blank lines and inline tasks.
  16589. ((looking-at "^[ \t]*$"))
  16590. ((looking-at org-outline-regexp-bol))
  16591. ;; We can't find less than 0 indentation.
  16592. ((zerop i) (throw 'exit (setq min-i 0)))
  16593. ((< i min-i) (setq min-i i))))
  16594. (forward-line))))
  16595. ;; Then indent each line so that a line indented to
  16596. ;; MIN-I becomes indented to IND. Ignore blank lines
  16597. ;; and inline tasks in the process.
  16598. (let ((delta (- ind min-i)))
  16599. (while (< (point) end)
  16600. (unless (or (looking-at "^[ \t]*$")
  16601. (looking-at org-outline-regexp-bol))
  16602. (org-indent-line-to (+ (org-get-indentation) delta)))
  16603. (forward-line)))))))
  16604. (skip-blanks
  16605. (function
  16606. ;; Return beginning of first non-blank line, starting from
  16607. ;; line at POS.
  16608. (lambda (pos)
  16609. (save-excursion
  16610. (goto-char pos)
  16611. (skip-chars-forward " \r\t\n")
  16612. (point-at-bol)))))
  16613. beg end)
  16614. ;; Determine boundaries of changes.
  16615. (if (org-region-active-p)
  16616. (setq beg (funcall skip-blanks (region-beginning))
  16617. end (copy-marker (region-end)))
  16618. (setq beg (funcall skip-blanks (point-at-bol))
  16619. end (copy-marker (point-at-eol))))
  16620. ;; Depending on the starting line, choose an action on the text
  16621. ;; between BEG and END.
  16622. (org-with-limited-levels
  16623. (save-excursion
  16624. (goto-char beg)
  16625. (cond
  16626. ;; Case 1. Start at an item: de-itemize. Note that it only
  16627. ;; happens when a region is active: `org-ctrl-c-minus'
  16628. ;; would call `org-cycle-list-bullet' otherwise.
  16629. ((org-at-item-p)
  16630. (while (< (point) end)
  16631. (when (org-at-item-p)
  16632. (skip-chars-forward " \t")
  16633. (delete-region (point) (match-end 0)))
  16634. (forward-line)))
  16635. ;; Case 2. Start at an heading: convert to items.
  16636. ((org-on-heading-p)
  16637. (let* ((bul (org-list-bullet-string "-"))
  16638. (bul-len (length bul))
  16639. ;; Indentation of the first heading. It should be
  16640. ;; relative to the indentation of its parent, if any.
  16641. (start-ind (save-excursion
  16642. (cond
  16643. ((not org-adapt-indentation) 0)
  16644. ((not (outline-previous-heading)) 0)
  16645. (t (length (match-string 0))))))
  16646. ;; Level of first heading. Further headings will be
  16647. ;; compared to it to determine hierarchy in the list.
  16648. (ref-level (org-reduced-level (org-outline-level))))
  16649. (while (< (point) end)
  16650. (let* ((level (org-reduced-level (org-outline-level)))
  16651. (delta (max 0 (- level ref-level))))
  16652. ;; If current headline is less indented than the first
  16653. ;; one, set it as reference, in order to preserve
  16654. ;; subtrees.
  16655. (when (< level ref-level) (setq ref-level level))
  16656. (replace-match bul t t)
  16657. (org-indent-line-to (+ start-ind (* delta bul-len)))
  16658. ;; Ensure all text down to END (or SECTION-END) belongs
  16659. ;; to the newly created item.
  16660. (let ((section-end (save-excursion
  16661. (or (outline-next-heading) (point)))))
  16662. (forward-line)
  16663. (funcall shift-text
  16664. (+ start-ind (* (1+ delta) bul-len))
  16665. (min end section-end)))))))
  16666. ;; Case 3. Normal line with ARG: turn each non-item line into
  16667. ;; an item.
  16668. (arg
  16669. (while (< (point) end)
  16670. (unless (or (org-on-heading-p) (org-at-item-p))
  16671. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16672. (replace-match
  16673. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  16674. (forward-line)))
  16675. ;; Case 4. Normal line without ARG: make the first line of
  16676. ;; region an item, and shift indentation of others
  16677. ;; lines to set them as item's body.
  16678. (t (let* ((bul (org-list-bullet-string "-"))
  16679. (bul-len (length bul))
  16680. (ref-ind (org-get-indentation)))
  16681. (skip-chars-forward " \t")
  16682. (insert bul)
  16683. (forward-line)
  16684. (while (< (point) end)
  16685. ;; Ensure that lines less indented than first one
  16686. ;; still get included in item body.
  16687. (funcall shift-text
  16688. (+ ref-ind bul-len)
  16689. (min end (save-excursion (or (outline-next-heading)
  16690. (point)))))
  16691. (forward-line)))))))))
  16692. (defun org-toggle-heading (&optional nstars)
  16693. "Convert headings to normal text, or items or text to headings.
  16694. If there is no active region, only the current line is considered.
  16695. If the first non blank line is an headline, remove the stars from
  16696. all headlines in the region.
  16697. If it is a plain list item, turn all plain list items into headings.
  16698. If it is a normal line, turn each and every normal line (i.e. not
  16699. an heading or an item) in the region into a heading.
  16700. When converting a line into a heading, the number of stars is chosen
  16701. such that the lines become children of the current entry. However,
  16702. when a prefix argument is given, its value determines the number of
  16703. stars to add."
  16704. (interactive "P")
  16705. (let ((skip-blanks
  16706. (function
  16707. ;; Return beginning of first non-blank line, starting from
  16708. ;; line at POS.
  16709. (lambda (pos)
  16710. (save-excursion
  16711. (goto-char pos)
  16712. (skip-chars-forward " \r\t\n")
  16713. (point-at-bol)))))
  16714. beg end)
  16715. ;; Determine boundaries of changes. If region ends at a bol, do
  16716. ;; not consider the last line to be in the region.
  16717. (if (org-region-active-p)
  16718. (setq beg (funcall skip-blanks (region-beginning))
  16719. end (copy-marker (save-excursion
  16720. (goto-char (region-end))
  16721. (if (bolp) (point) (point-at-eol)))))
  16722. (setq beg (funcall skip-blanks (point-at-bol))
  16723. end (copy-marker (point-at-eol))))
  16724. ;; Ensure inline tasks don't count as headings.
  16725. (org-with-limited-levels
  16726. (save-excursion
  16727. (goto-char beg)
  16728. (cond
  16729. ;; Case 1. Started at an heading: de-star headings.
  16730. ((org-on-heading-p)
  16731. (while (< (point) end)
  16732. (when (org-on-heading-p t)
  16733. (looking-at org-outline-regexp) (replace-match ""))
  16734. (forward-line)))
  16735. ;; Case 2. Started at an item: change items into headlines.
  16736. ;; One star will be added by `org-list-to-subtree'.
  16737. ((org-at-item-p)
  16738. (let* ((stars (make-string
  16739. (if nstars
  16740. ;; subtract the star that will be added again by
  16741. ;; `org-list-to-subtree'
  16742. (1- (prefix-numeric-value current-prefix-arg))
  16743. (or (org-current-level) 0))
  16744. ?*))
  16745. (add-stars
  16746. (cond (nstars "") ; stars from prefix only
  16747. ((equal stars "") "") ; before first heading
  16748. (org-odd-levels-only "*") ; inside heading, odd
  16749. (t "")))) ; inside heading, oddeven
  16750. (while (< (point) end)
  16751. (when (org-at-item-p)
  16752. ;; Pay attention to cases when region ends before list.
  16753. (let* ((struct (org-list-struct))
  16754. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  16755. (save-restriction
  16756. (narrow-to-region (point) list-end)
  16757. (insert
  16758. (org-list-to-subtree
  16759. (org-list-parse-list t)
  16760. '(:istart (concat stars add-stars (funcall get-stars depth))
  16761. :icount (concat stars add-stars (funcall get-stars depth))))))))
  16762. (forward-line))))
  16763. ;; Case 3. Started at normal text: make every line an heading,
  16764. ;; skipping headlines and items.
  16765. (t (let* ((stars (make-string
  16766. (if nstars
  16767. (prefix-numeric-value current-prefix-arg)
  16768. (or (org-current-level) 0))
  16769. ?*))
  16770. (add-stars
  16771. (cond (nstars "") ; stars from prefix only
  16772. ((equal stars "") "*") ; before first heading
  16773. (org-odd-levels-only "**") ; inside heading, odd
  16774. (t "*"))) ; inside heading, oddeven
  16775. (rpl (concat stars add-stars " ")))
  16776. (while (< (point) end)
  16777. (when (and (not (org-on-heading-p)) (not (org-at-item-p))
  16778. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  16779. (replace-match (concat rpl (match-string 2))))
  16780. (forward-line)))))))))
  16781. (defun org-meta-return (&optional arg)
  16782. "Insert a new heading or wrap a region in a table.
  16783. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16784. See the individual commands for more information."
  16785. (interactive "P")
  16786. (cond
  16787. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16788. ((org-at-table-p)
  16789. (call-interactively 'org-table-wrap-region))
  16790. (t (call-interactively 'org-insert-heading))))
  16791. ;;; Menu entries
  16792. ;; Define the Org-mode menus
  16793. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16794. '("Tbl"
  16795. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16796. ["Next Field" org-cycle (org-at-table-p)]
  16797. ["Previous Field" org-shifttab (org-at-table-p)]
  16798. ["Next Row" org-return (org-at-table-p)]
  16799. "--"
  16800. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16801. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16802. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16803. "--"
  16804. ("Column"
  16805. ["Move Column Left" org-metaleft (org-at-table-p)]
  16806. ["Move Column Right" org-metaright (org-at-table-p)]
  16807. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16808. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16809. ("Row"
  16810. ["Move Row Up" org-metaup (org-at-table-p)]
  16811. ["Move Row Down" org-metadown (org-at-table-p)]
  16812. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16813. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16814. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16815. "--"
  16816. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16817. ("Rectangle"
  16818. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16819. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16820. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16821. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16822. "--"
  16823. ("Calculate"
  16824. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16825. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16826. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16827. "--"
  16828. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16829. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16830. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16831. "--"
  16832. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16833. "--"
  16834. ["Sum Column/Rectangle" org-table-sum
  16835. (or (org-at-table-p) (org-region-active-p))]
  16836. ["Which Column?" org-table-current-column (org-at-table-p)])
  16837. ["Debug Formulas"
  16838. org-table-toggle-formula-debugger
  16839. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16840. ["Show Col/Row Numbers"
  16841. org-table-toggle-coordinate-overlays
  16842. :style toggle
  16843. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16844. "--"
  16845. ["Create" org-table-create (and (not (org-at-table-p))
  16846. org-enable-table-editor)]
  16847. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16848. ["Import from File" org-table-import (not (org-at-table-p))]
  16849. ["Export to File" org-table-export (org-at-table-p)]
  16850. "--"
  16851. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16852. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16853. '("Org"
  16854. ("Show/Hide"
  16855. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16856. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16857. ["Sparse Tree..." org-sparse-tree t]
  16858. ["Reveal Context" org-reveal t]
  16859. ["Show All" show-all t]
  16860. "--"
  16861. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16862. "--"
  16863. ["New Heading" org-insert-heading t]
  16864. ("Navigate Headings"
  16865. ["Up" outline-up-heading t]
  16866. ["Next" outline-next-visible-heading t]
  16867. ["Previous" outline-previous-visible-heading t]
  16868. ["Next Same Level" outline-forward-same-level t]
  16869. ["Previous Same Level" outline-backward-same-level t]
  16870. "--"
  16871. ["Jump" org-goto t])
  16872. ("Edit Structure"
  16873. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16874. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16875. "--"
  16876. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16877. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16878. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16879. "--"
  16880. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16881. "--"
  16882. ["Copy visible text" org-copy-visible t]
  16883. "--"
  16884. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16885. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16886. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16887. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16888. "--"
  16889. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16890. "--"
  16891. ["Convert to odd levels" org-convert-to-odd-levels t]
  16892. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16893. ("Editing"
  16894. ["Emphasis..." org-emphasize t]
  16895. ["Edit Source Example" org-edit-special t]
  16896. "--"
  16897. ["Footnote new/jump" org-footnote-action t]
  16898. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16899. ("Archive"
  16900. ["Archive (default method)" org-archive-subtree-default t]
  16901. "--"
  16902. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16903. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16904. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16905. )
  16906. "--"
  16907. ("Hyperlinks"
  16908. ["Store Link (Global)" org-store-link t]
  16909. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16910. ["Insert Link" org-insert-link t]
  16911. ["Follow Link" org-open-at-point t]
  16912. "--"
  16913. ["Next link" org-next-link t]
  16914. ["Previous link" org-previous-link t]
  16915. "--"
  16916. ["Descriptive Links"
  16917. org-toggle-link-display
  16918. :style radio
  16919. :selected org-descriptive-links
  16920. ]
  16921. ["Literal Links"
  16922. org-toggle-link-display
  16923. :style radio
  16924. :selected (not org-descriptive-links)])
  16925. "--"
  16926. ("TODO Lists"
  16927. ["TODO/DONE/-" org-todo t]
  16928. ("Select keyword"
  16929. ["Next keyword" org-shiftright (org-on-heading-p)]
  16930. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16931. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16932. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16933. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16934. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16935. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16936. "--"
  16937. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16938. :selected org-enforce-todo-dependencies :style toggle :active t]
  16939. "Settings for tree at point"
  16940. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16941. :selected (org-entry-get nil "ORDERED")
  16942. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16943. ["Do Children parallel" org-toggle-ordered-property :style radio
  16944. :selected (not (org-entry-get nil "ORDERED"))
  16945. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16946. "--"
  16947. ["Set Priority" org-priority t]
  16948. ["Priority Up" org-shiftup t]
  16949. ["Priority Down" org-shiftdown t]
  16950. "--"
  16951. ["Get news from all feeds" org-feed-update-all t]
  16952. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16953. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16954. ("TAGS and Properties"
  16955. ["Set Tags" org-set-tags-command t]
  16956. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16957. "--"
  16958. ["Set property" org-set-property t]
  16959. ["Column view of properties" org-columns t]
  16960. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16961. ("Dates and Scheduling"
  16962. ["Timestamp" org-time-stamp t]
  16963. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16964. ("Change Date"
  16965. ["1 Day Later" org-shiftright t]
  16966. ["1 Day Earlier" org-shiftleft t]
  16967. ["1 ... Later" org-shiftup t]
  16968. ["1 ... Earlier" org-shiftdown t])
  16969. ["Compute Time Range" org-evaluate-time-range t]
  16970. ["Schedule Item" org-schedule t]
  16971. ["Deadline" org-deadline t]
  16972. "--"
  16973. ["Custom time format" org-toggle-time-stamp-overlays
  16974. :style radio :selected org-display-custom-times]
  16975. "--"
  16976. ["Goto Calendar" org-goto-calendar t]
  16977. ["Date from Calendar" org-date-from-calendar t]
  16978. "--"
  16979. ["Start/Restart Timer" org-timer-start t]
  16980. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16981. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16982. ["Insert Timer String" org-timer t]
  16983. ["Insert Timer Item" org-timer-item t])
  16984. ("Logging work"
  16985. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16986. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16987. ["Clock out" org-clock-out t]
  16988. ["Clock cancel" org-clock-cancel t]
  16989. "--"
  16990. ["Mark as default task" org-clock-mark-default-task t]
  16991. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16992. ["Goto running clock" org-clock-goto t]
  16993. "--"
  16994. ["Display times" org-clock-display t]
  16995. ["Create clock table" org-clock-report t]
  16996. "--"
  16997. ["Record DONE time"
  16998. (progn (setq org-log-done (not org-log-done))
  16999. (message "Switching to %s will %s record a timestamp"
  17000. (car org-done-keywords)
  17001. (if org-log-done "automatically" "not")))
  17002. :style toggle :selected org-log-done])
  17003. "--"
  17004. ["Agenda Command..." org-agenda t]
  17005. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17006. ("File List for Agenda")
  17007. ("Special views current file"
  17008. ["TODO Tree" org-show-todo-tree t]
  17009. ["Check Deadlines" org-check-deadlines t]
  17010. ["Timeline" org-timeline t]
  17011. ["Tags/Property tree" org-match-sparse-tree t])
  17012. "--"
  17013. ["Export/Publish..." org-export t]
  17014. ("LaTeX"
  17015. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17016. :selected org-cdlatex-mode]
  17017. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17018. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17019. ["Modify math symbol" org-cdlatex-math-modify
  17020. (org-inside-LaTeX-fragment-p)]
  17021. ["Insert citation" org-reftex-citation t]
  17022. "--"
  17023. ["Template for BEAMER" org-insert-beamer-options-template t])
  17024. "--"
  17025. ("MobileOrg"
  17026. ["Push Files and Views" org-mobile-push t]
  17027. ["Get Captured and Flagged" org-mobile-pull t]
  17028. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17029. "--"
  17030. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17031. "--"
  17032. ("Documentation"
  17033. ["Show Version" org-version t]
  17034. ["Info Documentation" org-info t])
  17035. ("Customize"
  17036. ["Browse Org Group" org-customize t]
  17037. "--"
  17038. ["Expand This Menu" org-create-customize-menu
  17039. (fboundp 'customize-menu-create)])
  17040. ["Send bug report" org-submit-bug-report t]
  17041. "--"
  17042. ("Refresh/Reload"
  17043. ["Refresh setup current buffer" org-mode-restart t]
  17044. ["Reload Org (after update)" org-reload t]
  17045. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17046. ))
  17047. (defun org-info (&optional node)
  17048. "Read documentation for Org-mode in the info system.
  17049. With optional NODE, go directly to that node."
  17050. (interactive)
  17051. (info (format "(org)%s" (or node ""))))
  17052. ;;;###autoload
  17053. (defun org-submit-bug-report ()
  17054. "Submit a bug report on Org-mode via mail.
  17055. Don't hesitate to report any problems or inaccurate documentation.
  17056. If you don't have setup sending mail from (X)Emacs, please copy the
  17057. output buffer into your mail program, as it gives us important
  17058. information about your Org-mode version and configuration."
  17059. (interactive)
  17060. (require 'reporter)
  17061. (org-load-modules-maybe)
  17062. (org-require-autoloaded-modules)
  17063. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17064. (reporter-submit-bug-report
  17065. "emacs-orgmode@gnu.org"
  17066. (org-version)
  17067. (let (list)
  17068. (save-window-excursion
  17069. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17070. (delete-other-windows)
  17071. (erase-buffer)
  17072. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17073. We would like to add your full Org-mode and Outline configuration to the
  17074. bug report. This greatly simplifies the work of the maintainer and
  17075. other experts on the mailing list.
  17076. HOWEVER, some variables you have customized may contain private
  17077. information. The names of customers, colleagues, or friends, might
  17078. appear in the form of file names, tags, todo states, or search strings.
  17079. If you answer yes to the prompt, you might want to check and remove
  17080. such private information before sending the email.")
  17081. (add-text-properties (point-min) (point-max) '(face org-warning))
  17082. (when (yes-or-no-p "Include your Org-mode configuration ")
  17083. (mapatoms
  17084. (lambda (v)
  17085. (and (boundp v)
  17086. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17087. (or (and (symbol-value v)
  17088. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17089. (and
  17090. (get v 'custom-type) (get v 'standard-value)
  17091. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17092. (push v list)))))
  17093. (kill-buffer (get-buffer "*Warn about privacy*"))
  17094. list))
  17095. nil nil
  17096. "Remember to cover the basics, that is, what you expected to happen and
  17097. what in fact did happen. You don't know how to make a good report? See
  17098. http://orgmode.org/manual/Feedback.html#Feedback
  17099. Your bug report will be posted to the Org-mode mailing list.
  17100. ------------------------------------------------------------------------")
  17101. (save-excursion
  17102. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17103. (replace-match "\\1Bug: \\3 [\\2]")))))
  17104. (defun org-install-agenda-files-menu ()
  17105. (let ((bl (buffer-list)))
  17106. (save-excursion
  17107. (while bl
  17108. (set-buffer (pop bl))
  17109. (if (org-mode-p) (setq bl nil)))
  17110. (when (org-mode-p)
  17111. (easy-menu-change
  17112. '("Org") "File List for Agenda"
  17113. (append
  17114. (list
  17115. ["Edit File List" (org-edit-agenda-file-list) t]
  17116. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17117. ["Remove Current File from List" org-remove-file t]
  17118. ["Cycle through agenda files" org-cycle-agenda-files t]
  17119. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17120. "--")
  17121. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17122. ;;;; Documentation
  17123. ;;;###autoload
  17124. (defun org-require-autoloaded-modules ()
  17125. (interactive)
  17126. (mapc 'require
  17127. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17128. org-docbook org-exp org-html org-icalendar
  17129. org-id org-latex
  17130. org-publish org-remember org-table
  17131. org-timer org-xoxo)))
  17132. ;;;###autoload
  17133. (defun org-reload (&optional uncompiled)
  17134. "Reload all org lisp files.
  17135. With prefix arg UNCOMPILED, load the uncompiled versions."
  17136. (interactive "P")
  17137. (require 'find-func)
  17138. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17139. (dir-org (file-name-directory (org-find-library-name "org")))
  17140. (dir-org-contrib (ignore-errors
  17141. (file-name-directory
  17142. (org-find-library-name "org-contribdir"))))
  17143. (babel-files
  17144. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17145. (append (list nil "comint" "eval" "exp" "keys"
  17146. "lob" "ref" "table" "tangle")
  17147. (delq nil
  17148. (mapcar
  17149. (lambda (lang)
  17150. (when (cdr lang) (symbol-name (car lang))))
  17151. org-babel-load-languages)))))
  17152. (files
  17153. (append (directory-files dir-org t file-re)
  17154. babel-files
  17155. (and dir-org-contrib
  17156. (directory-files dir-org-contrib t file-re))))
  17157. (remove-re (concat (if (featurep 'xemacs)
  17158. "org-colview" "org-colview-xemacs")
  17159. "\\'")))
  17160. (setq files (mapcar 'file-name-sans-extension files))
  17161. (setq files (mapcar
  17162. (lambda (x) (if (string-match remove-re x) nil x))
  17163. files))
  17164. (setq files (delq nil files))
  17165. (mapc
  17166. (lambda (f)
  17167. (when (featurep (intern (file-name-nondirectory f)))
  17168. (if (and (not uncompiled)
  17169. (file-exists-p (concat f ".elc")))
  17170. (load (concat f ".elc") nil nil t)
  17171. (load (concat f ".el") nil nil t))))
  17172. files))
  17173. (org-version))
  17174. ;;;###autoload
  17175. (defun org-customize ()
  17176. "Call the customize function with org as argument."
  17177. (interactive)
  17178. (org-load-modules-maybe)
  17179. (org-require-autoloaded-modules)
  17180. (customize-browse 'org))
  17181. (defun org-create-customize-menu ()
  17182. "Create a full customization menu for Org-mode, insert it into the menu."
  17183. (interactive)
  17184. (org-load-modules-maybe)
  17185. (org-require-autoloaded-modules)
  17186. (if (fboundp 'customize-menu-create)
  17187. (progn
  17188. (easy-menu-change
  17189. '("Org") "Customize"
  17190. `(["Browse Org group" org-customize t]
  17191. "--"
  17192. ,(customize-menu-create 'org)
  17193. ["Set" Custom-set t]
  17194. ["Save" Custom-save t]
  17195. ["Reset to Current" Custom-reset-current t]
  17196. ["Reset to Saved" Custom-reset-saved t]
  17197. ["Reset to Standard Settings" Custom-reset-standard t]))
  17198. (message "\"Org\"-menu now contains full customization menu"))
  17199. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17200. ;;;; Miscellaneous stuff
  17201. ;;; Generally useful functions
  17202. (defun org-get-at-bol (property)
  17203. "Get text property PROPERTY at beginning of line."
  17204. (get-text-property (point-at-bol) property))
  17205. (defun org-find-text-property-in-string (prop s)
  17206. "Return the first non-nil value of property PROP in string S."
  17207. (or (get-text-property 0 prop s)
  17208. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17209. prop s)))
  17210. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17211. "Display the given MESSAGE as a warning."
  17212. (if (fboundp 'display-warning)
  17213. (display-warning 'org message
  17214. (if (featurep 'xemacs) 'warning :warning))
  17215. (let ((buf (get-buffer-create "*Org warnings*")))
  17216. (with-current-buffer buf
  17217. (goto-char (point-max))
  17218. (insert "Warning (Org): " message)
  17219. (unless (bolp)
  17220. (newline)))
  17221. (display-buffer buf)
  17222. (sit-for 0))))
  17223. (defun org-eval (form)
  17224. "Eval FORM and return result."
  17225. (condition-case error
  17226. (eval form)
  17227. (error (format "%%![Error: %s]" error))))
  17228. (defun org-in-commented-line ()
  17229. "Is point in a line starting with `#'?"
  17230. (equal (char-after (point-at-bol)) ?#))
  17231. (defun org-in-indented-comment-line ()
  17232. "Is point in a line starting with `#' after some white space?"
  17233. (save-excursion
  17234. (save-match-data
  17235. (goto-char (point-at-bol))
  17236. (looking-at "[ \t]*#"))))
  17237. (defun org-in-verbatim-emphasis ()
  17238. (save-match-data
  17239. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17240. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17241. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17242. (if (and marker (marker-buffer marker)
  17243. (buffer-live-p (marker-buffer marker)))
  17244. (progn
  17245. (org-pop-to-buffer-same-window (marker-buffer marker))
  17246. (if (or (> marker (point-max)) (< marker (point-min)))
  17247. (widen))
  17248. (goto-char marker)
  17249. (org-show-context 'org-goto))
  17250. (if bookmark
  17251. (bookmark-jump bookmark)
  17252. (error "Cannot find location"))))
  17253. (defun org-quote-csv-field (s)
  17254. "Quote field for inclusion in CSV material."
  17255. (if (string-match "[\",]" s)
  17256. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17257. s))
  17258. (defun org-force-self-insert (N)
  17259. "Needed to enforce self-insert under remapping."
  17260. (interactive "p")
  17261. (self-insert-command N))
  17262. (defun org-string-width (s)
  17263. "Compute width of string, ignoring invisible characters.
  17264. This ignores character with invisibility property `org-link', and also
  17265. characters with property `org-cwidth', because these will become invisible
  17266. upon the next fontification round."
  17267. (let (b l)
  17268. (when (or (eq t buffer-invisibility-spec)
  17269. (assq 'org-link buffer-invisibility-spec))
  17270. (while (setq b (text-property-any 0 (length s)
  17271. 'invisible 'org-link s))
  17272. (setq s (concat (substring s 0 b)
  17273. (substring s (or (next-single-property-change
  17274. b 'invisible s) (length s)))))))
  17275. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17276. (setq s (concat (substring s 0 b)
  17277. (substring s (or (next-single-property-change
  17278. b 'org-cwidth s) (length s))))))
  17279. (setq l (string-width s) b -1)
  17280. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17281. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17282. l))
  17283. (defun org-shorten-string (s maxlength)
  17284. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17285. If the string is shorter or has length MAXLENGTH, just return the
  17286. original string. If it is longer, the functions finds a space in the
  17287. string, breaks this string off at that locations and adds three dots
  17288. as ellipsis. Including the ellipsis, the string will not be longer
  17289. than MAXLENGTH. If finding a good breaking point in the string does
  17290. not work, the string is just chopped off in the middle of a word
  17291. if necessary."
  17292. (if (<= (length s) maxlength)
  17293. s
  17294. (let* ((n (max (- maxlength 4) 1))
  17295. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17296. (if (string-match re s)
  17297. (concat (match-string 1 s) "...")
  17298. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17299. (defun org-get-indentation (&optional line)
  17300. "Get the indentation of the current line, interpreting tabs.
  17301. When LINE is given, assume it represents a line and compute its indentation."
  17302. (if line
  17303. (if (string-match "^ *" (org-remove-tabs line))
  17304. (match-end 0))
  17305. (save-excursion
  17306. (beginning-of-line 1)
  17307. (skip-chars-forward " \t")
  17308. (current-column))))
  17309. (defun org-get-string-indentation (s)
  17310. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17311. (let ((n -1) (i 0) (w tab-width) c)
  17312. (catch 'exit
  17313. (while (< (setq n (1+ n)) (length s))
  17314. (setq c (aref s n))
  17315. (cond ((= c ?\ ) (setq i (1+ i)))
  17316. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17317. (t (throw 'exit t)))))
  17318. i))
  17319. (defun org-remove-tabs (s &optional width)
  17320. "Replace tabulators in S with spaces.
  17321. Assumes that s is a single line, starting in column 0."
  17322. (setq width (or width tab-width))
  17323. (while (string-match "\t" s)
  17324. (setq s (replace-match
  17325. (make-string
  17326. (- (* width (/ (+ (match-beginning 0) width) width))
  17327. (match-beginning 0)) ?\ )
  17328. t t s)))
  17329. s)
  17330. (defun org-fix-indentation (line ind)
  17331. "Fix indentation in LINE.
  17332. IND is a cons cell with target and minimum indentation.
  17333. If the current indentation in LINE is smaller than the minimum,
  17334. leave it alone. If it is larger than ind, set it to the target."
  17335. (let* ((l (org-remove-tabs line))
  17336. (i (org-get-indentation l))
  17337. (i1 (car ind)) (i2 (cdr ind)))
  17338. (if (>= i i2) (setq l (substring line i2)))
  17339. (if (> i1 0)
  17340. (concat (make-string i1 ?\ ) l)
  17341. l)))
  17342. (defun org-remove-indentation (code &optional n)
  17343. "Remove the maximum common indentation from the lines in CODE.
  17344. N may optionally be the number of spaces to remove."
  17345. (with-temp-buffer
  17346. (insert code)
  17347. (org-do-remove-indentation n)
  17348. (buffer-string)))
  17349. (defun org-do-remove-indentation (&optional n)
  17350. "Remove the maximum common indentation from the buffer."
  17351. (untabify (point-min) (point-max))
  17352. (let ((min 10000) re)
  17353. (if n
  17354. (setq min n)
  17355. (goto-char (point-min))
  17356. (while (re-search-forward "^ *[^ \n]" nil t)
  17357. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17358. (unless (or (= min 0) (= min 10000))
  17359. (setq re (format "^ \\{%d\\}" min))
  17360. (goto-char (point-min))
  17361. (while (re-search-forward re nil t)
  17362. (replace-match "")
  17363. (end-of-line 1))
  17364. min)))
  17365. (defun org-fill-template (template alist)
  17366. "Find each %key of ALIST in TEMPLATE and replace it."
  17367. (let ((case-fold-search nil)
  17368. entry key value)
  17369. (setq alist (sort (copy-sequence alist)
  17370. (lambda (a b) (< (length (car a)) (length (car b))))))
  17371. (while (setq entry (pop alist))
  17372. (setq template
  17373. (replace-regexp-in-string
  17374. (concat "%" (regexp-quote (car entry)))
  17375. (cdr entry) template t t)))
  17376. template))
  17377. (defun org-base-buffer (buffer)
  17378. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17379. (if (not buffer)
  17380. buffer
  17381. (or (buffer-base-buffer buffer)
  17382. buffer)))
  17383. (defun org-trim (s)
  17384. "Remove whitespace at beginning and end of string."
  17385. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17386. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17387. s)
  17388. (defun org-wrap (string &optional width lines)
  17389. "Wrap string to either a number of lines, or a width in characters.
  17390. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17391. that costs. If there is a word longer than WIDTH, the text is actually
  17392. wrapped to the length of that word.
  17393. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17394. many lines, whatever width that takes.
  17395. The return value is a list of lines, without newlines at the end."
  17396. (let* ((words (org-split-string string "[ \t\n]+"))
  17397. (maxword (apply 'max (mapcar 'org-string-width words)))
  17398. w ll)
  17399. (cond (width
  17400. (org-do-wrap words (max maxword width)))
  17401. (lines
  17402. (setq w maxword)
  17403. (setq ll (org-do-wrap words maxword))
  17404. (if (<= (length ll) lines)
  17405. ll
  17406. (setq ll words)
  17407. (while (> (length ll) lines)
  17408. (setq w (1+ w))
  17409. (setq ll (org-do-wrap words w)))
  17410. ll))
  17411. (t (error "Cannot wrap this")))))
  17412. (defun org-do-wrap (words width)
  17413. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17414. (let (lines line)
  17415. (while words
  17416. (setq line (pop words))
  17417. (while (and words (< (+ (length line) (length (car words))) width))
  17418. (setq line (concat line " " (pop words))))
  17419. (setq lines (push line lines)))
  17420. (nreverse lines)))
  17421. (defun org-split-string (string &optional separators)
  17422. "Splits STRING into substrings at SEPARATORS.
  17423. No empty strings are returned if there are matches at the beginning
  17424. and end of string."
  17425. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17426. (start 0)
  17427. notfirst
  17428. (list nil))
  17429. (while (and (string-match rexp string
  17430. (if (and notfirst
  17431. (= start (match-beginning 0))
  17432. (< start (length string)))
  17433. (1+ start) start))
  17434. (< (match-beginning 0) (length string)))
  17435. (setq notfirst t)
  17436. (or (eq (match-beginning 0) 0)
  17437. (and (eq (match-beginning 0) (match-end 0))
  17438. (eq (match-beginning 0) start))
  17439. (setq list
  17440. (cons (substring string start (match-beginning 0))
  17441. list)))
  17442. (setq start (match-end 0)))
  17443. (or (eq start (length string))
  17444. (setq list
  17445. (cons (substring string start)
  17446. list)))
  17447. (nreverse list)))
  17448. (defun org-quote-vert (s)
  17449. "Replace \"|\" with \"\\vert\"."
  17450. (while (string-match "|" s)
  17451. (setq s (replace-match "\\vert" t t s)))
  17452. s)
  17453. (defun org-uuidgen-p (s)
  17454. "Is S an ID created by UUIDGEN?"
  17455. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17456. (defun org-context ()
  17457. "Return a list of contexts of the current cursor position.
  17458. If several contexts apply, all are returned.
  17459. Each context entry is a list with a symbol naming the context, and
  17460. two positions indicating start and end of the context. Possible
  17461. contexts are:
  17462. :headline anywhere in a headline
  17463. :headline-stars on the leading stars in a headline
  17464. :todo-keyword on a TODO keyword (including DONE) in a headline
  17465. :tags on the TAGS in a headline
  17466. :priority on the priority cookie in a headline
  17467. :item on the first line of a plain list item
  17468. :item-bullet on the bullet/number of a plain list item
  17469. :checkbox on the checkbox in a plain list item
  17470. :table in an org-mode table
  17471. :table-special on a special filed in a table
  17472. :table-table in a table.el table
  17473. :link on a hyperlink
  17474. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17475. :target on a <<target>>
  17476. :radio-target on a <<<radio-target>>>
  17477. :latex-fragment on a LaTeX fragment
  17478. :latex-preview on a LaTeX fragment with overlayed preview image
  17479. This function expects the position to be visible because it uses font-lock
  17480. faces as a help to recognize the following contexts: :table-special, :link,
  17481. and :keyword."
  17482. (let* ((f (get-text-property (point) 'face))
  17483. (faces (if (listp f) f (list f)))
  17484. (p (point)) clist o)
  17485. ;; First the large context
  17486. (cond
  17487. ((org-on-heading-p t)
  17488. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17489. (when (progn
  17490. (beginning-of-line 1)
  17491. (looking-at org-todo-line-tags-regexp))
  17492. (push (org-point-in-group p 1 :headline-stars) clist)
  17493. (push (org-point-in-group p 2 :todo-keyword) clist)
  17494. (push (org-point-in-group p 4 :tags) clist))
  17495. (goto-char p)
  17496. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17497. (if (looking-at "\\[#[A-Z0-9]\\]")
  17498. (push (org-point-in-group p 0 :priority) clist)))
  17499. ((org-at-item-p)
  17500. (push (org-point-in-group p 2 :item-bullet) clist)
  17501. (push (list :item (point-at-bol)
  17502. (save-excursion (org-end-of-item) (point)))
  17503. clist)
  17504. (and (org-at-item-checkbox-p)
  17505. (push (org-point-in-group p 0 :checkbox) clist)))
  17506. ((org-at-table-p)
  17507. (push (list :table (org-table-begin) (org-table-end)) clist)
  17508. (if (memq 'org-formula faces)
  17509. (push (list :table-special
  17510. (previous-single-property-change p 'face)
  17511. (next-single-property-change p 'face)) clist)))
  17512. ((org-at-table-p 'any)
  17513. (push (list :table-table) clist)))
  17514. (goto-char p)
  17515. ;; Now the small context
  17516. (cond
  17517. ((org-at-timestamp-p)
  17518. (push (org-point-in-group p 0 :timestamp) clist))
  17519. ((memq 'org-link faces)
  17520. (push (list :link
  17521. (previous-single-property-change p 'face)
  17522. (next-single-property-change p 'face)) clist))
  17523. ((memq 'org-special-keyword faces)
  17524. (push (list :keyword
  17525. (previous-single-property-change p 'face)
  17526. (next-single-property-change p 'face)) clist))
  17527. ((org-on-target-p)
  17528. (push (org-point-in-group p 0 :target) clist)
  17529. (goto-char (1- (match-beginning 0)))
  17530. (if (looking-at org-radio-target-regexp)
  17531. (push (org-point-in-group p 0 :radio-target) clist))
  17532. (goto-char p))
  17533. ((setq o (car (delq nil
  17534. (mapcar
  17535. (lambda (x)
  17536. (if (memq x org-latex-fragment-image-overlays) x))
  17537. (overlays-at (point))))))
  17538. (push (list :latex-fragment
  17539. (overlay-start o) (overlay-end o)) clist)
  17540. (push (list :latex-preview
  17541. (overlay-start o) (overlay-end o)) clist))
  17542. ((org-inside-LaTeX-fragment-p)
  17543. ;; FIXME: positions wrong.
  17544. (push (list :latex-fragment (point) (point)) clist)))
  17545. (setq clist (nreverse (delq nil clist)))
  17546. clist))
  17547. ;; FIXME: Compare with at-regexp-p Do we need both?
  17548. (defun org-in-regexp (re &optional nlines visually)
  17549. "Check if point is inside a match of regexp.
  17550. Normally only the current line is checked, but you can include NLINES extra
  17551. lines both before and after point into the search.
  17552. If VISUALLY is set, require that the cursor is not after the match but
  17553. really on, so that the block visually is on the match."
  17554. (catch 'exit
  17555. (let ((pos (point))
  17556. (eol (point-at-eol (+ 1 (or nlines 0))))
  17557. (inc (if visually 1 0)))
  17558. (save-excursion
  17559. (beginning-of-line (- 1 (or nlines 0)))
  17560. (while (re-search-forward re eol t)
  17561. (if (and (<= (match-beginning 0) pos)
  17562. (>= (+ inc (match-end 0)) pos))
  17563. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17564. (defun org-at-regexp-p (regexp)
  17565. "Is point inside a match of REGEXP in the current line?"
  17566. (catch 'exit
  17567. (save-excursion
  17568. (let ((pos (point)) (end (point-at-eol)))
  17569. (beginning-of-line 1)
  17570. (while (re-search-forward regexp end t)
  17571. (if (and (<= (match-beginning 0) pos)
  17572. (>= (match-end 0) pos))
  17573. (throw 'exit t)))
  17574. nil))))
  17575. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  17576. "Non-nil when point is between matches of START-RE and END-RE.
  17577. Also return a non-nil value when point is on one of the matches.
  17578. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  17579. buffer positions. Default values are the positions of headlines
  17580. surrounding the point.
  17581. The functions returns a cons cell whose car (resp. cdr) is the
  17582. position before START-RE (resp. after END-RE)."
  17583. (save-match-data
  17584. (let ((pos (point))
  17585. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  17586. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  17587. beg end)
  17588. (save-excursion
  17589. ;; Point is on a block when on START-RE or if START-RE can be
  17590. ;; found before it...
  17591. (and (or (org-at-regexp-p start-re)
  17592. (re-search-backward start-re limit-up t))
  17593. (setq beg (match-beginning 0))
  17594. ;; ... and END-RE after it...
  17595. (goto-char (match-end 0))
  17596. (re-search-forward end-re limit-down t)
  17597. (> (setq end (match-end 0)) pos)
  17598. ;; ... without another START-RE in-between.
  17599. (goto-char (match-beginning 0))
  17600. (not (re-search-backward start-re (1+ beg) t))
  17601. ;; Return value.
  17602. (cons beg end))))))
  17603. (defun org-in-block-p (names)
  17604. "Is point inside any block whose name belongs to NAMES?
  17605. NAMES is a list of strings containing names of blocks."
  17606. (save-match-data
  17607. (catch 'exit
  17608. (let ((case-fold-search t)
  17609. (lim-up (save-excursion (outline-previous-heading)))
  17610. (lim-down (save-excursion (outline-next-heading))))
  17611. (mapc (lambda (name)
  17612. (let ((n (regexp-quote name)))
  17613. (when (org-between-regexps-p
  17614. (concat "^[ \t]*#\\+begin_" n)
  17615. (concat "^[ \t]*#\\+end_" n)
  17616. lim-up lim-down)
  17617. (throw 'exit t))))
  17618. names))
  17619. nil)))
  17620. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17621. "Call `multi-occur' with buffers for all agenda files."
  17622. (interactive "sOrg-files matching: \np")
  17623. (let* ((files (org-agenda-files))
  17624. (tnames (mapcar 'file-truename files))
  17625. (extra org-agenda-text-search-extra-files)
  17626. f)
  17627. (when (eq (car extra) 'agenda-archives)
  17628. (setq extra (cdr extra))
  17629. (setq files (org-add-archive-files files)))
  17630. (while (setq f (pop extra))
  17631. (unless (member (file-truename f) tnames)
  17632. (add-to-list 'files f 'append)
  17633. (add-to-list 'tnames (file-truename f) 'append)))
  17634. (multi-occur
  17635. (mapcar (lambda (x)
  17636. (with-current-buffer
  17637. (or (get-file-buffer x) (find-file-noselect x))
  17638. (widen)
  17639. (current-buffer)))
  17640. files)
  17641. regexp)))
  17642. (if (boundp 'occur-mode-find-occurrence-hook)
  17643. ;; Emacs 23
  17644. (add-hook 'occur-mode-find-occurrence-hook
  17645. (lambda ()
  17646. (when (org-mode-p)
  17647. (org-reveal))))
  17648. ;; Emacs 22
  17649. (defadvice occur-mode-goto-occurrence
  17650. (after org-occur-reveal activate)
  17651. (and (org-mode-p) (org-reveal)))
  17652. (defadvice occur-mode-goto-occurrence-other-window
  17653. (after org-occur-reveal activate)
  17654. (and (org-mode-p) (org-reveal)))
  17655. (defadvice occur-mode-display-occurrence
  17656. (after org-occur-reveal activate)
  17657. (when (org-mode-p)
  17658. (let ((pos (occur-mode-find-occurrence)))
  17659. (with-current-buffer (marker-buffer pos)
  17660. (save-excursion
  17661. (goto-char pos)
  17662. (org-reveal)))))))
  17663. (defun org-occur-link-in-agenda-files ()
  17664. "Create a link and search for it in the agendas.
  17665. The link is not stored in `org-stored-links', it is just created
  17666. for the search purpose."
  17667. (interactive)
  17668. (let ((link (condition-case nil
  17669. (org-store-link nil)
  17670. (error "Unable to create a link to here"))))
  17671. (org-occur-in-agenda-files (regexp-quote link))))
  17672. (defun org-uniquify (list)
  17673. "Remove duplicate elements from LIST."
  17674. (let (res)
  17675. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  17676. res))
  17677. (defun org-delete-all (elts list)
  17678. "Remove all elements in ELTS from LIST."
  17679. (while elts
  17680. (setq list (delete (pop elts) list)))
  17681. list)
  17682. (defun org-count (cl-item cl-seq)
  17683. "Count the number of occurrences of ITEM in SEQ.
  17684. Taken from `count' in cl-seq.el with all keyword arguments removed."
  17685. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  17686. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  17687. (while (< cl-start cl-end)
  17688. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  17689. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  17690. (setq cl-start (1+ cl-start)))
  17691. cl-count))
  17692. (defun org-remove-if (predicate seq)
  17693. "Remove everything from SEQ that fulfills PREDICATE."
  17694. (let (res e)
  17695. (while seq
  17696. (setq e (pop seq))
  17697. (if (not (funcall predicate e)) (push e res)))
  17698. (nreverse res)))
  17699. (defun org-remove-if-not (predicate seq)
  17700. "Remove everything from SEQ that does not fulfill PREDICATE."
  17701. (let (res e)
  17702. (while seq
  17703. (setq e (pop seq))
  17704. (if (funcall predicate e) (push e res)))
  17705. (nreverse res)))
  17706. (defun org-back-over-empty-lines ()
  17707. "Move backwards over whitespace, to the beginning of the first empty line.
  17708. Returns the number of empty lines passed."
  17709. (let ((pos (point)))
  17710. (if (cdr (assoc 'heading org-blank-before-new-entry))
  17711. (skip-chars-backward " \t\n\r")
  17712. (unless (eobp)
  17713. (forward-line -1)))
  17714. (beginning-of-line 2)
  17715. (goto-char (min (point) pos))
  17716. (count-lines (point) pos)))
  17717. (defun org-skip-whitespace ()
  17718. (skip-chars-forward " \t\n\r"))
  17719. (defun org-point-in-group (point group &optional context)
  17720. "Check if POINT is in match-group GROUP.
  17721. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  17722. match. If the match group does not exist or point is not inside it,
  17723. return nil."
  17724. (and (match-beginning group)
  17725. (>= point (match-beginning group))
  17726. (<= point (match-end group))
  17727. (if context
  17728. (list context (match-beginning group) (match-end group))
  17729. t)))
  17730. (defun org-switch-to-buffer-other-window (&rest args)
  17731. "Switch to buffer in a second window on the current frame.
  17732. In particular, do not allow pop-up frames.
  17733. Returns the newly created buffer."
  17734. (let (pop-up-frames special-display-buffer-names special-display-regexps
  17735. special-display-function)
  17736. (apply 'switch-to-buffer-other-window args)))
  17737. (defun org-combine-plists (&rest plists)
  17738. "Create a single property list from all plists in PLISTS.
  17739. The process starts by copying the first list, and then setting properties
  17740. from the other lists. Settings in the last list are the most significant
  17741. ones and overrule settings in the other lists."
  17742. (let ((rtn (copy-sequence (pop plists)))
  17743. p v ls)
  17744. (while plists
  17745. (setq ls (pop plists))
  17746. (while ls
  17747. (setq p (pop ls) v (pop ls))
  17748. (setq rtn (plist-put rtn p v))))
  17749. rtn))
  17750. (defun org-move-line-down (arg)
  17751. "Move the current line down. With prefix argument, move it past ARG lines."
  17752. (interactive "p")
  17753. (let ((col (current-column))
  17754. beg end pos)
  17755. (beginning-of-line 1) (setq beg (point))
  17756. (beginning-of-line 2) (setq end (point))
  17757. (beginning-of-line (+ 1 arg))
  17758. (setq pos (move-marker (make-marker) (point)))
  17759. (insert (delete-and-extract-region beg end))
  17760. (goto-char pos)
  17761. (org-move-to-column col)))
  17762. (defun org-move-line-up (arg)
  17763. "Move the current line up. With prefix argument, move it past ARG lines."
  17764. (interactive "p")
  17765. (let ((col (current-column))
  17766. beg end pos)
  17767. (beginning-of-line 1) (setq beg (point))
  17768. (beginning-of-line 2) (setq end (point))
  17769. (beginning-of-line (- arg))
  17770. (setq pos (move-marker (make-marker) (point)))
  17771. (insert (delete-and-extract-region beg end))
  17772. (goto-char pos)
  17773. (org-move-to-column col)))
  17774. (defun org-replace-escapes (string table)
  17775. "Replace %-escapes in STRING with values in TABLE.
  17776. TABLE is an association list with keys like \"%a\" and string values.
  17777. The sequences in STRING may contain normal field width and padding information,
  17778. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  17779. so values can contain further %-escapes if they are define later in TABLE."
  17780. (let ((tbl (copy-alist table))
  17781. (case-fold-search nil)
  17782. (pchg 0)
  17783. e re rpl)
  17784. (while (setq e (pop tbl))
  17785. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  17786. (when (and (cdr e) (string-match re (cdr e)))
  17787. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  17788. (safe "SREF"))
  17789. (add-text-properties 0 3 (list 'sref sref) safe)
  17790. (setcdr e (replace-match safe t t (cdr e)))))
  17791. (while (string-match re string)
  17792. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  17793. (cdr e)))
  17794. (setq string (replace-match rpl t t string))))
  17795. (while (setq pchg (next-property-change pchg string))
  17796. (let ((sref (get-text-property pchg 'sref string)))
  17797. (when (and sref (string-match "SREF" string pchg))
  17798. (setq string (replace-match sref t t string)))))
  17799. string))
  17800. (defun org-sublist (list start end)
  17801. "Return a section of LIST, from START to END.
  17802. Counting starts at 1."
  17803. (let (rtn (c start))
  17804. (setq list (nthcdr (1- start) list))
  17805. (while (and list (<= c end))
  17806. (push (pop list) rtn)
  17807. (setq c (1+ c)))
  17808. (nreverse rtn)))
  17809. (defun org-find-base-buffer-visiting (file)
  17810. "Like `find-buffer-visiting' but always return the base buffer and
  17811. not an indirect buffer."
  17812. (let ((buf (or (get-file-buffer file)
  17813. (find-buffer-visiting file))))
  17814. (if buf
  17815. (or (buffer-base-buffer buf) buf)
  17816. nil)))
  17817. (defun org-image-file-name-regexp (&optional extensions)
  17818. "Return regexp matching the file names of images.
  17819. If EXTENSIONS is given, only match these."
  17820. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  17821. (image-file-name-regexp)
  17822. (let ((image-file-name-extensions
  17823. (or extensions
  17824. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17825. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  17826. (concat "\\."
  17827. (regexp-opt (nconc (mapcar 'upcase
  17828. image-file-name-extensions)
  17829. image-file-name-extensions)
  17830. t)
  17831. "\\'"))))
  17832. (defun org-file-image-p (file &optional extensions)
  17833. "Return non-nil if FILE is an image."
  17834. (save-match-data
  17835. (string-match (org-image-file-name-regexp extensions) file)))
  17836. (defun org-get-cursor-date ()
  17837. "Return the date at cursor in as a time.
  17838. This works in the calendar and in the agenda, anywhere else it just
  17839. returns the current time."
  17840. (let (date day defd)
  17841. (cond
  17842. ((eq major-mode 'calendar-mode)
  17843. (setq date (calendar-cursor-to-date)
  17844. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17845. ((eq major-mode 'org-agenda-mode)
  17846. (setq day (get-text-property (point) 'day))
  17847. (if day
  17848. (setq date (calendar-gregorian-from-absolute day)
  17849. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17850. (nth 2 date))))))
  17851. (or defd (current-time))))
  17852. (defvar org-agenda-action-marker (make-marker)
  17853. "Marker pointing to the entry for the next agenda action.")
  17854. (defun org-mark-entry-for-agenda-action ()
  17855. "Mark the current entry as target of an agenda action.
  17856. Agenda actions are actions executed from the agenda with the key `k',
  17857. which make use of the date at the cursor."
  17858. (interactive)
  17859. (move-marker org-agenda-action-marker
  17860. (save-excursion (org-back-to-heading t) (point))
  17861. (current-buffer))
  17862. (message
  17863. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17864. (defun org-mark-subtree ()
  17865. "Mark the current subtree.
  17866. This puts point at the start of the current subtree, and mark at the end.
  17867. If point is in an inline task, mark that task instead."
  17868. (interactive)
  17869. (let ((inline-task-p
  17870. (and (featurep 'org-inlinetask)
  17871. (org-inlinetask-in-task-p)))
  17872. (beg))
  17873. ;; Get beginning of subtree
  17874. (cond
  17875. (inline-task-p (org-inlinetask-goto-beginning))
  17876. ((org-at-heading-p) (beginning-of-line))
  17877. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  17878. (setq beg (point))
  17879. ;; Get end of it
  17880. (if inline-task-p
  17881. (org-inlinetask-goto-end)
  17882. (org-end-of-subtree))
  17883. ;; Mark zone
  17884. (push-mark (point) nil t)
  17885. (goto-char beg)))
  17886. ;;; Paragraph filling stuff.
  17887. ;; We want this to be just right, so use the full arsenal.
  17888. (defun org-indent-line-function ()
  17889. "Indent line depending on context."
  17890. (interactive)
  17891. (let* ((pos (point))
  17892. (itemp (org-at-item-p))
  17893. (case-fold-search t)
  17894. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17895. (inline-task-p (and (featurep 'org-inlinetask)
  17896. (org-inlinetask-in-task-p)))
  17897. (inline-re (and inline-task-p
  17898. (org-inlinetask-outline-regexp)))
  17899. column)
  17900. (beginning-of-line 1)
  17901. (cond
  17902. ;; Comments
  17903. ((looking-at "# ") (setq column 0))
  17904. ;; Headings
  17905. ((looking-at org-outline-regexp) (setq column 0))
  17906. ;; Included files
  17907. ((looking-at "#\\+include:") (setq column 0))
  17908. ;; Footnote definition
  17909. ((looking-at org-footnote-definition-re) (setq column 0))
  17910. ;; Literal examples
  17911. ((looking-at "[ \t]*:\\( \\|$\\)")
  17912. (setq column (org-get-indentation))) ; do nothing
  17913. ;; Lists
  17914. ((ignore-errors (goto-char (org-in-item-p)))
  17915. (setq column (if itemp
  17916. (org-get-indentation)
  17917. (org-list-item-body-column (point))))
  17918. (goto-char pos))
  17919. ;; Drawers
  17920. ((and (looking-at "[ \t]*:END:")
  17921. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17922. (save-excursion
  17923. (goto-char (1- (match-beginning 1)))
  17924. (setq column (current-column))))
  17925. ;; Special blocks
  17926. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17927. (save-excursion
  17928. (re-search-backward
  17929. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17930. (setq column (org-get-indentation (match-string 0))))
  17931. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17932. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17933. (save-excursion
  17934. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17935. (setq column
  17936. (cond ((equal (downcase (match-string 1)) "src")
  17937. ;; src blocks: let `org-edit-src-exit' handle them
  17938. (org-get-indentation))
  17939. ((equal (downcase (match-string 1)) "example")
  17940. (max (org-get-indentation)
  17941. (org-get-indentation (match-string 0))))
  17942. (t
  17943. (org-get-indentation (match-string 0))))))
  17944. ;; This line has nothing special, look at the previous relevant
  17945. ;; line to compute indentation
  17946. (t
  17947. (beginning-of-line 0)
  17948. (while (and (not (bobp))
  17949. (not (looking-at org-drawer-regexp))
  17950. ;; When point started in an inline task, do not move
  17951. ;; above task starting line.
  17952. (not (and inline-task-p (looking-at inline-re)))
  17953. ;; Skip drawers, blocks, empty lines, verbatim,
  17954. ;; comments, tables, footnotes definitions, lists,
  17955. ;; inline tasks.
  17956. (or (and (looking-at "[ \t]*:END:")
  17957. (re-search-backward org-drawer-regexp nil t))
  17958. (and (looking-at "[ \t]*#\\+end_")
  17959. (re-search-backward "[ \t]*#\\+begin_"nil t))
  17960. (looking-at "[ \t]*[\n:#|]")
  17961. (looking-at org-footnote-definition-re)
  17962. (and (ignore-errors (goto-char (org-in-item-p)))
  17963. (goto-char
  17964. (org-list-get-top-point (org-list-struct))))
  17965. (and (not inline-task-p)
  17966. (featurep 'org-inlinetask)
  17967. (org-inlinetask-in-task-p)
  17968. (or (org-inlinetask-goto-beginning) t))))
  17969. (beginning-of-line 0))
  17970. (cond
  17971. ;; There was an heading above.
  17972. ((looking-at "\\*+[ \t]+")
  17973. (if (not org-adapt-indentation)
  17974. (setq column 0)
  17975. (goto-char (match-end 0))
  17976. (setq column (current-column))))
  17977. ;; A drawer had started and is unfinished
  17978. ((looking-at org-drawer-regexp)
  17979. (goto-char (1- (match-beginning 1)))
  17980. (setq column (current-column)))
  17981. ;; Else, nothing noticeable found: get indentation and go on.
  17982. (t (setq column (org-get-indentation))))))
  17983. ;; Now apply indentation and move cursor accordingly
  17984. (goto-char pos)
  17985. (if (<= (current-column) (current-indentation))
  17986. (org-indent-line-to column)
  17987. (save-excursion (org-indent-line-to column)))
  17988. ;; Special polishing for properties, see `org-property-format'
  17989. (setq column (current-column))
  17990. (beginning-of-line 1)
  17991. (if (looking-at
  17992. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17993. (replace-match (concat (match-string 1)
  17994. (format org-property-format
  17995. (match-string 2) (match-string 3)))
  17996. t t))
  17997. (org-move-to-column column)))
  17998. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17999. "Variable to store copy of `adaptive-fill-regexp'.
  18000. Since `adaptive-fill-regexp' is set to never match, we need to
  18001. store a backup of its value before entering `org-mode' so that
  18002. the functionality can be provided as a fall-back.")
  18003. (defun org-set-autofill-regexps ()
  18004. (interactive)
  18005. ;; In the paragraph separator we include headlines, because filling
  18006. ;; text in a line directly attached to a headline would otherwise
  18007. ;; fill the headline as well.
  18008. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18009. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18010. ;; The paragraph starter includes hand-formatted lists.
  18011. (org-set-local
  18012. 'paragraph-start
  18013. (concat
  18014. "\f" "\\|"
  18015. "[ ]*$" "\\|"
  18016. org-outline-regexp "\\|"
  18017. "[ \t]*#" "\\|"
  18018. (org-item-re) "\\|"
  18019. "[ \t]*[:|]" "\\|"
  18020. "\\$\\$" "\\|"
  18021. "\\\\\\(begin\\|end\\|[][]\\)"))
  18022. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18023. ;; But only if the user has not turned off tables or fixed-width regions
  18024. (org-set-local
  18025. 'auto-fill-inhibit-regexp
  18026. (concat org-outline-regexp
  18027. "\\|#\\+"
  18028. "\\|[ \t]*" org-keyword-time-regexp
  18029. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18030. (concat
  18031. "\\|[ \t]*["
  18032. (if org-enable-table-editor "|" "")
  18033. (if org-enable-fixed-width-editor ":" "")
  18034. "]"))))
  18035. ;; We use our own fill-paragraph function, to make sure that tables
  18036. ;; and fixed-width regions are not wrapped. That function will pass
  18037. ;; through to `fill-paragraph' when appropriate.
  18038. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18039. ;; Prevent auto-fill from inserting unwanted new items.
  18040. (org-set-local 'fill-nobreak-predicate
  18041. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  18042. fill-nobreak-predicate
  18043. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate)))
  18044. ;; Adaptive filling: To get full control, first make sure that
  18045. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18046. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18047. (org-set-local 'org-adaptive-fill-regexp-backup
  18048. adaptive-fill-regexp))
  18049. (org-set-local 'adaptive-fill-regexp "\000")
  18050. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18051. (org-set-local 'adaptive-fill-function
  18052. 'org-adaptive-fill-function)
  18053. (org-set-local
  18054. 'align-mode-rules-list
  18055. '((org-in-buffer-settings
  18056. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18057. (modes . '(org-mode))))))
  18058. (defun org-fill-item-nobreak-p ()
  18059. "Non-nil when a line break at point would insert a new item."
  18060. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18061. (defun org-fill-paragraph (&optional justify)
  18062. "Re-align a table, pass through to fill-paragraph if no table."
  18063. (let ((table-p (org-at-table-p))
  18064. (table.el-p (org-at-table.el-p))
  18065. (itemp (org-in-item-p)))
  18066. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18067. (save-excursion (goto-char (point-at-bol))
  18068. (looking-at org-outline-regexp)))
  18069. t) ; skip headlines
  18070. (table.el-p t) ; skip table.el tables
  18071. (table-p (org-table-align) t) ; align Org tables
  18072. (itemp ; align text in items
  18073. (let* ((struct (save-excursion (goto-char itemp)
  18074. (org-list-struct)))
  18075. (parents (org-list-parents-alist struct))
  18076. (children (org-list-get-children itemp struct parents))
  18077. beg end prev next prefix)
  18078. ;; Determine in which part of item point is: before
  18079. ;; first child, after last child, between two
  18080. ;; sub-lists, or simply in item if there's no child.
  18081. (cond
  18082. ((not children)
  18083. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18084. beg itemp
  18085. end (org-list-get-item-end itemp struct)))
  18086. ((< (point) (setq next (car children)))
  18087. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18088. beg itemp
  18089. end next))
  18090. ((> (point) (setq prev (car (last children))))
  18091. (setq beg (org-list-get-item-end prev struct)
  18092. end (org-list-get-item-end itemp struct)
  18093. prefix (save-excursion
  18094. (goto-char beg)
  18095. (skip-chars-forward " \t")
  18096. (make-string (current-column) ?\ ))))
  18097. (t (catch 'exit
  18098. (while (setq next (pop children))
  18099. (if (> (point) next)
  18100. (setq prev next)
  18101. (setq beg (org-list-get-item-end prev struct)
  18102. end next
  18103. prefix (save-excursion
  18104. (goto-char beg)
  18105. (skip-chars-forward " \t")
  18106. (make-string (current-column) ?\ )))
  18107. (throw 'exit nil))))))
  18108. ;; Use `fill-paragraph' with buffer narrowed to item
  18109. ;; without any child, and with our computed PREFIX.
  18110. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18111. (save-restriction
  18112. (narrow-to-region beg end)
  18113. (save-excursion (fill-paragraph justify)))) t))
  18114. ;; Special case where point is not in a list but is on
  18115. ;; a paragraph adjacent to a list: make sure this paragraph
  18116. ;; doesn't get merged with the end of the list by narrowing
  18117. ;; buffer first.
  18118. ((save-excursion (forward-paragraph -1)
  18119. (setq itemp (org-in-item-p)))
  18120. (let ((struct (save-excursion (goto-char itemp)
  18121. (org-list-struct))))
  18122. (save-restriction
  18123. (narrow-to-region (org-list-get-bottom-point struct)
  18124. (save-excursion (forward-paragraph 1)
  18125. (point)))
  18126. (fill-paragraph justify) t)))
  18127. ;; Else simply call `fill-paragraph'.
  18128. (t nil))))
  18129. ;; For reference, this is the default value of adaptive-fill-regexp
  18130. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18131. (defun org-adaptive-fill-function ()
  18132. "Return a fill prefix for org-mode files."
  18133. (let (itemp)
  18134. (save-excursion
  18135. (cond
  18136. ;; Comment line
  18137. ((looking-at "#[ \t]+")
  18138. (match-string-no-properties 0))
  18139. ;; Plain list item
  18140. ((org-at-item-p)
  18141. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18142. ;; Point is in a list after `backward-paragraph': original
  18143. ;; point wasn't in the list, or filling would have been taken
  18144. ;; care of by `org-auto-fill-function', but the list and the
  18145. ;; real paragraph are not separated by a blank line. Thus, move
  18146. ;; point after the list to go back to real paragraph and
  18147. ;; determine fill-prefix.
  18148. ((setq itemp (org-in-item-p))
  18149. (goto-char itemp)
  18150. (let* ((struct (org-list-struct))
  18151. (bottom (org-list-get-bottom-point struct)))
  18152. (goto-char bottom)
  18153. (make-string (org-get-indentation) ?\ )))
  18154. ;; Other text
  18155. ((looking-at org-adaptive-fill-regexp-backup)
  18156. (match-string-no-properties 0))))))
  18157. (defun org-auto-fill-function ()
  18158. "Auto-fill function."
  18159. (let (itemp prefix)
  18160. ;; When in a list, compute an appropriate fill-prefix and make
  18161. ;; sure it will be used by `do-auto-fill'.
  18162. (if (setq itemp (org-in-item-p))
  18163. (progn
  18164. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18165. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18166. (do-auto-fill)))
  18167. ;; Else just use `do-auto-fill'.
  18168. (do-auto-fill))))
  18169. ;;; Other stuff.
  18170. (defun org-toggle-fixed-width-section (arg)
  18171. "Toggle the fixed-width export.
  18172. If there is no active region, the QUOTE keyword at the current headline is
  18173. inserted or removed. When present, it causes the text between this headline
  18174. and the next to be exported as fixed-width text, and unmodified.
  18175. If there is an active region, this command adds or removes a colon as the
  18176. first character of this line. If the first character of a line is a colon,
  18177. this line is also exported in fixed-width font."
  18178. (interactive "P")
  18179. (let* ((cc 0)
  18180. (regionp (org-region-active-p))
  18181. (beg (if regionp (region-beginning) (point)))
  18182. (end (if regionp (region-end)))
  18183. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18184. (case-fold-search nil)
  18185. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18186. off)
  18187. (if regionp
  18188. (save-excursion
  18189. (goto-char beg)
  18190. (setq cc (current-column))
  18191. (beginning-of-line 1)
  18192. (setq off (looking-at re))
  18193. (while (> nlines 0)
  18194. (setq nlines (1- nlines))
  18195. (beginning-of-line 1)
  18196. (cond
  18197. (arg
  18198. (org-move-to-column cc t)
  18199. (insert ": \n")
  18200. (forward-line -1))
  18201. ((and off (looking-at re))
  18202. (replace-match "" t t nil 1))
  18203. ((not off) (org-move-to-column cc t) (insert ": ")))
  18204. (forward-line 1)))
  18205. (save-excursion
  18206. (org-back-to-heading)
  18207. (if (looking-at (concat org-outline-regexp
  18208. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  18209. (replace-match "" t t nil 1)
  18210. (if (looking-at org-outline-regexp)
  18211. (progn
  18212. (goto-char (match-end 0))
  18213. (insert org-quote-string " "))))))))
  18214. (defun org-reftex-citation ()
  18215. "Use reftex-citation to insert a citation into the buffer.
  18216. This looks for a line like
  18217. #+BIBLIOGRAPHY: foo plain option:-d
  18218. and derives from it that foo.bib is the bibliography file relevant
  18219. for this document. It then installs the necessary environment for RefTeX
  18220. to work in this buffer and calls `reftex-citation' to insert a citation
  18221. into the buffer.
  18222. Export of such citations to both LaTeX and HTML is handled by the contributed
  18223. package org-exp-bibtex by Taru Karttunen."
  18224. (interactive)
  18225. (let ((reftex-docstruct-symbol 'rds)
  18226. (reftex-cite-format "\\cite{%l}")
  18227. rds bib)
  18228. (save-excursion
  18229. (save-restriction
  18230. (widen)
  18231. (let ((case-fold-search t)
  18232. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18233. (if (not (save-excursion
  18234. (or (re-search-forward re nil t)
  18235. (re-search-backward re nil t))))
  18236. (error "No bibliography defined in file")
  18237. (setq bib (concat (match-string 1) ".bib")
  18238. rds (list (list 'bib bib)))))))
  18239. (call-interactively 'reftex-citation)))
  18240. ;;;; Functions extending outline functionality
  18241. (defun org-beginning-of-line (&optional arg)
  18242. "Go to the beginning of the current line. If that is invisible, continue
  18243. to a visible line beginning. This makes the function of C-a more intuitive.
  18244. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18245. first attempt, and only move to after the tags when the cursor is already
  18246. beyond the end of the headline."
  18247. (interactive "P")
  18248. (let ((pos (point))
  18249. (special (if (consp org-special-ctrl-a/e)
  18250. (car org-special-ctrl-a/e)
  18251. org-special-ctrl-a/e))
  18252. refpos)
  18253. (if (org-bound-and-true-p line-move-visual)
  18254. (beginning-of-visual-line 1)
  18255. (beginning-of-line 1))
  18256. (if (and arg (fboundp 'move-beginning-of-line))
  18257. (call-interactively 'move-beginning-of-line)
  18258. (if (bobp)
  18259. nil
  18260. (backward-char 1)
  18261. (if (org-truely-invisible-p)
  18262. (while (and (not (bobp)) (org-truely-invisible-p))
  18263. (backward-char 1)
  18264. (beginning-of-line 1))
  18265. (forward-char 1))))
  18266. (when special
  18267. (cond
  18268. ((and (looking-at org-complex-heading-regexp)
  18269. (= (char-after (match-end 1)) ?\ ))
  18270. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18271. (point-at-eol)))
  18272. (goto-char
  18273. (if (eq special t)
  18274. (cond ((> pos refpos) refpos)
  18275. ((= pos (point)) refpos)
  18276. (t (point)))
  18277. (cond ((> pos (point)) (point))
  18278. ((not (eq last-command this-command)) (point))
  18279. (t refpos)))))
  18280. ((org-at-item-p)
  18281. (goto-char
  18282. (if (eq special t)
  18283. (cond ((> pos (match-end 0)) (match-end 0))
  18284. ((= pos (point)) (match-end 0))
  18285. (t (point)))
  18286. (cond ((> pos (point)) (point))
  18287. ((not (eq last-command this-command)) (point))
  18288. (t (match-end 0))))))))
  18289. (org-no-warnings
  18290. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18291. (defun org-end-of-line (&optional arg)
  18292. "Go to the end of the line.
  18293. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18294. first attempt, and only move to after the tags when the cursor is already
  18295. beyond the end of the headline."
  18296. (interactive "P")
  18297. (let ((special (if (consp org-special-ctrl-a/e)
  18298. (cdr org-special-ctrl-a/e)
  18299. org-special-ctrl-a/e)))
  18300. (if (or (not special)
  18301. (not (org-on-heading-p))
  18302. arg)
  18303. (call-interactively
  18304. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18305. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18306. (t 'end-of-line)))
  18307. (let ((pos (point)))
  18308. (beginning-of-line 1)
  18309. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18310. (if (eq special t)
  18311. (if (or (< pos (match-beginning 1))
  18312. (= pos (match-end 0)))
  18313. (goto-char (match-beginning 1))
  18314. (goto-char (match-end 0)))
  18315. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18316. (goto-char (match-end 0))
  18317. (goto-char (match-beginning 1))))
  18318. (call-interactively (if (fboundp 'move-end-of-line)
  18319. 'move-end-of-line
  18320. 'end-of-line)))))
  18321. (org-no-warnings
  18322. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18323. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18324. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18325. (defun org-backward-sentence (&optional arg)
  18326. "Go to beginning of sentence, or beginning of table field.
  18327. This will call `backward-sentence' or `org-table-beginning-of-field',
  18328. depending on context."
  18329. (interactive "P")
  18330. (cond
  18331. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18332. (t (call-interactively 'backward-sentence))))
  18333. (defun org-forward-sentence (&optional arg)
  18334. "Go to end of sentence, or end of table field.
  18335. This will call `forward-sentence' or `org-table-end-of-field',
  18336. depending on context."
  18337. (interactive "P")
  18338. (cond
  18339. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18340. (t (call-interactively 'forward-sentence))))
  18341. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18342. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18343. (defun org-kill-line (&optional arg)
  18344. "Kill line, to tags or end of line."
  18345. (interactive "P")
  18346. (cond
  18347. ((or (not org-special-ctrl-k)
  18348. (bolp)
  18349. (not (org-on-heading-p)))
  18350. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18351. org-ctrl-k-protect-subtree)
  18352. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18353. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18354. (error "C-k aborted - would kill hidden subtree")))
  18355. (call-interactively 'kill-line))
  18356. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18357. (kill-region (point) (match-beginning 1))
  18358. (org-set-tags nil t))
  18359. (t (kill-region (point) (point-at-eol)))))
  18360. (define-key org-mode-map "\C-k" 'org-kill-line)
  18361. (defun org-yank (&optional arg)
  18362. "Yank. If the kill is a subtree, treat it specially.
  18363. This command will look at the current kill and check if is a single
  18364. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18365. cursor is at the beginning of a line or after the stars of a currently
  18366. empty headline, then the yank is handled specially. How exactly depends
  18367. on the value of the following variables, both set by default.
  18368. org-yank-folded-subtrees
  18369. When set, the subtree(s) will be folded after insertion, but only
  18370. if doing so would now swallow text after the yanked text.
  18371. org-yank-adjusted-subtrees
  18372. When set, the subtree will be promoted or demoted in order to
  18373. fit into the local outline tree structure, which means that the level
  18374. will be adjusted so that it becomes the smaller one of the two
  18375. *visible* surrounding headings.
  18376. Any prefix to this command will cause `yank' to be called directly with
  18377. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18378. will just
  18379. plainly yank the text as it is.
  18380. \[1] The test checks if the first non-white line is a heading
  18381. and if there are no other headings with fewer stars."
  18382. (interactive "P")
  18383. (org-yank-generic 'yank arg))
  18384. (defun org-yank-generic (command arg)
  18385. "Perform some yank-like command.
  18386. This function implements the behavior described in the `org-yank'
  18387. documentation. However, it has been generalized to work for any
  18388. interactive command with similar behavior."
  18389. ;; pretend to be command COMMAND
  18390. (setq this-command command)
  18391. (if arg
  18392. (call-interactively command)
  18393. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18394. (and (org-kill-is-subtree-p)
  18395. (or (bolp)
  18396. (and (looking-at "[ \t]*$")
  18397. (string-match
  18398. "\\`\\*+\\'"
  18399. (buffer-substring (point-at-bol) (point)))))))
  18400. swallowp)
  18401. (cond
  18402. ((and subtreep org-yank-folded-subtrees)
  18403. (let ((beg (point))
  18404. end)
  18405. (if (and subtreep org-yank-adjusted-subtrees)
  18406. (org-paste-subtree nil nil 'for-yank)
  18407. (call-interactively command))
  18408. (setq end (point))
  18409. (goto-char beg)
  18410. (when (and (bolp) subtreep
  18411. (not (setq swallowp
  18412. (org-yank-folding-would-swallow-text beg end))))
  18413. (org-with-limited-levels
  18414. (or (looking-at org-outline-regexp)
  18415. (re-search-forward org-outline-regexp-bol end t))
  18416. (while (and (< (point) end) (looking-at org-outline-regexp))
  18417. (hide-subtree)
  18418. (org-cycle-show-empty-lines 'folded)
  18419. (condition-case nil
  18420. (outline-forward-same-level 1)
  18421. (error (goto-char end))))))
  18422. (when swallowp
  18423. (message
  18424. "Inserted text not folded because that would swallow text"))
  18425. (goto-char end)
  18426. (skip-chars-forward " \t\n\r")
  18427. (beginning-of-line 1)
  18428. (push-mark beg 'nomsg)))
  18429. ((and subtreep org-yank-adjusted-subtrees)
  18430. (let ((beg (point-at-bol)))
  18431. (org-paste-subtree nil nil 'for-yank)
  18432. (push-mark beg 'nomsg)))
  18433. (t
  18434. (call-interactively command))))))
  18435. (defun org-yank-folding-would-swallow-text (beg end)
  18436. "Would hide-subtree at BEG swallow any text after END?"
  18437. (let (level)
  18438. (org-with-limited-levels
  18439. (save-excursion
  18440. (goto-char beg)
  18441. (when (or (looking-at org-outline-regexp)
  18442. (re-search-forward org-outline-regexp-bol end t))
  18443. (setq level (org-outline-level)))
  18444. (goto-char end)
  18445. (skip-chars-forward " \t\r\n\v\f")
  18446. (if (or (eobp)
  18447. (and (bolp) (looking-at org-outline-regexp)
  18448. (<= (org-outline-level) level)))
  18449. nil ; Nothing would be swallowed
  18450. t))))) ; something would swallow
  18451. (define-key org-mode-map "\C-y" 'org-yank)
  18452. (defun org-truely-invisible-p ()
  18453. "Check if point is at a character currently not visible.
  18454. This version does not only check the character property, but also
  18455. `visible-mode'."
  18456. ;; Early versions of noutline don't have `outline-invisible-p'.
  18457. (if (org-bound-and-true-p visible-mode)
  18458. nil
  18459. (outline-invisible-p)))
  18460. (defun org-invisible-p2 ()
  18461. "Check if point is at a character currently not visible."
  18462. (save-excursion
  18463. (if (and (eolp) (not (bobp))) (backward-char 1))
  18464. ;; Early versions of noutline don't have `outline-invisible-p'.
  18465. (outline-invisible-p)))
  18466. (defun org-back-to-heading (&optional invisible-ok)
  18467. "Call `outline-back-to-heading', but provide a better error message."
  18468. (condition-case nil
  18469. (outline-back-to-heading invisible-ok)
  18470. (error (error "Before first headline at position %d in buffer %s"
  18471. (point) (current-buffer)))))
  18472. (defun org-beginning-of-defun ()
  18473. "Go to the beginning of the subtree, i.e. back to the heading."
  18474. (org-back-to-heading))
  18475. (defun org-end-of-defun ()
  18476. "Go to the end of the subtree."
  18477. (org-end-of-subtree nil t))
  18478. (defun org-before-first-heading-p ()
  18479. "Before first heading?"
  18480. (save-excursion
  18481. (end-of-line)
  18482. (null (re-search-backward org-outline-regexp-bol nil t))))
  18483. (defun org-on-heading-p (&optional ignored)
  18484. (outline-on-heading-p t))
  18485. (defun org-at-heading-p (&optional ignored)
  18486. (outline-on-heading-p t))
  18487. (defun org-point-at-end-of-empty-headline ()
  18488. "If point is at the end of an empty headline, return t, else nil.
  18489. If the heading only contains a TODO keyword, it is still still considered
  18490. empty."
  18491. (and (looking-at "[ \t]*$")
  18492. (save-excursion
  18493. (beginning-of-line 1)
  18494. (let ((case-fold-search nil))
  18495. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  18496. "\\)?[ \t]*$"))))))
  18497. (defun org-at-heading-or-item-p ()
  18498. (or (org-on-heading-p) (org-at-item-p)))
  18499. (defun org-on-target-p ()
  18500. (or (org-in-regexp org-radio-target-regexp)
  18501. (org-in-regexp org-target-regexp)))
  18502. (defun org-up-heading-all (arg)
  18503. "Move to the heading line of which the present line is a subheading.
  18504. This function considers both visible and invisible heading lines.
  18505. With argument, move up ARG levels."
  18506. (if (fboundp 'outline-up-heading-all)
  18507. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18508. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18509. (defun org-up-heading-safe ()
  18510. "Move to the heading line of which the present line is a subheading.
  18511. This version will not throw an error. It will return the level of the
  18512. headline found, or nil if no higher level is found.
  18513. Also, this function will be a lot faster than `outline-up-heading',
  18514. because it relies on stars being the outline starters. This can really
  18515. make a significant difference in outlines with very many siblings."
  18516. (let (start-level re)
  18517. (org-back-to-heading t)
  18518. (setq start-level (funcall outline-level))
  18519. (if (equal start-level 1)
  18520. nil
  18521. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18522. (if (re-search-backward re nil t)
  18523. (funcall outline-level)))))
  18524. (defun org-first-sibling-p ()
  18525. "Is this heading the first child of its parents?"
  18526. (interactive)
  18527. (let ((re org-outline-regexp-bol)
  18528. level l)
  18529. (unless (org-at-heading-p t)
  18530. (error "Not at a heading"))
  18531. (setq level (funcall outline-level))
  18532. (save-excursion
  18533. (if (not (re-search-backward re nil t))
  18534. t
  18535. (setq l (funcall outline-level))
  18536. (< l level)))))
  18537. (defun org-goto-sibling (&optional previous)
  18538. "Goto the next sibling, even if it is invisible.
  18539. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18540. when a sibling was found. When none is found, return nil and don't
  18541. move point."
  18542. (let ((fun (if previous 're-search-backward 're-search-forward))
  18543. (pos (point))
  18544. (re org-outline-regexp-bol)
  18545. level l)
  18546. (when (condition-case nil (org-back-to-heading t) (error nil))
  18547. (setq level (funcall outline-level))
  18548. (catch 'exit
  18549. (or previous (forward-char 1))
  18550. (while (funcall fun re nil t)
  18551. (setq l (funcall outline-level))
  18552. (when (< l level) (goto-char pos) (throw 'exit nil))
  18553. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18554. (goto-char pos)
  18555. nil))))
  18556. (defun org-show-siblings ()
  18557. "Show all siblings of the current headline."
  18558. (save-excursion
  18559. (while (org-goto-sibling) (org-flag-heading nil)))
  18560. (save-excursion
  18561. (while (org-goto-sibling 'previous)
  18562. (org-flag-heading nil))))
  18563. (defun org-goto-first-child ()
  18564. "Goto the first child, even if it is invisible.
  18565. Return t when a child was found. Otherwise don't move point and
  18566. return nil."
  18567. (let (level (pos (point)) (re org-outline-regexp-bol))
  18568. (when (condition-case nil (org-back-to-heading t) (error nil))
  18569. (setq level (outline-level))
  18570. (forward-char 1)
  18571. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18572. (progn (goto-char (match-beginning 0)) t)
  18573. (goto-char pos) nil))))
  18574. (defun org-show-hidden-entry ()
  18575. "Show an entry where even the heading is hidden."
  18576. (save-excursion
  18577. (org-show-entry)))
  18578. (defun org-flag-heading (flag &optional entry)
  18579. "Flag the current heading. FLAG non-nil means make invisible.
  18580. When ENTRY is non-nil, show the entire entry."
  18581. (save-excursion
  18582. (org-back-to-heading t)
  18583. ;; Check if we should show the entire entry
  18584. (if entry
  18585. (progn
  18586. (org-show-entry)
  18587. (save-excursion
  18588. (and (outline-next-heading)
  18589. (org-flag-heading nil))))
  18590. (outline-flag-region (max (point-min) (1- (point)))
  18591. (save-excursion (outline-end-of-heading) (point))
  18592. flag))))
  18593. (defun org-get-next-sibling ()
  18594. "Move to next heading of the same level, and return point.
  18595. If there is no such heading, return nil.
  18596. This is like outline-next-sibling, but invisible headings are ok."
  18597. (let ((level (funcall outline-level)))
  18598. (outline-next-heading)
  18599. (while (and (not (eobp)) (> (funcall outline-level) level))
  18600. (outline-next-heading))
  18601. (if (or (eobp) (< (funcall outline-level) level))
  18602. nil
  18603. (point))))
  18604. (defun org-get-last-sibling ()
  18605. "Move to previous heading of the same level, and return point.
  18606. If there is no such heading, return nil."
  18607. (let ((opoint (point))
  18608. (level (funcall outline-level)))
  18609. (outline-previous-heading)
  18610. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18611. (while (and (> (funcall outline-level) level)
  18612. (not (bobp)))
  18613. (outline-previous-heading))
  18614. (if (< (funcall outline-level) level)
  18615. nil
  18616. (point)))))
  18617. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18618. ;; This contains an exact copy of the original function, but it uses
  18619. ;; `org-back-to-heading', to make it work also in invisible
  18620. ;; trees. And is uses an invisible-OK argument.
  18621. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  18622. ;; Furthermore, when used inside Org, finding the end of a large subtree
  18623. ;; with many children and grandchildren etc, this can be much faster
  18624. ;; than the outline version.
  18625. (org-back-to-heading invisible-OK)
  18626. (let ((first t)
  18627. (level (funcall outline-level)))
  18628. (if (and (org-mode-p) (< level 1000))
  18629. ;; A true heading (not a plain list item), in Org-mode
  18630. ;; This means we can easily find the end by looking
  18631. ;; only for the right number of stars. Using a regexp to do
  18632. ;; this is so much faster than using a Lisp loop.
  18633. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  18634. (forward-char 1)
  18635. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  18636. ;; something else, do it the slow way
  18637. (while (and (not (eobp))
  18638. (or first (> (funcall outline-level) level)))
  18639. (setq first nil)
  18640. (outline-next-heading)))
  18641. (unless to-heading
  18642. (if (memq (preceding-char) '(?\n ?\^M))
  18643. (progn
  18644. ;; Go to end of line before heading
  18645. (forward-char -1)
  18646. (if (memq (preceding-char) '(?\n ?\^M))
  18647. ;; leave blank line before heading
  18648. (forward-char -1))))))
  18649. (point))
  18650. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  18651. "Use Org version in org-mode, for dramatic speed-up."
  18652. (if (org-mode-p)
  18653. (progn
  18654. (org-end-of-subtree nil t)
  18655. (unless (eobp) (backward-char 1)))
  18656. ad-do-it))
  18657. ;; FIXME This should not match :END: for custom drawers?
  18658. (defun org-end-of-meta-data-and-drawers ()
  18659. "Jump to the first text after meta data and drawers in the current entry.
  18660. This will move over empty lines, lines with planning time stamps,
  18661. clocking lines, and drawers."
  18662. (org-back-to-heading t)
  18663. (let ((end (save-excursion (outline-next-heading) (point)))
  18664. (re (concat "\\(" org-drawer-regexp "\\)"
  18665. "\\|" "[ \t]*" org-keyword-time-regexp)))
  18666. (forward-line 1)
  18667. (while (re-search-forward re end t)
  18668. (if (not (match-end 1))
  18669. ;; empty or planning line
  18670. (forward-line 1)
  18671. ;; a drawer, find the end
  18672. (re-search-forward "^[ \t]*:END:" end 'move)
  18673. (forward-line 1)))
  18674. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  18675. (point)))
  18676. (defun org-forward-same-level (arg &optional invisible-ok)
  18677. "Move forward to the arg'th subheading at same level as this one.
  18678. Stop at the first and last subheadings of a superior heading.
  18679. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  18680. it wil also look at invisible ones."
  18681. (interactive "p")
  18682. (org-back-to-heading invisible-ok)
  18683. (org-on-heading-p)
  18684. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18685. (re (format "^\\*\\{1,%d\\} " level))
  18686. l)
  18687. (forward-char 1)
  18688. (while (> arg 0)
  18689. (while (and (re-search-forward re nil 'move)
  18690. (setq l (- (match-end 0) (match-beginning 0) 1))
  18691. (= l level)
  18692. (not invisible-ok)
  18693. (progn (backward-char 1) (outline-invisible-p)))
  18694. (if (< l level) (setq arg 1)))
  18695. (setq arg (1- arg)))
  18696. (beginning-of-line 1)))
  18697. (defun org-backward-same-level (arg &optional invisible-ok)
  18698. "Move backward to the arg'th subheading at same level as this one.
  18699. Stop at the first and last subheadings of a superior heading."
  18700. (interactive "p")
  18701. (org-back-to-heading)
  18702. (org-on-heading-p)
  18703. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18704. (re (format "^\\*\\{1,%d\\} " level))
  18705. l)
  18706. (while (> arg 0)
  18707. (while (and (re-search-backward re nil 'move)
  18708. (setq l (- (match-end 0) (match-beginning 0) 1))
  18709. (= l level)
  18710. (not invisible-ok)
  18711. (outline-invisible-p))
  18712. (if (< l level) (setq arg 1)))
  18713. (setq arg (1- arg)))))
  18714. (defun org-show-subtree ()
  18715. "Show everything after this heading at deeper levels."
  18716. (outline-flag-region
  18717. (point)
  18718. (save-excursion
  18719. (org-end-of-subtree t t))
  18720. nil))
  18721. (defun org-show-entry ()
  18722. "Show the body directly following this heading.
  18723. Show the heading too, if it is currently invisible."
  18724. (interactive)
  18725. (save-excursion
  18726. (condition-case nil
  18727. (progn
  18728. (org-back-to-heading t)
  18729. (outline-flag-region
  18730. (max (point-min) (1- (point)))
  18731. (save-excursion
  18732. (if (re-search-forward
  18733. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  18734. (match-beginning 1)
  18735. (point-max)))
  18736. nil)
  18737. (org-cycle-hide-drawers 'children))
  18738. (error nil))))
  18739. (defun org-make-options-regexp (kwds &optional extra)
  18740. "Make a regular expression for keyword lines."
  18741. (concat
  18742. "^"
  18743. "#?[ \t]*\\+\\("
  18744. (mapconcat 'regexp-quote kwds "\\|")
  18745. (if extra (concat "\\|" extra))
  18746. "\\):[ \t]*"
  18747. "\\(.*\\)"))
  18748. ;; Make isearch reveal the necessary context
  18749. (defun org-isearch-end ()
  18750. "Reveal context after isearch exits."
  18751. (when isearch-success ; only if search was successful
  18752. (if (featurep 'xemacs)
  18753. ;; Under XEmacs, the hook is run in the correct place,
  18754. ;; we directly show the context.
  18755. (org-show-context 'isearch)
  18756. ;; In Emacs the hook runs *before* restoring the overlays.
  18757. ;; So we have to use a one-time post-command-hook to do this.
  18758. ;; (Emacs 22 has a special variable, see function `org-mode')
  18759. (unless (and (boundp 'isearch-mode-end-hook-quit)
  18760. isearch-mode-end-hook-quit)
  18761. ;; Only when the isearch was not quitted.
  18762. (org-add-hook 'post-command-hook 'org-isearch-post-command
  18763. 'append 'local)))))
  18764. (defun org-isearch-post-command ()
  18765. "Remove self from hook, and show context."
  18766. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  18767. (org-show-context 'isearch))
  18768. ;;;; Integration with and fixes for other packages
  18769. ;;; Imenu support
  18770. (defvar org-imenu-markers nil
  18771. "All markers currently used by Imenu.")
  18772. (make-variable-buffer-local 'org-imenu-markers)
  18773. (defun org-imenu-new-marker (&optional pos)
  18774. "Return a new marker for use by Imenu, and remember the marker."
  18775. (let ((m (make-marker)))
  18776. (move-marker m (or pos (point)))
  18777. (push m org-imenu-markers)
  18778. m))
  18779. (defun org-imenu-get-tree ()
  18780. "Produce the index for Imenu."
  18781. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  18782. (setq org-imenu-markers nil)
  18783. (let* ((n org-imenu-depth)
  18784. (re (concat "^" (org-get-limited-outline-regexp)))
  18785. (subs (make-vector (1+ n) nil))
  18786. (last-level 0)
  18787. m level head)
  18788. (save-excursion
  18789. (save-restriction
  18790. (widen)
  18791. (goto-char (point-max))
  18792. (while (re-search-backward re nil t)
  18793. (setq level (org-reduced-level (funcall outline-level)))
  18794. (when (<= level n)
  18795. (looking-at org-complex-heading-regexp)
  18796. (setq head (org-link-display-format
  18797. (org-match-string-no-properties 4))
  18798. m (org-imenu-new-marker))
  18799. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  18800. (if (>= level last-level)
  18801. (push (cons head m) (aref subs level))
  18802. (push (cons head (aref subs (1+ level))) (aref subs level))
  18803. (loop for i from (1+ level) to n do (aset subs i nil)))
  18804. (setq last-level level)))))
  18805. (aref subs 1)))
  18806. (eval-after-load "imenu"
  18807. '(progn
  18808. (add-hook 'imenu-after-jump-hook
  18809. (lambda ()
  18810. (if (org-mode-p)
  18811. (org-show-context 'org-goto))))))
  18812. (defun org-link-display-format (link)
  18813. "Replace a link with either the description, or the link target
  18814. if no description is present"
  18815. (save-match-data
  18816. (if (string-match org-bracket-link-analytic-regexp link)
  18817. (replace-match (if (match-end 5)
  18818. (match-string 5 link)
  18819. (concat (match-string 1 link)
  18820. (match-string 3 link)))
  18821. nil t link)
  18822. link)))
  18823. (defun org-toggle-link-display ()
  18824. "Toggle the literal or descriptive display of links."
  18825. (interactive)
  18826. (if org-descriptive-links
  18827. (progn (org-remove-from-invisibility-spec '(org-link))
  18828. (org-restart-font-lock)
  18829. (setq org-descriptive-links nil))
  18830. (progn (add-to-invisibility-spec '(org-link))
  18831. (org-restart-font-lock)
  18832. (setq org-descriptive-links t))))
  18833. ;; Speedbar support
  18834. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  18835. "Overlay marking the agenda restriction line in speedbar.")
  18836. (overlay-put org-speedbar-restriction-lock-overlay
  18837. 'face 'org-agenda-restriction-lock)
  18838. (overlay-put org-speedbar-restriction-lock-overlay
  18839. 'help-echo "Agendas are currently limited to this item.")
  18840. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  18841. (defun org-speedbar-set-agenda-restriction ()
  18842. "Restrict future agenda commands to the location at point in speedbar.
  18843. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  18844. (interactive)
  18845. (require 'org-agenda)
  18846. (let (p m tp np dir txt)
  18847. (cond
  18848. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18849. 'org-imenu t))
  18850. (setq m (get-text-property p 'org-imenu-marker))
  18851. (with-current-buffer (marker-buffer m)
  18852. (goto-char m)
  18853. (org-agenda-set-restriction-lock 'subtree)))
  18854. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18855. 'speedbar-function 'speedbar-find-file))
  18856. (setq tp (previous-single-property-change
  18857. (1+ p) 'speedbar-function)
  18858. np (next-single-property-change
  18859. tp 'speedbar-function)
  18860. dir (speedbar-line-directory)
  18861. txt (buffer-substring-no-properties (or tp (point-min))
  18862. (or np (point-max))))
  18863. (with-current-buffer (find-file-noselect
  18864. (let ((default-directory dir))
  18865. (expand-file-name txt)))
  18866. (unless (org-mode-p)
  18867. (error "Cannot restrict to non-Org-mode file"))
  18868. (org-agenda-set-restriction-lock 'file)))
  18869. (t (error "Don't know how to restrict Org-mode's agenda")))
  18870. (move-overlay org-speedbar-restriction-lock-overlay
  18871. (point-at-bol) (point-at-eol))
  18872. (setq current-prefix-arg nil)
  18873. (org-agenda-maybe-redo)))
  18874. (eval-after-load "speedbar"
  18875. '(progn
  18876. (speedbar-add-supported-extension ".org")
  18877. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  18878. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  18879. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  18880. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18881. (add-hook 'speedbar-visiting-tag-hook
  18882. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  18883. ;;; Fixes and Hacks for problems with other packages
  18884. ;; Make flyspell not check words in links, to not mess up our keymap
  18885. (defun org-mode-flyspell-verify ()
  18886. "Don't let flyspell put overlays at active buttons, or on
  18887. {todo,all-time,additional-option-like}-keywords."
  18888. (let ((pos (max (1- (point)) (point-min)))
  18889. (word (thing-at-point 'word)))
  18890. (and (not (get-text-property pos 'keymap))
  18891. (not (get-text-property pos 'org-no-flyspell))
  18892. (not (member word org-todo-keywords-1))
  18893. (not (member word org-all-time-keywords))
  18894. (not (member word org-additional-option-like-keywords)))))
  18895. (defun org-remove-flyspell-overlays-in (beg end)
  18896. "Remove flyspell overlays in region."
  18897. (and (org-bound-and-true-p flyspell-mode)
  18898. (fboundp 'flyspell-delete-region-overlays)
  18899. (flyspell-delete-region-overlays beg end))
  18900. (add-text-properties beg end '(org-no-flyspell t)))
  18901. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  18902. (eval-after-load "bookmark"
  18903. '(if (boundp 'bookmark-after-jump-hook)
  18904. ;; We can use the hook
  18905. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  18906. ;; Hook not available, use advice
  18907. (defadvice bookmark-jump (after org-make-visible activate)
  18908. "Make the position visible."
  18909. (org-bookmark-jump-unhide))))
  18910. ;; Make sure saveplace shows the location if it was hidden
  18911. (eval-after-load "saveplace"
  18912. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  18913. "Make the position visible."
  18914. (org-bookmark-jump-unhide)))
  18915. ;; Make sure ecb shows the location if it was hidden
  18916. (eval-after-load "ecb"
  18917. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  18918. "Make hierarchy visible when jumping into location from ECB tree buffer."
  18919. (if (org-mode-p)
  18920. (org-show-context))))
  18921. (defun org-bookmark-jump-unhide ()
  18922. "Unhide the current position, to show the bookmark location."
  18923. (and (org-mode-p)
  18924. (or (outline-invisible-p)
  18925. (save-excursion (goto-char (max (point-min) (1- (point))))
  18926. (outline-invisible-p)))
  18927. (org-show-context 'bookmark-jump)))
  18928. ;; Make session.el ignore our circular variable
  18929. (eval-after-load "session"
  18930. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  18931. ;;;; Experimental code
  18932. (defun org-closed-in-range ()
  18933. "Sparse tree of items closed in a certain time range.
  18934. Still experimental, may disappear in the future."
  18935. (interactive)
  18936. ;; Get the time interval from the user.
  18937. (let* ((time1 (org-float-time
  18938. (org-read-date nil 'to-time nil "Starting date: ")))
  18939. (time2 (org-float-time
  18940. (org-read-date nil 'to-time nil "End date:")))
  18941. ;; callback function
  18942. (callback (lambda ()
  18943. (let ((time
  18944. (org-float-time
  18945. (apply 'encode-time
  18946. (org-parse-time-string
  18947. (match-string 1))))))
  18948. ;; check if time in interval
  18949. (and (>= time time1) (<= time time2))))))
  18950. ;; make tree, check each match with the callback
  18951. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  18952. ;;;; Finish up
  18953. (provide 'org)
  18954. (run-hooks 'org-load-hook)
  18955. ;;; org.el ends here