org.el 762 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.5
  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. ;; babel
  100. (require 'ob)
  101. (require 'ob-table)
  102. (require 'ob-lob)
  103. (require 'ob-ref)
  104. (require 'ob-tangle)
  105. (require 'ob-comint)
  106. (require 'ob-keys)
  107. ;; load languages based on value of `org-babel-load-languages'
  108. (defvar org-babel-load-languages)
  109. ;;;###autoload
  110. (defun org-babel-do-load-languages (sym value)
  111. "Load the languages defined in `org-babel-load-languages'."
  112. (set-default sym value)
  113. (mapc (lambda (pair)
  114. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  115. (if active
  116. (progn
  117. (require (intern (concat "ob-" lang))))
  118. (progn
  119. (funcall 'fmakunbound
  120. (intern (concat "org-babel-execute:" lang)))
  121. (funcall 'fmakunbound
  122. (intern (concat "org-babel-expand-body:" lang)))))))
  123. org-babel-load-languages))
  124. (defcustom org-babel-load-languages '((emacs-lisp . t))
  125. "Languages which can be evaluated in Org-mode buffers.
  126. This list can be used to load support for any of the languages
  127. below, note that each language will depend on a different set of
  128. system executables and/or Emacs modes. When a language is
  129. \"loaded\", then code blocks in that language can be evaluated
  130. with `org-babel-execute-src-block' bound by default to C-c
  131. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  132. be set to remove code block evaluation from the C-c C-c
  133. keybinding. By default only Emacs Lisp (which has no
  134. requirements) is loaded."
  135. :group 'org-babel
  136. :set 'org-babel-do-load-languages
  137. :type '(alist :tag "Babel Languages"
  138. :key-type
  139. (choice
  140. (const :tag "Awk" awk)
  141. (const :tag "C" C)
  142. (const :tag "R" R)
  143. (const :tag "Asymptote" asymptote)
  144. (const :tag "Calc" calc)
  145. (const :tag "Clojure" clojure)
  146. (const :tag "CSS" css)
  147. (const :tag "Ditaa" ditaa)
  148. (const :tag "Dot" dot)
  149. (const :tag "Emacs Lisp" emacs-lisp)
  150. (const :tag "Gnuplot" gnuplot)
  151. (const :tag "Haskell" haskell)
  152. (const :tag "Javascript" js)
  153. (const :tag "Latex" latex)
  154. (const :tag "Ledger" ledger)
  155. (const :tag "Maxima" maxima)
  156. (const :tag "Matlab" matlab)
  157. (const :tag "Mscgen" mscgen)
  158. (const :tag "Ocaml" ocaml)
  159. (const :tag "Octave" octave)
  160. (const :tag "Org" org)
  161. (const :tag "Perl" perl)
  162. (const :tag "PlantUML" plantuml)
  163. (const :tag "Python" python)
  164. (const :tag "Ruby" ruby)
  165. (const :tag "Sass" sass)
  166. (const :tag "Scheme" scheme)
  167. (const :tag "Screen" screen)
  168. (const :tag "Shell Script" sh)
  169. (const :tag "Sql" sql)
  170. (const :tag "Sqlite" sqlite))
  171. :value-type (boolean :tag "Activate" :value t)))
  172. ;;;; Customization variables
  173. (defcustom org-clone-delete-id nil
  174. "Remove ID property of clones of a subtree.
  175. When non-nil, clones of a subtree don't inherit the ID property.
  176. Otherwise they inherit the ID property with a new unique
  177. identifier."
  178. :type 'boolean
  179. :group 'org-id)
  180. ;;; Version
  181. (defconst org-version "7.5"
  182. "The version number of the file org.el.")
  183. (defun org-version (&optional here)
  184. "Show the org-mode version in the echo area.
  185. With prefix arg HERE, insert it at point."
  186. (interactive "P")
  187. (let* ((origin default-directory)
  188. (version org-version)
  189. (git-version)
  190. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  191. (when (and (file-exists-p (expand-file-name ".git" dir))
  192. (executable-find "git"))
  193. (unwind-protect
  194. (progn
  195. (cd dir)
  196. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  197. (with-current-buffer "*Shell Command Output*"
  198. (goto-char (point-min))
  199. (setq git-version (buffer-substring (point) (point-at-eol))))
  200. (subst-char-in-string ?- ?. git-version t)
  201. (when (string-match "\\S-"
  202. (shell-command-to-string
  203. "git diff-index --name-only HEAD --"))
  204. (setq git-version (concat git-version ".dirty")))
  205. (setq version (concat version " (" git-version ")"))))
  206. (cd origin)))
  207. (setq version (format "Org-mode version %s" version))
  208. (if here (insert version))
  209. (message version)))
  210. ;;; Compatibility constants
  211. ;;; The custom variables
  212. (defgroup org nil
  213. "Outline-based notes management and organizer."
  214. :tag "Org"
  215. :group 'outlines
  216. :group 'calendar)
  217. (defcustom org-mode-hook nil
  218. "Mode hook for Org-mode, run after the mode was turned on."
  219. :group 'org
  220. :type 'hook)
  221. (defcustom org-load-hook nil
  222. "Hook that is run after org.el has been loaded."
  223. :group 'org
  224. :type 'hook)
  225. (defvar org-modules) ; defined below
  226. (defvar org-modules-loaded nil
  227. "Have the modules been loaded already?")
  228. (defun org-load-modules-maybe (&optional force)
  229. "Load all extensions listed in `org-modules'."
  230. (when (or force (not org-modules-loaded))
  231. (mapc (lambda (ext)
  232. (condition-case nil (require ext)
  233. (error (message "Problems while trying to load feature `%s'" ext))))
  234. org-modules)
  235. (setq org-modules-loaded t)))
  236. (defun org-set-modules (var value)
  237. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  238. (set var value)
  239. (when (featurep 'org)
  240. (org-load-modules-maybe 'force)))
  241. (when (org-bound-and-true-p org-modules)
  242. (let ((a (member 'org-infojs org-modules)))
  243. (and a (setcar a 'org-jsinfo))))
  244. (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)
  245. "Modules that should always be loaded together with org.el.
  246. If a description starts with <C>, the file is not part of Emacs
  247. and loading it will require that you have downloaded and properly installed
  248. the org-mode distribution.
  249. You can also use this system to load external packages (i.e. neither Org
  250. core modules, nor modules from the CONTRIB directory). Just add symbols
  251. to the end of the list. If the package is called org-xyz.el, then you need
  252. to add the symbol `xyz', and the package must have a call to
  253. (provide 'org-xyz)"
  254. :group 'org
  255. :set 'org-set-modules
  256. :type
  257. '(set :greedy t
  258. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  259. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  260. (const :tag " crypt: Encryption of subtrees" org-crypt)
  261. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  262. (const :tag " docview: Links to doc-view buffers" org-docview)
  263. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  264. (const :tag " id: Global IDs for identifying entries" org-id)
  265. (const :tag " info: Links to Info nodes" org-info)
  266. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  267. (const :tag " habit: Track your consistency with habits" org-habit)
  268. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  269. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  270. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  271. (const :tag " mew Links to Mew folders/messages" org-mew)
  272. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  273. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  274. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  275. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  276. (const :tag " vm: Links to VM folders/messages" org-vm)
  277. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  278. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  279. (const :tag " mouse: Additional mouse support" org-mouse)
  280. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  281. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  282. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  283. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  284. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  285. (const :tag "C collector: Collect properties into tables" org-collector)
  286. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  287. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  288. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  289. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  290. (const :tag "C eval: Include command output as text" org-eval)
  291. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  292. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  293. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  294. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  295. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  296. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  297. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  298. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  299. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  300. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  301. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  302. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  303. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  304. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  305. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  306. (const :tag "C registry: A registry for Org-mode links" org-registry)
  307. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  308. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  309. (const :tag "C secretary: Team management with org-mode" org-secretary)
  310. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  311. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  312. (const :tag "C track: Keep up with Org-mode development" org-track)
  313. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  314. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  315. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  316. (defcustom org-support-shift-select nil
  317. "Non-nil means make shift-cursor commands select text when possible.
  318. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  319. selecting a region, or enlarge regions started in this way.
  320. In Org-mode, in special contexts, these same keys are used for other
  321. purposes, important enough to compete with shift selection. Org tries
  322. to balance these needs by supporting `shift-select-mode' outside these
  323. special contexts, under control of this variable.
  324. The default of this variable is nil, to avoid confusing behavior. Shifted
  325. cursor keys will then execute Org commands in the following contexts:
  326. - on a headline, changing TODO state (left/right) and priority (up/down)
  327. - on a time stamp, changing the time
  328. - in a plain list item, changing the bullet type
  329. - in a property definition line, switching between allowed values
  330. - in the BEGIN line of a clock table (changing the time block).
  331. Outside these contexts, the commands will throw an error.
  332. When this variable is t and the cursor is not in a special context,
  333. Org-mode will support shift-selection for making and enlarging regions.
  334. To make this more effective, the bullet cycling will no longer happen
  335. anywhere in an item line, but only if the cursor is exactly on the bullet.
  336. If you set this variable to the symbol `always', then the keys
  337. will not be special in headlines, property lines, and item lines, to make
  338. shift selection work there as well. If this is what you want, you can
  339. use the following alternative commands: `C-c C-t' and `C-c ,' to
  340. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  341. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  342. edited by hand or in column view.
  343. However, when the cursor is on a timestamp, shift-cursor commands
  344. will still edit the time stamp - this is just too good to give up.
  345. XEmacs user should have this variable set to nil, because shift-select-mode
  346. is Emacs 23 only."
  347. :group 'org
  348. :type '(choice
  349. (const :tag "Never" nil)
  350. (const :tag "When outside special context" t)
  351. (const :tag "Everywhere except timestamps" always)))
  352. (defgroup org-startup nil
  353. "Options concerning startup of Org-mode."
  354. :tag "Org Startup"
  355. :group 'org)
  356. (defcustom org-startup-folded t
  357. "Non-nil means entering Org-mode will switch to OVERVIEW.
  358. This can also be configured on a per-file basis by adding one of
  359. the following lines anywhere in the buffer:
  360. #+STARTUP: fold (or `overview', this is equivalent)
  361. #+STARTUP: nofold (or `showall', this is equivalent)
  362. #+STARTUP: content
  363. #+STARTUP: showeverything"
  364. :group 'org-startup
  365. :type '(choice
  366. (const :tag "nofold: show all" nil)
  367. (const :tag "fold: overview" t)
  368. (const :tag "content: all headlines" content)
  369. (const :tag "show everything, even drawers" showeverything)))
  370. (defcustom org-startup-truncated t
  371. "Non-nil means entering Org-mode will set `truncate-lines'.
  372. This is useful since some lines containing links can be very long and
  373. uninteresting. Also tables look terrible when wrapped."
  374. :group 'org-startup
  375. :type 'boolean)
  376. (defcustom org-startup-indented nil
  377. "Non-nil means turn on `org-indent-mode' on startup.
  378. This can also be configured on a per-file basis by adding one of
  379. the following lines anywhere in the buffer:
  380. #+STARTUP: indent
  381. #+STARTUP: noindent"
  382. :group 'org-structure
  383. :type '(choice
  384. (const :tag "Not" nil)
  385. (const :tag "Globally (slow on startup in large files)" t)))
  386. (defcustom org-use-sub-superscripts t
  387. "Non-nil means interpret \"_\" and \"^\" for export.
  388. When this option is turned on, you can use TeX-like syntax for sub- and
  389. superscripts. Several characters after \"_\" or \"^\" will be
  390. considered as a single item - so grouping with {} is normally not
  391. needed. For example, the following things will be parsed as single
  392. sub- or superscripts.
  393. 10^24 or 10^tau several digits will be considered 1 item.
  394. 10^-12 or 10^-tau a leading sign with digits or a word
  395. x^2-y^3 will be read as x^2 - y^3, because items are
  396. terminated by almost any nonword/nondigit char.
  397. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  398. Still, ambiguity is possible - so when in doubt use {} to enclose the
  399. sub/superscript. If you set this variable to the symbol `{}',
  400. the braces are *required* in order to trigger interpretations as
  401. sub/superscript. This can be helpful in documents that need \"_\"
  402. frequently in plain text.
  403. Not all export backends support this, but HTML does.
  404. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  405. :group 'org-startup
  406. :group 'org-export-translation
  407. :type '(choice
  408. (const :tag "Always interpret" t)
  409. (const :tag "Only with braces" {})
  410. (const :tag "Never interpret" nil)))
  411. (if (fboundp 'defvaralias)
  412. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  413. (defcustom org-startup-with-beamer-mode nil
  414. "Non-nil means turn on `org-beamer-mode' on startup.
  415. This can also be configured on a per-file basis by adding one of
  416. the following lines anywhere in the buffer:
  417. #+STARTUP: beamer"
  418. :group 'org-startup
  419. :type 'boolean)
  420. (defcustom org-startup-align-all-tables nil
  421. "Non-nil means align all tables when visiting a file.
  422. This is useful when the column width in tables is forced with <N> cookies
  423. in table fields. Such tables will look correct only after the first re-align.
  424. This can also be configured on a per-file basis by adding one of
  425. the following lines anywhere in the buffer:
  426. #+STARTUP: align
  427. #+STARTUP: noalign"
  428. :group 'org-startup
  429. :type 'boolean)
  430. (defcustom org-startup-with-inline-images nil
  431. "Non-nil means show inline images when loading a new Org file.
  432. This can also be configured on a per-file basis by adding one of
  433. the following lines anywhere in the buffer:
  434. #+STARTUP: inlineimages
  435. #+STARTUP: noinlineimages"
  436. :group 'org-startup
  437. :type 'boolean)
  438. (defcustom org-insert-mode-line-in-empty-file nil
  439. "Non-nil means insert the first line setting Org-mode in empty files.
  440. When the function `org-mode' is called interactively in an empty file, this
  441. normally means that the file name does not automatically trigger Org-mode.
  442. To ensure that the file will always be in Org-mode in the future, a
  443. line enforcing Org-mode will be inserted into the buffer, if this option
  444. has been set."
  445. :group 'org-startup
  446. :type 'boolean)
  447. (defcustom org-replace-disputed-keys nil
  448. "Non-nil means use alternative key bindings for some keys.
  449. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  450. These keys are also used by other packages like shift-selection-mode'
  451. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  452. If you want to use Org-mode together with one of these other modes,
  453. or more generally if you would like to move some Org-mode commands to
  454. other keys, set this variable and configure the keys with the variable
  455. `org-disputed-keys'.
  456. This option is only relevant at load-time of Org-mode, and must be set
  457. *before* org.el is loaded. Changing it requires a restart of Emacs to
  458. become effective."
  459. :group 'org-startup
  460. :type 'boolean)
  461. (defcustom org-use-extra-keys nil
  462. "Non-nil means use extra key sequence definitions for certain commands.
  463. This happens automatically if you run XEmacs or if `window-system'
  464. is nil. This variable lets you do the same manually. You must
  465. set it before loading org.
  466. Example: on Carbon Emacs 22 running graphically, with an external
  467. keyboard on a Powerbook, the default way of setting M-left might
  468. not work for either Alt or ESC. Setting this variable will make
  469. it work for ESC."
  470. :group 'org-startup
  471. :type 'boolean)
  472. (if (fboundp 'defvaralias)
  473. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  474. (defcustom org-disputed-keys
  475. '(([(shift up)] . [(meta p)])
  476. ([(shift down)] . [(meta n)])
  477. ([(shift left)] . [(meta -)])
  478. ([(shift right)] . [(meta +)])
  479. ([(control shift right)] . [(meta shift +)])
  480. ([(control shift left)] . [(meta shift -)]))
  481. "Keys for which Org-mode and other modes compete.
  482. This is an alist, cars are the default keys, second element specifies
  483. the alternative to use when `org-replace-disputed-keys' is t.
  484. Keys can be specified in any syntax supported by `define-key'.
  485. The value of this option takes effect only at Org-mode's startup,
  486. therefore you'll have to restart Emacs to apply it after changing."
  487. :group 'org-startup
  488. :type 'alist)
  489. (defun org-key (key)
  490. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  491. Or return the original if not disputed.
  492. Also apply the translations defined in `org-xemacs-key-equivalents'."
  493. (when org-replace-disputed-keys
  494. (let* ((nkey (key-description key))
  495. (x (org-find-if (lambda (x)
  496. (equal (key-description (car x)) nkey))
  497. org-disputed-keys)))
  498. (setq key (if x (cdr x) key))))
  499. (when (featurep 'xemacs)
  500. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  501. key)
  502. (defun org-find-if (predicate seq)
  503. (catch 'exit
  504. (while seq
  505. (if (funcall predicate (car seq))
  506. (throw 'exit (car seq))
  507. (pop seq)))))
  508. (defun org-defkey (keymap key def)
  509. "Define a key, possibly translated, as returned by `org-key'."
  510. (define-key keymap (org-key key) def))
  511. (defcustom org-ellipsis nil
  512. "The ellipsis to use in the Org-mode outline.
  513. When nil, just use the standard three dots. When a string, use that instead,
  514. When a face, use the standard 3 dots, but with the specified face.
  515. The change affects only Org-mode (which will then use its own display table).
  516. Changing this requires executing `M-x org-mode' in a buffer to become
  517. effective."
  518. :group 'org-startup
  519. :type '(choice (const :tag "Default" nil)
  520. (face :tag "Face" :value org-warning)
  521. (string :tag "String" :value "...#")))
  522. (defvar org-display-table nil
  523. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  524. (defgroup org-keywords nil
  525. "Keywords in Org-mode."
  526. :tag "Org Keywords"
  527. :group 'org)
  528. (defcustom org-deadline-string "DEADLINE:"
  529. "String to mark deadline entries.
  530. A deadline is this string, followed by a time stamp. Should be a word,
  531. terminated by a colon. You can insert a schedule keyword and
  532. a timestamp with \\[org-deadline].
  533. Changes become only effective after restarting Emacs."
  534. :group 'org-keywords
  535. :type 'string)
  536. (defcustom org-scheduled-string "SCHEDULED:"
  537. "String to mark scheduled TODO entries.
  538. A schedule is this string, followed by a time stamp. Should be a word,
  539. terminated by a colon. You can insert a schedule keyword and
  540. a timestamp with \\[org-schedule].
  541. Changes become only effective after restarting Emacs."
  542. :group 'org-keywords
  543. :type 'string)
  544. (defcustom org-closed-string "CLOSED:"
  545. "String used as the prefix for timestamps logging closing a TODO entry."
  546. :group 'org-keywords
  547. :type 'string)
  548. (defcustom org-clock-string "CLOCK:"
  549. "String used as prefix for timestamps clocking work hours on an item."
  550. :group 'org-keywords
  551. :type 'string)
  552. (defcustom org-comment-string "COMMENT"
  553. "Entries starting with this keyword will never be exported.
  554. An entry can be toggled between COMMENT and normal with
  555. \\[org-toggle-comment].
  556. Changes become only effective after restarting Emacs."
  557. :group 'org-keywords
  558. :type 'string)
  559. (defcustom org-quote-string "QUOTE"
  560. "Entries starting with this keyword will be exported in fixed-width font.
  561. Quoting applies only to the text in the entry following the headline, and does
  562. not extend beyond the next headline, even if that is lower level.
  563. An entry can be toggled between QUOTE and normal with
  564. \\[org-toggle-fixed-width-section]."
  565. :group 'org-keywords
  566. :type 'string)
  567. (defconst org-repeat-re
  568. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  569. "Regular expression for specifying repeated events.
  570. After a match, group 1 contains the repeat expression.")
  571. (defgroup org-structure nil
  572. "Options concerning the general structure of Org-mode files."
  573. :tag "Org Structure"
  574. :group 'org)
  575. (defgroup org-reveal-location nil
  576. "Options about how to make context of a location visible."
  577. :tag "Org Reveal Location"
  578. :group 'org-structure)
  579. (defconst org-context-choice
  580. '(choice
  581. (const :tag "Always" t)
  582. (const :tag "Never" nil)
  583. (repeat :greedy t :tag "Individual contexts"
  584. (cons
  585. (choice :tag "Context"
  586. (const agenda)
  587. (const org-goto)
  588. (const occur-tree)
  589. (const tags-tree)
  590. (const link-search)
  591. (const mark-goto)
  592. (const bookmark-jump)
  593. (const isearch)
  594. (const default))
  595. (boolean))))
  596. "Contexts for the reveal options.")
  597. (defcustom org-show-hierarchy-above '((default . t))
  598. "Non-nil means show full hierarchy when revealing a location.
  599. Org-mode often shows locations in an org-mode file which might have
  600. been invisible before. When this is set, the hierarchy of headings
  601. above the exposed location is shown.
  602. Turning this off for example for sparse trees makes them very compact.
  603. Instead of t, this can also be an alist specifying this option for different
  604. contexts. Valid contexts are
  605. agenda when exposing an entry from the agenda
  606. org-goto when using the command `org-goto' on key C-c C-j
  607. occur-tree when using the command `org-occur' on key C-c /
  608. tags-tree when constructing a sparse tree based on tags matches
  609. link-search when exposing search matches associated with a link
  610. mark-goto when exposing the jump goal of a mark
  611. bookmark-jump when exposing a bookmark location
  612. isearch when exiting from an incremental search
  613. default default for all contexts not set explicitly"
  614. :group 'org-reveal-location
  615. :type org-context-choice)
  616. (defcustom org-show-following-heading '((default . nil))
  617. "Non-nil means show following heading when revealing a location.
  618. Org-mode often shows locations in an org-mode file which might have
  619. been invisible before. When this is set, the heading following the
  620. match is shown.
  621. Turning this off for example for sparse trees makes them very compact,
  622. but makes it harder to edit the location of the match. In such a case,
  623. use the command \\[org-reveal] to show more context.
  624. Instead of t, this can also be an alist specifying this option for different
  625. contexts. See `org-show-hierarchy-above' for valid contexts."
  626. :group 'org-reveal-location
  627. :type org-context-choice)
  628. (defcustom org-show-siblings '((default . nil) (isearch t))
  629. "Non-nil means show all sibling 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 sibling of the current entry
  632. heading are all made visible. If `org-show-hierarchy-above' is t,
  633. the same happens on each level of the hierarchy above the current entry.
  634. By default this is on for the isearch context, off for all other contexts.
  635. Turning this off for example for sparse trees makes them very compact,
  636. but makes it harder to edit the location of the match. In such a case,
  637. use the command \\[org-reveal] to show more context.
  638. Instead of t, this can also be an alist specifying this option for different
  639. contexts. See `org-show-hierarchy-above' for valid contexts."
  640. :group 'org-reveal-location
  641. :type org-context-choice)
  642. (defcustom org-show-entry-below '((default . nil))
  643. "Non-nil means show the entry below a headline when revealing a location.
  644. Org-mode often shows locations in an org-mode file which might have
  645. been invisible before. When this is set, the text below the headline that is
  646. exposed is also shown.
  647. By default this is off for all contexts.
  648. Instead of t, this can also be an alist specifying this option for different
  649. contexts. See `org-show-hierarchy-above' for valid contexts."
  650. :group 'org-reveal-location
  651. :type org-context-choice)
  652. (defcustom org-indirect-buffer-display 'other-window
  653. "How should indirect tree buffers be displayed?
  654. This applies to indirect buffers created with the commands
  655. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  656. Valid values are:
  657. current-window Display in the current window
  658. other-window Just display in another window.
  659. dedicated-frame Create one new frame, and re-use it each time.
  660. new-frame Make a new frame each time. Note that in this case
  661. previously-made indirect buffers are kept, and you need to
  662. kill these buffers yourself."
  663. :group 'org-structure
  664. :group 'org-agenda-windows
  665. :type '(choice
  666. (const :tag "In current window" current-window)
  667. (const :tag "In current frame, other window" other-window)
  668. (const :tag "Each time a new frame" new-frame)
  669. (const :tag "One dedicated frame" dedicated-frame)))
  670. (defcustom org-use-speed-commands nil
  671. "Non-nil means activate single letter commands at beginning of a headline.
  672. This may also be a function to test for appropriate locations where speed
  673. commands should be active."
  674. :group 'org-structure
  675. :type '(choice
  676. (const :tag "Never" nil)
  677. (const :tag "At beginning of headline stars" t)
  678. (function)))
  679. (defcustom org-speed-commands-user nil
  680. "Alist of additional speed commands.
  681. This list will be checked before `org-speed-commands-default'
  682. when the variable `org-use-speed-commands' is non-nil
  683. and when the cursor is at the beginning of a headline.
  684. The car if each entry is a string with a single letter, which must
  685. be assigned to `self-insert-command' in the global map.
  686. The cdr is either a command to be called interactively, a function
  687. to be called, or a form to be evaluated.
  688. An entry that is just a list with a single string will be interpreted
  689. as a descriptive headline that will be added when listing the speed
  690. commands in the Help buffer using the `?' speed command."
  691. :group 'org-structure
  692. :type '(repeat :value ("k" . ignore)
  693. (choice :value ("k" . ignore)
  694. (list :tag "Descriptive Headline" (string :tag "Headline"))
  695. (cons :tag "Letter and Command"
  696. (string :tag "Command letter")
  697. (choice
  698. (function)
  699. (sexp))))))
  700. (defgroup org-cycle nil
  701. "Options concerning visibility cycling in Org-mode."
  702. :tag "Org Cycle"
  703. :group 'org-structure)
  704. (defcustom org-cycle-skip-children-state-if-no-children t
  705. "Non-nil means skip CHILDREN state in entries that don't have any."
  706. :group 'org-cycle
  707. :type 'boolean)
  708. (defcustom org-cycle-max-level nil
  709. "Maximum level which should still be subject to visibility cycling.
  710. Levels higher than this will, for cycling, be treated as text, not a headline.
  711. When `org-odd-levels-only' is set, a value of N in this variable actually
  712. means 2N-1 stars as the limiting headline.
  713. When nil, cycle all levels.
  714. Note that the limiting level of cycling is also influenced by
  715. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  716. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  717. than its value."
  718. :group 'org-cycle
  719. :type '(choice
  720. (const :tag "No limit" nil)
  721. (integer :tag "Maximum level")))
  722. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  723. "Names of drawers. Drawers are not opened by cycling on the headline above.
  724. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  725. this:
  726. :DRAWERNAME:
  727. .....
  728. :END:
  729. The drawer \"PROPERTIES\" is special for capturing properties through
  730. the property API.
  731. Drawers can be defined on the per-file basis with a line like:
  732. #+DRAWERS: HIDDEN STATE PROPERTIES"
  733. :group 'org-structure
  734. :group 'org-cycle
  735. :type '(repeat (string :tag "Drawer Name")))
  736. (defcustom org-hide-block-startup nil
  737. "Non-nil means entering Org-mode will fold all blocks.
  738. This can also be set in on a per-file basis with
  739. #+STARTUP: hideblocks
  740. #+STARTUP: showblocks"
  741. :group 'org-startup
  742. :group 'org-cycle
  743. :type 'boolean)
  744. (defcustom org-cycle-global-at-bob nil
  745. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  746. This makes it possible to do global cycling without having to use S-TAB or
  747. \\[universal-argument] TAB. For this special case to work, the first line \
  748. of the buffer
  749. must not be a headline - it may be empty or some other text. When used in
  750. this way, `org-cycle-hook' is disables temporarily, to make sure the
  751. cursor stays at the beginning of the buffer.
  752. When this option is nil, don't do anything special at the beginning
  753. of the buffer."
  754. :group 'org-cycle
  755. :type 'boolean)
  756. (defcustom org-cycle-level-after-item/entry-creation t
  757. "Non-nil means cycle entry level or item indentation in new empty entries.
  758. When the cursor is at the end of an empty headline, i.e with only stars
  759. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  760. and then all possible ancestor states, before returning to the original state.
  761. This makes data entry extremely fast: M-RET to create a new headline,
  762. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  763. When the cursor is at the end of an empty plain list item, one TAB will
  764. make it a subitem, two or more tabs will back up to make this an item
  765. higher up in the item hierarchy."
  766. :group 'org-cycle
  767. :type 'boolean)
  768. (defcustom org-cycle-emulate-tab t
  769. "Where should `org-cycle' emulate TAB.
  770. nil Never
  771. white Only in completely white lines
  772. whitestart Only at the beginning of lines, before the first non-white char
  773. t Everywhere except in headlines
  774. exc-hl-bol Everywhere except at the start of a headline
  775. If TAB is used in a place where it does not emulate TAB, the current subtree
  776. visibility is cycled."
  777. :group 'org-cycle
  778. :type '(choice (const :tag "Never" nil)
  779. (const :tag "Only in completely white lines" white)
  780. (const :tag "Before first char in a line" whitestart)
  781. (const :tag "Everywhere except in headlines" t)
  782. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  783. ))
  784. (defcustom org-cycle-separator-lines 2
  785. "Number of empty lines needed to keep an empty line between collapsed trees.
  786. If you leave an empty line between the end of a subtree and the following
  787. headline, this empty line is hidden when the subtree is folded.
  788. Org-mode will leave (exactly) one empty line visible if the number of
  789. empty lines is equal or larger to the number given in this variable.
  790. So the default 2 means at least 2 empty lines after the end of a subtree
  791. are needed to produce free space between a collapsed subtree and the
  792. following headline.
  793. If the number is negative, and the number of empty lines is at least -N,
  794. all empty lines are shown.
  795. Special case: when 0, never leave empty lines in collapsed view."
  796. :group 'org-cycle
  797. :type 'integer)
  798. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  799. (defcustom org-pre-cycle-hook nil
  800. "Hook that is run before visibility cycling is happening.
  801. The function(s) in this hook must accept a single argument which indicates
  802. the new state that will be set right after running this hook. The
  803. argument is a symbol. Before a global state change, it can have the values
  804. `overview', `content', or `all'. Before a local state change, it can have
  805. the values `folded', `children', or `subtree'."
  806. :group 'org-cycle
  807. :type 'hook)
  808. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  809. org-cycle-hide-drawers
  810. org-cycle-show-empty-lines
  811. org-optimize-window-after-visibility-change)
  812. "Hook that is run after `org-cycle' has changed the buffer visibility.
  813. The function(s) in this hook must accept a single argument which indicates
  814. the new state that was set by the most recent `org-cycle' command. The
  815. argument is a symbol. After a global state change, it can have the values
  816. `overview', `content', or `all'. After a local state change, it can have
  817. the values `folded', `children', or `subtree'."
  818. :group 'org-cycle
  819. :type 'hook)
  820. (defgroup org-edit-structure nil
  821. "Options concerning structure editing in Org-mode."
  822. :tag "Org Edit Structure"
  823. :group 'org-structure)
  824. (defcustom org-odd-levels-only nil
  825. "Non-nil means skip even levels and only use odd levels for the outline.
  826. This has the effect that two stars are being added/taken away in
  827. promotion/demotion commands. It also influences how levels are
  828. handled by the exporters.
  829. Changing it requires restart of `font-lock-mode' to become effective
  830. for fontification also in regions already fontified.
  831. You may also set this on a per-file basis by adding one of the following
  832. lines to the buffer:
  833. #+STARTUP: odd
  834. #+STARTUP: oddeven"
  835. :group 'org-edit-structure
  836. :group 'org-appearance
  837. :type 'boolean)
  838. (defcustom org-adapt-indentation t
  839. "Non-nil means adapt indentation to outline node level.
  840. When this variable is set, Org assumes that you write outlines by
  841. indenting text in each node to align with the headline (after the stars).
  842. The following issues are influenced by this variable:
  843. - When this is set and the *entire* text in an entry is indented, the
  844. indentation is increased by one space in a demotion command, and
  845. decreased by one in a promotion command. If any line in the entry
  846. body starts with text at column 0, indentation is not changed at all.
  847. - Property drawers and planning information is inserted indented when
  848. this variable s set. When nil, they will not be indented.
  849. - TAB indents a line relative to context. The lines below a headline
  850. will be indented when this variable is set.
  851. Note that this is all about true indentation, by adding and removing
  852. space characters. See also `org-indent.el' which does level-dependent
  853. indentation in a virtual way, i.e. at display time in Emacs."
  854. :group 'org-edit-structure
  855. :type 'boolean)
  856. (defcustom org-special-ctrl-a/e nil
  857. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  858. When t, `C-a' will bring back the cursor to the beginning of the
  859. headline text, i.e. after the stars and after a possible TODO keyword.
  860. In an item, this will be the position after the bullet.
  861. When the cursor is already at that position, another `C-a' will bring
  862. it to the beginning of the line.
  863. `C-e' will jump to the end of the headline, ignoring the presence of tags
  864. in the headline. A second `C-e' will then jump to the true end of the
  865. line, after any tags. This also means that, when this variable is
  866. non-nil, `C-e' also will never jump beyond the end of the heading of a
  867. folded section, i.e. not after the ellipses.
  868. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  869. going to the true line boundary first. Only a directly following, identical
  870. keypress will bring the cursor to the special positions.
  871. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  872. set separately."
  873. :group 'org-edit-structure
  874. :type '(choice
  875. (const :tag "off" nil)
  876. (const :tag "on: after stars/bullet and before tags first" t)
  877. (const :tag "reversed: true line boundary first" reversed)
  878. (cons :tag "Set C-a and C-e separately"
  879. (choice :tag "Special C-a"
  880. (const :tag "off" nil)
  881. (const :tag "on: after stars/bullet first" t)
  882. (const :tag "reversed: before stars/bullet first" reversed))
  883. (choice :tag "Special C-e"
  884. (const :tag "off" nil)
  885. (const :tag "on: before tags first" t)
  886. (const :tag "reversed: after tags first" reversed)))))
  887. (if (fboundp 'defvaralias)
  888. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  889. (defcustom org-special-ctrl-k nil
  890. "Non-nil means `C-k' will behave specially in headlines.
  891. When nil, `C-k' will call the default `kill-line' command.
  892. When t, the following will happen while the cursor is in the headline:
  893. - When the cursor is at the beginning of a headline, kill the entire
  894. line and possible the folded subtree below the line.
  895. - When in the middle of the headline text, kill the headline up to the tags.
  896. - When after the headline text, kill the tags."
  897. :group 'org-edit-structure
  898. :type 'boolean)
  899. (defcustom org-ctrl-k-protect-subtree nil
  900. "Non-nil means, do not delete a hidden subtree with C-k.
  901. When set to the symbol `error', simply throw an error when C-k is
  902. used to kill (part-of) a headline that has hidden text behind it.
  903. Any other non-nil value will result in a query to the user, if it is
  904. OK to kill that hidden subtree. When nil, kill without remorse."
  905. :group 'org-edit-structure
  906. :type '(choice
  907. (const :tag "Do not protect hidden subtrees" nil)
  908. (const :tag "Protect hidden subtrees with a security query" t)
  909. (const :tag "Never kill a hidden subtree with C-k" error)))
  910. (defcustom org-yank-folded-subtrees t
  911. "Non-nil means when yanking subtrees, fold them.
  912. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  913. it starts with a heading and all other headings in it are either children
  914. or siblings, then fold all the subtrees. However, do this only if no
  915. text after the yank would be swallowed into a folded tree by this action."
  916. :group 'org-edit-structure
  917. :type 'boolean)
  918. (defcustom org-yank-adjusted-subtrees nil
  919. "Non-nil means when yanking subtrees, adjust the level.
  920. With this setting, `org-paste-subtree' is used to insert the subtree, see
  921. this function for details."
  922. :group 'org-edit-structure
  923. :type 'boolean)
  924. (defcustom org-M-RET-may-split-line '((default . t))
  925. "Non-nil means M-RET will split the line at the cursor position.
  926. When nil, it will go to the end of the line before making a
  927. new line.
  928. You may also set this option in a different way for different
  929. contexts. Valid contexts are:
  930. headline when creating a new headline
  931. item when creating a new item
  932. table in a table field
  933. default the value to be used for all contexts not explicitly
  934. customized"
  935. :group 'org-structure
  936. :group 'org-table
  937. :type '(choice
  938. (const :tag "Always" t)
  939. (const :tag "Never" nil)
  940. (repeat :greedy t :tag "Individual contexts"
  941. (cons
  942. (choice :tag "Context"
  943. (const headline)
  944. (const item)
  945. (const table)
  946. (const default))
  947. (boolean)))))
  948. (defcustom org-insert-heading-respect-content nil
  949. "Non-nil means insert new headings after the current subtree.
  950. When nil, the new heading is created directly after the current line.
  951. The commands \\[org-insert-heading-respect-content] and
  952. \\[org-insert-todo-heading-respect-content] turn this variable on
  953. for the duration of the command."
  954. :group 'org-structure
  955. :type 'boolean)
  956. (defcustom org-blank-before-new-entry '((heading . auto)
  957. (plain-list-item . auto))
  958. "Should `org-insert-heading' leave a blank line before new heading/item?
  959. The value is an alist, with `heading' and `plain-list-item' as car,
  960. and a boolean flag as cdr. The cdr may also be the symbol `auto', and then
  961. Org will look at the surrounding headings/items and try to make an
  962. intelligent decision whether to insert a blank line or not.
  963. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  964. set, the setting here is ignored and no empty line is inserted, to avoid
  965. breaking the list structure."
  966. :group 'org-edit-structure
  967. :type '(list
  968. (cons (const heading)
  969. (choice (const :tag "Never" nil)
  970. (const :tag "Always" t)
  971. (const :tag "Auto" auto)))
  972. (cons (const plain-list-item)
  973. (choice (const :tag "Never" nil)
  974. (const :tag "Always" t)
  975. (const :tag "Auto" auto)))))
  976. (defcustom org-insert-heading-hook nil
  977. "Hook being run after inserting a new heading."
  978. :group 'org-edit-structure
  979. :type 'hook)
  980. (defcustom org-enable-fixed-width-editor t
  981. "Non-nil means lines starting with \":\" are treated as fixed-width.
  982. This currently only means they are never auto-wrapped.
  983. When nil, such lines will be treated like ordinary lines.
  984. See also the QUOTE keyword."
  985. :group 'org-edit-structure
  986. :type 'boolean)
  987. (defcustom org-goto-auto-isearch t
  988. "Non-nil means typing characters in `org-goto' starts incremental search."
  989. :group 'org-edit-structure
  990. :type 'boolean)
  991. (defgroup org-sparse-trees nil
  992. "Options concerning sparse trees in Org-mode."
  993. :tag "Org Sparse Trees"
  994. :group 'org-structure)
  995. (defcustom org-highlight-sparse-tree-matches t
  996. "Non-nil means highlight all matches that define a sparse tree.
  997. The highlights will automatically disappear the next time the buffer is
  998. changed by an edit command."
  999. :group 'org-sparse-trees
  1000. :type 'boolean)
  1001. (defcustom org-remove-highlights-with-change t
  1002. "Non-nil means any change to the buffer will remove temporary highlights.
  1003. Such highlights are created by `org-occur' and `org-clock-display'.
  1004. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1005. The highlights created by `org-preview-latex-fragment' always need
  1006. `C-c C-c' to be removed."
  1007. :group 'org-sparse-trees
  1008. :group 'org-time
  1009. :type 'boolean)
  1010. (defcustom org-occur-hook '(org-first-headline-recenter)
  1011. "Hook that is run after `org-occur' has constructed a sparse tree.
  1012. This can be used to recenter the window to show as much of the structure
  1013. as possible."
  1014. :group 'org-sparse-trees
  1015. :type 'hook)
  1016. (defgroup org-imenu-and-speedbar nil
  1017. "Options concerning imenu and speedbar in Org-mode."
  1018. :tag "Org Imenu and Speedbar"
  1019. :group 'org-structure)
  1020. (defcustom org-imenu-depth 2
  1021. "The maximum level for Imenu access to Org-mode headlines.
  1022. This also applied for speedbar access."
  1023. :group 'org-imenu-and-speedbar
  1024. :type 'integer)
  1025. (defgroup org-table nil
  1026. "Options concerning tables in Org-mode."
  1027. :tag "Org Table"
  1028. :group 'org)
  1029. (defcustom org-enable-table-editor 'optimized
  1030. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1031. When nil, such lines will be treated like ordinary lines.
  1032. When equal to the symbol `optimized', the table editor will be optimized to
  1033. do the following:
  1034. - Automatic overwrite mode in front of whitespace in table fields.
  1035. This makes the structure of the table stay in tact as long as the edited
  1036. field does not exceed the column width.
  1037. - Minimize the number of realigns. Normally, the table is aligned each time
  1038. TAB or RET are pressed to move to another field. With optimization this
  1039. happens only if changes to a field might have changed the column width.
  1040. Optimization requires replacing the functions `self-insert-command',
  1041. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1042. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1043. very good at guessing when a re-align will be necessary, but you can always
  1044. force one with \\[org-ctrl-c-ctrl-c].
  1045. If you would like to use the optimized version in Org-mode, but the
  1046. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1047. This variable can be used to turn on and off the table editor during a session,
  1048. but in order to toggle optimization, a restart is required.
  1049. See also the variable `org-table-auto-blank-field'."
  1050. :group 'org-table
  1051. :type '(choice
  1052. (const :tag "off" nil)
  1053. (const :tag "on" t)
  1054. (const :tag "on, optimized" optimized)))
  1055. (defcustom org-self-insert-cluster-for-undo t
  1056. "Non-nil means cluster self-insert commands for undo when possible.
  1057. If this is set, then, like in the Emacs command loop, 20 consecutive
  1058. characters will be undone together.
  1059. This is configurable, because there is some impact on typing performance."
  1060. :group 'org-table
  1061. :type 'boolean)
  1062. (defcustom org-table-tab-recognizes-table.el t
  1063. "Non-nil means TAB will automatically notice a table.el table.
  1064. When it sees such a table, it moves point into it and - if necessary -
  1065. calls `table-recognize-table'."
  1066. :group 'org-table-editing
  1067. :type 'boolean)
  1068. (defgroup org-link nil
  1069. "Options concerning links in Org-mode."
  1070. :tag "Org Link"
  1071. :group 'org)
  1072. (defvar org-link-abbrev-alist-local nil
  1073. "Buffer-local version of `org-link-abbrev-alist', which see.
  1074. The value of this is taken from the #+LINK lines.")
  1075. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1076. (defcustom org-link-abbrev-alist nil
  1077. "Alist of link abbreviations.
  1078. The car of each element is a string, to be replaced at the start of a link.
  1079. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1080. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1081. [[linkkey:tag][description]]
  1082. The 'linkkey' must be a word word, starting with a letter, followed
  1083. by letters, numbers, '-' or '_'.
  1084. If REPLACE is a string, the tag will simply be appended to create the link.
  1085. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1086. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1087. be inserted at that point (see the function `url-hexify-string').
  1088. REPLACE may also be a function that will be called with the tag as the
  1089. only argument to create the link, which should be returned as a string.
  1090. See the manual for examples."
  1091. :group 'org-link
  1092. :type '(repeat
  1093. (cons
  1094. (string :tag "Protocol")
  1095. (choice
  1096. (string :tag "Format")
  1097. (function)))))
  1098. (defcustom org-descriptive-links t
  1099. "Non-nil means hide link part and only show description of bracket links.
  1100. Bracket links are like [[link][description]]. This variable sets the initial
  1101. state in new org-mode buffers. The setting can then be toggled on a
  1102. per-buffer basis from the Org->Hyperlinks menu."
  1103. :group 'org-link
  1104. :type 'boolean)
  1105. (defcustom org-link-file-path-type 'adaptive
  1106. "How the path name in file links should be stored.
  1107. Valid values are:
  1108. relative Relative to the current directory, i.e. the directory of the file
  1109. into which the link is being inserted.
  1110. absolute Absolute path, if possible with ~ for home directory.
  1111. noabbrev Absolute path, no abbreviation of home directory.
  1112. adaptive Use relative path for files in the current directory and sub-
  1113. directories of it. For other files, use an absolute path."
  1114. :group 'org-link
  1115. :type '(choice
  1116. (const relative)
  1117. (const absolute)
  1118. (const noabbrev)
  1119. (const adaptive)))
  1120. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1121. "Types of links that should be activated in Org-mode files.
  1122. This is a list of symbols, each leading to the activation of a certain link
  1123. type. In principle, it does not hurt to turn on most link types - there may
  1124. be a small gain when turning off unused link types. The types are:
  1125. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1126. angle Links in angular brackets that may contain whitespace like
  1127. <bbdb:Carsten Dominik>.
  1128. plain Plain links in normal text, no whitespace, like http://google.com.
  1129. radio Text that is matched by a radio target, see manual for details.
  1130. tag Tag settings in a headline (link to tag search).
  1131. date Time stamps (link to calendar).
  1132. footnote Footnote labels.
  1133. Changing this variable requires a restart of Emacs to become effective."
  1134. :group 'org-link
  1135. :type '(set :greedy t
  1136. (const :tag "Double bracket links" bracket)
  1137. (const :tag "Angular bracket links" angle)
  1138. (const :tag "Plain text links" plain)
  1139. (const :tag "Radio target matches" radio)
  1140. (const :tag "Tags" tag)
  1141. (const :tag "Timestamps" date)
  1142. (const :tag "Footnotes" footnote)))
  1143. (defcustom org-make-link-description-function nil
  1144. "Function to use to generate link descriptions from links.
  1145. If nil the link location will be used. This function must take
  1146. two parameters; the first is the link and the second the
  1147. description `org-insert-link' has generated, and should return the
  1148. description to use."
  1149. :group 'org-link
  1150. :type 'function)
  1151. (defgroup org-link-store nil
  1152. "Options concerning storing links in Org-mode."
  1153. :tag "Org Store Link"
  1154. :group 'org-link)
  1155. (defcustom org-email-link-description-format "Email %c: %.30s"
  1156. "Format of the description part of a link to an email or usenet message.
  1157. The following %-escapes will be replaced by corresponding information:
  1158. %F full \"From\" field
  1159. %f name, taken from \"From\" field, address if no name
  1160. %T full \"To\" field
  1161. %t first name in \"To\" field, address if no name
  1162. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1163. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1164. %s subject
  1165. %d date
  1166. %m message-id.
  1167. You may use normal field width specification between the % and the letter.
  1168. This is for example useful to limit the length of the subject.
  1169. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1170. :group 'org-link-store
  1171. :type 'string)
  1172. (defcustom org-from-is-user-regexp
  1173. (let (r1 r2)
  1174. (when (and user-mail-address (not (string= user-mail-address "")))
  1175. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1176. (when (and user-full-name (not (string= user-full-name "")))
  1177. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1178. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1179. "Regexp matched against the \"From:\" header of an email or usenet message.
  1180. It should match if the message is from the user him/herself."
  1181. :group 'org-link-store
  1182. :type 'regexp)
  1183. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1184. "Non-nil means storing a link to an Org file will use entry IDs.
  1185. Note that before this variable is even considered, org-id must be loaded,
  1186. so please customize `org-modules' and turn it on.
  1187. The variable can have the following values:
  1188. t Create an ID if needed to make a link to the current entry.
  1189. create-if-interactive
  1190. If `org-store-link' is called directly (interactively, as a user
  1191. command), do create an ID to support the link. But when doing the
  1192. job for remember, only use the ID if it already exists. The
  1193. purpose of this setting is to avoid proliferation of unwanted
  1194. IDs, just because you happen to be in an Org file when you
  1195. call `org-remember' that automatically and preemptively
  1196. creates a link. If you do want to get an ID link in a remember
  1197. template to an entry not having an ID, create it first by
  1198. explicitly creating a link to it, using `C-c C-l' first.
  1199. create-if-interactive-and-no-custom-id
  1200. Like create-if-interactive, but do not create an ID if there is
  1201. a CUSTOM_ID property defined in the entry. This is the default.
  1202. use-existing
  1203. Use existing ID, do not create one.
  1204. nil Never use an ID to make a link, instead link using a text search for
  1205. the headline text."
  1206. :group 'org-link-store
  1207. :type '(choice
  1208. (const :tag "Create ID to make link" t)
  1209. (const :tag "Create if storing link interactively"
  1210. create-if-interactive)
  1211. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1212. create-if-interactive-and-no-custom-id)
  1213. (const :tag "Only use existing" use-existing)
  1214. (const :tag "Do not use ID to create link" nil)))
  1215. (defcustom org-context-in-file-links t
  1216. "Non-nil means file links from `org-store-link' contain context.
  1217. A search string will be added to the file name with :: as separator and
  1218. used to find the context when the link is activated by the command
  1219. `org-open-at-point'. When this option is t, the entire active region
  1220. will be placed in the search string of the file link. If set to a
  1221. positive integer, only the first n lines of context will be stored.
  1222. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1223. negates this setting for the duration of the command."
  1224. :group 'org-link-store
  1225. :type '(choice boolean integer))
  1226. (defcustom org-keep-stored-link-after-insertion nil
  1227. "Non-nil means keep link in list for entire session.
  1228. The command `org-store-link' adds a link pointing to the current
  1229. location to an internal list. These links accumulate during a session.
  1230. The command `org-insert-link' can be used to insert links into any
  1231. Org-mode file (offering completion for all stored links). When this
  1232. option is nil, every link which has been inserted once using \\[org-insert-link]
  1233. will be removed from the list, to make completing the unused links
  1234. more efficient."
  1235. :group 'org-link-store
  1236. :type 'boolean)
  1237. (defgroup org-link-follow nil
  1238. "Options concerning following links in Org-mode."
  1239. :tag "Org Follow Link"
  1240. :group 'org-link)
  1241. (defcustom org-link-translation-function nil
  1242. "Function to translate links with different syntax to Org syntax.
  1243. This can be used to translate links created for example by the Planner
  1244. or emacs-wiki packages to Org syntax.
  1245. The function must accept two parameters, a TYPE containing the link
  1246. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1247. which is everything after the link protocol. It should return a cons
  1248. with possibly modified values of type and path.
  1249. Org contains a function for this, so if you set this variable to
  1250. `org-translate-link-from-planner', you should be able follow many
  1251. links created by planner."
  1252. :group 'org-link-follow
  1253. :type 'function)
  1254. (defcustom org-follow-link-hook nil
  1255. "Hook that is run after a link has been followed."
  1256. :group 'org-link-follow
  1257. :type 'hook)
  1258. (defcustom org-tab-follows-link nil
  1259. "Non-nil means on links TAB will follow the link.
  1260. Needs to be set before org.el is loaded.
  1261. This really should not be used, it does not make sense, and the
  1262. implementation is bad."
  1263. :group 'org-link-follow
  1264. :type 'boolean)
  1265. (defcustom org-return-follows-link nil
  1266. "Non-nil means on links RET will follow the link."
  1267. :group 'org-link-follow
  1268. :type 'boolean)
  1269. (defcustom org-mouse-1-follows-link
  1270. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1271. "Non-nil means mouse-1 on a link will follow the link.
  1272. A longer mouse click will still set point. Does not work on XEmacs.
  1273. Needs to be set before org.el is loaded."
  1274. :group 'org-link-follow
  1275. :type 'boolean)
  1276. (defcustom org-mark-ring-length 4
  1277. "Number of different positions to be recorded in the ring.
  1278. Changing this requires a restart of Emacs to work correctly."
  1279. :group 'org-link-follow
  1280. :type 'integer)
  1281. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1282. "Non-nil means internal links in Org files must exactly match a headline.
  1283. When nil, the link search tries to match a phrase with all words
  1284. in the search text."
  1285. :group 'org-link-follow
  1286. :type '(choice
  1287. (const :tag "Use fuzy text search" nil)
  1288. (const :tag "Match only exact headline" t)
  1289. (const :tag "Match extact headline or query to create it"
  1290. query-to-create)))
  1291. (defcustom org-link-frame-setup
  1292. '((vm . vm-visit-folder-other-frame)
  1293. (gnus . org-gnus-no-new-news)
  1294. (file . find-file-other-window)
  1295. (wl . wl-other-frame))
  1296. "Setup the frame configuration for following links.
  1297. When following a link with Emacs, it may often be useful to display
  1298. this link in another window or frame. This variable can be used to
  1299. set this up for the different types of links.
  1300. For VM, use any of
  1301. `vm-visit-folder'
  1302. `vm-visit-folder-other-window'
  1303. `vm-visit-folder-other-frame'
  1304. For Gnus, use any of
  1305. `gnus'
  1306. `gnus-other-frame'
  1307. `org-gnus-no-new-news'
  1308. For FILE, use any of
  1309. `find-file'
  1310. `find-file-other-window'
  1311. `find-file-other-frame'
  1312. For Wanderlust use any of
  1313. `wl'
  1314. `wl-other-frame'
  1315. For the calendar, use the variable `calendar-setup'.
  1316. For BBDB, it is currently only possible to display the matches in
  1317. another window."
  1318. :group 'org-link-follow
  1319. :type '(list
  1320. (cons (const vm)
  1321. (choice
  1322. (const vm-visit-folder)
  1323. (const vm-visit-folder-other-window)
  1324. (const vm-visit-folder-other-frame)))
  1325. (cons (const gnus)
  1326. (choice
  1327. (const gnus)
  1328. (const gnus-other-frame)
  1329. (const org-gnus-no-new-news)))
  1330. (cons (const file)
  1331. (choice
  1332. (const find-file)
  1333. (const find-file-other-window)
  1334. (const find-file-other-frame)))
  1335. (cons (const wl)
  1336. (choice
  1337. (const wl)
  1338. (const wl-other-frame)))))
  1339. (defcustom org-display-internal-link-with-indirect-buffer nil
  1340. "Non-nil means use indirect buffer to display infile links.
  1341. Activating internal links (from one location in a file to another location
  1342. in the same file) normally just jumps to the location. When the link is
  1343. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1344. is displayed in
  1345. another window. When this option is set, the other window actually displays
  1346. an indirect buffer clone of the current buffer, to avoid any visibility
  1347. changes to the current buffer."
  1348. :group 'org-link-follow
  1349. :type 'boolean)
  1350. (defcustom org-open-non-existing-files nil
  1351. "Non-nil means `org-open-file' will open non-existing files.
  1352. When nil, an error will be generated.
  1353. This variable applies only to external applications because they
  1354. might choke on non-existing files. If the link is to a file that
  1355. will be opened in Emacs, the variable is ignored."
  1356. :group 'org-link-follow
  1357. :type 'boolean)
  1358. (defcustom org-open-directory-means-index-dot-org nil
  1359. "Non-nil means a link to a directory really means to index.org.
  1360. When nil, following a directory link will run dired or open a finder/explorer
  1361. window on that directory."
  1362. :group 'org-link-follow
  1363. :type 'boolean)
  1364. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1365. "Function and arguments to call for following mailto links.
  1366. This is a list with the first element being a Lisp function, and the
  1367. remaining elements being arguments to the function. In string arguments,
  1368. %a will be replaced by the address, and %s will be replaced by the subject
  1369. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1370. :group 'org-link-follow
  1371. :type '(choice
  1372. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1373. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1374. (const :tag "message-mail" (message-mail "%a" "%s"))
  1375. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1376. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1377. "Non-nil means ask for confirmation before executing shell links.
  1378. Shell links can be dangerous: just think about a link
  1379. [[shell:rm -rf ~/*][Google Search]]
  1380. This link would show up in your Org-mode document as \"Google Search\",
  1381. but really it would remove your entire home directory.
  1382. Therefore we advise against setting this variable to nil.
  1383. Just change it to `y-or-n-p' if you want to confirm with a
  1384. single keystroke rather than having to type \"yes\"."
  1385. :group 'org-link-follow
  1386. :type '(choice
  1387. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1388. (const :tag "with y-or-n (faster)" y-or-n-p)
  1389. (const :tag "no confirmation (dangerous)" nil)))
  1390. (put 'org-confirm-shell-link-function
  1391. 'safe-local-variable
  1392. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1393. (defcustom org-confirm-shell-link-not-regexp ""
  1394. "A regexp to skip confirmation for shell links."
  1395. :group 'org-link-follow
  1396. :type 'regexp)
  1397. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1398. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1399. Elisp links can be dangerous: just think about a link
  1400. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1401. This link would show up in your Org-mode document as \"Google Search\",
  1402. but really it would remove your entire home directory.
  1403. Therefore we advise against setting this variable to nil.
  1404. Just change it to `y-or-n-p' if you want to confirm with a
  1405. single keystroke rather than having to type \"yes\"."
  1406. :group 'org-link-follow
  1407. :type '(choice
  1408. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1409. (const :tag "with y-or-n (faster)" y-or-n-p)
  1410. (const :tag "no confirmation (dangerous)" nil)))
  1411. (put 'org-confirm-shell-link-function
  1412. 'safe-local-variable
  1413. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1414. (defcustom org-confirm-elisp-link-not-regexp ""
  1415. "A regexp to skip confirmation for Elisp links."
  1416. :group 'org-link-follow
  1417. :type 'regexp)
  1418. (defconst org-file-apps-defaults-gnu
  1419. '((remote . emacs)
  1420. (system . mailcap)
  1421. (t . mailcap))
  1422. "Default file applications on a UNIX or GNU/Linux system.
  1423. See `org-file-apps'.")
  1424. (defconst org-file-apps-defaults-macosx
  1425. '((remote . emacs)
  1426. (t . "open %s")
  1427. (system . "open %s")
  1428. ("ps.gz" . "gv %s")
  1429. ("eps.gz" . "gv %s")
  1430. ("dvi" . "xdvi %s")
  1431. ("fig" . "xfig %s"))
  1432. "Default file applications on a MacOS X system.
  1433. The system \"open\" is known as a default, but we use X11 applications
  1434. for some files for which the OS does not have a good default.
  1435. See `org-file-apps'.")
  1436. (defconst org-file-apps-defaults-windowsnt
  1437. (list
  1438. '(remote . emacs)
  1439. (cons t
  1440. (list (if (featurep 'xemacs)
  1441. 'mswindows-shell-execute
  1442. 'w32-shell-execute)
  1443. "open" 'file))
  1444. (cons 'system
  1445. (list (if (featurep 'xemacs)
  1446. 'mswindows-shell-execute
  1447. 'w32-shell-execute)
  1448. "open" 'file)))
  1449. "Default file applications on a Windows NT system.
  1450. The system \"open\" is used for most files.
  1451. See `org-file-apps'.")
  1452. (defcustom org-file-apps
  1453. '(
  1454. (auto-mode . emacs)
  1455. ("\\.mm\\'" . default)
  1456. ("\\.x?html?\\'" . default)
  1457. ("\\.pdf\\'" . default)
  1458. )
  1459. "External applications for opening `file:path' items in a document.
  1460. Org-mode uses system defaults for different file types, but
  1461. you can use this variable to set the application for a given file
  1462. extension. The entries in this list are cons cells where the car identifies
  1463. files and the cdr the corresponding command. Possible values for the
  1464. file identifier are
  1465. \"string\" A string as a file identifier can be interpreted in different
  1466. ways, depending on its contents:
  1467. - Alphanumeric characters only:
  1468. Match links with this file extension.
  1469. Example: (\"pdf\" . \"evince %s\")
  1470. to open PDFs with evince.
  1471. - Regular expression: Match links where the
  1472. filename matches the regexp. If you want to
  1473. use groups here, use shy groups.
  1474. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1475. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1476. to open *.html and *.xhtml with firefox.
  1477. - Regular expression which contains (non-shy) groups:
  1478. Match links where the whole link, including \"::\", and
  1479. anything after that, matches the regexp.
  1480. In a custom command string, %1, %2, etc. are replaced with
  1481. the parts of the link that were matched by the groups.
  1482. For backwards compatibility, if a command string is given
  1483. that does not use any of the group matches, this case is
  1484. handled identically to the second one (i.e. match against
  1485. file name only).
  1486. In a custom lisp form, you can access the group matches with
  1487. (match-string n link).
  1488. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1489. to open [[file:document.pdf::5]] with evince at page 5.
  1490. `directory' Matches a directory
  1491. `remote' Matches a remote file, accessible through tramp or efs.
  1492. Remote files most likely should be visited through Emacs
  1493. because external applications cannot handle such paths.
  1494. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1495. so all files Emacs knows how to handle. Using this with
  1496. command `emacs' will open most files in Emacs. Beware that this
  1497. will also open html files inside Emacs, unless you add
  1498. (\"html\" . default) to the list as well.
  1499. t Default for files not matched by any of the other options.
  1500. `system' The system command to open files, like `open' on Windows
  1501. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1502. that will be selected if you call `C-c C-o' with a double
  1503. \\[universal-argument] \\[universal-argument] prefix.
  1504. Possible values for the command are:
  1505. `emacs' The file will be visited by the current Emacs process.
  1506. `default' Use the default application for this file type, which is the
  1507. association for t in the list, most likely in the system-specific
  1508. part.
  1509. This can be used to overrule an unwanted setting in the
  1510. system-specific variable.
  1511. `system' Use the system command for opening files, like \"open\".
  1512. This command is specified by the entry whose car is `system'.
  1513. Most likely, the system-specific version of this variable
  1514. does define this command, but you can overrule/replace it
  1515. here.
  1516. string A command to be executed by a shell; %s will be replaced
  1517. by the path to the file.
  1518. sexp A Lisp form which will be evaluated. The file path will
  1519. be available in the Lisp variable `file'.
  1520. For more examples, see the system specific constants
  1521. `org-file-apps-defaults-macosx'
  1522. `org-file-apps-defaults-windowsnt'
  1523. `org-file-apps-defaults-gnu'."
  1524. :group 'org-link-follow
  1525. :type '(repeat
  1526. (cons (choice :value ""
  1527. (string :tag "Extension")
  1528. (const :tag "System command to open files" system)
  1529. (const :tag "Default for unrecognized files" t)
  1530. (const :tag "Remote file" remote)
  1531. (const :tag "Links to a directory" directory)
  1532. (const :tag "Any files that have Emacs modes"
  1533. auto-mode))
  1534. (choice :value ""
  1535. (const :tag "Visit with Emacs" emacs)
  1536. (const :tag "Use default" default)
  1537. (const :tag "Use the system command" system)
  1538. (string :tag "Command")
  1539. (sexp :tag "Lisp form")))))
  1540. (defgroup org-refile nil
  1541. "Options concerning refiling entries in Org-mode."
  1542. :tag "Org Refile"
  1543. :group 'org)
  1544. (defcustom org-directory "~/org"
  1545. "Directory with org files.
  1546. This is just a default location to look for Org files. There is no need
  1547. at all to put your files into this directory. It is only used in the
  1548. following situations:
  1549. 1. When a remember template specifies a target file that is not an
  1550. absolute path. The path will then be interpreted relative to
  1551. `org-directory'
  1552. 2. When a remember note is filed away in an interactive way (when exiting the
  1553. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1554. with `org-directory' as the default path."
  1555. :group 'org-refile
  1556. :group 'org-remember
  1557. :type 'directory)
  1558. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1559. "Default target for storing notes.
  1560. Used as a fall back file for org-remember.el and org-capture.el, for
  1561. templates that do not specify a target file."
  1562. :group 'org-refile
  1563. :group 'org-remember
  1564. :type '(choice
  1565. (const :tag "Default from remember-data-file" nil)
  1566. file))
  1567. (defcustom org-goto-interface 'outline
  1568. "The default interface to be used for `org-goto'.
  1569. Allowed values are:
  1570. outline The interface shows an outline of the relevant file
  1571. and the correct heading is found by moving through
  1572. the outline or by searching with incremental search.
  1573. outline-path-completion Headlines in the current buffer are offered via
  1574. completion. This is the interface also used by
  1575. the refile command."
  1576. :group 'org-refile
  1577. :type '(choice
  1578. (const :tag "Outline" outline)
  1579. (const :tag "Outline-path-completion" outline-path-completion)))
  1580. (defcustom org-goto-max-level 5
  1581. "Maximum target level when running `org-goto' with refile interface."
  1582. :group 'org-refile
  1583. :type 'integer)
  1584. (defcustom org-reverse-note-order nil
  1585. "Non-nil means store new notes at the beginning of a file or entry.
  1586. When nil, new notes will be filed to the end of a file or entry.
  1587. This can also be a list with cons cells of regular expressions that
  1588. are matched against file names, and values."
  1589. :group 'org-remember
  1590. :group 'org-refile
  1591. :type '(choice
  1592. (const :tag "Reverse always" t)
  1593. (const :tag "Reverse never" nil)
  1594. (repeat :tag "By file name regexp"
  1595. (cons regexp boolean))))
  1596. (defcustom org-log-refile nil
  1597. "Information to record when a task is refiled.
  1598. Possible values are:
  1599. nil Don't add anything
  1600. time Add a time stamp to the task
  1601. note Prompt for a note and add it with template `org-log-note-headings'
  1602. This option can also be set with on a per-file-basis with
  1603. #+STARTUP: nologrefile
  1604. #+STARTUP: logrefile
  1605. #+STARTUP: lognoterefile
  1606. You can have local logging settings for a subtree by setting the LOGGING
  1607. property to one or more of these keywords.
  1608. When bulk-refiling from the agenda, the value `note' is forbidden and
  1609. will temporarily be changed to `time'."
  1610. :group 'org-refile
  1611. :group 'org-progress
  1612. :type '(choice
  1613. (const :tag "No logging" nil)
  1614. (const :tag "Record timestamp" time)
  1615. (const :tag "Record timestamp with note." note)))
  1616. (defcustom org-refile-targets nil
  1617. "Targets for refiling entries with \\[org-refile].
  1618. This is list of cons cells. Each cell contains:
  1619. - a specification of the files to be considered, either a list of files,
  1620. or a symbol whose function or variable value will be used to retrieve
  1621. a file name or a list of file names. If you use `org-agenda-files' for
  1622. that, all agenda files will be scanned for targets. Nil means consider
  1623. headings in the current buffer.
  1624. - A specification of how to find candidate refile targets. This may be
  1625. any of:
  1626. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1627. This tag has to be present in all target headlines, inheritance will
  1628. not be considered.
  1629. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1630. todo keyword.
  1631. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1632. headlines that are refiling targets.
  1633. - a cons cell (:level . N). Any headline of level N is considered a target.
  1634. Note that, when `org-odd-levels-only' is set, level corresponds to
  1635. order in hierarchy, not to the number of stars.
  1636. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1637. Note that, when `org-odd-levels-only' is set, level corresponds to
  1638. order in hierarchy, not to the number of stars.
  1639. You can set the variable `org-refile-target-verify-function' to a function
  1640. to verify each headline found by the simple criteria above.
  1641. When this variable is nil, all top-level headlines in the current buffer
  1642. are used, equivalent to the value `((nil . (:level . 1))'."
  1643. :group 'org-refile
  1644. :type '(repeat
  1645. (cons
  1646. (choice :value org-agenda-files
  1647. (const :tag "All agenda files" org-agenda-files)
  1648. (const :tag "Current buffer" nil)
  1649. (function) (variable) (file))
  1650. (choice :tag "Identify target headline by"
  1651. (cons :tag "Specific tag" (const :value :tag) (string))
  1652. (cons :tag "TODO keyword" (const :value :todo) (string))
  1653. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1654. (cons :tag "Level number" (const :value :level) (integer))
  1655. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1656. (defcustom org-refile-target-verify-function nil
  1657. "Function to verify if the headline at point should be a refile target.
  1658. The function will be called without arguments, with point at the
  1659. beginning of the headline. It should return t and leave point
  1660. where it is if the headline is a valid target for refiling.
  1661. If the target should not be selected, the function must return nil.
  1662. In addition to this, it may move point to a place from where the search
  1663. should be continued. For example, the function may decide that the entire
  1664. subtree of the current entry should be excluded and move point to the end
  1665. of the subtree."
  1666. :group 'org-refile
  1667. :type 'function)
  1668. (defcustom org-refile-use-cache nil
  1669. "Non-nil means cache refile targets to speed up the process.
  1670. The cache for a particular file will be updated automatically when
  1671. the buffer has been killed, or when any of the marker used for flagging
  1672. refile targets no longer points at a live buffer.
  1673. If you have added new entries to a buffer that might themselves be targets,
  1674. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1675. find that easier, `C-u C-u C-u C-c C-w'."
  1676. :group 'org-refile
  1677. :type 'boolean)
  1678. (defcustom org-refile-use-outline-path nil
  1679. "Non-nil means provide refile targets as paths.
  1680. So a level 3 headline will be available as level1/level2/level3.
  1681. When the value is `file', also include the file name (without directory)
  1682. into the path. In this case, you can also stop the completion after
  1683. the file name, to get entries inserted as top level in the file.
  1684. When `full-file-path', include the full file path."
  1685. :group 'org-refile
  1686. :type '(choice
  1687. (const :tag "Not" nil)
  1688. (const :tag "Yes" t)
  1689. (const :tag "Start with file name" file)
  1690. (const :tag "Start with full file path" full-file-path)))
  1691. (defcustom org-outline-path-complete-in-steps t
  1692. "Non-nil means complete the outline path in hierarchical steps.
  1693. When Org-mode uses the refile interface to select an outline path
  1694. \(see variable `org-refile-use-outline-path'), the completion of
  1695. the path can be done is a single go, or if can be done in steps down
  1696. the headline hierarchy. Going in steps is probably the best if you
  1697. do not use a special completion package like `ido' or `icicles'.
  1698. However, when using these packages, going in one step can be very
  1699. fast, while still showing the whole path to the entry."
  1700. :group 'org-refile
  1701. :type 'boolean)
  1702. (defcustom org-refile-allow-creating-parent-nodes nil
  1703. "Non-nil means allow to create new nodes as refile targets.
  1704. New nodes are then created by adding \"/new node name\" to the completion
  1705. of an existing node. When the value of this variable is `confirm',
  1706. new node creation must be confirmed by the user (recommended)
  1707. When nil, the completion must match an existing entry.
  1708. Note that, if the new heading is not seen by the criteria
  1709. listed in `org-refile-targets', multiple instances of the same
  1710. heading would be created by trying again to file under the new
  1711. heading."
  1712. :group 'org-refile
  1713. :type '(choice
  1714. (const :tag "Never" nil)
  1715. (const :tag "Always" t)
  1716. (const :tag "Prompt for confirmation" confirm)))
  1717. (defgroup org-todo nil
  1718. "Options concerning TODO items in Org-mode."
  1719. :tag "Org TODO"
  1720. :group 'org)
  1721. (defgroup org-progress nil
  1722. "Options concerning Progress logging in Org-mode."
  1723. :tag "Org Progress"
  1724. :group 'org-time)
  1725. (defvar org-todo-interpretation-widgets
  1726. '(
  1727. (:tag "Sequence (cycling hits every state)" sequence)
  1728. (:tag "Type (cycling directly to DONE)" type))
  1729. "The available interpretation symbols for customizing `org-todo-keywords'.
  1730. Interested libraries should add to this list.")
  1731. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1732. "List of TODO entry keyword sequences and their interpretation.
  1733. \\<org-mode-map>This is a list of sequences.
  1734. Each sequence starts with a symbol, either `sequence' or `type',
  1735. indicating if the keywords should be interpreted as a sequence of
  1736. action steps, or as different types of TODO items. The first
  1737. keywords are states requiring action - these states will select a headline
  1738. for inclusion into the global TODO list Org-mode produces. If one of
  1739. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1740. signify that no further action is necessary. If \"|\" is not found,
  1741. the last keyword is treated as the only DONE state of the sequence.
  1742. The command \\[org-todo] cycles an entry through these states, and one
  1743. additional state where no keyword is present. For details about this
  1744. cycling, see the manual.
  1745. TODO keywords and interpretation can also be set on a per-file basis with
  1746. the special #+SEQ_TODO and #+TYP_TODO lines.
  1747. Each keyword can optionally specify a character for fast state selection
  1748. \(in combination with the variable `org-use-fast-todo-selection')
  1749. and specifiers for state change logging, using the same syntax
  1750. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1751. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1752. indicates to record a time stamp each time this state is selected.
  1753. Each keyword may also specify if a timestamp or a note should be
  1754. recorded when entering or leaving the state, by adding additional
  1755. characters in the parenthesis after the keyword. This looks like this:
  1756. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1757. record only the time of the state change. With X and Y being either
  1758. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1759. Y when leaving the state if and only if the *target* state does not
  1760. define X. You may omit any of the fast-selection key or X or /Y,
  1761. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1762. For backward compatibility, this variable may also be just a list
  1763. of keywords - in this case the interpretation (sequence or type) will be
  1764. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1765. :group 'org-todo
  1766. :group 'org-keywords
  1767. :type '(choice
  1768. (repeat :tag "Old syntax, just keywords"
  1769. (string :tag "Keyword"))
  1770. (repeat :tag "New syntax"
  1771. (cons
  1772. (choice
  1773. :tag "Interpretation"
  1774. ;;Quick and dirty way to see
  1775. ;;`org-todo-interpretations'. This takes the
  1776. ;;place of item arguments
  1777. :convert-widget
  1778. (lambda (widget)
  1779. (widget-put widget
  1780. :args (mapcar
  1781. #'(lambda (x)
  1782. (widget-convert
  1783. (cons 'const x)))
  1784. org-todo-interpretation-widgets))
  1785. widget))
  1786. (repeat
  1787. (string :tag "Keyword"))))))
  1788. (defvar org-todo-keywords-1 nil
  1789. "All TODO and DONE keywords active in a buffer.")
  1790. (make-variable-buffer-local 'org-todo-keywords-1)
  1791. (defvar org-todo-keywords-for-agenda nil)
  1792. (defvar org-done-keywords-for-agenda nil)
  1793. (defvar org-drawers-for-agenda nil)
  1794. (defvar org-todo-keyword-alist-for-agenda nil)
  1795. (defvar org-tag-alist-for-agenda nil)
  1796. (defvar org-agenda-contributing-files nil)
  1797. (defvar org-not-done-keywords nil)
  1798. (make-variable-buffer-local 'org-not-done-keywords)
  1799. (defvar org-done-keywords nil)
  1800. (make-variable-buffer-local 'org-done-keywords)
  1801. (defvar org-todo-heads nil)
  1802. (make-variable-buffer-local 'org-todo-heads)
  1803. (defvar org-todo-sets nil)
  1804. (make-variable-buffer-local 'org-todo-sets)
  1805. (defvar org-todo-log-states nil)
  1806. (make-variable-buffer-local 'org-todo-log-states)
  1807. (defvar org-todo-kwd-alist nil)
  1808. (make-variable-buffer-local 'org-todo-kwd-alist)
  1809. (defvar org-todo-key-alist nil)
  1810. (make-variable-buffer-local 'org-todo-key-alist)
  1811. (defvar org-todo-key-trigger nil)
  1812. (make-variable-buffer-local 'org-todo-key-trigger)
  1813. (defcustom org-todo-interpretation 'sequence
  1814. "Controls how TODO keywords are interpreted.
  1815. This variable is in principle obsolete and is only used for
  1816. backward compatibility, if the interpretation of todo keywords is
  1817. not given already in `org-todo-keywords'. See that variable for
  1818. more information."
  1819. :group 'org-todo
  1820. :group 'org-keywords
  1821. :type '(choice (const sequence)
  1822. (const type)))
  1823. (defcustom org-use-fast-todo-selection t
  1824. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1825. This variable describes if and under what circumstances the cycling
  1826. mechanism for TODO keywords will be replaced by a single-key, direct
  1827. selection scheme.
  1828. When nil, fast selection is never used.
  1829. When the symbol `prefix', it will be used when `org-todo' is called with
  1830. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1831. in an agenda buffer.
  1832. When t, fast selection is used by default. In this case, the prefix
  1833. argument forces cycling instead.
  1834. In all cases, the special interface is only used if access keys have actually
  1835. been assigned by the user, i.e. if keywords in the configuration are followed
  1836. by a letter in parenthesis, like TODO(t)."
  1837. :group 'org-todo
  1838. :type '(choice
  1839. (const :tag "Never" nil)
  1840. (const :tag "By default" t)
  1841. (const :tag "Only with C-u C-c C-t" prefix)))
  1842. (defcustom org-provide-todo-statistics t
  1843. "Non-nil means update todo statistics after insert and toggle.
  1844. ALL-HEADLINES means update todo statistics by including headlines
  1845. with no TODO keyword as well, counting them as not done.
  1846. A list of TODO keywords means the same, but skip keywords that are
  1847. not in this list.
  1848. When this is set, todo statistics is updated in the parent of the
  1849. current entry each time a todo state is changed."
  1850. :group 'org-todo
  1851. :type '(choice
  1852. (const :tag "Yes, only for TODO entries" t)
  1853. (const :tag "Yes, including all entries" 'all-headlines)
  1854. (repeat :tag "Yes, for TODOs in this list"
  1855. (string :tag "TODO keyword"))
  1856. (other :tag "No TODO statistics" nil)))
  1857. (defcustom org-hierarchical-todo-statistics t
  1858. "Non-nil means TODO statistics covers just direct children.
  1859. When nil, all entries in the subtree are considered.
  1860. This has only an effect if `org-provide-todo-statistics' is set.
  1861. To set this to nil for only a single subtree, use a COOKIE_DATA
  1862. property and include the word \"recursive\" into the value."
  1863. :group 'org-todo
  1864. :type 'boolean)
  1865. (defcustom org-after-todo-state-change-hook nil
  1866. "Hook which is run after the state of a TODO item was changed.
  1867. The new state (a string with a TODO keyword, or nil) is available in the
  1868. Lisp variable `state'."
  1869. :group 'org-todo
  1870. :type 'hook)
  1871. (defvar org-blocker-hook nil
  1872. "Hook for functions that are allowed to block a state change.
  1873. Each function gets as its single argument a property list, see
  1874. `org-trigger-hook' for more information about this list.
  1875. If any of the functions in this hook returns nil, the state change
  1876. is blocked.")
  1877. (defvar org-trigger-hook nil
  1878. "Hook for functions that are triggered by a state change.
  1879. Each function gets as its single argument a property list with at least
  1880. the following elements:
  1881. (:type type-of-change :position pos-at-entry-start
  1882. :from old-state :to new-state)
  1883. Depending on the type, more properties may be present.
  1884. This mechanism is currently implemented for:
  1885. TODO state changes
  1886. ------------------
  1887. :type todo-state-change
  1888. :from previous state (keyword as a string), or nil, or a symbol
  1889. 'todo' or 'done', to indicate the general type of state.
  1890. :to new state, like in :from")
  1891. (defcustom org-enforce-todo-dependencies nil
  1892. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1893. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1894. be blocked if any prior sibling is not yet done.
  1895. Finally, if the parent is blocked because of ordered siblings of its own,
  1896. the child will also be blocked.
  1897. This variable needs to be set before org.el is loaded, and you need to
  1898. restart Emacs after a change to make the change effective. The only way
  1899. to change is while Emacs is running is through the customize interface."
  1900. :set (lambda (var val)
  1901. (set var val)
  1902. (if val
  1903. (add-hook 'org-blocker-hook
  1904. 'org-block-todo-from-children-or-siblings-or-parent)
  1905. (remove-hook 'org-blocker-hook
  1906. 'org-block-todo-from-children-or-siblings-or-parent)))
  1907. :group 'org-todo
  1908. :type 'boolean)
  1909. (defcustom org-enforce-todo-checkbox-dependencies nil
  1910. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1911. When this is nil, checkboxes have no influence on switching TODO states.
  1912. When non-nil, you first need to check off all check boxes before the TODO
  1913. entry can be switched to DONE.
  1914. This variable needs to be set before org.el is loaded, and you need to
  1915. restart Emacs after a change to make the change effective. The only way
  1916. to change is while Emacs is running is through the customize interface."
  1917. :set (lambda (var val)
  1918. (set var val)
  1919. (if val
  1920. (add-hook 'org-blocker-hook
  1921. 'org-block-todo-from-checkboxes)
  1922. (remove-hook 'org-blocker-hook
  1923. 'org-block-todo-from-checkboxes)))
  1924. :group 'org-todo
  1925. :type 'boolean)
  1926. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1927. "Non-nil means inserting a TODO heading is treated as state change.
  1928. So when the command \\[org-insert-todo-heading] is used, state change
  1929. logging will apply if appropriate. When nil, the new TODO item will
  1930. be inserted directly, and no logging will take place."
  1931. :group 'org-todo
  1932. :type 'boolean)
  1933. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1934. "Non-nil means switching TODO states with S-cursor counts as state change.
  1935. This is the default behavior. However, setting this to nil allows a
  1936. convenient way to select a TODO state and bypass any logging associated
  1937. with that."
  1938. :group 'org-todo
  1939. :type 'boolean)
  1940. (defcustom org-todo-state-tags-triggers nil
  1941. "Tag changes that should be triggered by TODO state changes.
  1942. This is a list. Each entry is
  1943. (state-change (tag . flag) .......)
  1944. State-change can be a string with a state, and empty string to indicate the
  1945. state that has no TODO keyword, or it can be one of the symbols `todo'
  1946. or `done', meaning any not-done or done state, respectively."
  1947. :group 'org-todo
  1948. :group 'org-tags
  1949. :type '(repeat
  1950. (cons (choice :tag "When changing to"
  1951. (const :tag "Not-done state" todo)
  1952. (const :tag "Done state" done)
  1953. (string :tag "State"))
  1954. (repeat
  1955. (cons :tag "Tag action"
  1956. (string :tag "Tag")
  1957. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1958. (defcustom org-log-done nil
  1959. "Information to record when a task moves to the DONE state.
  1960. Possible values are:
  1961. nil Don't add anything, just change the keyword
  1962. time Add a time stamp to the task
  1963. note Prompt for a note and add it with template `org-log-note-headings'
  1964. This option can also be set with on a per-file-basis with
  1965. #+STARTUP: nologdone
  1966. #+STARTUP: logdone
  1967. #+STARTUP: lognotedone
  1968. You can have local logging settings for a subtree by setting the LOGGING
  1969. property to one or more of these keywords."
  1970. :group 'org-todo
  1971. :group 'org-progress
  1972. :type '(choice
  1973. (const :tag "No logging" nil)
  1974. (const :tag "Record CLOSED timestamp" time)
  1975. (const :tag "Record CLOSED timestamp with note." note)))
  1976. ;; Normalize old uses of org-log-done.
  1977. (cond
  1978. ((eq org-log-done t) (setq org-log-done 'time))
  1979. ((and (listp org-log-done) (memq 'done org-log-done))
  1980. (setq org-log-done 'note)))
  1981. (defcustom org-log-reschedule nil
  1982. "Information to record when the scheduling date of a tasks is modified.
  1983. Possible values are:
  1984. nil Don't add anything, just change the date
  1985. time Add a time stamp to the task
  1986. note Prompt for a note and add it with template `org-log-note-headings'
  1987. This option can also be set with on a per-file-basis with
  1988. #+STARTUP: nologreschedule
  1989. #+STARTUP: logreschedule
  1990. #+STARTUP: lognotereschedule"
  1991. :group 'org-todo
  1992. :group 'org-progress
  1993. :type '(choice
  1994. (const :tag "No logging" nil)
  1995. (const :tag "Record timestamp" time)
  1996. (const :tag "Record timestamp with note." note)))
  1997. (defcustom org-log-redeadline nil
  1998. "Information to record when the deadline date of a tasks is modified.
  1999. Possible values are:
  2000. nil Don't add anything, just change the date
  2001. time Add a time stamp to the task
  2002. note Prompt for a note and add it with template `org-log-note-headings'
  2003. This option can also be set with on a per-file-basis with
  2004. #+STARTUP: nologredeadline
  2005. #+STARTUP: logredeadline
  2006. #+STARTUP: lognoteredeadline
  2007. You can have local logging settings for a subtree by setting the LOGGING
  2008. property to one or more of these keywords."
  2009. :group 'org-todo
  2010. :group 'org-progress
  2011. :type '(choice
  2012. (const :tag "No logging" nil)
  2013. (const :tag "Record timestamp" time)
  2014. (const :tag "Record timestamp with note." note)))
  2015. (defcustom org-log-note-clock-out nil
  2016. "Non-nil means record a note when clocking out of an item.
  2017. This can also be configured on a per-file basis by adding one of
  2018. the following lines anywhere in the buffer:
  2019. #+STARTUP: lognoteclock-out
  2020. #+STARTUP: nolognoteclock-out"
  2021. :group 'org-todo
  2022. :group 'org-progress
  2023. :type 'boolean)
  2024. (defcustom org-log-done-with-time t
  2025. "Non-nil means the CLOSED time stamp will contain date and time.
  2026. When nil, only the date will be recorded."
  2027. :group 'org-progress
  2028. :type 'boolean)
  2029. (defcustom org-log-note-headings
  2030. '((done . "CLOSING NOTE %t")
  2031. (state . "State %-12s from %-12S %t")
  2032. (note . "Note taken on %t")
  2033. (reschedule . "Rescheduled from %S on %t")
  2034. (delschedule . "Not scheduled, was %S on %t")
  2035. (redeadline . "New deadline from %S on %t")
  2036. (deldeadline . "Removed deadline, was %S on %t")
  2037. (refile . "Refiled on %t")
  2038. (clock-out . ""))
  2039. "Headings for notes added to entries.
  2040. The value is an alist, with the car being a symbol indicating the note
  2041. context, and the cdr is the heading to be used. The heading may also be the
  2042. empty string.
  2043. %t in the heading will be replaced by a time stamp.
  2044. %T will be an active time stamp instead the default inactive one
  2045. %s will be replaced by the new TODO state, in double quotes.
  2046. %S will be replaced by the old TODO state, in double quotes.
  2047. %u will be replaced by the user name.
  2048. %U will be replaced by the full user name.
  2049. In fact, it is not a good idea to change the `state' entry, because
  2050. agenda log mode depends on the format of these entries."
  2051. :group 'org-todo
  2052. :group 'org-progress
  2053. :type '(list :greedy t
  2054. (cons (const :tag "Heading when closing an item" done) string)
  2055. (cons (const :tag
  2056. "Heading when changing todo state (todo sequence only)"
  2057. state) string)
  2058. (cons (const :tag "Heading when just taking a note" note) string)
  2059. (cons (const :tag "Heading when clocking out" clock-out) string)
  2060. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2061. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2062. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2063. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2064. (cons (const :tag "Heading when refiling" refile) string)))
  2065. (unless (assq 'note org-log-note-headings)
  2066. (push '(note . "%t") org-log-note-headings))
  2067. (defcustom org-log-into-drawer nil
  2068. "Non-nil means insert state change notes and time stamps into a drawer.
  2069. When nil, state changes notes will be inserted after the headline and
  2070. any scheduling and clock lines, but not inside a drawer.
  2071. The value of this variable should be the name of the drawer to use.
  2072. LOGBOOK is proposed as the default drawer for this purpose, you can
  2073. also set this to a string to define the drawer of your choice.
  2074. A value of t is also allowed, representing \"LOGBOOK\".
  2075. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2076. will be ignored.
  2077. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2078. a subtree."
  2079. :group 'org-todo
  2080. :group 'org-progress
  2081. :type '(choice
  2082. (const :tag "Not into a drawer" nil)
  2083. (const :tag "LOGBOOK" t)
  2084. (string :tag "Other")))
  2085. (if (fboundp 'defvaralias)
  2086. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2087. (defun org-log-into-drawer ()
  2088. "Return the value of `org-log-into-drawer', but let properties overrule.
  2089. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2090. used instead of the default value."
  2091. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2092. (cond
  2093. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2094. ((equal p "t") "LOGBOOK")
  2095. (t p))))
  2096. (defcustom org-log-state-notes-insert-after-drawers nil
  2097. "Non-nil means insert state change notes after any drawers in entry.
  2098. Only the drawers that *immediately* follow the headline and the
  2099. deadline/scheduled line are skipped.
  2100. When nil, insert notes right after the heading and perhaps the line
  2101. with deadline/scheduling if present.
  2102. This variable will have no effect if `org-log-into-drawer' is
  2103. set."
  2104. :group 'org-todo
  2105. :group 'org-progress
  2106. :type 'boolean)
  2107. (defcustom org-log-states-order-reversed t
  2108. "Non-nil means the latest state note will be directly after heading.
  2109. When nil, the state change notes will be ordered according to time."
  2110. :group 'org-todo
  2111. :group 'org-progress
  2112. :type 'boolean)
  2113. (defcustom org-todo-repeat-to-state nil
  2114. "The TODO state to which a repeater should return the repeating task.
  2115. By default this is the first task in a TODO sequence, or the previous state
  2116. in a TODO_TYP set. But you can specify another task here.
  2117. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2118. :group 'org-todo
  2119. :type '(choice (const :tag "Head of sequence" nil)
  2120. (string :tag "Specific state")))
  2121. (defcustom org-log-repeat 'time
  2122. "Non-nil means record moving through the DONE state when triggering repeat.
  2123. An auto-repeating task is immediately switched back to TODO when
  2124. marked DONE. If you are not logging state changes (by adding \"@\"
  2125. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2126. record a closing note, there will be no record of the task moving
  2127. through DONE. This variable forces taking a note anyway.
  2128. nil Don't force a record
  2129. time Record a time stamp
  2130. note Record a note
  2131. This option can also be set with on a per-file-basis with
  2132. #+STARTUP: logrepeat
  2133. #+STARTUP: lognoterepeat
  2134. #+STARTUP: nologrepeat
  2135. You can have local logging settings for a subtree by setting the LOGGING
  2136. property to one or more of these keywords."
  2137. :group 'org-todo
  2138. :group 'org-progress
  2139. :type '(choice
  2140. (const :tag "Don't force a record" nil)
  2141. (const :tag "Force recording the DONE state" time)
  2142. (const :tag "Force recording a note with the DONE state" note)))
  2143. (defgroup org-priorities nil
  2144. "Priorities in Org-mode."
  2145. :tag "Org Priorities"
  2146. :group 'org-todo)
  2147. (defcustom org-enable-priority-commands t
  2148. "Non-nil means priority commands are active.
  2149. When nil, these commands will be disabled, so that you never accidentally
  2150. set a priority."
  2151. :group 'org-priorities
  2152. :type 'boolean)
  2153. (defcustom org-highest-priority ?A
  2154. "The highest priority of TODO items. A character like ?A, ?B etc.
  2155. Must have a smaller ASCII number than `org-lowest-priority'."
  2156. :group 'org-priorities
  2157. :type 'character)
  2158. (defcustom org-lowest-priority ?C
  2159. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2160. Must have a larger ASCII number than `org-highest-priority'."
  2161. :group 'org-priorities
  2162. :type 'character)
  2163. (defcustom org-default-priority ?B
  2164. "The default priority of TODO items.
  2165. This is the priority an item gets if no explicit priority is given.
  2166. When starting to cycle on an empty priority the first step in the cycle
  2167. depends on `org-priority-start-cycle-with-default'. The resulting first
  2168. step priority must not exceed the range from `org-highest-priority' to
  2169. `org-lowest-priority' which means that `org-default-priority' has to be
  2170. in this range exclusive or inclusive the range boundaries. Else the
  2171. first step refuses to set the default and the second will fall back
  2172. to (depending on the command used) the highest or lowest priority."
  2173. :group 'org-priorities
  2174. :type 'character)
  2175. (defcustom org-priority-start-cycle-with-default t
  2176. "Non-nil means start with default priority when starting to cycle.
  2177. When this is nil, the first step in the cycle will be (depending on the
  2178. command used) one higher or lower than the default priority.
  2179. See also `org-default-priority'."
  2180. :group 'org-priorities
  2181. :type 'boolean)
  2182. (defcustom org-get-priority-function nil
  2183. "Function to extract the priority from a string.
  2184. The string is normally the headline. If this is nil Org computes the
  2185. priority from the priority cookie like [#A] in the headline. It returns
  2186. an integer, increasing by 1000 for each priority level.
  2187. The user can set a different function here, which should take a string
  2188. as an argument and return the numeric priority."
  2189. :group 'org-priorities
  2190. :type 'function)
  2191. (defgroup org-time nil
  2192. "Options concerning time stamps and deadlines in Org-mode."
  2193. :tag "Org Time"
  2194. :group 'org)
  2195. (defcustom org-insert-labeled-timestamps-at-point nil
  2196. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2197. When nil, these labeled time stamps are forces into the second line of an
  2198. entry, just after the headline. When scheduling from the global TODO list,
  2199. the time stamp will always be forced into the second line."
  2200. :group 'org-time
  2201. :type 'boolean)
  2202. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2203. "Formats for `format-time-string' which are used for time stamps.
  2204. It is not recommended to change this constant.")
  2205. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2206. "Number of minutes to round time stamps to.
  2207. These are two values, the first applies when first creating a time stamp.
  2208. The second applies when changing it with the commands `S-up' and `S-down'.
  2209. When changing the time stamp, this means that it will change in steps
  2210. of N minutes, as given by the second value.
  2211. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2212. numbers should be factors of 60, so for example 5, 10, 15.
  2213. When this is larger than 1, you can still force an exact time stamp by using
  2214. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2215. and by using a prefix arg to `S-up/down' to specify the exact number
  2216. of minutes to shift."
  2217. :group 'org-time
  2218. :get #'(lambda (var) ; Make sure both elements are there
  2219. (if (integerp (default-value var))
  2220. (list (default-value var) 5)
  2221. (default-value var)))
  2222. :type '(list
  2223. (integer :tag "when inserting times")
  2224. (integer :tag "when modifying times")))
  2225. ;; Normalize old customizations of this variable.
  2226. (when (integerp org-time-stamp-rounding-minutes)
  2227. (setq org-time-stamp-rounding-minutes
  2228. (list org-time-stamp-rounding-minutes
  2229. org-time-stamp-rounding-minutes)))
  2230. (defcustom org-display-custom-times nil
  2231. "Non-nil means overlay custom formats over all time stamps.
  2232. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2233. To turn this on on a per-file basis, insert anywhere in the file:
  2234. #+STARTUP: customtime"
  2235. :group 'org-time
  2236. :set 'set-default
  2237. :type 'sexp)
  2238. (make-variable-buffer-local 'org-display-custom-times)
  2239. (defcustom org-time-stamp-custom-formats
  2240. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2241. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2242. These are overlayed over the default ISO format if the variable
  2243. `org-display-custom-times' is set. Time like %H:%M should be at the
  2244. end of the second format. The custom formats are also honored by export
  2245. commands, if custom time display is turned on at the time of export."
  2246. :group 'org-time
  2247. :type 'sexp)
  2248. (defun org-time-stamp-format (&optional long inactive)
  2249. "Get the right format for a time string."
  2250. (let ((f (if long (cdr org-time-stamp-formats)
  2251. (car org-time-stamp-formats))))
  2252. (if inactive
  2253. (concat "[" (substring f 1 -1) "]")
  2254. f)))
  2255. (defcustom org-time-clocksum-format "%d:%02d"
  2256. "The format string used when creating CLOCKSUM lines.
  2257. This is also used when org-mode generates a time duration."
  2258. :group 'org-time
  2259. :type 'string)
  2260. (defcustom org-time-clocksum-use-fractional nil
  2261. "If non-nil, \\[org-clock-display] uses fractional times.
  2262. org-mode generates a time duration."
  2263. :group 'org-time
  2264. :type 'boolean)
  2265. (defcustom org-time-clocksum-fractional-format "%.2f"
  2266. "The format string used when creating CLOCKSUM lines, or when
  2267. org-mode generates a time duration."
  2268. :group 'org-time
  2269. :type 'string)
  2270. (defcustom org-deadline-warning-days 14
  2271. "No. of days before expiration during which a deadline becomes active.
  2272. This variable governs the display in sparse trees and in the agenda.
  2273. When 0 or negative, it means use this number (the absolute value of it)
  2274. even if a deadline has a different individual lead time specified.
  2275. Custom commands can set this variable in the options section."
  2276. :group 'org-time
  2277. :group 'org-agenda-daily/weekly
  2278. :type 'integer)
  2279. (defcustom org-read-date-prefer-future t
  2280. "Non-nil means assume future for incomplete date input from user.
  2281. This affects the following situations:
  2282. 1. The user gives a month but not a year.
  2283. For example, if it is April and you enter \"feb 2\", this will be read
  2284. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2285. 2. The user gives a day, but no month.
  2286. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2287. read this as the third of *next* month. However, if you enter \"17\",
  2288. it will be considered as *this* month.
  2289. If you set this variable to the symbol `time', then also the following
  2290. will work:
  2291. 3. If the user gives a time, but no day. If the time is before now,
  2292. to will be interpreted as tomorrow.
  2293. Currently none of this works for ISO week specifications.
  2294. When this option is nil, the current day, month and year will always be
  2295. used as defaults.
  2296. See also `org-agenda-jump-prefer-future'."
  2297. :group 'org-time
  2298. :type '(choice
  2299. (const :tag "Never" nil)
  2300. (const :tag "Check month and day" t)
  2301. (const :tag "Check month, day, and time" time)))
  2302. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2303. "Should the agenda jump command prefer the future for incomplete dates?
  2304. The default is to do the same as configured in `org-read-date-prefer-future'.
  2305. But you can also set a deviating value here.
  2306. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2307. :group 'org-agenda
  2308. :group 'org-time
  2309. :type '(choice
  2310. (const :tag "Use org-read-date-prefer-future"
  2311. org-read-date-prefer-future)
  2312. (const :tag "Never" nil)
  2313. (const :tag "Always" t)))
  2314. (defcustom org-read-date-force-compatible-dates t
  2315. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2316. Depending on the system Emacs is running on, certain dates cannot
  2317. be represented with the type used internally to represent time.
  2318. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2319. correctly. Some systems allow for earlier dates, some for later,
  2320. some for both. One way to find out it to insert any date into an
  2321. Org buffer, putting the cursor on the year and hitting S-up and
  2322. S-down to test the range.
  2323. When this variable is set to t, the date/time prompt will not let
  2324. you specify dates outside the 1970-2037 range, so it is certain that
  2325. these dates will work in whatever version of Emacs you are
  2326. running, and also that you can move a file from one Emacs implementation
  2327. to another. WHenever Org is forcing the year for you, it will display
  2328. a message and beep.
  2329. When this variable is nil, Org will check if the date is
  2330. representable in the specific Emacs implementation you are using.
  2331. If not, it will force a year, usually the current year, and beep
  2332. to remind you. Currently this setting is not recommended because
  2333. the likelihood that you will open your Org files in an Emacs that
  2334. has limited date range is not negligible.
  2335. A workaround for this problem is to use diary sexp dates for time
  2336. stamps outside of this range."
  2337. :group 'org-time
  2338. :type 'boolean)
  2339. (defcustom org-read-date-display-live t
  2340. "Non-nil means display current interpretation of date prompt live.
  2341. This display will be in an overlay, in the minibuffer."
  2342. :group 'org-time
  2343. :type 'boolean)
  2344. (defcustom org-read-date-popup-calendar t
  2345. "Non-nil means pop up a calendar when prompting for a date.
  2346. In the calendar, the date can be selected with mouse-1. However, the
  2347. minibuffer will also be active, and you can simply enter the date as well.
  2348. When nil, only the minibuffer will be available."
  2349. :group 'org-time
  2350. :type 'boolean)
  2351. (if (fboundp 'defvaralias)
  2352. (defvaralias 'org-popup-calendar-for-date-prompt
  2353. 'org-read-date-popup-calendar))
  2354. (defcustom org-read-date-minibuffer-setup-hook nil
  2355. "Hook to be used to set up keys for the date/time interface.
  2356. Add key definitions to `minibuffer-local-map', which will be a temporary
  2357. copy."
  2358. :group 'org-time
  2359. :type 'hook)
  2360. (defcustom org-extend-today-until 0
  2361. "The hour when your day really ends. Must be an integer.
  2362. This has influence for the following applications:
  2363. - When switching the agenda to \"today\". It it is still earlier than
  2364. the time given here, the day recognized as TODAY is actually yesterday.
  2365. - When a date is read from the user and it is still before the time given
  2366. here, the current date and time will be assumed to be yesterday, 23:59.
  2367. Also, timestamps inserted in remember templates follow this rule.
  2368. IMPORTANT: This is a feature whose implementation is and likely will
  2369. remain incomplete. Really, it is only here because past midnight seems to
  2370. be the favorite working time of John Wiegley :-)"
  2371. :group 'org-time
  2372. :type 'integer)
  2373. (defcustom org-edit-timestamp-down-means-later nil
  2374. "Non-nil means S-down will increase the time in a time stamp.
  2375. When nil, S-up will increase."
  2376. :group 'org-time
  2377. :type 'boolean)
  2378. (defcustom org-calendar-follow-timestamp-change t
  2379. "Non-nil means make the calendar window follow timestamp changes.
  2380. When a timestamp is modified and the calendar window is visible, it will be
  2381. moved to the new date."
  2382. :group 'org-time
  2383. :type 'boolean)
  2384. (defgroup org-tags nil
  2385. "Options concerning tags in Org-mode."
  2386. :tag "Org Tags"
  2387. :group 'org)
  2388. (defcustom org-tag-alist nil
  2389. "List of tags allowed in Org-mode files.
  2390. When this list is nil, Org-mode will base TAG input on what is already in the
  2391. buffer.
  2392. The value of this variable is an alist, the car of each entry must be a
  2393. keyword as a string, the cdr may be a character that is used to select
  2394. that tag through the fast-tag-selection interface.
  2395. See the manual for details."
  2396. :group 'org-tags
  2397. :type '(repeat
  2398. (choice
  2399. (cons (string :tag "Tag name")
  2400. (character :tag "Access char"))
  2401. (list :tag "Start radio group"
  2402. (const :startgroup)
  2403. (option (string :tag "Group description")))
  2404. (list :tag "End radio group"
  2405. (const :endgroup)
  2406. (option (string :tag "Group description")))
  2407. (const :tag "New line" (:newline)))))
  2408. (defcustom org-tag-persistent-alist nil
  2409. "List of tags that will always appear in all Org-mode files.
  2410. This is in addition to any in buffer settings or customizations
  2411. of `org-tag-alist'.
  2412. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2413. The value of this variable is an alist, the car of each entry must be a
  2414. keyword as a string, the cdr may be a character that is used to select
  2415. that tag through the fast-tag-selection interface.
  2416. See the manual for details.
  2417. To disable these tags on a per-file basis, insert anywhere in the file:
  2418. #+STARTUP: noptag"
  2419. :group 'org-tags
  2420. :type '(repeat
  2421. (choice
  2422. (cons (string :tag "Tag name")
  2423. (character :tag "Access char"))
  2424. (const :tag "Start radio group" (:startgroup))
  2425. (const :tag "End radio group" (:endgroup))
  2426. (const :tag "New line" (:newline)))))
  2427. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2428. "If non-nil, always offer completion for all tags of all agenda files.
  2429. Instead of customizing this variable directly, you might want to
  2430. set it locally for capture buffers, because there no list of
  2431. tags in that file can be created dynamically (there are none).
  2432. (add-hook 'org-capture-mode-hook
  2433. (lambda ()
  2434. (set (make-local-variable
  2435. 'org-complete-tags-always-offer-all-agenda-tags)
  2436. t)))"
  2437. :group 'org-tags
  2438. :type 'boolean)
  2439. (defvar org-file-tags nil
  2440. "List of tags that can be inherited by all entries in the file.
  2441. The tags will be inherited if the variable `org-use-tag-inheritance'
  2442. says they should be.
  2443. This variable is populated from #+FILETAGS lines.")
  2444. (defcustom org-use-fast-tag-selection 'auto
  2445. "Non-nil means use fast tag selection scheme.
  2446. This is a special interface to select and deselect tags with single keys.
  2447. When nil, fast selection is never used.
  2448. When the symbol `auto', fast selection is used if and only if selection
  2449. characters for tags have been configured, either through the variable
  2450. `org-tag-alist' or through a #+TAGS line in the buffer.
  2451. When t, fast selection is always used and selection keys are assigned
  2452. automatically if necessary."
  2453. :group 'org-tags
  2454. :type '(choice
  2455. (const :tag "Always" t)
  2456. (const :tag "Never" nil)
  2457. (const :tag "When selection characters are configured" 'auto)))
  2458. (defcustom org-fast-tag-selection-single-key nil
  2459. "Non-nil means fast tag selection exits after first change.
  2460. When nil, you have to press RET to exit it.
  2461. During fast tag selection, you can toggle this flag with `C-c'.
  2462. This variable can also have the value `expert'. In this case, the window
  2463. displaying the tags menu is not even shown, until you press C-c again."
  2464. :group 'org-tags
  2465. :type '(choice
  2466. (const :tag "No" nil)
  2467. (const :tag "Yes" t)
  2468. (const :tag "Expert" expert)))
  2469. (defvar org-fast-tag-selection-include-todo nil
  2470. "Non-nil means fast tags selection interface will also offer TODO states.
  2471. This is an undocumented feature, you should not rely on it.")
  2472. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2473. "The column to which tags should be indented in a headline.
  2474. If this number is positive, it specifies the column. If it is negative,
  2475. it means that the tags should be flushright to that column. For example,
  2476. -80 works well for a normal 80 character screen."
  2477. :group 'org-tags
  2478. :type 'integer)
  2479. (defcustom org-auto-align-tags t
  2480. "Non-nil keeps tags aligned when modifying headlines.
  2481. Some operations (i.e. demoting) change the length of a headline and
  2482. therefore shift the tags around. With this option turned on, after
  2483. each such operation the tags are again aligned to `org-tags-column'."
  2484. :group 'org-tags
  2485. :type 'boolean)
  2486. (defcustom org-use-tag-inheritance t
  2487. "Non-nil means tags in levels apply also for sublevels.
  2488. When nil, only the tags directly given in a specific line apply there.
  2489. This may also be a list of tags that should be inherited, or a regexp that
  2490. matches tags that should be inherited. Additional control is possible
  2491. with the variable `org-tags-exclude-from-inheritance' which gives an
  2492. explicit list of tags to be excluded from inheritance., even if the value of
  2493. `org-use-tag-inheritance' would select it for inheritance.
  2494. If this option is t, a match early-on in a tree can lead to a large
  2495. number of matches in the subtree when constructing the agenda or creating
  2496. a sparse tree. If you only want to see the first match in a tree during
  2497. a search, check out the variable `org-tags-match-list-sublevels'."
  2498. :group 'org-tags
  2499. :type '(choice
  2500. (const :tag "Not" nil)
  2501. (const :tag "Always" t)
  2502. (repeat :tag "Specific tags" (string :tag "Tag"))
  2503. (regexp :tag "Tags matched by regexp")))
  2504. (defcustom org-tags-exclude-from-inheritance nil
  2505. "List of tags that should never be inherited.
  2506. This is a way to exclude a few tags from inheritance. For way to do
  2507. the opposite, to actively allow inheritance for selected tags,
  2508. see the variable `org-use-tag-inheritance'."
  2509. :group 'org-tags
  2510. :type '(repeat (string :tag "Tag")))
  2511. (defun org-tag-inherit-p (tag)
  2512. "Check if TAG is one that should be inherited."
  2513. (cond
  2514. ((member tag org-tags-exclude-from-inheritance) nil)
  2515. ((eq org-use-tag-inheritance t) t)
  2516. ((not org-use-tag-inheritance) nil)
  2517. ((stringp org-use-tag-inheritance)
  2518. (string-match org-use-tag-inheritance tag))
  2519. ((listp org-use-tag-inheritance)
  2520. (member tag org-use-tag-inheritance))
  2521. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2522. (defcustom org-tags-match-list-sublevels t
  2523. "Non-nil means list also sublevels of headlines matching a search.
  2524. This variable applies to tags/property searches, and also to stuck
  2525. projects because this search is based on a tags match as well.
  2526. When set to the symbol `indented', sublevels are indented with
  2527. leading dots.
  2528. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2529. the sublevels of a headline matching a tag search often also match
  2530. the same search. Listing all of them can create very long lists.
  2531. Setting this variable to nil causes subtrees of a match to be skipped.
  2532. This variable is semi-obsolete and probably should always be true. It
  2533. is better to limit inheritance to certain tags using the variables
  2534. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2535. :group 'org-tags
  2536. :type '(choice
  2537. (const :tag "No, don't list them" nil)
  2538. (const :tag "Yes, do list them" t)
  2539. (const :tag "List them, indented with leading dots" indented)))
  2540. (defcustom org-tags-sort-function nil
  2541. "When set, tags are sorted using this function as a comparator."
  2542. :group 'org-tags
  2543. :type '(choice
  2544. (const :tag "No sorting" nil)
  2545. (const :tag "Alphabetical" string<)
  2546. (const :tag "Reverse alphabetical" string>)
  2547. (function :tag "Custom function" nil)))
  2548. (defvar org-tags-history nil
  2549. "History of minibuffer reads for tags.")
  2550. (defvar org-last-tags-completion-table nil
  2551. "The last used completion table for tags.")
  2552. (defvar org-after-tags-change-hook nil
  2553. "Hook that is run after the tags in a line have changed.")
  2554. (defgroup org-properties nil
  2555. "Options concerning properties in Org-mode."
  2556. :tag "Org Properties"
  2557. :group 'org)
  2558. (defcustom org-property-format "%-10s %s"
  2559. "How property key/value pairs should be formatted by `indent-line'.
  2560. When `indent-line' hits a property definition, it will format the line
  2561. according to this format, mainly to make sure that the values are
  2562. lined-up with respect to each other."
  2563. :group 'org-properties
  2564. :type 'string)
  2565. (defcustom org-use-property-inheritance nil
  2566. "Non-nil means properties apply also for sublevels.
  2567. This setting is chiefly used during property searches. Turning it on can
  2568. cause significant overhead when doing a search, which is why it is not
  2569. on by default.
  2570. When nil, only the properties directly given in the current entry count.
  2571. When t, every property is inherited. The value may also be a list of
  2572. properties that should have inheritance, or a regular expression matching
  2573. properties that should be inherited.
  2574. However, note that some special properties use inheritance under special
  2575. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2576. and the properties ending in \"_ALL\" when they are used as descriptor
  2577. for valid values of a property.
  2578. Note for programmers:
  2579. When querying an entry with `org-entry-get', you can control if inheritance
  2580. should be used. By default, `org-entry-get' looks only at the local
  2581. properties. You can request inheritance by setting the inherit argument
  2582. to t (to force inheritance) or to `selective' (to respect the setting
  2583. in this variable)."
  2584. :group 'org-properties
  2585. :type '(choice
  2586. (const :tag "Not" nil)
  2587. (const :tag "Always" t)
  2588. (repeat :tag "Specific properties" (string :tag "Property"))
  2589. (regexp :tag "Properties matched by regexp")))
  2590. (defun org-property-inherit-p (property)
  2591. "Check if PROPERTY is one that should be inherited."
  2592. (cond
  2593. ((eq org-use-property-inheritance t) t)
  2594. ((not org-use-property-inheritance) nil)
  2595. ((stringp org-use-property-inheritance)
  2596. (string-match org-use-property-inheritance property))
  2597. ((listp org-use-property-inheritance)
  2598. (member property org-use-property-inheritance))
  2599. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2600. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2601. "The default column format, if no other format has been defined.
  2602. This variable can be set on the per-file basis by inserting a line
  2603. #+COLUMNS: %25ITEM ....."
  2604. :group 'org-properties
  2605. :type 'string)
  2606. (defcustom org-columns-ellipses ".."
  2607. "The ellipses to be used when a field in column view is truncated.
  2608. When this is the empty string, as many characters as possible are shown,
  2609. but then there will be no visual indication that the field has been truncated.
  2610. When this is a string of length N, the last N characters of a truncated
  2611. field are replaced by this string. If the column is narrower than the
  2612. ellipses string, only part of the ellipses string will be shown."
  2613. :group 'org-properties
  2614. :type 'string)
  2615. (defcustom org-columns-modify-value-for-display-function nil
  2616. "Function that modifies values for display in column view.
  2617. For example, it can be used to cut out a certain part from a time stamp.
  2618. The function must take 2 arguments:
  2619. column-title The title of the column (*not* the property name)
  2620. value The value that should be modified.
  2621. The function should return the value that should be displayed,
  2622. or nil if the normal value should be used."
  2623. :group 'org-properties
  2624. :type 'function)
  2625. (defcustom org-effort-property "Effort"
  2626. "The property that is being used to keep track of effort estimates.
  2627. Effort estimates given in this property need to have the format H:MM."
  2628. :group 'org-properties
  2629. :group 'org-progress
  2630. :type '(string :tag "Property"))
  2631. (defconst org-global-properties-fixed
  2632. '(("VISIBILITY_ALL" . "folded children content all")
  2633. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2634. "List of property/value pairs that can be inherited by any entry.
  2635. These are fixed values, for the preset properties. The user variable
  2636. that can be used to add to this list is `org-global-properties'.
  2637. The entries in this list are cons cells where the car is a property
  2638. name and cdr is a string with the value. If the value represents
  2639. multiple items like an \"_ALL\" property, separate the items by
  2640. spaces.")
  2641. (defcustom org-global-properties nil
  2642. "List of property/value pairs that can be inherited by any entry.
  2643. This list will be combined with the constant `org-global-properties-fixed'.
  2644. The entries in this list are cons cells where the car is a property
  2645. name and cdr is a string with the value.
  2646. You can set buffer-local values for the same purpose in the variable
  2647. `org-file-properties' this by adding lines like
  2648. #+PROPERTY: NAME VALUE"
  2649. :group 'org-properties
  2650. :type '(repeat
  2651. (cons (string :tag "Property")
  2652. (string :tag "Value"))))
  2653. (defvar org-file-properties nil
  2654. "List of property/value pairs that can be inherited by any entry.
  2655. Valid for the current buffer.
  2656. This variable is populated from #+PROPERTY lines.")
  2657. (make-variable-buffer-local 'org-file-properties)
  2658. (defgroup org-agenda nil
  2659. "Options concerning agenda views in Org-mode."
  2660. :tag "Org Agenda"
  2661. :group 'org)
  2662. (defvar org-category nil
  2663. "Variable used by org files to set a category for agenda display.
  2664. Such files should use a file variable to set it, for example
  2665. # -*- mode: org; org-category: \"ELisp\"
  2666. or contain a special line
  2667. #+CATEGORY: ELisp
  2668. If the file does not specify a category, then file's base name
  2669. is used instead.")
  2670. (make-variable-buffer-local 'org-category)
  2671. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2672. (defcustom org-agenda-files nil
  2673. "The files to be used for agenda display.
  2674. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2675. \\[org-remove-file]. You can also use customize to edit the list.
  2676. If an entry is a directory, all files in that directory that are matched by
  2677. `org-agenda-file-regexp' will be part of the file list.
  2678. If the value of the variable is not a list but a single file name, then
  2679. the list of agenda files is actually stored and maintained in that file, one
  2680. agenda file per line. In this file paths can be given relative to
  2681. `org-directory'. Tilde expansion and environment variable substitution
  2682. are also made."
  2683. :group 'org-agenda
  2684. :type '(choice
  2685. (repeat :tag "List of files and directories" file)
  2686. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2687. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2688. "Regular expression to match files for `org-agenda-files'.
  2689. If any element in the list in that variable contains a directory instead
  2690. of a normal file, all files in that directory that are matched by this
  2691. regular expression will be included."
  2692. :group 'org-agenda
  2693. :type 'regexp)
  2694. (defcustom org-agenda-text-search-extra-files nil
  2695. "List of extra files to be searched by text search commands.
  2696. These files will be search in addition to the agenda files by the
  2697. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2698. Note that these files will only be searched for text search commands,
  2699. not for the other agenda views like todo lists, tag searches or the weekly
  2700. agenda. This variable is intended to list notes and possibly archive files
  2701. that should also be searched by these two commands.
  2702. In fact, if the first element in the list is the symbol `agenda-archives',
  2703. than all archive files of all agenda files will be added to the search
  2704. scope."
  2705. :group 'org-agenda
  2706. :type '(set :greedy t
  2707. (const :tag "Agenda Archives" agenda-archives)
  2708. (repeat :inline t (file))))
  2709. (if (fboundp 'defvaralias)
  2710. (defvaralias 'org-agenda-multi-occur-extra-files
  2711. 'org-agenda-text-search-extra-files))
  2712. (defcustom org-agenda-skip-unavailable-files nil
  2713. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2714. A nil value means to remove them, after a query, from the list."
  2715. :group 'org-agenda
  2716. :type 'boolean)
  2717. (defcustom org-calendar-to-agenda-key [?c]
  2718. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2719. The command `org-calendar-goto-agenda' will be bound to this key. The
  2720. default is the character `c' because then `c' can be used to switch back and
  2721. forth between agenda and calendar."
  2722. :group 'org-agenda
  2723. :type 'sexp)
  2724. (defcustom org-calendar-agenda-action-key [?k]
  2725. "The key to be installed in `calendar-mode-map' for agenda-action.
  2726. The command `org-agenda-action' will be bound to this key. The
  2727. default is the character `k' because we use the same key in the agenda."
  2728. :group 'org-agenda
  2729. :type 'sexp)
  2730. (defcustom org-calendar-insert-diary-entry-key [?i]
  2731. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2732. This option is irrelevant until `org-agenda-diary-file' has been configured
  2733. to point to an Org-mode file. When that is the case, the command
  2734. `org-agenda-diary-entry' will be bound to the key given here, by default
  2735. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2736. if you want to continue doing this, you need to change this to a different
  2737. key."
  2738. :group 'org-agenda
  2739. :type 'sexp)
  2740. (defcustom org-agenda-diary-file 'diary-file
  2741. "File to which to add new entries with the `i' key in agenda and calendar.
  2742. When this is the symbol `diary-file', the functionality in the Emacs
  2743. calendar will be used to add entries to the `diary-file'. But when this
  2744. points to a file, `org-agenda-diary-entry' will be used instead."
  2745. :group 'org-agenda
  2746. :type '(choice
  2747. (const :tag "The standard Emacs diary file" diary-file)
  2748. (file :tag "Special Org file diary entries")))
  2749. (eval-after-load "calendar"
  2750. '(progn
  2751. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2752. 'org-calendar-goto-agenda)
  2753. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2754. 'org-agenda-action)
  2755. (add-hook 'calendar-mode-hook
  2756. (lambda ()
  2757. (unless (eq org-agenda-diary-file 'diary-file)
  2758. (define-key calendar-mode-map
  2759. org-calendar-insert-diary-entry-key
  2760. 'org-agenda-diary-entry))))))
  2761. (defgroup org-latex nil
  2762. "Options for embedding LaTeX code into Org-mode."
  2763. :tag "Org LaTeX"
  2764. :group 'org)
  2765. (defcustom org-format-latex-options
  2766. '(:foreground default :background default :scale 1.0
  2767. :html-foreground "Black" :html-background "Transparent"
  2768. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2769. "Options for creating images from LaTeX fragments.
  2770. This is a property list with the following properties:
  2771. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2772. `default' means use the foreground of the default face.
  2773. :background the background color, or \"Transparent\".
  2774. `default' means use the background of the default face.
  2775. :scale a scaling factor for the size of the images, to get more pixels
  2776. :html-foreground, :html-background, :html-scale
  2777. the same numbers for HTML export.
  2778. :matchers a list indicating which matchers should be used to
  2779. find LaTeX fragments. Valid members of this list are:
  2780. \"begin\" find environments
  2781. \"$1\" find single characters surrounded by $.$
  2782. \"$\" find math expressions surrounded by $...$
  2783. \"$$\" find math expressions surrounded by $$....$$
  2784. \"\\(\" find math expressions surrounded by \\(...\\)
  2785. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2786. :group 'org-latex
  2787. :type 'plist)
  2788. (defcustom org-format-latex-signal-error t
  2789. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2790. When nil, just push out a message."
  2791. :group 'org-latex
  2792. :type 'boolean)
  2793. (defcustom org-format-latex-header "\\documentclass{article}
  2794. \\usepackage[usenames]{color}
  2795. \\usepackage{amsmath}
  2796. \\usepackage[mathscr]{eucal}
  2797. \\pagestyle{empty} % do not remove
  2798. \[PACKAGES]
  2799. \[DEFAULT-PACKAGES]
  2800. % The settings below are copied from fullpage.sty
  2801. \\setlength{\\textwidth}{\\paperwidth}
  2802. \\addtolength{\\textwidth}{-3cm}
  2803. \\setlength{\\oddsidemargin}{1.5cm}
  2804. \\addtolength{\\oddsidemargin}{-2.54cm}
  2805. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2806. \\setlength{\\textheight}{\\paperheight}
  2807. \\addtolength{\\textheight}{-\\headheight}
  2808. \\addtolength{\\textheight}{-\\headsep}
  2809. \\addtolength{\\textheight}{-\\footskip}
  2810. \\addtolength{\\textheight}{-3cm}
  2811. \\setlength{\\topmargin}{1.5cm}
  2812. \\addtolength{\\topmargin}{-2.54cm}"
  2813. "The document header used for processing LaTeX fragments.
  2814. It is imperative that this header make sure that no page number
  2815. appears on the page. The package defined in the variables
  2816. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2817. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2818. will be appended."
  2819. :group 'org-latex
  2820. :type 'string)
  2821. (defvar org-format-latex-header-extra nil)
  2822. (defun org-set-packages-alist (var val)
  2823. "Set the packages alist and make sure it has 3 elements per entry."
  2824. (set var (mapcar (lambda (x)
  2825. (if (and (consp x) (= (length x) 2))
  2826. (list (car x) (nth 1 x) t)
  2827. x))
  2828. val)))
  2829. (defun org-get-packages-alist (var)
  2830. "Get the packages alist and make sure it has 3 elements per entry."
  2831. (mapcar (lambda (x)
  2832. (if (and (consp x) (= (length x) 2))
  2833. (list (car x) (nth 1 x) t)
  2834. x))
  2835. (default-value var)))
  2836. ;; The following variables are defined here because is it also used
  2837. ;; when formatting latex fragments. Originally it was part of the
  2838. ;; LaTeX exporter, which is why the name includes "export".
  2839. (defcustom org-export-latex-default-packages-alist
  2840. '(("AUTO" "inputenc" t)
  2841. ("T1" "fontenc" t)
  2842. ("" "fixltx2e" nil)
  2843. ("" "graphicx" t)
  2844. ("" "longtable" nil)
  2845. ("" "float" nil)
  2846. ("" "wrapfig" nil)
  2847. ("" "soul" t)
  2848. ("" "textcomp" t)
  2849. ("" "marvosym" t)
  2850. ("" "wasysym" t)
  2851. ("" "latexsym" t)
  2852. ("" "amssymb" t)
  2853. ("" "hyperref" nil)
  2854. "\\tolerance=1000"
  2855. )
  2856. "Alist of default packages to be inserted in the header.
  2857. Change this only if one of the packages here causes an incompatibility
  2858. with another package you are using.
  2859. The packages in this list are needed by one part or another of Org-mode
  2860. to function properly.
  2861. - inputenc, fontenc: for basic font and character selection
  2862. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2863. for interpreting the entities in `org-entities'. You can skip some of these
  2864. packages if you don't use any of the symbols in it.
  2865. - graphicx: for including images
  2866. - float, wrapfig: for figure placement
  2867. - longtable: for long tables
  2868. - hyperref: for cross references
  2869. Therefore you should not modify this variable unless you know what you
  2870. are doing. The one reason to change it anyway is that you might be loading
  2871. some other package that conflicts with one of the default packages.
  2872. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2873. If SNIPPET-FLAG is t, the package also needs to be included when
  2874. compiling LaTeX snippets into images for inclusion into HTML."
  2875. :group 'org-export-latex
  2876. :set 'org-set-packages-alist
  2877. :get 'org-get-packages-alist
  2878. :type '(repeat
  2879. (choice
  2880. (list :tag "options/package pair"
  2881. (string :tag "options")
  2882. (string :tag "package")
  2883. (boolean :tag "Snippet"))
  2884. (string :tag "A line of LaTeX"))))
  2885. (defcustom org-export-latex-packages-alist nil
  2886. "Alist of packages to be inserted in every LaTeX header.
  2887. These will be inserted after `org-export-latex-default-packages-alist'.
  2888. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2889. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2890. turning LaTeX snippets into images for inclusion into HTML.
  2891. Make sure that you only list packages here which:
  2892. - you want in every file
  2893. - do not conflict with the default packages in
  2894. `org-export-latex-default-packages-alist'
  2895. - do not conflict with the setup in `org-format-latex-header'."
  2896. :group 'org-export-latex
  2897. :set 'org-set-packages-alist
  2898. :get 'org-get-packages-alist
  2899. :type '(repeat
  2900. (choice
  2901. (list :tag "options/package pair"
  2902. (string :tag "options")
  2903. (string :tag "package")
  2904. (boolean :tag "Snippet"))
  2905. (string :tag "A line of LaTeX"))))
  2906. (defgroup org-appearance nil
  2907. "Settings for Org-mode appearance."
  2908. :tag "Org Appearance"
  2909. :group 'org)
  2910. (defcustom org-level-color-stars-only nil
  2911. "Non-nil means fontify only the stars in each headline.
  2912. When nil, the entire headline is fontified.
  2913. Changing it requires restart of `font-lock-mode' to become effective
  2914. also in regions already fontified."
  2915. :group 'org-appearance
  2916. :type 'boolean)
  2917. (defcustom org-hide-leading-stars nil
  2918. "Non-nil means hide the first N-1 stars in a headline.
  2919. This works by using the face `org-hide' for these stars. This
  2920. face is white for a light background, and black for a dark
  2921. background. You may have to customize the face `org-hide' to
  2922. make this work.
  2923. Changing it requires restart of `font-lock-mode' to become effective
  2924. also in regions already fontified.
  2925. You may also set this on a per-file basis by adding one of the following
  2926. lines to the buffer:
  2927. #+STARTUP: hidestars
  2928. #+STARTUP: showstars"
  2929. :group 'org-appearance
  2930. :type 'boolean)
  2931. (defcustom org-hidden-keywords nil
  2932. "List of symbols corresponding to keywords to be hidden the org buffer.
  2933. For example, a value '(title) for this list will make the document's title
  2934. appear in the buffer without the initial #+TITLE: keyword."
  2935. :group 'org-appearance
  2936. :type '(set (const :tag "#+AUTHOR" author)
  2937. (const :tag "#+DATE" date)
  2938. (const :tag "#+EMAIL" email)
  2939. (const :tag "#+TITLE" title)))
  2940. (defcustom org-fontify-done-headline nil
  2941. "Non-nil means change the face of a headline if it is marked DONE.
  2942. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2943. When this is non-nil, the headline after the keyword is set to the
  2944. `org-headline-done' as an additional indication."
  2945. :group 'org-appearance
  2946. :type 'boolean)
  2947. (defcustom org-fontify-emphasized-text t
  2948. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2949. Changing this variable requires a restart of Emacs to take effect."
  2950. :group 'org-appearance
  2951. :type 'boolean)
  2952. (defcustom org-fontify-whole-heading-line nil
  2953. "Non-nil means fontify the whole line for headings.
  2954. This is useful when setting a background color for the
  2955. org-level-* faces."
  2956. :group 'org-appearance
  2957. :type 'boolean)
  2958. (defcustom org-highlight-latex-fragments-and-specials nil
  2959. "Non-nil means fontify what is treated specially by the exporters."
  2960. :group 'org-appearance
  2961. :type 'boolean)
  2962. (defcustom org-hide-emphasis-markers nil
  2963. "Non-nil mean font-lock should hide the emphasis marker characters."
  2964. :group 'org-appearance
  2965. :type 'boolean)
  2966. (defcustom org-pretty-entities nil
  2967. "Non-nil means show entities as UTF8 characters.
  2968. When nil, the \\name form remains in the buffer."
  2969. :group 'org-appearance
  2970. :type 'boolean)
  2971. (defcustom org-pretty-entities-include-sub-superscripts t
  2972. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2973. :group 'org-appearance
  2974. :type 'boolean)
  2975. (defvar org-emph-re nil
  2976. "Regular expression for matching emphasis.
  2977. After a match, the match groups contain these elements:
  2978. 0 The match of the full regular expression, including the characters
  2979. before and after the proper match
  2980. 1 The character before the proper match, or empty at beginning of line
  2981. 2 The proper match, including the leading and trailing markers
  2982. 3 The leading marker like * or /, indicating the type of highlighting
  2983. 4 The text between the emphasis markers, not including the markers
  2984. 5 The character after the match, empty at the end of a line")
  2985. (defvar org-verbatim-re nil
  2986. "Regular expression for matching verbatim text.")
  2987. (defvar org-emphasis-regexp-components) ; defined just below
  2988. (defvar org-emphasis-alist) ; defined just below
  2989. (defun org-set-emph-re (var val)
  2990. "Set variable and compute the emphasis regular expression."
  2991. (set var val)
  2992. (when (and (boundp 'org-emphasis-alist)
  2993. (boundp 'org-emphasis-regexp-components)
  2994. org-emphasis-alist org-emphasis-regexp-components)
  2995. (let* ((e org-emphasis-regexp-components)
  2996. (pre (car e))
  2997. (post (nth 1 e))
  2998. (border (nth 2 e))
  2999. (body (nth 3 e))
  3000. (nl (nth 4 e))
  3001. (body1 (concat body "*?"))
  3002. (markers (mapconcat 'car org-emphasis-alist ""))
  3003. (vmarkers (mapconcat
  3004. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3005. org-emphasis-alist "")))
  3006. ;; make sure special characters appear at the right position in the class
  3007. (if (string-match "\\^" markers)
  3008. (setq markers (concat (replace-match "" t t markers) "^")))
  3009. (if (string-match "-" markers)
  3010. (setq markers (concat (replace-match "" t t markers) "-")))
  3011. (if (string-match "\\^" vmarkers)
  3012. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3013. (if (string-match "-" vmarkers)
  3014. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3015. (if (> nl 0)
  3016. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3017. (int-to-string nl) "\\}")))
  3018. ;; Make the regexp
  3019. (setq org-emph-re
  3020. (concat "\\([" pre "]\\|^\\)"
  3021. "\\("
  3022. "\\([" markers "]\\)"
  3023. "\\("
  3024. "[^" border "]\\|"
  3025. "[^" border "]"
  3026. body1
  3027. "[^" border "]"
  3028. "\\)"
  3029. "\\3\\)"
  3030. "\\([" post "]\\|$\\)"))
  3031. (setq org-verbatim-re
  3032. (concat "\\([" pre "]\\|^\\)"
  3033. "\\("
  3034. "\\([" vmarkers "]\\)"
  3035. "\\("
  3036. "[^" border "]\\|"
  3037. "[^" border "]"
  3038. body1
  3039. "[^" border "]"
  3040. "\\)"
  3041. "\\3\\)"
  3042. "\\([" post "]\\|$\\)")))))
  3043. (defcustom org-emphasis-regexp-components
  3044. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3045. "Components used to build the regular expression for emphasis.
  3046. This is a list with five entries. Terminology: In an emphasis string
  3047. like \" *strong word* \", we call the initial space PREMATCH, the final
  3048. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3049. and \"trong wor\" is the body. The different components in this variable
  3050. specify what is allowed/forbidden in each part:
  3051. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3052. post Chars allowed as postmatch. End of line will be allowed too.
  3053. border The chars *forbidden* as border characters.
  3054. body-regexp A regexp like \".\" to match a body character. Don't use
  3055. non-shy groups here, and don't allow newline here.
  3056. newline The maximum number of newlines allowed in an emphasis exp.
  3057. Use customize to modify this, or restart Emacs after changing it."
  3058. :group 'org-appearance
  3059. :set 'org-set-emph-re
  3060. :type '(list
  3061. (sexp :tag "Allowed chars in pre ")
  3062. (sexp :tag "Allowed chars in post ")
  3063. (sexp :tag "Forbidden chars in border ")
  3064. (sexp :tag "Regexp for body ")
  3065. (integer :tag "number of newlines allowed")
  3066. (option (boolean :tag "Please ignore this button"))))
  3067. (defcustom org-emphasis-alist
  3068. `(("*" bold "<b>" "</b>")
  3069. ("/" italic "<i>" "</i>")
  3070. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3071. ("=" org-code "<code>" "</code>" verbatim)
  3072. ("~" org-verbatim "<code>" "</code>" verbatim)
  3073. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3074. "<del>" "</del>")
  3075. )
  3076. "Special syntax for emphasized text.
  3077. Text starting and ending with a special character will be emphasized, for
  3078. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3079. characters, the face to be used by font-lock for highlighting in Org-mode
  3080. Emacs buffers, and the HTML tags to be used for this.
  3081. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3082. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3083. Use customize to modify this, or restart Emacs after changing it."
  3084. :group 'org-appearance
  3085. :set 'org-set-emph-re
  3086. :type '(repeat
  3087. (list
  3088. (string :tag "Marker character")
  3089. (choice
  3090. (face :tag "Font-lock-face")
  3091. (plist :tag "Face property list"))
  3092. (string :tag "HTML start tag")
  3093. (string :tag "HTML end tag")
  3094. (option (const verbatim)))))
  3095. (defvar org-protecting-blocks
  3096. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3097. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3098. This is needed for font-lock setup.")
  3099. ;;; Miscellaneous options
  3100. (defgroup org-completion nil
  3101. "Completion in Org-mode."
  3102. :tag "Org Completion"
  3103. :group 'org)
  3104. (defcustom org-completion-use-ido nil
  3105. "Non-nil means use ido completion wherever possible.
  3106. Note that `ido-mode' must be active for this variable to be relevant.
  3107. If you decide to turn this variable on, you might well want to turn off
  3108. `org-outline-path-complete-in-steps'.
  3109. See also `org-completion-use-iswitchb'."
  3110. :group 'org-completion
  3111. :type 'boolean)
  3112. (defcustom org-completion-use-iswitchb nil
  3113. "Non-nil means use iswitchb completion wherever possible.
  3114. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3115. If you decide to turn this variable on, you might well want to turn off
  3116. `org-outline-path-complete-in-steps'.
  3117. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3118. :group 'org-completion
  3119. :type 'boolean)
  3120. (defcustom org-completion-fallback-command 'hippie-expand
  3121. "The expansion command called by \\[pcomplete] in normal context.
  3122. Normal means, no org-mode-specific context."
  3123. :group 'org-completion
  3124. :type 'function)
  3125. ;;; Functions and variables from their packages
  3126. ;; Declared here to avoid compiler warnings
  3127. ;; XEmacs only
  3128. (defvar outline-mode-menu-heading)
  3129. (defvar outline-mode-menu-show)
  3130. (defvar outline-mode-menu-hide)
  3131. (defvar zmacs-regions) ; XEmacs regions
  3132. ;; Emacs only
  3133. (defvar mark-active)
  3134. ;; Various packages
  3135. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3136. (declare-function calendar-forward-day "cal-move" (arg))
  3137. (declare-function calendar-goto-date "cal-move" (date))
  3138. (declare-function calendar-goto-today "cal-move" ())
  3139. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3140. (defvar calc-embedded-close-formula)
  3141. (defvar calc-embedded-open-formula)
  3142. (declare-function cdlatex-tab "ext:cdlatex" ())
  3143. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3144. (defvar font-lock-unfontify-region-function)
  3145. (declare-function iswitchb-read-buffer "iswitchb"
  3146. (prompt &optional default require-match start matches-set))
  3147. (defvar iswitchb-temp-buflist)
  3148. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3149. (defvar org-agenda-tags-todo-honor-ignore-options)
  3150. (declare-function org-agenda-skip "org-agenda" ())
  3151. (declare-function
  3152. org-format-agenda-item "org-agenda"
  3153. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3154. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3155. (declare-function org-agenda-change-all-lines "org-agenda"
  3156. (newhead hdmarker &optional fixface just-this))
  3157. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3158. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3159. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3160. (beg end))
  3161. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3162. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3163. "org-agenda" (&optional end))
  3164. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3165. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3166. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3167. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3168. (declare-function org-indent-mode "org-indent" (&optional arg))
  3169. (declare-function parse-time-string "parse-time" (string))
  3170. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3171. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3172. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3173. (defvar remember-data-file)
  3174. (defvar texmathp-why)
  3175. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3176. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3177. (defvar w3m-current-url)
  3178. (defvar w3m-current-title)
  3179. (defvar org-latex-regexps)
  3180. ;;; Autoload and prepare some org modules
  3181. ;; Some table stuff that needs to be defined here, because it is used
  3182. ;; by the functions setting up org-mode or checking for table context.
  3183. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3184. "Detect an org-type or table-type table.")
  3185. (defconst org-table-line-regexp "^[ \t]*|"
  3186. "Detect an org-type table line.")
  3187. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3188. "Detect an org-type table line.")
  3189. (defconst org-table-hline-regexp "^[ \t]*|-"
  3190. "Detect an org-type table hline.")
  3191. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3192. "Detect a table-type table hline.")
  3193. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3194. "Detect the first line outside a table when searching from within it.
  3195. This works for both table types.")
  3196. ;; Autoload the functions in org-table.el that are needed by functions here.
  3197. (eval-and-compile
  3198. (org-autoload "org-table"
  3199. '(org-table-align org-table-begin org-table-blank-field
  3200. org-table-convert org-table-convert-region org-table-copy-down
  3201. org-table-copy-region org-table-create
  3202. org-table-create-or-convert-from-region
  3203. org-table-create-with-table.el org-table-current-dline
  3204. org-table-cut-region org-table-delete-column org-table-edit-field
  3205. org-table-edit-formulas org-table-end org-table-eval-formula
  3206. org-table-export org-table-field-info
  3207. org-table-get-stored-formulas org-table-goto-column
  3208. org-table-hline-and-move org-table-import org-table-insert-column
  3209. org-table-insert-hline org-table-insert-row org-table-iterate
  3210. org-table-justify-field-maybe org-table-kill-row
  3211. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3212. org-table-move-column org-table-move-column-left
  3213. org-table-move-column-right org-table-move-row
  3214. org-table-move-row-down org-table-move-row-up
  3215. org-table-next-field org-table-next-row org-table-paste-rectangle
  3216. org-table-previous-field org-table-recalculate
  3217. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3218. org-table-toggle-coordinate-overlays
  3219. org-table-toggle-formula-debugger org-table-wrap-region
  3220. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3221. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3222. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3223. (defun org-at-table-p (&optional table-type)
  3224. "Return t if the cursor is inside an org-type table.
  3225. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3226. (if org-enable-table-editor
  3227. (save-excursion
  3228. (beginning-of-line 1)
  3229. (looking-at (if table-type org-table-any-line-regexp
  3230. org-table-line-regexp)))
  3231. nil))
  3232. (defsubst org-table-p () (org-at-table-p))
  3233. (defun org-at-table.el-p ()
  3234. "Return t if and only if we are at a table.el table."
  3235. (and (org-at-table-p 'any)
  3236. (save-excursion
  3237. (goto-char (org-table-begin 'any))
  3238. (looking-at org-table1-hline-regexp))))
  3239. (defun org-table-recognize-table.el ()
  3240. "If there is a table.el table nearby, recognize it and move into it."
  3241. (if org-table-tab-recognizes-table.el
  3242. (if (org-at-table.el-p)
  3243. (progn
  3244. (beginning-of-line 1)
  3245. (if (looking-at org-table-dataline-regexp)
  3246. nil
  3247. (if (looking-at org-table1-hline-regexp)
  3248. (progn
  3249. (beginning-of-line 2)
  3250. (if (looking-at org-table-any-border-regexp)
  3251. (beginning-of-line -1)))))
  3252. (if (re-search-forward "|" (org-table-end t) t)
  3253. (progn
  3254. (require 'table)
  3255. (if (table--at-cell-p (point))
  3256. t
  3257. (message "recognizing table.el table...")
  3258. (table-recognize-table)
  3259. (message "recognizing table.el table...done")))
  3260. (error "This should not happen"))
  3261. t)
  3262. nil)
  3263. nil))
  3264. (defun org-at-table-hline-p ()
  3265. "Return t if the cursor is inside a hline in a table."
  3266. (if org-enable-table-editor
  3267. (save-excursion
  3268. (beginning-of-line 1)
  3269. (looking-at org-table-hline-regexp))
  3270. nil))
  3271. (defvar org-table-clean-did-remove-column nil)
  3272. (defun org-table-map-tables (function &optional quietly)
  3273. "Apply FUNCTION to the start of all tables in the buffer."
  3274. (save-excursion
  3275. (save-restriction
  3276. (widen)
  3277. (goto-char (point-min))
  3278. (while (re-search-forward org-table-any-line-regexp nil t)
  3279. (unless quietly
  3280. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3281. (beginning-of-line 1)
  3282. (when (looking-at org-table-line-regexp)
  3283. (save-excursion (funcall function))
  3284. (or (looking-at org-table-line-regexp)
  3285. (forward-char 1)))
  3286. (re-search-forward org-table-any-border-regexp nil 1))))
  3287. (unless quietly (message "Mapping tables: done")))
  3288. ;; Declare and autoload functions from org-exp.el & Co
  3289. (declare-function org-default-export-plist "org-exp")
  3290. (declare-function org-infile-export-plist "org-exp")
  3291. (declare-function org-get-current-options "org-exp")
  3292. (eval-and-compile
  3293. (org-autoload "org-exp"
  3294. '(org-export org-export-visible
  3295. org-insert-export-options-template
  3296. org-table-clean-before-export))
  3297. (org-autoload "org-ascii"
  3298. '(org-export-as-ascii org-export-ascii-preprocess
  3299. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3300. org-export-region-as-ascii))
  3301. (org-autoload "org-latex"
  3302. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3303. org-replace-region-by-latex org-export-region-as-latex
  3304. org-export-as-latex org-export-as-pdf
  3305. org-export-as-pdf-and-open))
  3306. (org-autoload "org-html"
  3307. '(org-export-as-html-and-open
  3308. org-export-as-html-batch org-export-as-html-to-buffer
  3309. org-replace-region-by-html org-export-region-as-html
  3310. org-export-as-html))
  3311. (org-autoload "org-docbook"
  3312. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3313. org-replace-region-by-docbook org-export-region-as-docbook
  3314. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3315. org-export-as-docbook))
  3316. (org-autoload "org-icalendar"
  3317. '(org-export-icalendar-this-file
  3318. org-export-icalendar-all-agenda-files
  3319. org-export-icalendar-combine-agenda-files))
  3320. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3321. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3322. ;; Declare and autoload functions from org-agenda.el
  3323. (eval-and-compile
  3324. (org-autoload "org-agenda"
  3325. '(org-agenda org-agenda-list org-search-view
  3326. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3327. org-diary org-agenda-to-appt
  3328. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3329. ;; Autoload org-remember
  3330. (eval-and-compile
  3331. (org-autoload "org-remember"
  3332. '(org-remember-insinuate org-remember-annotation
  3333. org-remember-apply-template org-remember org-remember-handler)))
  3334. (eval-and-compile
  3335. (org-autoload "org-capture"
  3336. '(org-capture org-capture-insert-template-here
  3337. org-capture-import-remember-templates)))
  3338. ;; Autoload org-clock.el
  3339. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3340. (beg end))
  3341. (declare-function org-clock-update-mode-line "org-clock" ())
  3342. (declare-function org-resolve-clocks "org-clock"
  3343. (&optional also-non-dangling-p prompt last-valid))
  3344. (defvar org-clock-start-time)
  3345. (defvar org-clock-marker (make-marker)
  3346. "Marker recording the last clock-in.")
  3347. (defvar org-clock-hd-marker (make-marker)
  3348. "Marker recording the last clock-in, but the headline position.")
  3349. (defvar org-clock-heading ""
  3350. "The heading of the current clock entry.")
  3351. (defun org-clock-is-active ()
  3352. "Return non-nil if clock is currently running.
  3353. The return value is actually the clock marker."
  3354. (marker-buffer org-clock-marker))
  3355. (eval-and-compile
  3356. (org-autoload
  3357. "org-clock"
  3358. '(org-clock-in org-clock-out org-clock-cancel
  3359. org-clock-goto org-clock-sum org-clock-display
  3360. org-clock-remove-overlays org-clock-report
  3361. org-clocktable-shift org-dblock-write:clocktable
  3362. org-get-clocktable org-resolve-clocks)))
  3363. (defun org-clock-update-time-maybe ()
  3364. "If this is a CLOCK line, update it and return t.
  3365. Otherwise, return nil."
  3366. (interactive)
  3367. (save-excursion
  3368. (beginning-of-line 1)
  3369. (skip-chars-forward " \t")
  3370. (when (looking-at org-clock-string)
  3371. (let ((re (concat "[ \t]*" org-clock-string
  3372. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3373. "\\([ \t]*=>.*\\)?\\)?"))
  3374. ts te h m s neg)
  3375. (cond
  3376. ((not (looking-at re))
  3377. nil)
  3378. ((not (match-end 2))
  3379. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3380. (> org-clock-marker (point))
  3381. (<= org-clock-marker (point-at-eol)))
  3382. ;; The clock is running here
  3383. (setq org-clock-start-time
  3384. (apply 'encode-time
  3385. (org-parse-time-string (match-string 1))))
  3386. (org-clock-update-mode-line)))
  3387. (t
  3388. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3389. (end-of-line 1)
  3390. (setq ts (match-string 1)
  3391. te (match-string 3))
  3392. (setq s (- (org-float-time
  3393. (apply 'encode-time (org-parse-time-string te)))
  3394. (org-float-time
  3395. (apply 'encode-time (org-parse-time-string ts))))
  3396. neg (< s 0)
  3397. s (abs s)
  3398. h (floor (/ s 3600))
  3399. s (- s (* 3600 h))
  3400. m (floor (/ s 60))
  3401. s (- s (* 60 s)))
  3402. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3403. t))))))
  3404. (defun org-check-running-clock ()
  3405. "Check if the current buffer contains the running clock.
  3406. If yes, offer to stop it and to save the buffer with the changes."
  3407. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3408. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3409. (buffer-name))))
  3410. (org-clock-out)
  3411. (when (y-or-n-p "Save changed buffer?")
  3412. (save-buffer))))
  3413. (defun org-clocktable-try-shift (dir n)
  3414. "Check if this line starts a clock table, if yes, shift the time block."
  3415. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3416. (org-clocktable-shift dir n)))
  3417. ;; Autoload org-timer.el
  3418. (eval-and-compile
  3419. (org-autoload
  3420. "org-timer"
  3421. '(org-timer-start org-timer org-timer-item
  3422. org-timer-change-times-in-region
  3423. org-timer-set-timer
  3424. org-timer-reset-timers
  3425. org-timer-show-remaining-time)))
  3426. ;; Autoload org-feed.el
  3427. (eval-and-compile
  3428. (org-autoload
  3429. "org-feed"
  3430. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3431. ;; Autoload org-indent.el
  3432. ;; Define the variable already here, to make sure we have it.
  3433. (defvar org-indent-mode nil
  3434. "Non-nil if Org-Indent mode is enabled.
  3435. Use the command `org-indent-mode' to change this variable.")
  3436. (eval-and-compile
  3437. (org-autoload
  3438. "org-indent"
  3439. '(org-indent-mode)))
  3440. ;; Autoload org-mobile.el
  3441. (eval-and-compile
  3442. (org-autoload
  3443. "org-mobile"
  3444. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3445. ;; Autoload archiving code
  3446. ;; The stuff that is needed for cycling and tags has to be defined here.
  3447. (defgroup org-archive nil
  3448. "Options concerning archiving in Org-mode."
  3449. :tag "Org Archive"
  3450. :group 'org-structure)
  3451. (defcustom org-archive-location "%s_archive::"
  3452. "The location where subtrees should be archived.
  3453. The value of this variable is a string, consisting of two parts,
  3454. separated by a double-colon. The first part is a filename and
  3455. the second part is a headline.
  3456. When the filename is omitted, archiving happens in the same file.
  3457. %s in the filename will be replaced by the current file
  3458. name (without the directory part). Archiving to a different file
  3459. is useful to keep archived entries from contributing to the
  3460. Org-mode Agenda.
  3461. The archived entries will be filed as subtrees of the specified
  3462. headline. When the headline is omitted, the subtrees are simply
  3463. filed away at the end of the file, as top-level entries. Also in
  3464. the heading you can use %s to represent the file name, this can be
  3465. useful when using the same archive for a number of different files.
  3466. Here are a few examples:
  3467. \"%s_archive::\"
  3468. If the current file is Projects.org, archive in file
  3469. Projects.org_archive, as top-level trees. This is the default.
  3470. \"::* Archived Tasks\"
  3471. Archive in the current file, under the top-level headline
  3472. \"* Archived Tasks\".
  3473. \"~/org/archive.org::\"
  3474. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3475. \"~/org/archive.org::From %s\"
  3476. Archive in file ~/org/archive.org (absolute path), under headlines
  3477. \"From FILENAME\" where file name is the current file name.
  3478. \"basement::** Finished Tasks\"
  3479. Archive in file ./basement (relative path), as level 3 trees
  3480. below the level 2 heading \"** Finished Tasks\".
  3481. You may set this option on a per-file basis by adding to the buffer a
  3482. line like
  3483. #+ARCHIVE: basement::** Finished Tasks
  3484. You may also define it locally for a subtree by setting an ARCHIVE property
  3485. in the entry. If such a property is found in an entry, or anywhere up
  3486. the hierarchy, it will be used."
  3487. :group 'org-archive
  3488. :type 'string)
  3489. (defcustom org-archive-tag "ARCHIVE"
  3490. "The tag that marks a subtree as archived.
  3491. An archived subtree does not open during visibility cycling, and does
  3492. not contribute to the agenda listings.
  3493. After changing this, font-lock must be restarted in the relevant buffers to
  3494. get the proper fontification."
  3495. :group 'org-archive
  3496. :group 'org-keywords
  3497. :type 'string)
  3498. (defcustom org-agenda-skip-archived-trees t
  3499. "Non-nil means the agenda will skip any items located in archived trees.
  3500. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3501. variable is no longer recommended, you should leave it at the value t.
  3502. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3503. :group 'org-archive
  3504. :group 'org-agenda-skip
  3505. :type 'boolean)
  3506. (defcustom org-columns-skip-archived-trees t
  3507. "Non-nil means ignore archived trees when creating column view."
  3508. :group 'org-archive
  3509. :group 'org-properties
  3510. :type 'boolean)
  3511. (defcustom org-cycle-open-archived-trees nil
  3512. "Non-nil means `org-cycle' will open archived trees.
  3513. An archived tree is a tree marked with the tag ARCHIVE.
  3514. When nil, archived trees will stay folded. You can still open them with
  3515. normal outline commands like `show-all', but not with the cycling commands."
  3516. :group 'org-archive
  3517. :group 'org-cycle
  3518. :type 'boolean)
  3519. (defcustom org-sparse-tree-open-archived-trees nil
  3520. "Non-nil means sparse tree construction shows matches in archived trees.
  3521. When nil, matches in these trees are highlighted, but the trees are kept in
  3522. collapsed state."
  3523. :group 'org-archive
  3524. :group 'org-sparse-trees
  3525. :type 'boolean)
  3526. (defun org-cycle-hide-archived-subtrees (state)
  3527. "Re-hide all archived subtrees after a visibility state change."
  3528. (when (and (not org-cycle-open-archived-trees)
  3529. (not (memq state '(overview folded))))
  3530. (save-excursion
  3531. (let* ((globalp (memq state '(contents all)))
  3532. (beg (if globalp (point-min) (point)))
  3533. (end (if globalp (point-max) (org-end-of-subtree t))))
  3534. (org-hide-archived-subtrees beg end)
  3535. (goto-char beg)
  3536. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3537. (message "%s" (substitute-command-keys
  3538. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3539. (defun org-force-cycle-archived ()
  3540. "Cycle subtree even if it is archived."
  3541. (interactive)
  3542. (setq this-command 'org-cycle)
  3543. (let ((org-cycle-open-archived-trees t))
  3544. (call-interactively 'org-cycle)))
  3545. (defun org-hide-archived-subtrees (beg end)
  3546. "Re-hide all archived subtrees after a visibility state change."
  3547. (save-excursion
  3548. (let* ((re (concat ":" org-archive-tag ":")))
  3549. (goto-char beg)
  3550. (while (re-search-forward re end t)
  3551. (when (org-on-heading-p)
  3552. (org-flag-subtree t)
  3553. (org-end-of-subtree t))))))
  3554. (defun org-flag-subtree (flag)
  3555. (save-excursion
  3556. (org-back-to-heading t)
  3557. (outline-end-of-heading)
  3558. (outline-flag-region (point)
  3559. (progn (org-end-of-subtree t) (point))
  3560. flag)))
  3561. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3562. (eval-and-compile
  3563. (org-autoload "org-archive"
  3564. '(org-add-archive-files org-archive-subtree
  3565. org-archive-to-archive-sibling org-toggle-archive-tag
  3566. org-archive-subtree-default
  3567. org-archive-subtree-default-with-confirmation)))
  3568. ;; Autoload Column View Code
  3569. (declare-function org-columns-number-to-string "org-colview")
  3570. (declare-function org-columns-get-format-and-top-level "org-colview")
  3571. (declare-function org-columns-compute "org-colview")
  3572. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3573. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3574. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3575. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3576. ;; Autoload ID code
  3577. (declare-function org-id-store-link "org-id")
  3578. (declare-function org-id-locations-load "org-id")
  3579. (declare-function org-id-locations-save "org-id")
  3580. (defvar org-id-track-globally)
  3581. (org-autoload "org-id"
  3582. '(org-id-get-create org-id-new org-id-copy org-id-get
  3583. org-id-get-with-outline-path-completion
  3584. org-id-get-with-outline-drilling org-id-store-link
  3585. org-id-goto org-id-find org-id-store-link))
  3586. ;; Autoload Plotting Code
  3587. (org-autoload "org-plot"
  3588. '(org-plot/gnuplot))
  3589. ;;; Variables for pre-computed regular expressions, all buffer local
  3590. (defvar org-drawer-regexp nil
  3591. "Matches first line of a hidden block.")
  3592. (make-variable-buffer-local 'org-drawer-regexp)
  3593. (defvar org-todo-regexp nil
  3594. "Matches any of the TODO state keywords.")
  3595. (make-variable-buffer-local 'org-todo-regexp)
  3596. (defvar org-not-done-regexp nil
  3597. "Matches any of the TODO state keywords except the last one.")
  3598. (make-variable-buffer-local 'org-not-done-regexp)
  3599. (defvar org-not-done-heading-regexp nil
  3600. "Matches a TODO headline that is not done.")
  3601. (make-variable-buffer-local 'org-not-done-regexp)
  3602. (defvar org-todo-line-regexp nil
  3603. "Matches a headline and puts TODO state into group 2 if present.")
  3604. (make-variable-buffer-local 'org-todo-line-regexp)
  3605. (defvar org-complex-heading-regexp nil
  3606. "Matches a headline and puts everything into groups:
  3607. group 1: the stars
  3608. group 2: The todo keyword, maybe
  3609. group 3: Priority cookie
  3610. group 4: True headline
  3611. group 5: Tags")
  3612. (make-variable-buffer-local 'org-complex-heading-regexp)
  3613. (defvar org-complex-heading-regexp-format nil
  3614. "Printf format to make regexp to match an exact headline.
  3615. This regexp will match the headline of any node which hase the exact
  3616. headline text that is put into the format, but may have any TODO state,
  3617. priority and tags.")
  3618. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3619. (defvar org-todo-line-tags-regexp nil
  3620. "Matches a headline and puts TODO state into group 2 if present.
  3621. Also put tags into group 4 if tags are present.")
  3622. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3623. (defvar org-nl-done-regexp nil
  3624. "Matches newline followed by a headline with the DONE keyword.")
  3625. (make-variable-buffer-local 'org-nl-done-regexp)
  3626. (defvar org-looking-at-done-regexp nil
  3627. "Matches the DONE keyword a point.")
  3628. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3629. (defvar org-ds-keyword-length 12
  3630. "Maximum length of the Deadline and SCHEDULED keywords.")
  3631. (make-variable-buffer-local 'org-ds-keyword-length)
  3632. (defvar org-deadline-regexp nil
  3633. "Matches the DEADLINE keyword.")
  3634. (make-variable-buffer-local 'org-deadline-regexp)
  3635. (defvar org-deadline-time-regexp nil
  3636. "Matches the DEADLINE keyword together with a time stamp.")
  3637. (make-variable-buffer-local 'org-deadline-time-regexp)
  3638. (defvar org-deadline-line-regexp nil
  3639. "Matches the DEADLINE keyword and the rest of the line.")
  3640. (make-variable-buffer-local 'org-deadline-line-regexp)
  3641. (defvar org-scheduled-regexp nil
  3642. "Matches the SCHEDULED keyword.")
  3643. (make-variable-buffer-local 'org-scheduled-regexp)
  3644. (defvar org-scheduled-time-regexp nil
  3645. "Matches the SCHEDULED keyword together with a time stamp.")
  3646. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3647. (defvar org-closed-time-regexp nil
  3648. "Matches the CLOSED keyword together with a time stamp.")
  3649. (make-variable-buffer-local 'org-closed-time-regexp)
  3650. (defvar org-keyword-time-regexp nil
  3651. "Matches any of the 4 keywords, together with the time stamp.")
  3652. (make-variable-buffer-local 'org-keyword-time-regexp)
  3653. (defvar org-keyword-time-not-clock-regexp nil
  3654. "Matches any of the 3 keywords, together with the time stamp.")
  3655. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3656. (defvar org-maybe-keyword-time-regexp nil
  3657. "Matches a timestamp, possibly preceded by a keyword.")
  3658. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3659. (defvar org-planning-or-clock-line-re nil
  3660. "Matches a line with planning or clock info.")
  3661. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3662. (defvar org-all-time-keywords nil
  3663. "List of time keywords.")
  3664. (make-variable-buffer-local 'org-all-time-keywords)
  3665. (defconst org-plain-time-of-day-regexp
  3666. (concat
  3667. "\\(\\<[012]?[0-9]"
  3668. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3669. "\\(--?"
  3670. "\\(\\<[012]?[0-9]"
  3671. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3672. "\\)?")
  3673. "Regular expression to match a plain time or time range.
  3674. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3675. groups carry important information:
  3676. 0 the full match
  3677. 1 the first time, range or not
  3678. 8 the second time, if it is a range.")
  3679. (defconst org-plain-time-extension-regexp
  3680. (concat
  3681. "\\(\\<[012]?[0-9]"
  3682. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3683. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3684. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3685. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3686. groups carry important information:
  3687. 0 the full match
  3688. 7 hours of duration
  3689. 9 minutes of duration")
  3690. (defconst org-stamp-time-of-day-regexp
  3691. (concat
  3692. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3693. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3694. "\\(--?"
  3695. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3696. "Regular expression to match a timestamp time or time range.
  3697. After a match, the following groups carry important information:
  3698. 0 the full match
  3699. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3700. 2 the first time, range or not
  3701. 4 the second time, if it is a range.")
  3702. (defconst org-startup-options
  3703. '(("fold" org-startup-folded t)
  3704. ("overview" org-startup-folded t)
  3705. ("nofold" org-startup-folded nil)
  3706. ("showall" org-startup-folded nil)
  3707. ("showeverything" org-startup-folded showeverything)
  3708. ("content" org-startup-folded content)
  3709. ("indent" org-startup-indented t)
  3710. ("noindent" org-startup-indented nil)
  3711. ("hidestars" org-hide-leading-stars t)
  3712. ("showstars" org-hide-leading-stars nil)
  3713. ("odd" org-odd-levels-only t)
  3714. ("oddeven" org-odd-levels-only nil)
  3715. ("align" org-startup-align-all-tables t)
  3716. ("noalign" org-startup-align-all-tables nil)
  3717. ("inlineimages" org-startup-with-inline-images t)
  3718. ("noinlineimages" org-startup-with-inline-images nil)
  3719. ("customtime" org-display-custom-times t)
  3720. ("logdone" org-log-done time)
  3721. ("lognotedone" org-log-done note)
  3722. ("nologdone" org-log-done nil)
  3723. ("lognoteclock-out" org-log-note-clock-out t)
  3724. ("nolognoteclock-out" org-log-note-clock-out nil)
  3725. ("logrepeat" org-log-repeat state)
  3726. ("lognoterepeat" org-log-repeat note)
  3727. ("nologrepeat" org-log-repeat nil)
  3728. ("logreschedule" org-log-reschedule time)
  3729. ("lognotereschedule" org-log-reschedule note)
  3730. ("nologreschedule" org-log-reschedule nil)
  3731. ("logredeadline" org-log-redeadline time)
  3732. ("lognoteredeadline" org-log-redeadline note)
  3733. ("nologredeadline" org-log-redeadline nil)
  3734. ("logrefile" org-log-refile time)
  3735. ("lognoterefile" org-log-refile note)
  3736. ("nologrefile" org-log-refile nil)
  3737. ("fninline" org-footnote-define-inline t)
  3738. ("nofninline" org-footnote-define-inline nil)
  3739. ("fnlocal" org-footnote-section nil)
  3740. ("fnauto" org-footnote-auto-label t)
  3741. ("fnprompt" org-footnote-auto-label nil)
  3742. ("fnconfirm" org-footnote-auto-label confirm)
  3743. ("fnplain" org-footnote-auto-label plain)
  3744. ("fnadjust" org-footnote-auto-adjust t)
  3745. ("nofnadjust" org-footnote-auto-adjust nil)
  3746. ("constcgs" constants-unit-system cgs)
  3747. ("constSI" constants-unit-system SI)
  3748. ("noptag" org-tag-persistent-alist nil)
  3749. ("hideblocks" org-hide-block-startup t)
  3750. ("nohideblocks" org-hide-block-startup nil)
  3751. ("beamer" org-startup-with-beamer-mode t)
  3752. ("entitiespretty" org-pretty-entities t)
  3753. ("entitiesplain" org-pretty-entities nil))
  3754. "Variable associated with STARTUP options for org-mode.
  3755. Each element is a list of three items: The startup options as written
  3756. in the #+STARTUP line, the corresponding variable, and the value to
  3757. set this variable to if the option is found. An optional forth element PUSH
  3758. means to push this value onto the list in the variable.")
  3759. (defun org-set-regexps-and-options ()
  3760. "Precompute regular expressions for current buffer."
  3761. (when (org-mode-p)
  3762. (org-set-local 'org-todo-kwd-alist nil)
  3763. (org-set-local 'org-todo-key-alist nil)
  3764. (org-set-local 'org-todo-key-trigger nil)
  3765. (org-set-local 'org-todo-keywords-1 nil)
  3766. (org-set-local 'org-done-keywords nil)
  3767. (org-set-local 'org-todo-heads nil)
  3768. (org-set-local 'org-todo-sets nil)
  3769. (org-set-local 'org-todo-log-states nil)
  3770. (org-set-local 'org-file-properties nil)
  3771. (org-set-local 'org-file-tags nil)
  3772. (let ((re (org-make-options-regexp
  3773. '("CATEGORY" "TODO" "COLUMNS"
  3774. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3775. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3776. "OPTIONS")
  3777. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3778. (splitre "[ \t]+")
  3779. (scripts org-use-sub-superscripts)
  3780. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3781. tail sep kws1 prio props ftags drawers beamer-p
  3782. ext-setup-or-nil setup-contents (start 0))
  3783. (save-excursion
  3784. (save-restriction
  3785. (widen)
  3786. (goto-char (point-min))
  3787. (while (or (and ext-setup-or-nil
  3788. (string-match re ext-setup-or-nil start)
  3789. (setq start (match-end 0)))
  3790. (and (setq ext-setup-or-nil nil start 0)
  3791. (re-search-forward re nil t)))
  3792. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3793. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3794. (if (stringp value) (setq value (org-trim value)))
  3795. (cond
  3796. ((equal key "CATEGORY")
  3797. (setq cat value))
  3798. ((member key '("SEQ_TODO" "TODO"))
  3799. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3800. ((equal key "TYP_TODO")
  3801. (push (cons 'type (org-split-string value splitre)) kwds))
  3802. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3803. ;; general TODO-like setup
  3804. (push (cons (intern (downcase (match-string 1 key)))
  3805. (org-split-string value splitre)) kwds))
  3806. ((equal key "TAGS")
  3807. (setq tags (append tags (if tags '("\\n") nil)
  3808. (org-split-string value splitre))))
  3809. ((equal key "COLUMNS")
  3810. (org-set-local 'org-columns-default-format value))
  3811. ((equal key "LINK")
  3812. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3813. (push (cons (match-string 1 value)
  3814. (org-trim (match-string 2 value)))
  3815. links)))
  3816. ((equal key "PRIORITIES")
  3817. (setq prio (org-split-string value " +")))
  3818. ((equal key "PROPERTY")
  3819. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3820. (push (cons (match-string 1 value) (match-string 2 value))
  3821. props)))
  3822. ((equal key "FILETAGS")
  3823. (when (string-match "\\S-" value)
  3824. (setq ftags
  3825. (append
  3826. ftags
  3827. (apply 'append
  3828. (mapcar (lambda (x) (org-split-string x ":"))
  3829. (org-split-string value)))))))
  3830. ((equal key "DRAWERS")
  3831. (setq drawers (org-split-string value splitre)))
  3832. ((equal key "CONSTANTS")
  3833. (setq const (append const (org-split-string value splitre))))
  3834. ((equal key "STARTUP")
  3835. (let ((opts (org-split-string value splitre))
  3836. l var val)
  3837. (while (setq l (pop opts))
  3838. (when (setq l (assoc l org-startup-options))
  3839. (setq var (nth 1 l) val (nth 2 l))
  3840. (if (not (nth 3 l))
  3841. (set (make-local-variable var) val)
  3842. (if (not (listp (symbol-value var)))
  3843. (set (make-local-variable var) nil))
  3844. (set (make-local-variable var) (symbol-value var))
  3845. (add-to-list var val))))))
  3846. ((equal key "ARCHIVE")
  3847. (setq arch value)
  3848. (remove-text-properties 0 (length arch)
  3849. '(face t fontified t) arch))
  3850. ((equal key "LATEX_CLASS")
  3851. (setq beamer-p (equal value "beamer")))
  3852. ((equal key "OPTIONS")
  3853. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3854. (setq scripts (read (match-string 2 value)))))
  3855. ((equal key "SETUPFILE")
  3856. (setq setup-contents (org-file-contents
  3857. (expand-file-name
  3858. (org-remove-double-quotes value))
  3859. 'noerror))
  3860. (if (not ext-setup-or-nil)
  3861. (setq ext-setup-or-nil setup-contents start 0)
  3862. (setq ext-setup-or-nil
  3863. (concat (substring ext-setup-or-nil 0 start)
  3864. "\n" setup-contents "\n"
  3865. (substring ext-setup-or-nil start)))))
  3866. ))))
  3867. (org-set-local 'org-use-sub-superscripts scripts)
  3868. (when cat
  3869. (org-set-local 'org-category (intern cat))
  3870. (push (cons "CATEGORY" cat) props))
  3871. (when prio
  3872. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3873. (setq prio (mapcar 'string-to-char prio))
  3874. (org-set-local 'org-highest-priority (nth 0 prio))
  3875. (org-set-local 'org-lowest-priority (nth 1 prio))
  3876. (org-set-local 'org-default-priority (nth 2 prio)))
  3877. (and props (org-set-local 'org-file-properties (nreverse props)))
  3878. (and ftags (org-set-local 'org-file-tags
  3879. (mapcar 'org-add-prop-inherited ftags)))
  3880. (and drawers (org-set-local 'org-drawers drawers))
  3881. (and arch (org-set-local 'org-archive-location arch))
  3882. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3883. ;; Process the TODO keywords
  3884. (unless kwds
  3885. ;; Use the global values as if they had been given locally.
  3886. (setq kwds (default-value 'org-todo-keywords))
  3887. (if (stringp (car kwds))
  3888. (setq kwds (list (cons org-todo-interpretation
  3889. (default-value 'org-todo-keywords)))))
  3890. (setq kwds (reverse kwds)))
  3891. (setq kwds (nreverse kwds))
  3892. (let (inter kws kw)
  3893. (while (setq kws (pop kwds))
  3894. (let ((kws (or
  3895. (run-hook-with-args-until-success
  3896. 'org-todo-setup-filter-hook kws)
  3897. kws)))
  3898. (setq inter (pop kws) sep (member "|" kws)
  3899. kws0 (delete "|" (copy-sequence kws))
  3900. kwsa nil
  3901. kws1 (mapcar
  3902. (lambda (x)
  3903. ;; 1 2
  3904. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3905. (progn
  3906. (setq kw (match-string 1 x)
  3907. key (and (match-end 2) (match-string 2 x))
  3908. log (org-extract-log-state-settings x))
  3909. (push (cons kw (and key (string-to-char key))) kwsa)
  3910. (and log (push log org-todo-log-states))
  3911. kw)
  3912. (error "Invalid TODO keyword %s" x)))
  3913. kws0)
  3914. kwsa (if kwsa (append '((:startgroup))
  3915. (nreverse kwsa)
  3916. '((:endgroup))))
  3917. hw (car kws1)
  3918. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3919. tail (list inter hw (car dws) (org-last dws))))
  3920. (add-to-list 'org-todo-heads hw 'append)
  3921. (push kws1 org-todo-sets)
  3922. (setq org-done-keywords (append org-done-keywords dws nil))
  3923. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3924. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3925. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3926. (setq org-todo-sets (nreverse org-todo-sets)
  3927. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3928. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3929. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3930. ;; Process the constants
  3931. (when const
  3932. (let (e cst)
  3933. (while (setq e (pop const))
  3934. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3935. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3936. (setq org-table-formula-constants-local cst)))
  3937. ;; Process the tags.
  3938. (when tags
  3939. (let (e tgs)
  3940. (while (setq e (pop tags))
  3941. (cond
  3942. ((equal e "{") (push '(:startgroup) tgs))
  3943. ((equal e "}") (push '(:endgroup) tgs))
  3944. ((equal e "\\n") (push '(:newline) tgs))
  3945. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3946. (push (cons (match-string 1 e)
  3947. (string-to-char (match-string 2 e)))
  3948. tgs))
  3949. (t (push (list e) tgs))))
  3950. (org-set-local 'org-tag-alist nil)
  3951. (while (setq e (pop tgs))
  3952. (or (and (stringp (car e))
  3953. (assoc (car e) org-tag-alist))
  3954. (push e org-tag-alist)))))
  3955. ;; Compute the regular expressions and other local variables
  3956. (if (not org-done-keywords)
  3957. (setq org-done-keywords (and org-todo-keywords-1
  3958. (list (org-last org-todo-keywords-1)))))
  3959. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3960. (length org-scheduled-string)
  3961. (length org-clock-string)
  3962. (length org-closed-string)))
  3963. org-drawer-regexp
  3964. (concat "^[ \t]*:\\("
  3965. (mapconcat 'regexp-quote org-drawers "\\|")
  3966. "\\):[ \t]*$")
  3967. org-not-done-keywords
  3968. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3969. org-todo-regexp
  3970. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3971. "\\|") "\\)\\>")
  3972. org-not-done-regexp
  3973. (concat "\\<\\("
  3974. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3975. "\\)\\>")
  3976. org-not-done-heading-regexp
  3977. (concat "^\\(\\*+\\)[ \t]+\\("
  3978. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3979. "\\)\\>")
  3980. org-todo-line-regexp
  3981. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3982. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3983. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3984. org-complex-heading-regexp
  3985. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3986. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3987. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3988. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  3989. org-complex-heading-regexp-format
  3990. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3991. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3992. "\\)\\>\\)?"
  3993. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3994. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3995. "[ \t]*\\(%s\\)"
  3996. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3997. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  3998. org-nl-done-regexp
  3999. (concat "\n\\*+[ \t]+"
  4000. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4001. "\\)" "\\>")
  4002. org-todo-line-tags-regexp
  4003. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4004. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4005. (org-re
  4006. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  4007. org-looking-at-done-regexp
  4008. (concat "^" "\\(?:"
  4009. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4010. "\\>")
  4011. org-deadline-regexp (concat "\\<" org-deadline-string)
  4012. org-deadline-time-regexp
  4013. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4014. org-deadline-line-regexp
  4015. (concat "\\<\\(" org-deadline-string "\\).*")
  4016. org-scheduled-regexp
  4017. (concat "\\<" org-scheduled-string)
  4018. org-scheduled-time-regexp
  4019. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4020. org-closed-time-regexp
  4021. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4022. org-keyword-time-regexp
  4023. (concat "\\<\\(" org-scheduled-string
  4024. "\\|" org-deadline-string
  4025. "\\|" org-closed-string
  4026. "\\|" org-clock-string "\\)"
  4027. " *[[<]\\([^]>]+\\)[]>]")
  4028. org-keyword-time-not-clock-regexp
  4029. (concat "\\<\\(" org-scheduled-string
  4030. "\\|" org-deadline-string
  4031. "\\|" org-closed-string
  4032. "\\)"
  4033. " *[[<]\\([^]>]+\\)[]>]")
  4034. org-maybe-keyword-time-regexp
  4035. (concat "\\(\\<\\(" org-scheduled-string
  4036. "\\|" org-deadline-string
  4037. "\\|" org-closed-string
  4038. "\\|" org-clock-string "\\)\\)?"
  4039. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4040. org-planning-or-clock-line-re
  4041. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4042. "\\|" org-deadline-string
  4043. "\\|" org-closed-string "\\|" org-clock-string
  4044. "\\)\\>\\)")
  4045. org-all-time-keywords
  4046. (mapcar (lambda (w) (substring w 0 -1))
  4047. (list org-scheduled-string org-deadline-string
  4048. org-clock-string org-closed-string))
  4049. )
  4050. (org-compute-latex-and-specials-regexp)
  4051. (org-set-font-lock-defaults))))
  4052. (defun org-file-contents (file &optional noerror)
  4053. "Return the contents of FILE, as a string."
  4054. (if (or (not file)
  4055. (not (file-readable-p file)))
  4056. (if noerror
  4057. (progn
  4058. (message "Cannot read file \"%s\"" file)
  4059. (ding) (sit-for 2)
  4060. "")
  4061. (error "Cannot read file \"%s\"" file))
  4062. (with-temp-buffer
  4063. (insert-file-contents file)
  4064. (buffer-string))))
  4065. (defun org-extract-log-state-settings (x)
  4066. "Extract the log state setting from a TODO keyword string.
  4067. This will extract info from a string like \"WAIT(w@/!)\"."
  4068. (let (kw key log1 log2)
  4069. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4070. (setq kw (match-string 1 x)
  4071. key (and (match-end 2) (match-string 2 x))
  4072. log1 (and (match-end 3) (match-string 3 x))
  4073. log2 (and (match-end 4) (match-string 4 x)))
  4074. (and (or log1 log2)
  4075. (list kw
  4076. (and log1 (if (equal log1 "!") 'time 'note))
  4077. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4078. (defun org-remove-keyword-keys (list)
  4079. "Remove a pair of parenthesis at the end of each string in LIST."
  4080. (mapcar (lambda (x)
  4081. (if (string-match "(.*)$" x)
  4082. (substring x 0 (match-beginning 0))
  4083. x))
  4084. list))
  4085. (defun org-assign-fast-keys (alist)
  4086. "Assign fast keys to a keyword-key alist.
  4087. Respect keys that are already there."
  4088. (let (new e (alt ?0))
  4089. (while (setq e (pop alist))
  4090. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4091. (cdr e)) ;; Key already assigned.
  4092. (push e new)
  4093. (let ((clist (string-to-list (downcase (car e))))
  4094. (used (append new alist)))
  4095. (when (= (car clist) ?@)
  4096. (pop clist))
  4097. (while (and clist (rassoc (car clist) used))
  4098. (pop clist))
  4099. (unless clist
  4100. (while (rassoc alt used)
  4101. (incf alt)))
  4102. (push (cons (car e) (or (car clist) alt)) new))))
  4103. (nreverse new)))
  4104. ;;; Some variables used in various places
  4105. (defvar org-window-configuration nil
  4106. "Used in various places to store a window configuration.")
  4107. (defvar org-selected-window nil
  4108. "Used in various places to store a window configuration.")
  4109. (defvar org-finish-function nil
  4110. "Function to be called when `C-c C-c' is used.
  4111. This is for getting out of special buffers like remember.")
  4112. ;; FIXME: Occasionally check by commenting these, to make sure
  4113. ;; no other functions uses these, forgetting to let-bind them.
  4114. (defvar entry)
  4115. (defvar last-state)
  4116. (defvar date)
  4117. ;; Defined somewhere in this file, but used before definition.
  4118. (defvar org-entities) ;; defined in org-entities.el
  4119. (defvar org-struct-menu)
  4120. (defvar org-org-menu)
  4121. (defvar org-tbl-menu)
  4122. ;;;; Define the Org-mode
  4123. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4124. (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"))
  4125. ;; We use a before-change function to check if a table might need
  4126. ;; an update.
  4127. (defvar org-table-may-need-update t
  4128. "Indicates that a table might need an update.
  4129. This variable is set by `org-before-change-function'.
  4130. `org-table-align' sets it back to nil.")
  4131. (defun org-before-change-function (beg end)
  4132. "Every change indicates that a table might need an update."
  4133. (setq org-table-may-need-update t))
  4134. (defvar org-mode-map)
  4135. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4136. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4137. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4138. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4139. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4140. (defvar org-table-buffer-is-an nil)
  4141. (defconst org-outline-regexp "\\*+ ")
  4142. ;;;###autoload
  4143. (define-derived-mode org-mode outline-mode "Org"
  4144. "Outline-based notes management and organizer, alias
  4145. \"Carsten's outline-mode for keeping track of everything.\"
  4146. Org-mode develops organizational tasks around a NOTES file which
  4147. contains information about projects as plain text. Org-mode is
  4148. implemented on top of outline-mode, which is ideal to keep the content
  4149. of large files well structured. It supports ToDo items, deadlines and
  4150. time stamps, which magically appear in the diary listing of the Emacs
  4151. calendar. Tables are easily created with a built-in table editor.
  4152. Plain text URL-like links connect to websites, emails (VM), Usenet
  4153. messages (Gnus), BBDB entries, and any files related to the project.
  4154. For printing and sharing of notes, an Org-mode file (or a part of it)
  4155. can be exported as a structured ASCII or HTML file.
  4156. The following commands are available:
  4157. \\{org-mode-map}"
  4158. ;; Get rid of Outline menus, they are not needed
  4159. ;; Need to do this here because define-derived-mode sets up
  4160. ;; the keymap so late. Still, it is a waste to call this each time
  4161. ;; we switch another buffer into org-mode.
  4162. (if (featurep 'xemacs)
  4163. (when (boundp 'outline-mode-menu-heading)
  4164. ;; Assume this is Greg's port, it uses easymenu
  4165. (easy-menu-remove outline-mode-menu-heading)
  4166. (easy-menu-remove outline-mode-menu-show)
  4167. (easy-menu-remove outline-mode-menu-hide))
  4168. (define-key org-mode-map [menu-bar headings] 'undefined)
  4169. (define-key org-mode-map [menu-bar hide] 'undefined)
  4170. (define-key org-mode-map [menu-bar show] 'undefined))
  4171. (org-load-modules-maybe)
  4172. (easy-menu-add org-org-menu)
  4173. (easy-menu-add org-tbl-menu)
  4174. (org-install-agenda-files-menu)
  4175. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4176. (add-to-invisibility-spec '(org-cwidth))
  4177. (add-to-invisibility-spec '(org-hide-block . t))
  4178. (when (featurep 'xemacs)
  4179. (org-set-local 'line-move-ignore-invisible t))
  4180. (org-set-local 'outline-regexp org-outline-regexp)
  4181. (org-set-local 'outline-level 'org-outline-level)
  4182. (when (and org-ellipsis
  4183. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4184. (fboundp 'make-glyph-code))
  4185. (unless org-display-table
  4186. (setq org-display-table (make-display-table)))
  4187. (set-display-table-slot
  4188. org-display-table 4
  4189. (vconcat (mapcar
  4190. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4191. org-ellipsis)))
  4192. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4193. (setq buffer-display-table org-display-table))
  4194. (org-set-regexps-and-options)
  4195. (when (and org-tag-faces (not org-tags-special-faces-re))
  4196. ;; tag faces set outside customize.... force initialization.
  4197. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4198. ;; Calc embedded
  4199. (org-set-local 'calc-embedded-open-mode "# ")
  4200. (modify-syntax-entry ?@ "w")
  4201. (if org-startup-truncated (setq truncate-lines t))
  4202. (org-set-local 'font-lock-unfontify-region-function
  4203. 'org-unfontify-region)
  4204. ;; Activate before-change-function
  4205. (org-set-local 'org-table-may-need-update t)
  4206. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4207. 'local)
  4208. ;; Check for running clock before killing a buffer
  4209. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4210. ;; Paragraphs and auto-filling
  4211. (org-set-autofill-regexps)
  4212. (setq indent-line-function 'org-indent-line-function)
  4213. (org-update-radio-target-regexp)
  4214. ;; Beginning/end of defun
  4215. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4216. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4217. ;; Next error for sparse trees
  4218. (org-set-local 'next-error-function 'org-occur-next-match)
  4219. ;; Make sure dependence stuff works reliably, even for users who set it
  4220. ;; too late :-(
  4221. (if org-enforce-todo-dependencies
  4222. (add-hook 'org-blocker-hook
  4223. 'org-block-todo-from-children-or-siblings-or-parent)
  4224. (remove-hook 'org-blocker-hook
  4225. 'org-block-todo-from-children-or-siblings-or-parent))
  4226. (if org-enforce-todo-checkbox-dependencies
  4227. (add-hook 'org-blocker-hook
  4228. 'org-block-todo-from-checkboxes)
  4229. (remove-hook 'org-blocker-hook
  4230. 'org-block-todo-from-checkboxes))
  4231. ;; Comment characters
  4232. (org-set-local 'comment-start "#")
  4233. (org-set-local 'comment-padding " ")
  4234. ;; Align options lines
  4235. (org-set-local
  4236. 'align-mode-rules-list
  4237. '((org-in-buffer-settings
  4238. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4239. (modes . '(org-mode)))))
  4240. ;; Imenu
  4241. (org-set-local 'imenu-create-index-function
  4242. 'org-imenu-get-tree)
  4243. ;; Make isearch reveal context
  4244. (if (or (featurep 'xemacs)
  4245. (not (boundp 'outline-isearch-open-invisible-function)))
  4246. ;; Emacs 21 and XEmacs make use of the hook
  4247. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4248. ;; Emacs 22 deals with this through a special variable
  4249. (org-set-local 'outline-isearch-open-invisible-function
  4250. (lambda (&rest ignore) (org-show-context 'isearch))))
  4251. ;; Turn on org-beamer-mode?
  4252. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4253. ;; Setup the pcomplete hooks
  4254. (set (make-local-variable 'pcomplete-command-completion-function)
  4255. 'org-pcomplete-initial)
  4256. (set (make-local-variable 'pcomplete-command-name-function)
  4257. 'org-command-at-point)
  4258. (set (make-local-variable 'pcomplete-default-completion-function)
  4259. 'ignore)
  4260. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4261. 'org-parse-arguments)
  4262. (set (make-local-variable 'pcomplete-termination-string) "")
  4263. ;; If empty file that did not turn on org-mode automatically, make it to.
  4264. (if (and org-insert-mode-line-in-empty-file
  4265. (interactive-p)
  4266. (= (point-min) (point-max)))
  4267. (insert "# -*- mode: org -*-\n\n"))
  4268. (unless org-inhibit-startup
  4269. (when org-startup-align-all-tables
  4270. (let ((bmp (buffer-modified-p)))
  4271. (org-table-map-tables 'org-table-align 'quietly)
  4272. (set-buffer-modified-p bmp)))
  4273. (when org-startup-with-inline-images
  4274. (org-display-inline-images))
  4275. (when org-startup-indented
  4276. (require 'org-indent)
  4277. (org-indent-mode 1))
  4278. (unless org-inhibit-startup-visibility-stuff
  4279. (org-set-startup-visibility))))
  4280. (when (fboundp 'abbrev-table-put)
  4281. (abbrev-table-put org-mode-abbrev-table
  4282. :parents (list text-mode-abbrev-table)))
  4283. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4284. (defun org-current-time ()
  4285. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4286. (if (> (car org-time-stamp-rounding-minutes) 1)
  4287. (let ((r (car org-time-stamp-rounding-minutes))
  4288. (time (decode-time)))
  4289. (apply 'encode-time
  4290. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4291. (nthcdr 2 time))))
  4292. (current-time)))
  4293. (defun org-today ()
  4294. "Return today date, considering `org-extend-today-until'."
  4295. (time-to-days
  4296. (time-subtract (current-time)
  4297. (list 0 (* 3600 org-extend-today-until) 0))))
  4298. ;;;; Font-Lock stuff, including the activators
  4299. (defvar org-mouse-map (make-sparse-keymap))
  4300. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4301. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4302. (when org-mouse-1-follows-link
  4303. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4304. (when org-tab-follows-link
  4305. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4306. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4307. (require 'font-lock)
  4308. (defconst org-non-link-chars "]\t\n\r<>")
  4309. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4310. "shell" "elisp" "doi" "message"))
  4311. (defvar org-link-types-re nil
  4312. "Matches a link that has a url-like prefix like \"http:\"")
  4313. (defvar org-link-re-with-space nil
  4314. "Matches a link with spaces, optional angular brackets around it.")
  4315. (defvar org-link-re-with-space2 nil
  4316. "Matches a link with spaces, optional angular brackets around it.")
  4317. (defvar org-link-re-with-space3 nil
  4318. "Matches a link with spaces, only for internal part in bracket links.")
  4319. (defvar org-angle-link-re nil
  4320. "Matches link with angular brackets, spaces are allowed.")
  4321. (defvar org-plain-link-re nil
  4322. "Matches plain link, without spaces.")
  4323. (defvar org-bracket-link-regexp nil
  4324. "Matches a link in double brackets.")
  4325. (defvar org-bracket-link-analytic-regexp nil
  4326. "Regular expression used to analyze links.
  4327. Here is what the match groups contain after a match:
  4328. 1: http:
  4329. 2: http
  4330. 3: path
  4331. 4: [desc]
  4332. 5: desc")
  4333. (defvar org-bracket-link-analytic-regexp++ nil
  4334. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4335. (defvar org-any-link-re nil
  4336. "Regular expression matching any link.")
  4337. (defcustom org-match-sexp-depth 3
  4338. "Number of stacked braces for sub/superscript matching.
  4339. This has to be set before loading org.el to be effective."
  4340. :group 'org-export-translation ; ??????????????????????????/
  4341. :type 'integer)
  4342. (defun org-create-multibrace-regexp (left right n)
  4343. "Create a regular expression which will match a balanced sexp.
  4344. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4345. as single character strings.
  4346. The regexp returned will match the entire expression including the
  4347. delimiters. It will also define a single group which contains the
  4348. match except for the outermost delimiters. The maximum depth of
  4349. stacked delimiters is N. Escaping delimiters is not possible."
  4350. (let* ((nothing (concat "[^" left right "]*?"))
  4351. (or "\\|")
  4352. (re nothing)
  4353. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4354. (while (> n 1)
  4355. (setq n (1- n)
  4356. re (concat re or next)
  4357. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4358. (concat left "\\(" re "\\)" right)))
  4359. (defvar org-match-substring-regexp
  4360. (concat
  4361. "\\([^\\]\\)\\([_^]\\)\\("
  4362. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4363. "\\|"
  4364. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4365. "\\|"
  4366. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4367. "The regular expression matching a sub- or superscript.")
  4368. (defvar org-match-substring-with-braces-regexp
  4369. (concat
  4370. "\\([^\\]\\)\\([_^]\\)\\("
  4371. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4372. "\\)")
  4373. "The regular expression matching a sub- or superscript, forcing braces.")
  4374. (defun org-make-link-regexps ()
  4375. "Update the link regular expressions.
  4376. This should be called after the variable `org-link-types' has changed."
  4377. (setq org-link-types-re
  4378. (concat
  4379. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4380. org-link-re-with-space
  4381. (concat
  4382. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4383. "\\([^" org-non-link-chars " ]"
  4384. "[^" org-non-link-chars "]*"
  4385. "[^" org-non-link-chars " ]\\)>?")
  4386. org-link-re-with-space2
  4387. (concat
  4388. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4389. "\\([^" org-non-link-chars " ]"
  4390. "[^\t\n\r]*"
  4391. "[^" org-non-link-chars " ]\\)>?")
  4392. org-link-re-with-space3
  4393. (concat
  4394. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4395. "\\([^" org-non-link-chars " ]"
  4396. "[^\t\n\r]*\\)")
  4397. org-angle-link-re
  4398. (concat
  4399. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4400. "\\([^" org-non-link-chars " ]"
  4401. "[^" org-non-link-chars "]*"
  4402. "\\)>")
  4403. org-plain-link-re
  4404. (concat
  4405. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4406. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4407. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4408. org-bracket-link-regexp
  4409. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4410. org-bracket-link-analytic-regexp
  4411. (concat
  4412. "\\[\\["
  4413. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4414. "\\([^]]+\\)"
  4415. "\\]"
  4416. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4417. "\\]")
  4418. org-bracket-link-analytic-regexp++
  4419. (concat
  4420. "\\[\\["
  4421. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4422. "\\([^]]+\\)"
  4423. "\\]"
  4424. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4425. "\\]")
  4426. org-any-link-re
  4427. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4428. org-angle-link-re "\\)\\|\\("
  4429. org-plain-link-re "\\)")))
  4430. (org-make-link-regexps)
  4431. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4432. "Regular expression for fast time stamp matching.")
  4433. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?\\)[]>]"
  4434. "Regular expression for fast time stamp matching.")
  4435. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4436. "Regular expression matching time strings for analysis.
  4437. This one does not require the space after the date, so it can be used
  4438. on a string that terminates immediately after the date.")
  4439. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4440. "Regular expression matching time strings for analysis.")
  4441. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4442. "Regular expression matching time stamps, with groups.")
  4443. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4444. "Regular expression matching time stamps (also [..]), with groups.")
  4445. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4446. "Regular expression matching a time stamp range.")
  4447. (defconst org-tr-regexp-both
  4448. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4449. "Regular expression matching a time stamp range.")
  4450. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4451. org-ts-regexp "\\)?")
  4452. "Regular expression matching a time stamp or time stamp range.")
  4453. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4454. org-ts-regexp-both "\\)?")
  4455. "Regular expression matching a time stamp or time stamp range.
  4456. The time stamps may be either active or inactive.")
  4457. (defvar org-emph-face nil)
  4458. (defun org-do-emphasis-faces (limit)
  4459. "Run through the buffer and add overlays to links."
  4460. (let (rtn a)
  4461. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4462. (if (not (= (char-after (match-beginning 3))
  4463. (char-after (match-beginning 4))))
  4464. (progn
  4465. (setq rtn t)
  4466. (setq a (assoc (match-string 3) org-emphasis-alist))
  4467. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4468. 'face
  4469. (nth 1 a))
  4470. (and (nth 4 a)
  4471. (org-remove-flyspell-overlays-in
  4472. (match-beginning 0) (match-end 0)))
  4473. (add-text-properties (match-beginning 2) (match-end 2)
  4474. '(font-lock-multiline t org-emphasis t))
  4475. (when org-hide-emphasis-markers
  4476. (add-text-properties (match-end 4) (match-beginning 5)
  4477. '(invisible org-link))
  4478. (add-text-properties (match-beginning 3) (match-end 3)
  4479. '(invisible org-link)))))
  4480. (backward-char 1))
  4481. rtn))
  4482. (defun org-emphasize (&optional char)
  4483. "Insert or change an emphasis, i.e. a font like bold or italic.
  4484. If there is an active region, change that region to a new emphasis.
  4485. If there is no region, just insert the marker characters and position
  4486. the cursor between them.
  4487. CHAR should be either the marker character, or the first character of the
  4488. HTML tag associated with that emphasis. If CHAR is a space, the means
  4489. to remove the emphasis of the selected region.
  4490. If char is not given (for example in an interactive call) it
  4491. will be prompted for."
  4492. (interactive)
  4493. (let ((eal org-emphasis-alist) e det
  4494. (erc org-emphasis-regexp-components)
  4495. (prompt "")
  4496. (string "") beg end move tag c s)
  4497. (if (org-region-active-p)
  4498. (setq beg (region-beginning) end (region-end)
  4499. string (buffer-substring beg end))
  4500. (setq move t))
  4501. (while (setq e (pop eal))
  4502. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4503. c (aref tag 0))
  4504. (push (cons c (string-to-char (car e))) det)
  4505. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4506. (substring tag 1)))))
  4507. (setq det (nreverse det))
  4508. (unless char
  4509. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4510. (setq char (read-char-exclusive)))
  4511. (setq char (or (cdr (assoc char det)) char))
  4512. (if (equal char ?\ )
  4513. (setq s "" move nil)
  4514. (unless (assoc (char-to-string char) org-emphasis-alist)
  4515. (error "No such emphasis marker: \"%c\"" char))
  4516. (setq s (char-to-string char)))
  4517. (while (and (> (length string) 1)
  4518. (equal (substring string 0 1) (substring string -1))
  4519. (assoc (substring string 0 1) org-emphasis-alist))
  4520. (setq string (substring string 1 -1)))
  4521. (setq string (concat s string s))
  4522. (if beg (delete-region beg end))
  4523. (unless (or (bolp)
  4524. (string-match (concat "[" (nth 0 erc) "\n]")
  4525. (char-to-string (char-before (point)))))
  4526. (insert " "))
  4527. (unless (or (eobp)
  4528. (string-match (concat "[" (nth 1 erc) "\n]")
  4529. (char-to-string (char-after (point)))))
  4530. (insert " ") (backward-char 1))
  4531. (insert string)
  4532. (and move (backward-char 1))))
  4533. (defconst org-nonsticky-props
  4534. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4535. (defsubst org-rear-nonsticky-at (pos)
  4536. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4537. (defun org-activate-plain-links (limit)
  4538. "Run through the buffer and add overlays to links."
  4539. (catch 'exit
  4540. (let (f)
  4541. (if (re-search-forward org-plain-link-re limit t)
  4542. (progn
  4543. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4544. (setq f (get-text-property (match-beginning 0) 'face))
  4545. (if (or (eq f 'org-tag)
  4546. (and (listp f) (memq 'org-tag f)))
  4547. nil
  4548. (add-text-properties (match-beginning 0) (match-end 0)
  4549. (list 'mouse-face 'highlight
  4550. 'face 'org-link
  4551. 'keymap org-mouse-map))
  4552. (org-rear-nonsticky-at (match-end 0)))
  4553. t)))))
  4554. (defun org-activate-code (limit)
  4555. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4556. (progn
  4557. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4558. (remove-text-properties (match-beginning 0) (match-end 0)
  4559. '(display t invisible t intangible t))
  4560. t)))
  4561. (defcustom org-src-fontify-natively nil
  4562. "When non-nil, fontify code in code blocks."
  4563. :type 'boolean
  4564. :group 'org-appearance
  4565. :group 'org-babel)
  4566. (defun org-fontify-meta-lines-and-blocks (limit)
  4567. (condition-case nil
  4568. (org-fontify-meta-lines-and-blocks-1 limit)
  4569. (error (message "org-mode fontification error"))))
  4570. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4571. "Fontify #+ lines and blocks, in the correct ways."
  4572. (let ((case-fold-search t))
  4573. (if (re-search-forward
  4574. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4575. limit t)
  4576. (let ((beg (match-beginning 0))
  4577. (block-start (match-end 0))
  4578. (block-end nil)
  4579. (lang (match-string 7))
  4580. (beg1 (line-beginning-position 2))
  4581. (dc1 (downcase (match-string 2)))
  4582. (dc3 (downcase (match-string 3)))
  4583. end end1 quoting block-type ovl)
  4584. (cond
  4585. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4586. ;; a single line of backend-specific content
  4587. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4588. (remove-text-properties (match-beginning 0) (match-end 0)
  4589. '(display t invisible t intangible t))
  4590. (add-text-properties (match-beginning 1) (match-end 3)
  4591. '(font-lock-fontified t face org-meta-line))
  4592. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4593. '(font-lock-fontified t face org-block))
  4594. ; for backend-specific code
  4595. t)
  4596. ((and (match-end 4) (equal dc3 "begin"))
  4597. ;; Truly a block
  4598. (setq block-type (downcase (match-string 5))
  4599. quoting (member block-type org-protecting-blocks))
  4600. (when (re-search-forward
  4601. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4602. nil t) ;; on purpose, we look further than LIMIT
  4603. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4604. (setq block-end (match-beginning 0))
  4605. (when quoting
  4606. (remove-text-properties beg end
  4607. '(display t invisible t intangible t)))
  4608. (add-text-properties
  4609. beg end
  4610. '(font-lock-fontified t font-lock-multiline t))
  4611. (add-text-properties beg beg1 '(face org-meta-line))
  4612. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4613. ; for end_src
  4614. (cond
  4615. ((and lang org-src-fontify-natively)
  4616. (org-src-font-lock-fontify-block lang block-start block-end)
  4617. ;; remove old background overlays
  4618. (mapc (lambda (ov)
  4619. (if (eq (overlay-get ov 'face) 'org-block-background)
  4620. (delete-overlay ov)))
  4621. (overlays-at (/ (+ beg1 block-end) 2)))
  4622. ;; add a background overlay
  4623. (setq ovl (make-overlay beg1 block-end))
  4624. (overlay-put ovl 'face 'org-block-background)
  4625. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4626. (quoting
  4627. (add-text-properties beg1 (+ end1 1) '(face org-block)))
  4628. ; end of source block
  4629. ((not org-fontify-quote-and-verse-blocks))
  4630. ((string= block-type "quote")
  4631. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4632. ((string= block-type "verse")
  4633. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4634. (add-text-properties beg beg1 '(face org-block-begin-line))
  4635. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4636. t))
  4637. ((member dc1 '("title:" "author:" "email:" "date:"))
  4638. (add-text-properties
  4639. beg (match-end 3)
  4640. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4641. '(font-lock-fontified t invisible t)
  4642. '(font-lock-fontified t face org-document-info-keyword)))
  4643. (add-text-properties
  4644. (match-beginning 6) (match-end 6)
  4645. (if (string-equal dc1 "title:")
  4646. '(font-lock-fontified t face org-document-title)
  4647. '(font-lock-fontified t face org-document-info))))
  4648. ((not (member (char-after beg) '(?\ ?\t)))
  4649. ;; just any other in-buffer setting, but not indented
  4650. (add-text-properties
  4651. beg (1+ (match-end 0))
  4652. '(font-lock-fontified t face org-meta-line))
  4653. t)
  4654. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4655. "orgtbl:" "tblfm:" "tblname:" "result:"
  4656. "results:" "source:" "srcname:" "call:"))
  4657. (and (match-end 4) (equal dc3 "attr")))
  4658. (add-text-properties
  4659. beg (match-end 0)
  4660. '(font-lock-fontified t face org-meta-line))
  4661. t)
  4662. ((member dc3 '(" " ""))
  4663. (add-text-properties
  4664. beg (match-end 0)
  4665. '(font-lock-fontified t face font-lock-comment-face)))
  4666. (t nil))))))
  4667. (defun org-activate-angle-links (limit)
  4668. "Run through the buffer and add overlays to links."
  4669. (if (re-search-forward org-angle-link-re limit t)
  4670. (progn
  4671. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4672. (add-text-properties (match-beginning 0) (match-end 0)
  4673. (list 'mouse-face 'highlight
  4674. 'keymap org-mouse-map))
  4675. (org-rear-nonsticky-at (match-end 0))
  4676. t)))
  4677. (defun org-activate-footnote-links (limit)
  4678. "Run through the buffer and add overlays to links."
  4679. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4680. (when fn
  4681. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4682. (org-remove-flyspell-overlays-in beg end)
  4683. (add-text-properties beg end
  4684. (list 'mouse-face 'highlight
  4685. 'keymap org-mouse-map
  4686. 'help-echo
  4687. (if (= (point-at-bol) beg)
  4688. "Footnote definition"
  4689. "Footnote reference")))
  4690. (save-excursion
  4691. (goto-char beg)
  4692. (looking-at (regexp-quote (buffer-substring beg end))))))))
  4693. (defun org-activate-bracket-links (limit)
  4694. "Run through the buffer and add overlays to bracketed links."
  4695. (if (re-search-forward org-bracket-link-regexp limit t)
  4696. (let* ((help (concat "LINK: "
  4697. (org-match-string-no-properties 1)))
  4698. ;; FIXME: above we should remove the escapes.
  4699. ;; but that requires another match, protecting match data,
  4700. ;; a lot of overhead for font-lock.
  4701. (ip (org-maybe-intangible
  4702. (list 'invisible 'org-link
  4703. 'keymap org-mouse-map 'mouse-face 'highlight
  4704. 'font-lock-multiline t 'help-echo help)))
  4705. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4706. 'font-lock-multiline t 'help-echo help)))
  4707. ;; We need to remove the invisible property here. Table narrowing
  4708. ;; may have made some of this invisible.
  4709. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4710. (remove-text-properties (match-beginning 0) (match-end 0)
  4711. '(invisible nil))
  4712. (if (match-end 3)
  4713. (progn
  4714. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4715. (org-rear-nonsticky-at (match-beginning 3))
  4716. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4717. (org-rear-nonsticky-at (match-end 3))
  4718. (add-text-properties (match-end 3) (match-end 0) ip)
  4719. (org-rear-nonsticky-at (match-end 0)))
  4720. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4721. (org-rear-nonsticky-at (match-beginning 1))
  4722. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4723. (org-rear-nonsticky-at (match-end 1))
  4724. (add-text-properties (match-end 1) (match-end 0) ip)
  4725. (org-rear-nonsticky-at (match-end 0)))
  4726. t)))
  4727. (defun org-activate-dates (limit)
  4728. "Run through the buffer and add overlays to dates."
  4729. (if (re-search-forward org-tsr-regexp-both limit t)
  4730. (progn
  4731. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4732. (add-text-properties (match-beginning 0) (match-end 0)
  4733. (list 'mouse-face 'highlight
  4734. 'keymap org-mouse-map))
  4735. (org-rear-nonsticky-at (match-end 0))
  4736. (when org-display-custom-times
  4737. (if (match-end 3)
  4738. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4739. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4740. t)))
  4741. (defvar org-target-link-regexp nil
  4742. "Regular expression matching radio targets in plain text.")
  4743. (make-variable-buffer-local 'org-target-link-regexp)
  4744. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4745. "Regular expression matching a link target.")
  4746. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4747. "Regular expression matching a radio target.")
  4748. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4749. "Regular expression matching any target.")
  4750. (defun org-activate-target-links (limit)
  4751. "Run through the buffer and add overlays to target matches."
  4752. (when org-target-link-regexp
  4753. (let ((case-fold-search t))
  4754. (if (re-search-forward org-target-link-regexp limit t)
  4755. (progn
  4756. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4757. (add-text-properties (match-beginning 0) (match-end 0)
  4758. (list 'mouse-face 'highlight
  4759. 'keymap org-mouse-map
  4760. 'help-echo "Radio target link"
  4761. 'org-linked-text t))
  4762. (org-rear-nonsticky-at (match-end 0))
  4763. t)))))
  4764. (defun org-update-radio-target-regexp ()
  4765. "Find all radio targets in this file and update the regular expression."
  4766. (interactive)
  4767. (when (memq 'radio org-activate-links)
  4768. (setq org-target-link-regexp
  4769. (org-make-target-link-regexp (org-all-targets 'radio)))
  4770. (org-restart-font-lock)))
  4771. (defun org-hide-wide-columns (limit)
  4772. (let (s e)
  4773. (setq s (text-property-any (point) (or limit (point-max))
  4774. 'org-cwidth t))
  4775. (when s
  4776. (setq e (next-single-property-change s 'org-cwidth))
  4777. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4778. (goto-char e)
  4779. t)))
  4780. (defvar org-latex-and-specials-regexp nil
  4781. "Regular expression for highlighting export special stuff.")
  4782. (defvar org-match-substring-regexp)
  4783. (defvar org-match-substring-with-braces-regexp)
  4784. ;; This should be with the exporter code, but we also use if for font-locking
  4785. (defconst org-export-html-special-string-regexps
  4786. '(("\\\\-" . "&shy;")
  4787. ("---\\([^-]\\)" . "&mdash;\\1")
  4788. ("--\\([^-]\\)" . "&ndash;\\1")
  4789. ("\\.\\.\\." . "&hellip;"))
  4790. "Regular expressions for special string conversion.")
  4791. (defun org-compute-latex-and-specials-regexp ()
  4792. "Compute regular expression for stuff treated specially by exporters."
  4793. (if (not org-highlight-latex-fragments-and-specials)
  4794. (org-set-local 'org-latex-and-specials-regexp nil)
  4795. (require 'org-exp)
  4796. (let*
  4797. ((matchers (plist-get org-format-latex-options :matchers))
  4798. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4799. org-latex-regexps)))
  4800. (org-export-allow-BIND nil)
  4801. (options (org-combine-plists (org-default-export-plist)
  4802. (org-infile-export-plist)))
  4803. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4804. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4805. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4806. (org-export-html-expand (plist-get options :expand-quoted-html))
  4807. (org-export-with-special-strings (plist-get options :special-strings))
  4808. (re-sub
  4809. (cond
  4810. ((equal org-export-with-sub-superscripts '{})
  4811. (list org-match-substring-with-braces-regexp))
  4812. (org-export-with-sub-superscripts
  4813. (list org-match-substring-regexp))
  4814. (t nil)))
  4815. (re-latex
  4816. (if org-export-with-LaTeX-fragments
  4817. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4818. (re-macros
  4819. (if org-export-with-TeX-macros
  4820. (list (concat "\\\\"
  4821. (regexp-opt
  4822. (append
  4823. (delq nil
  4824. (mapcar 'car-safe
  4825. (append org-entities-user
  4826. org-entities)))
  4827. (if (boundp 'org-latex-entities)
  4828. (mapcar (lambda (x)
  4829. (or (car-safe x) x))
  4830. org-latex-entities)
  4831. nil))
  4832. 'words))) ; FIXME
  4833. ))
  4834. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4835. (re-special (if org-export-with-special-strings
  4836. (mapcar (lambda (x) (car x))
  4837. org-export-html-special-string-regexps)))
  4838. (re-rest
  4839. (delq nil
  4840. (list
  4841. (if org-export-html-expand "@<[^>\n]+>")
  4842. ))))
  4843. (org-set-local
  4844. 'org-latex-and-specials-regexp
  4845. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4846. re-rest) "\\|")))))
  4847. (defun org-do-latex-and-special-faces (limit)
  4848. "Run through the buffer and add overlays to links."
  4849. (when org-latex-and-specials-regexp
  4850. (let (rtn d)
  4851. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4852. limit t))
  4853. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4854. 'face))
  4855. '(org-code org-verbatim underline)))
  4856. (progn
  4857. (setq rtn t
  4858. d (cond ((member (char-after (1+ (match-beginning 0)))
  4859. '(?_ ?^)) 1)
  4860. (t 0)))
  4861. (font-lock-prepend-text-property
  4862. (+ d (match-beginning 0)) (match-end 0)
  4863. 'face 'org-latex-and-export-specials)
  4864. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4865. '(font-lock-multiline t)))))
  4866. rtn)))
  4867. (defun org-restart-font-lock ()
  4868. "Restart `font-lock-mode', to force refontification."
  4869. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4870. (font-lock-mode -1)
  4871. (font-lock-mode 1)))
  4872. (defun org-all-targets (&optional radio)
  4873. "Return a list of all targets in this file.
  4874. With optional argument RADIO, only find radio targets."
  4875. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4876. rtn)
  4877. (save-excursion
  4878. (goto-char (point-min))
  4879. (while (re-search-forward re nil t)
  4880. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4881. rtn)))
  4882. (defun org-make-target-link-regexp (targets)
  4883. "Make regular expression matching all strings in TARGETS.
  4884. The regular expression finds the targets also if there is a line break
  4885. between words."
  4886. (and targets
  4887. (concat
  4888. "\\<\\("
  4889. (mapconcat
  4890. (lambda (x)
  4891. (setq x (regexp-quote x))
  4892. (while (string-match " +" x)
  4893. (setq x (replace-match "\\s-+" t t x)))
  4894. x)
  4895. targets
  4896. "\\|")
  4897. "\\)\\>")))
  4898. (defun org-activate-tags (limit)
  4899. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4900. (progn
  4901. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4902. (add-text-properties (match-beginning 1) (match-end 1)
  4903. (list 'mouse-face 'highlight
  4904. 'keymap org-mouse-map))
  4905. (org-rear-nonsticky-at (match-end 1))
  4906. t)))
  4907. (defun org-outline-level ()
  4908. "Compute the outline level of the heading at point.
  4909. This function assumes that the cursor is at the beginning of a line matched
  4910. by `outline-regexp'. Otherwise it returns garbage.
  4911. If this is called at a normal headline, the level is the number of stars.
  4912. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  4913. (save-excursion
  4914. (looking-at outline-regexp)
  4915. (1- (- (match-end 0) (match-beginning 0)))))
  4916. (defvar org-font-lock-keywords nil)
  4917. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4918. "Regular expression matching a property line.")
  4919. (defvar org-font-lock-hook nil
  4920. "Functions to be called for special font lock stuff.")
  4921. (defvar org-font-lock-set-keywords-hook nil
  4922. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4923. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4924. it is installed to be used by font lock. This can be useful if something
  4925. needs to be inserted at a specific position in the font-lock sequence.")
  4926. (defun org-font-lock-hook (limit)
  4927. (run-hook-with-args 'org-font-lock-hook limit))
  4928. (defun org-set-font-lock-defaults ()
  4929. (let* ((em org-fontify-emphasized-text)
  4930. (lk org-activate-links)
  4931. (org-font-lock-extra-keywords
  4932. (list
  4933. ;; Call the hook
  4934. '(org-font-lock-hook)
  4935. ;; Headlines
  4936. `(,(if org-fontify-whole-heading-line
  4937. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4938. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4939. (1 (org-get-level-face 1))
  4940. (2 (org-get-level-face 2))
  4941. (3 (org-get-level-face 3)))
  4942. ;; Table lines
  4943. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4944. (1 'org-table t))
  4945. ;; Table internals
  4946. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4947. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4948. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4949. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  4950. ;; Drawers
  4951. (list org-drawer-regexp '(0 'org-special-keyword t))
  4952. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4953. ;; Properties
  4954. (list org-property-re
  4955. '(1 'org-special-keyword t)
  4956. '(3 'org-property-value t))
  4957. ;; Links
  4958. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4959. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4960. (if (memq 'plain lk) '(org-activate-plain-links))
  4961. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4962. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4963. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4964. (if (memq 'footnote lk) '(org-activate-footnote-links
  4965. (0 'org-footnote t)))
  4966. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4967. '(org-hide-wide-columns (0 nil append))
  4968. ;; TODO lines
  4969. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4970. '(1 (org-get-todo-face 1) t))
  4971. ;; DONE
  4972. (if org-fontify-done-headline
  4973. (list (concat "^[*]+ +\\<\\("
  4974. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4975. "\\)\\(.*\\)")
  4976. '(2 'org-headline-done t))
  4977. nil)
  4978. ;; Priorities
  4979. '(org-font-lock-add-priority-faces)
  4980. ;; Tags
  4981. '(org-font-lock-add-tag-faces)
  4982. ;; Special keywords
  4983. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4984. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4985. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4986. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4987. ;; Emphasis
  4988. (if em
  4989. (if (featurep 'xemacs)
  4990. '(org-do-emphasis-faces (0 nil append))
  4991. '(org-do-emphasis-faces)))
  4992. ;; Checkboxes
  4993. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4994. 1 'org-checkbox prepend)
  4995. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4996. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4997. (0 (org-get-checkbox-statistics-face) t)))
  4998. ;; Description list items
  4999. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5000. 1 'bold prepend)
  5001. ;; ARCHIVEd headings
  5002. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  5003. '(1 'org-archived prepend))
  5004. ;; Specials
  5005. '(org-do-latex-and-special-faces)
  5006. '(org-fontify-entities)
  5007. '(org-raise-scripts)
  5008. ;; Code
  5009. '(org-activate-code (1 'org-code t))
  5010. ;; COMMENT
  5011. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5012. "\\|" org-quote-string "\\)\\>")
  5013. '(1 'org-special-keyword t))
  5014. '("^#.*" (0 'font-lock-comment-face t))
  5015. ;; Blocks and meta lines
  5016. '(org-fontify-meta-lines-and-blocks)
  5017. )))
  5018. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5019. (run-hooks 'org-font-lock-set-keywords-hook)
  5020. ;; Now set the full font-lock-keywords
  5021. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5022. (org-set-local 'font-lock-defaults
  5023. '(org-font-lock-keywords t nil nil backward-paragraph))
  5024. (kill-local-variable 'font-lock-keywords) nil))
  5025. (defun org-toggle-pretty-entities ()
  5026. "Toggle the composition display of entities as UTF8 characters."
  5027. (interactive)
  5028. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5029. (org-restart-font-lock)
  5030. (if org-pretty-entities
  5031. (message "Entities are displayed as UTF8 characers")
  5032. (save-restriction
  5033. (widen)
  5034. (org-decompose-region (point-min) (point-max))
  5035. (message "Entities are displayed plain"))))
  5036. (defun org-fontify-entities (limit)
  5037. "Find an entity to fontify."
  5038. (let (ee)
  5039. (when org-pretty-entities
  5040. (catch 'match
  5041. (while (re-search-forward
  5042. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  5043. limit t)
  5044. (if (and (not (org-in-indented-comment-line))
  5045. (setq ee (org-entity-get (match-string 1)))
  5046. (= (length (nth 6 ee)) 1))
  5047. (progn
  5048. (add-text-properties
  5049. (match-beginning 0) (match-end 1)
  5050. (list 'font-lock-fontified t))
  5051. (compose-region (match-beginning 0) (match-end 1)
  5052. (nth 6 ee) nil)
  5053. (backward-char 1)
  5054. (throw 'match t))))
  5055. nil))))
  5056. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5057. "Fontify string S like in Org-mode."
  5058. (with-temp-buffer
  5059. (insert s)
  5060. (let ((org-odd-levels-only odd-levels))
  5061. (org-mode)
  5062. (font-lock-fontify-buffer)
  5063. (buffer-string))))
  5064. (defvar org-m nil)
  5065. (defvar org-l nil)
  5066. (defvar org-f nil)
  5067. (defun org-get-level-face (n)
  5068. "Get the right face for match N in font-lock matching of headlines."
  5069. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5070. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5071. (if org-cycle-level-faces
  5072. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5073. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5074. (cond
  5075. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5076. ((eq n 2) org-f)
  5077. (t (if org-level-color-stars-only nil org-f))))
  5078. (defun org-get-todo-face (kwd)
  5079. "Get the right face for a TODO keyword KWD.
  5080. If KWD is a number, get the corresponding match group."
  5081. (if (numberp kwd) (setq kwd (match-string kwd)))
  5082. (or (org-face-from-face-or-color
  5083. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5084. (and (member kwd org-done-keywords) 'org-done)
  5085. 'org-todo))
  5086. (defun org-face-from-face-or-color (context inherit face-or-color)
  5087. "Create a face list that inherits INHERIT, but sets the foreground color.
  5088. When FACE-OR-COLOR is not a string, just return it."
  5089. (if (stringp face-or-color)
  5090. (list :inherit inherit
  5091. (cdr (assoc context org-faces-easy-properties))
  5092. face-or-color)
  5093. face-or-color))
  5094. (defun org-font-lock-add-tag-faces (limit)
  5095. "Add the special tag faces."
  5096. (when (and org-tag-faces org-tags-special-faces-re)
  5097. (while (re-search-forward org-tags-special-faces-re limit t)
  5098. (add-text-properties (match-beginning 1) (match-end 1)
  5099. (list 'face (org-get-tag-face 1)
  5100. 'font-lock-fontified t))
  5101. (backward-char 1))))
  5102. (defun org-font-lock-add-priority-faces (limit)
  5103. "Add the special priority faces."
  5104. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5105. (add-text-properties
  5106. (match-beginning 0) (match-end 0)
  5107. (list 'face (or (org-face-from-face-or-color
  5108. 'priority 'org-special-keyword
  5109. (cdr (assoc (char-after (match-beginning 1))
  5110. org-priority-faces)))
  5111. 'org-special-keyword)
  5112. 'font-lock-fontified t))))
  5113. (defun org-get-tag-face (kwd)
  5114. "Get the right face for a TODO keyword KWD.
  5115. If KWD is a number, get the corresponding match group."
  5116. (if (numberp kwd) (setq kwd (match-string kwd)))
  5117. (or (org-face-from-face-or-color
  5118. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5119. 'org-tag))
  5120. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5121. "Remove fontification and activation overlays from links."
  5122. (font-lock-default-unfontify-region beg end)
  5123. (let* ((buffer-undo-list t)
  5124. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5125. (inhibit-modification-hooks t)
  5126. deactivate-mark buffer-file-name buffer-file-truename)
  5127. (org-decompose-region beg end)
  5128. (remove-text-properties
  5129. beg end
  5130. (if org-indent-mode
  5131. ;; also remove line-prefix and wrap-prefix properties
  5132. '(mouse-face t keymap t org-linked-text t
  5133. invisible t intangible t
  5134. line-prefix t wrap-prefix t
  5135. org-no-flyspell t org-emphasis t)
  5136. '(mouse-face t keymap t org-linked-text t
  5137. invisible t intangible t
  5138. org-no-flyspell t org-emphasis t)))
  5139. (org-remove-font-lock-display-properties beg end)))
  5140. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5141. ((raise 0.3) (height 0.7))
  5142. ((raise -0.5))
  5143. ((raise 0.5)))
  5144. "Display properties for showing superscripts and subscripts.")
  5145. (defun org-remove-font-lock-display-properties (beg end)
  5146. "Remove specific display properties that have been added by font lock.
  5147. The will remove the raise properties that are used to show superscripts
  5148. and subscripts."
  5149. (let (next prop)
  5150. (while (< beg end)
  5151. (setq next (next-single-property-change beg 'display nil end)
  5152. prop (get-text-property beg 'display))
  5153. (if (member prop org-script-display)
  5154. (put-text-property beg next 'display nil))
  5155. (setq beg next))))
  5156. (defun org-raise-scripts (limit)
  5157. "Add raise properties to sub/superscripts."
  5158. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5159. (if (re-search-forward
  5160. (if (eq org-use-sub-superscripts t)
  5161. org-match-substring-regexp
  5162. org-match-substring-with-braces-regexp)
  5163. limit t)
  5164. (let* ((pos (point)) table-p comment-p
  5165. (mpos (match-beginning 3))
  5166. (emph-p (get-text-property mpos 'org-emphasis))
  5167. (link-p (get-text-property mpos 'mouse-face))
  5168. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5169. (goto-char (point-at-bol))
  5170. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5171. comment-p (org-looking-at-p "[ \t]*#"))
  5172. (goto-char pos)
  5173. ;; FIXME: Should we go back one character here, for a_b^c
  5174. ;; (goto-char (1- pos)) ;????????????????????
  5175. (if (or comment-p emph-p link-p keyw-p)
  5176. t
  5177. (put-text-property (match-beginning 3) (match-end 0)
  5178. 'display
  5179. (if (equal (char-after (match-beginning 2)) ?^)
  5180. (nth (if table-p 3 1) org-script-display)
  5181. (nth (if table-p 2 0) org-script-display)))
  5182. (add-text-properties (match-beginning 2) (match-end 2)
  5183. (list 'invisible t
  5184. 'org-dwidth t 'org-dwidth-n 1))
  5185. (if (and (eq (char-after (match-beginning 3)) ?{)
  5186. (eq (char-before (match-end 3)) ?}))
  5187. (progn
  5188. (add-text-properties
  5189. (match-beginning 3) (1+ (match-beginning 3))
  5190. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5191. (add-text-properties
  5192. (1- (match-end 3)) (match-end 3)
  5193. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5194. t)))))
  5195. ;;;; Visibility cycling, including org-goto and indirect buffer
  5196. ;;; Cycling
  5197. (defvar org-cycle-global-status nil)
  5198. (make-variable-buffer-local 'org-cycle-global-status)
  5199. (defvar org-cycle-subtree-status nil)
  5200. (make-variable-buffer-local 'org-cycle-subtree-status)
  5201. ;;;###autoload
  5202. (defvar org-inlinetask-min-level)
  5203. (defun org-cycle (&optional arg)
  5204. "TAB-action and visibility cycling for Org-mode.
  5205. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5206. is outline visibility cycling, but it also invokes other actions
  5207. in special contexts.
  5208. - When this function is called with a prefix argument, rotate the entire
  5209. buffer through 3 states (global cycling)
  5210. 1. OVERVIEW: Show only top-level headlines.
  5211. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5212. 3. SHOW ALL: Show everything.
  5213. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5214. determined by the variable `org-startup-folded', and by any VISIBILITY
  5215. properties in the buffer.
  5216. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5217. including any drawers.
  5218. - When inside a table, re-align the table and move to the next field.
  5219. - When point is at the beginning of a headline, rotate the subtree started
  5220. by this line through 3 different states (local cycling)
  5221. 1. FOLDED: Only the main headline is shown.
  5222. 2. CHILDREN: The main headline and the direct children are shown.
  5223. From this state, you can move to one of the children
  5224. and zoom in further.
  5225. 3. SUBTREE: Show the entire subtree, including body text.
  5226. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5227. - When point is at the beginning of an empty headline and the variable
  5228. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5229. of the headline by demoting and promoting it to likely levels. This
  5230. speeds up creation document structure by pressing TAB once or several
  5231. times right after creating a new headline.
  5232. - When there is a numeric prefix, go up to a heading with level ARG, do
  5233. a `show-subtree' and return to the previous cursor position. If ARG
  5234. is negative, go up that many levels.
  5235. - When point is not at the beginning of a headline, execute the global
  5236. binding for TAB, which is re-indenting the line. See the option
  5237. `org-cycle-emulate-tab' for details.
  5238. - Special case: if point is at the beginning of the buffer and there is
  5239. no headline in line 1, this function will act as if called with prefix arg
  5240. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5241. But only if also the variable `org-cycle-global-at-bob' is t."
  5242. (interactive "P")
  5243. (org-load-modules-maybe)
  5244. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5245. (and org-cycle-level-after-item/entry-creation
  5246. (or (org-cycle-level)
  5247. (org-cycle-item-indentation))))
  5248. (let* ((limit-level
  5249. (or org-cycle-max-level
  5250. (and (boundp 'org-inlinetask-min-level)
  5251. org-inlinetask-min-level
  5252. (1- org-inlinetask-min-level))))
  5253. (nstars (and limit-level
  5254. (if org-odd-levels-only
  5255. (and limit-level (1- (* limit-level 2)))
  5256. limit-level)))
  5257. (outline-regexp
  5258. (if (not (org-mode-p))
  5259. outline-regexp
  5260. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5261. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5262. (not (looking-at outline-regexp))))
  5263. (org-cycle-hook
  5264. (if bob-special
  5265. (delq 'org-optimize-window-after-visibility-change
  5266. (copy-sequence org-cycle-hook))
  5267. org-cycle-hook))
  5268. (pos (point)))
  5269. (if (or bob-special (equal arg '(4)))
  5270. ;; special case: use global cycling
  5271. (setq arg t))
  5272. (cond
  5273. ((equal arg '(16))
  5274. (setq last-command 'dummy)
  5275. (org-set-startup-visibility)
  5276. (message "Startup visibility, plus VISIBILITY properties"))
  5277. ((equal arg '(64))
  5278. (show-all)
  5279. (message "Entire buffer visible, including drawers"))
  5280. ;; Table: enter it or move to the next field.
  5281. ((org-at-table-p 'any)
  5282. (if (org-at-table.el-p)
  5283. (message "Use C-c ' to edit table.el tables")
  5284. (if arg (org-table-edit-field t)
  5285. (org-table-justify-field-maybe)
  5286. (call-interactively 'org-table-next-field))))
  5287. ((run-hook-with-args-until-success
  5288. 'org-tab-after-check-for-table-hook))
  5289. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5290. ((eq arg t) (org-cycle-internal-global))
  5291. ;; Drawers: delegate to `org-flag-drawer'.
  5292. ((and org-drawers org-drawer-regexp
  5293. (save-excursion
  5294. (beginning-of-line 1)
  5295. (looking-at org-drawer-regexp)))
  5296. (org-flag-drawer ; toggle block visibility
  5297. (not (get-char-property (match-end 0) 'invisible))))
  5298. ;; Show-subtree, ARG levels up from here.
  5299. ((integerp arg)
  5300. (save-excursion
  5301. (org-back-to-heading)
  5302. (outline-up-heading (if (< arg 0) (- arg)
  5303. (- (funcall outline-level) arg)))
  5304. (org-show-subtree)))
  5305. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5306. ((and (featurep 'org-inlinetask)
  5307. (org-inlinetask-at-task-p)
  5308. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5309. (org-inlinetask-toggle-visibility))
  5310. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5311. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5312. (save-excursion (beginning-of-line 1)
  5313. (looking-at outline-regexp)))
  5314. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5315. (org-cycle-internal-local))
  5316. ;; From there: TAB emulation and template completion.
  5317. (buffer-read-only (org-back-to-heading))
  5318. ((run-hook-with-args-until-success
  5319. 'org-tab-after-check-for-cycling-hook))
  5320. ((org-try-structure-completion))
  5321. ((org-try-cdlatex-tab))
  5322. ((run-hook-with-args-until-success
  5323. 'org-tab-before-tab-emulation-hook))
  5324. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5325. (or (not (bolp))
  5326. (not (looking-at outline-regexp))))
  5327. (call-interactively (global-key-binding "\t")))
  5328. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5329. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5330. (or (and (eq org-cycle-emulate-tab 'white)
  5331. (= (match-end 0) (point-at-eol)))
  5332. (and (eq org-cycle-emulate-tab 'whitestart)
  5333. (>= (match-end 0) pos))))
  5334. t
  5335. (eq org-cycle-emulate-tab t))
  5336. (call-interactively (global-key-binding "\t")))
  5337. (t (save-excursion
  5338. (org-back-to-heading)
  5339. (org-cycle)))))))
  5340. (defun org-cycle-internal-global ()
  5341. "Do the global cycling action."
  5342. (cond
  5343. ((and (eq last-command this-command)
  5344. (eq org-cycle-global-status 'overview))
  5345. ;; We just created the overview - now do table of contents
  5346. ;; This can be slow in very large buffers, so indicate action
  5347. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5348. (message "CONTENTS...")
  5349. (org-content)
  5350. (message "CONTENTS...done")
  5351. (setq org-cycle-global-status 'contents)
  5352. (run-hook-with-args 'org-cycle-hook 'contents))
  5353. ((and (eq last-command this-command)
  5354. (eq org-cycle-global-status 'contents))
  5355. ;; We just showed the table of contents - now show everything
  5356. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5357. (show-all)
  5358. (message "SHOW ALL")
  5359. (setq org-cycle-global-status 'all)
  5360. (run-hook-with-args 'org-cycle-hook 'all))
  5361. (t
  5362. ;; Default action: go to overview
  5363. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5364. (org-overview)
  5365. (message "OVERVIEW")
  5366. (setq org-cycle-global-status 'overview)
  5367. (run-hook-with-args 'org-cycle-hook 'overview))))
  5368. (defun org-cycle-internal-local ()
  5369. "Do the local cycling action."
  5370. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5371. ;; First, determine end of headline (EOH), end of subtree or item
  5372. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5373. (save-excursion
  5374. (if (org-at-item-p)
  5375. (progn
  5376. (beginning-of-line)
  5377. (setq struct (org-list-struct))
  5378. (setq eoh (point-at-eol))
  5379. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5380. (setq has-children (org-list-has-child-p (point) struct)))
  5381. (org-back-to-heading)
  5382. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5383. (setq eos (save-excursion
  5384. (org-end-of-subtree t)
  5385. (unless (eobp)
  5386. (skip-chars-forward " \t\n"))
  5387. (if (eobp) (point) (1- (point)))))
  5388. (setq has-children
  5389. (or (save-excursion
  5390. (let ((level (funcall outline-level)))
  5391. (outline-next-heading)
  5392. (and (org-at-heading-p t)
  5393. (> (funcall outline-level) level))))
  5394. (save-excursion
  5395. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5396. ;; Determine end invisible part of buffer (EOL)
  5397. (beginning-of-line 2)
  5398. ;; XEmacs doesn't have `next-single-char-property-change'
  5399. (if (featurep 'xemacs)
  5400. (while (and (not (eobp)) ;; this is like `next-line'
  5401. (get-char-property (1- (point)) 'invisible))
  5402. (beginning-of-line 2))
  5403. (while (and (not (eobp)) ;; this is like `next-line'
  5404. (get-char-property (1- (point)) 'invisible))
  5405. (goto-char (next-single-char-property-change (point) 'invisible))
  5406. (and (eolp) (beginning-of-line 2))))
  5407. (setq eol (point)))
  5408. ;; Find out what to do next and set `this-command'
  5409. (cond
  5410. ((= eos eoh)
  5411. ;; Nothing is hidden behind this heading
  5412. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5413. (message "EMPTY ENTRY")
  5414. (setq org-cycle-subtree-status nil)
  5415. (save-excursion
  5416. (goto-char eos)
  5417. (outline-next-heading)
  5418. (if (outline-invisible-p) (org-flag-heading nil))))
  5419. ((and (or (>= eol eos)
  5420. (not (string-match "\\S-" (buffer-substring eol eos))))
  5421. (or has-children
  5422. (not (setq children-skipped
  5423. org-cycle-skip-children-state-if-no-children))))
  5424. ;; Entire subtree is hidden in one line: children view
  5425. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5426. (if (org-at-item-p)
  5427. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5428. (org-show-entry)
  5429. (show-children)
  5430. ;; Fold every list in subtree to top-level items.
  5431. (when (eq org-cycle-include-plain-lists 'integrate)
  5432. (save-excursion
  5433. (org-back-to-heading)
  5434. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5435. (beginning-of-line 1)
  5436. (let* ((struct (org-list-struct))
  5437. (prevs (org-list-prevs-alist struct))
  5438. (end (org-list-get-bottom-point struct)))
  5439. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5440. (org-list-get-all-items (point) struct prevs))
  5441. (goto-char end))))))
  5442. (message "CHILDREN")
  5443. (save-excursion
  5444. (goto-char eos)
  5445. (outline-next-heading)
  5446. (if (outline-invisible-p) (org-flag-heading nil)))
  5447. (setq org-cycle-subtree-status 'children)
  5448. (run-hook-with-args 'org-cycle-hook 'children))
  5449. ((or children-skipped
  5450. (and (eq last-command this-command)
  5451. (eq org-cycle-subtree-status 'children)))
  5452. ;; We just showed the children, or no children are there,
  5453. ;; now show everything.
  5454. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5455. (outline-flag-region eoh eos nil)
  5456. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5457. (setq org-cycle-subtree-status 'subtree)
  5458. (run-hook-with-args 'org-cycle-hook 'subtree))
  5459. (t
  5460. ;; Default action: hide the subtree.
  5461. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5462. (outline-flag-region eoh eos t)
  5463. (message "FOLDED")
  5464. (setq org-cycle-subtree-status 'folded)
  5465. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5466. ;;;###autoload
  5467. (defun org-global-cycle (&optional arg)
  5468. "Cycle the global visibility. For details see `org-cycle'.
  5469. With \\[universal-argument] prefix arg, switch to startup visibility.
  5470. With a numeric prefix, show all headlines up to that level."
  5471. (interactive "P")
  5472. (let ((org-cycle-include-plain-lists
  5473. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5474. (cond
  5475. ((integerp arg)
  5476. (show-all)
  5477. (hide-sublevels arg)
  5478. (setq org-cycle-global-status 'contents))
  5479. ((equal arg '(4))
  5480. (org-set-startup-visibility)
  5481. (message "Startup visibility, plus VISIBILITY properties."))
  5482. (t
  5483. (org-cycle '(4))))))
  5484. (defun org-set-startup-visibility ()
  5485. "Set the visibility required by startup options and properties."
  5486. (cond
  5487. ((eq org-startup-folded t)
  5488. (org-cycle '(4)))
  5489. ((eq org-startup-folded 'content)
  5490. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5491. (org-cycle '(4)) (org-cycle '(4)))))
  5492. (unless (eq org-startup-folded 'showeverything)
  5493. (if org-hide-block-startup (org-hide-block-all))
  5494. (org-set-visibility-according-to-property 'no-cleanup)
  5495. (org-cycle-hide-archived-subtrees 'all)
  5496. (org-cycle-hide-drawers 'all)
  5497. (org-cycle-show-empty-lines t)))
  5498. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5499. "Switch subtree visibilities according to :VISIBILITY: property."
  5500. (interactive)
  5501. (let (org-show-entry-below state)
  5502. (save-excursion
  5503. (goto-char (point-min))
  5504. (while (re-search-forward
  5505. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5506. nil t)
  5507. (setq state (match-string 1))
  5508. (save-excursion
  5509. (org-back-to-heading t)
  5510. (hide-subtree)
  5511. (org-reveal)
  5512. (cond
  5513. ((equal state '("fold" "folded"))
  5514. (hide-subtree))
  5515. ((equal state "children")
  5516. (org-show-hidden-entry)
  5517. (show-children))
  5518. ((equal state "content")
  5519. (save-excursion
  5520. (save-restriction
  5521. (org-narrow-to-subtree)
  5522. (org-content))))
  5523. ((member state '("all" "showall"))
  5524. (show-subtree)))))
  5525. (unless no-cleanup
  5526. (org-cycle-hide-archived-subtrees 'all)
  5527. (org-cycle-hide-drawers 'all)
  5528. (org-cycle-show-empty-lines 'all)))))
  5529. (defun org-overview ()
  5530. "Switch to overview mode, showing only top-level headlines.
  5531. Really, this shows all headlines with level equal or greater than the level
  5532. of the first headline in the buffer. This is important, because if the
  5533. first headline is not level one, then (hide-sublevels 1) gives confusing
  5534. results."
  5535. (interactive)
  5536. (let ((level (save-excursion
  5537. (goto-char (point-min))
  5538. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5539. (progn
  5540. (goto-char (match-beginning 0))
  5541. (funcall outline-level))))))
  5542. (and level (hide-sublevels level))))
  5543. (defun org-content (&optional arg)
  5544. "Show all headlines in the buffer, like a table of contents.
  5545. With numerical argument N, show content up to level N."
  5546. (interactive "P")
  5547. (save-excursion
  5548. ;; Visit all headings and show their offspring
  5549. (and (integerp arg) (org-overview))
  5550. (goto-char (point-max))
  5551. (catch 'exit
  5552. (while (and (progn (condition-case nil
  5553. (outline-previous-visible-heading 1)
  5554. (error (goto-char (point-min))))
  5555. t)
  5556. (looking-at outline-regexp))
  5557. (if (integerp arg)
  5558. (show-children (1- arg))
  5559. (show-branches))
  5560. (if (bobp) (throw 'exit nil))))))
  5561. (defun org-optimize-window-after-visibility-change (state)
  5562. "Adjust the window after a change in outline visibility.
  5563. This function is the default value of the hook `org-cycle-hook'."
  5564. (when (get-buffer-window (current-buffer))
  5565. (cond
  5566. ((eq state 'content) nil)
  5567. ((eq state 'all) nil)
  5568. ((eq state 'folded) nil)
  5569. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5570. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5571. (defun org-remove-empty-overlays-at (pos)
  5572. "Remove outline overlays that do not contain non-white stuff."
  5573. (mapc
  5574. (lambda (o)
  5575. (and (eq 'outline (overlay-get o 'invisible))
  5576. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5577. (overlay-end o))))
  5578. (delete-overlay o)))
  5579. (overlays-at pos)))
  5580. (defun org-clean-visibility-after-subtree-move ()
  5581. "Fix visibility issues after moving a subtree."
  5582. ;; First, find a reasonable region to look at:
  5583. ;; Start two siblings above, end three below
  5584. (let* ((beg (save-excursion
  5585. (and (org-get-last-sibling)
  5586. (org-get-last-sibling))
  5587. (point)))
  5588. (end (save-excursion
  5589. (and (org-get-next-sibling)
  5590. (org-get-next-sibling)
  5591. (org-get-next-sibling))
  5592. (if (org-at-heading-p)
  5593. (point-at-eol)
  5594. (point))))
  5595. (level (looking-at "\\*+"))
  5596. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5597. (save-excursion
  5598. (save-restriction
  5599. (narrow-to-region beg end)
  5600. (when re
  5601. ;; Properly fold already folded siblings
  5602. (goto-char (point-min))
  5603. (while (re-search-forward re nil t)
  5604. (if (and (not (outline-invisible-p))
  5605. (save-excursion
  5606. (goto-char (point-at-eol)) (outline-invisible-p)))
  5607. (hide-entry))))
  5608. (org-cycle-show-empty-lines 'overview)
  5609. (org-cycle-hide-drawers 'overview)))))
  5610. (defun org-cycle-show-empty-lines (state)
  5611. "Show empty lines above all visible headlines.
  5612. The region to be covered depends on STATE when called through
  5613. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5614. entire buffer covered. Note that an empty line is only shown if there
  5615. are at least `org-cycle-separator-lines' empty lines before the headline."
  5616. (when (not (= org-cycle-separator-lines 0))
  5617. (save-excursion
  5618. (let* ((n (abs org-cycle-separator-lines))
  5619. (re (cond
  5620. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5621. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5622. (t (let ((ns (number-to-string (- n 2))))
  5623. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5624. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5625. beg end b e)
  5626. (cond
  5627. ((memq state '(overview contents t))
  5628. (setq beg (point-min) end (point-max)))
  5629. ((memq state '(children folded))
  5630. (setq beg (point) end (progn (org-end-of-subtree t t)
  5631. (beginning-of-line 2)
  5632. (point)))))
  5633. (when beg
  5634. (goto-char beg)
  5635. (while (re-search-forward re end t)
  5636. (unless (get-char-property (match-end 1) 'invisible)
  5637. (setq e (match-end 1))
  5638. (if (< org-cycle-separator-lines 0)
  5639. (setq b (save-excursion
  5640. (goto-char (match-beginning 0))
  5641. (org-back-over-empty-lines)
  5642. (if (save-excursion
  5643. (goto-char (max (point-min) (1- (point))))
  5644. (org-on-heading-p))
  5645. (1- (point))
  5646. (point))))
  5647. (setq b (match-beginning 1)))
  5648. (outline-flag-region b e nil)))))))
  5649. ;; Never hide empty lines at the end of the file.
  5650. (save-excursion
  5651. (goto-char (point-max))
  5652. (outline-previous-heading)
  5653. (outline-end-of-heading)
  5654. (if (and (looking-at "[ \t\n]+")
  5655. (= (match-end 0) (point-max)))
  5656. (outline-flag-region (point) (match-end 0) nil))))
  5657. (defun org-show-empty-lines-in-parent ()
  5658. "Move to the parent and re-show empty lines before visible headlines."
  5659. (save-excursion
  5660. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5661. (org-cycle-show-empty-lines context))))
  5662. (defun org-files-list ()
  5663. "Return `org-agenda-files' list, plus all open org-mode files.
  5664. This is useful for operations that need to scan all of a user's
  5665. open and agenda-wise Org files."
  5666. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5667. (dolist (buf (buffer-list))
  5668. (with-current-buffer buf
  5669. (if (and (org-mode-p) (buffer-file-name))
  5670. (let ((file (expand-file-name (buffer-file-name))))
  5671. (unless (member file files)
  5672. (push file files))))))
  5673. files))
  5674. (defsubst org-entry-beginning-position ()
  5675. "Return the beginning position of the current entry."
  5676. (save-excursion (outline-back-to-heading t) (point)))
  5677. (defsubst org-entry-end-position ()
  5678. "Return the end position of the current entry."
  5679. (save-excursion (outline-next-heading) (point)))
  5680. (defun org-cycle-hide-drawers (state)
  5681. "Re-hide all drawers after a visibility state change."
  5682. (when (and (org-mode-p)
  5683. (not (memq state '(overview folded contents))))
  5684. (save-excursion
  5685. (let* ((globalp (memq state '(contents all)))
  5686. (beg (if globalp (point-min) (point)))
  5687. (end (if globalp (point-max)
  5688. (if (eq state 'children)
  5689. (save-excursion (outline-next-heading) (point))
  5690. (org-end-of-subtree t)))))
  5691. (goto-char beg)
  5692. (while (re-search-forward org-drawer-regexp end t)
  5693. (org-flag-drawer t))))))
  5694. (defun org-flag-drawer (flag)
  5695. (save-excursion
  5696. (beginning-of-line 1)
  5697. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5698. (let ((b (match-end 0))
  5699. (outline-regexp org-outline-regexp))
  5700. (if (re-search-forward
  5701. "^[ \t]*:END:"
  5702. (save-excursion (outline-next-heading) (point)) t)
  5703. (outline-flag-region b (point-at-eol) flag)
  5704. (error ":END: line missing at position %s" b))))))
  5705. (defun org-subtree-end-visible-p ()
  5706. "Is the end of the current subtree visible?"
  5707. (pos-visible-in-window-p
  5708. (save-excursion (org-end-of-subtree t) (point))))
  5709. (defun org-first-headline-recenter (&optional N)
  5710. "Move cursor to the first headline and recenter the headline.
  5711. Optional argument N means put the headline into the Nth line of the window."
  5712. (goto-char (point-min))
  5713. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5714. (beginning-of-line)
  5715. (recenter (prefix-numeric-value N))))
  5716. ;;; Saving and restoring visibility
  5717. (defun org-outline-overlay-data (&optional use-markers)
  5718. "Return a list of the locations of all outline overlays.
  5719. These are overlays with the `invisible' property value `outline'.
  5720. The return value is a list of cons cells, with start and stop
  5721. positions for each overlay.
  5722. If USE-MARKERS is set, return the positions as markers."
  5723. (let (beg end)
  5724. (save-excursion
  5725. (save-restriction
  5726. (widen)
  5727. (delq nil
  5728. (mapcar (lambda (o)
  5729. (when (eq (overlay-get o 'invisible) 'outline)
  5730. (setq beg (overlay-start o)
  5731. end (overlay-end o))
  5732. (and beg end (> end beg)
  5733. (if use-markers
  5734. (cons (move-marker (make-marker) beg)
  5735. (move-marker (make-marker) end))
  5736. (cons beg end)))))
  5737. (overlays-in (point-min) (point-max))))))))
  5738. (defun org-set-outline-overlay-data (data)
  5739. "Create visibility overlays for all positions in DATA.
  5740. DATA should have been made by `org-outline-overlay-data'."
  5741. (let (o)
  5742. (save-excursion
  5743. (save-restriction
  5744. (widen)
  5745. (show-all)
  5746. (mapc (lambda (c)
  5747. (setq o (make-overlay (car c) (cdr c)))
  5748. (overlay-put o 'invisible 'outline))
  5749. data)))))
  5750. ;;; Folding of blocks
  5751. (defconst org-block-regexp
  5752. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  5753. "Regular expression for hiding blocks.")
  5754. (defvar org-hide-block-overlays nil
  5755. "Overlays hiding blocks.")
  5756. (make-variable-buffer-local 'org-hide-block-overlays)
  5757. (defun org-block-map (function &optional start end)
  5758. "Call FUNCTION at the head of all source blocks in the current buffer.
  5759. Optional arguments START and END can be used to limit the range."
  5760. (let ((start (or start (point-min)))
  5761. (end (or end (point-max))))
  5762. (save-excursion
  5763. (goto-char start)
  5764. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5765. (save-excursion
  5766. (save-match-data
  5767. (goto-char (match-beginning 0))
  5768. (funcall function)))))))
  5769. (defun org-hide-block-toggle-all ()
  5770. "Toggle the visibility of all blocks in the current buffer."
  5771. (org-block-map #'org-hide-block-toggle))
  5772. (defun org-hide-block-all ()
  5773. "Fold all blocks in the current buffer."
  5774. (interactive)
  5775. (org-show-block-all)
  5776. (org-block-map #'org-hide-block-toggle-maybe))
  5777. (defun org-show-block-all ()
  5778. "Unfold all blocks in the current buffer."
  5779. (interactive)
  5780. (mapc 'delete-overlay org-hide-block-overlays)
  5781. (setq org-hide-block-overlays nil))
  5782. (defun org-hide-block-toggle-maybe ()
  5783. "Toggle visibility of block at point."
  5784. (interactive)
  5785. (let ((case-fold-search t))
  5786. (if (save-excursion
  5787. (beginning-of-line 1)
  5788. (looking-at org-block-regexp))
  5789. (progn (org-hide-block-toggle)
  5790. t) ;; to signal that we took action
  5791. nil))) ;; to signal that we did not
  5792. (defun org-hide-block-toggle (&optional force)
  5793. "Toggle the visibility of the current block."
  5794. (interactive)
  5795. (save-excursion
  5796. (beginning-of-line)
  5797. (if (re-search-forward org-block-regexp nil t)
  5798. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5799. (end (match-end 0)) ;; end of entire body
  5800. ov)
  5801. (if (memq t (mapcar (lambda (overlay)
  5802. (eq (overlay-get overlay 'invisible)
  5803. 'org-hide-block))
  5804. (overlays-at start)))
  5805. (if (or (not force) (eq force 'off))
  5806. (mapc (lambda (ov)
  5807. (when (member ov org-hide-block-overlays)
  5808. (setq org-hide-block-overlays
  5809. (delq ov org-hide-block-overlays)))
  5810. (when (eq (overlay-get ov 'invisible)
  5811. 'org-hide-block)
  5812. (delete-overlay ov)))
  5813. (overlays-at start)))
  5814. (setq ov (make-overlay start end))
  5815. (overlay-put ov 'invisible 'org-hide-block)
  5816. ;; make the block accessible to isearch
  5817. (overlay-put
  5818. ov 'isearch-open-invisible
  5819. (lambda (ov)
  5820. (when (member ov org-hide-block-overlays)
  5821. (setq org-hide-block-overlays
  5822. (delq ov org-hide-block-overlays)))
  5823. (when (eq (overlay-get ov 'invisible)
  5824. 'org-hide-block)
  5825. (delete-overlay ov))))
  5826. (push ov org-hide-block-overlays)))
  5827. (error "Not looking at a source block"))))
  5828. ;; org-tab-after-check-for-cycling-hook
  5829. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5830. ;; Remove overlays when changing major mode
  5831. (add-hook 'org-mode-hook
  5832. (lambda () (org-add-hook 'change-major-mode-hook
  5833. 'org-show-block-all 'append 'local)))
  5834. ;;; Org-goto
  5835. (defvar org-goto-window-configuration nil)
  5836. (defvar org-goto-marker nil)
  5837. (defvar org-goto-map
  5838. (let ((map (make-sparse-keymap)))
  5839. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5840. (while (setq cmd (pop cmds))
  5841. (substitute-key-definition cmd cmd map global-map)))
  5842. (suppress-keymap map)
  5843. (org-defkey map "\C-m" 'org-goto-ret)
  5844. (org-defkey map [(return)] 'org-goto-ret)
  5845. (org-defkey map [(left)] 'org-goto-left)
  5846. (org-defkey map [(right)] 'org-goto-right)
  5847. (org-defkey map [(control ?g)] 'org-goto-quit)
  5848. (org-defkey map "\C-i" 'org-cycle)
  5849. (org-defkey map [(tab)] 'org-cycle)
  5850. (org-defkey map [(down)] 'outline-next-visible-heading)
  5851. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5852. (if org-goto-auto-isearch
  5853. (if (fboundp 'define-key-after)
  5854. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5855. nil)
  5856. (org-defkey map "q" 'org-goto-quit)
  5857. (org-defkey map "n" 'outline-next-visible-heading)
  5858. (org-defkey map "p" 'outline-previous-visible-heading)
  5859. (org-defkey map "f" 'outline-forward-same-level)
  5860. (org-defkey map "b" 'outline-backward-same-level)
  5861. (org-defkey map "u" 'outline-up-heading))
  5862. (org-defkey map "/" 'org-occur)
  5863. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5864. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5865. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5866. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5867. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5868. map))
  5869. (defconst org-goto-help
  5870. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5871. RET=jump to location [Q]uit and return to previous location
  5872. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5873. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5874. ;; FIXME: Docstring does not mention both interfaces
  5875. (defun org-goto (&optional alternative-interface)
  5876. "Look up a different location in the current file, keeping current visibility.
  5877. When you want look-up or go to a different location in a document, the
  5878. fastest way is often to fold the entire buffer and then dive into the tree.
  5879. This method has the disadvantage, that the previous location will be folded,
  5880. which may not be what you want.
  5881. This command works around this by showing a copy of the current buffer
  5882. in an indirect buffer, in overview mode. You can dive into the tree in
  5883. that copy, use org-occur and incremental search to find a location.
  5884. When pressing RET or `Q', the command returns to the original buffer in
  5885. which the visibility is still unchanged. After RET is will also jump to
  5886. the location selected in the indirect buffer and expose the headline
  5887. hierarchy above."
  5888. (interactive "P")
  5889. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5890. (org-refile-use-outline-path t)
  5891. (org-refile-target-verify-function nil)
  5892. (interface
  5893. (if (not alternative-interface)
  5894. org-goto-interface
  5895. (if (eq org-goto-interface 'outline)
  5896. 'outline-path-completion
  5897. 'outline)))
  5898. (org-goto-start-pos (point))
  5899. (selected-point
  5900. (if (eq interface 'outline)
  5901. (car (org-get-location (current-buffer) org-goto-help))
  5902. (let ((pa (org-refile-get-location "Goto")))
  5903. (org-refile-check-position pa)
  5904. (nth 3 pa)))))
  5905. (if selected-point
  5906. (progn
  5907. (org-mark-ring-push org-goto-start-pos)
  5908. (goto-char selected-point)
  5909. (if (or (outline-invisible-p) (org-invisible-p2))
  5910. (org-show-context 'org-goto)))
  5911. (message "Quit"))))
  5912. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5913. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5914. (defvar org-goto-local-auto-isearch-map) ; defined below
  5915. (defun org-get-location (buf help)
  5916. "Let the user select a location in the Org-mode buffer BUF.
  5917. This function uses a recursive edit. It returns the selected position
  5918. or nil."
  5919. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5920. (isearch-hide-immediately nil)
  5921. (isearch-search-fun-function
  5922. (lambda () 'org-goto-local-search-headings))
  5923. (org-goto-selected-point org-goto-exit-command)
  5924. (pop-up-frames nil)
  5925. (special-display-buffer-names nil)
  5926. (special-display-regexps nil)
  5927. (special-display-function nil))
  5928. (save-excursion
  5929. (save-window-excursion
  5930. (delete-other-windows)
  5931. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5932. (switch-to-buffer
  5933. (condition-case nil
  5934. (make-indirect-buffer (current-buffer) "*org-goto*")
  5935. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5936. (with-output-to-temp-buffer "*Help*"
  5937. (princ help))
  5938. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5939. (setq buffer-read-only nil)
  5940. (let ((org-startup-truncated t)
  5941. (org-startup-folded nil)
  5942. (org-startup-align-all-tables nil))
  5943. (org-mode)
  5944. (org-overview))
  5945. (setq buffer-read-only t)
  5946. (if (and (boundp 'org-goto-start-pos)
  5947. (integer-or-marker-p org-goto-start-pos))
  5948. (let ((org-show-hierarchy-above t)
  5949. (org-show-siblings t)
  5950. (org-show-following-heading t))
  5951. (goto-char org-goto-start-pos)
  5952. (and (outline-invisible-p) (org-show-context)))
  5953. (goto-char (point-min)))
  5954. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5955. (message "Select location and press RET")
  5956. (use-local-map org-goto-map)
  5957. (recursive-edit)
  5958. ))
  5959. (kill-buffer "*org-goto*")
  5960. (cons org-goto-selected-point org-goto-exit-command)))
  5961. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5962. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5963. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5964. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5965. (defun org-goto-local-search-headings (string bound noerror)
  5966. "Search and make sure that any matches are in headlines."
  5967. (catch 'return
  5968. (while (if isearch-forward
  5969. (search-forward string bound noerror)
  5970. (search-backward string bound noerror))
  5971. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5972. (and (member :headline context)
  5973. (not (member :tags context))))
  5974. (throw 'return (point))))))
  5975. (defun org-goto-local-auto-isearch ()
  5976. "Start isearch."
  5977. (interactive)
  5978. (goto-char (point-min))
  5979. (let ((keys (this-command-keys)))
  5980. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5981. (isearch-mode t)
  5982. (isearch-process-search-char (string-to-char keys)))))
  5983. (defun org-goto-ret (&optional arg)
  5984. "Finish `org-goto' by going to the new location."
  5985. (interactive "P")
  5986. (setq org-goto-selected-point (point)
  5987. org-goto-exit-command 'return)
  5988. (throw 'exit nil))
  5989. (defun org-goto-left ()
  5990. "Finish `org-goto' by going to the new location."
  5991. (interactive)
  5992. (if (org-on-heading-p)
  5993. (progn
  5994. (beginning-of-line 1)
  5995. (setq org-goto-selected-point (point)
  5996. org-goto-exit-command 'left)
  5997. (throw 'exit nil))
  5998. (error "Not on a heading")))
  5999. (defun org-goto-right ()
  6000. "Finish `org-goto' by going to the new location."
  6001. (interactive)
  6002. (if (org-on-heading-p)
  6003. (progn
  6004. (setq org-goto-selected-point (point)
  6005. org-goto-exit-command 'right)
  6006. (throw 'exit nil))
  6007. (error "Not on a heading")))
  6008. (defun org-goto-quit ()
  6009. "Finish `org-goto' without cursor motion."
  6010. (interactive)
  6011. (setq org-goto-selected-point nil)
  6012. (setq org-goto-exit-command 'quit)
  6013. (throw 'exit nil))
  6014. ;;; Indirect buffer display of subtrees
  6015. (defvar org-indirect-dedicated-frame nil
  6016. "This is the frame being used for indirect tree display.")
  6017. (defvar org-last-indirect-buffer nil)
  6018. (defun org-tree-to-indirect-buffer (&optional arg)
  6019. "Create indirect buffer and narrow it to current subtree.
  6020. With numerical prefix ARG, go up to this level and then take that tree.
  6021. If ARG is negative, go up that many levels.
  6022. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6023. indirect buffer previously made with this command, to avoid proliferation of
  6024. indirect buffers. However, when you call the command with a \
  6025. \\[universal-argument] prefix, or
  6026. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6027. is kept so that you can work with several indirect buffers at the same time.
  6028. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6029. \\[universal-argument] prefix also
  6030. requests that a new frame be made for the new buffer, so that the dedicated
  6031. frame is not changed."
  6032. (interactive "P")
  6033. (let ((cbuf (current-buffer))
  6034. (cwin (selected-window))
  6035. (pos (point))
  6036. beg end level heading ibuf)
  6037. (save-excursion
  6038. (org-back-to-heading t)
  6039. (when (numberp arg)
  6040. (setq level (org-outline-level))
  6041. (if (< arg 0) (setq arg (+ level arg)))
  6042. (while (> (setq level (org-outline-level)) arg)
  6043. (outline-up-heading 1 t)))
  6044. (setq beg (point)
  6045. heading (org-get-heading))
  6046. (org-end-of-subtree t t)
  6047. (if (org-on-heading-p) (backward-char 1))
  6048. (setq end (point)))
  6049. (if (and (buffer-live-p org-last-indirect-buffer)
  6050. (not (eq org-indirect-buffer-display 'new-frame))
  6051. (not arg))
  6052. (kill-buffer org-last-indirect-buffer))
  6053. (setq ibuf (org-get-indirect-buffer cbuf)
  6054. org-last-indirect-buffer ibuf)
  6055. (cond
  6056. ((or (eq org-indirect-buffer-display 'new-frame)
  6057. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6058. (select-frame (make-frame))
  6059. (delete-other-windows)
  6060. (switch-to-buffer ibuf)
  6061. (org-set-frame-title heading))
  6062. ((eq org-indirect-buffer-display 'dedicated-frame)
  6063. (raise-frame
  6064. (select-frame (or (and org-indirect-dedicated-frame
  6065. (frame-live-p org-indirect-dedicated-frame)
  6066. org-indirect-dedicated-frame)
  6067. (setq org-indirect-dedicated-frame (make-frame)))))
  6068. (delete-other-windows)
  6069. (switch-to-buffer ibuf)
  6070. (org-set-frame-title (concat "Indirect: " heading)))
  6071. ((eq org-indirect-buffer-display 'current-window)
  6072. (switch-to-buffer ibuf))
  6073. ((eq org-indirect-buffer-display 'other-window)
  6074. (pop-to-buffer ibuf))
  6075. (t (error "Invalid value")))
  6076. (if (featurep 'xemacs)
  6077. (save-excursion (org-mode) (turn-on-font-lock)))
  6078. (narrow-to-region beg end)
  6079. (show-all)
  6080. (goto-char pos)
  6081. (and (window-live-p cwin) (select-window cwin))))
  6082. (defun org-get-indirect-buffer (&optional buffer)
  6083. (setq buffer (or buffer (current-buffer)))
  6084. (let ((n 1) (base (buffer-name buffer)) bname)
  6085. (while (buffer-live-p
  6086. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6087. (setq n (1+ n)))
  6088. (condition-case nil
  6089. (make-indirect-buffer buffer bname 'clone)
  6090. (error (make-indirect-buffer buffer bname)))))
  6091. (defun org-set-frame-title (title)
  6092. "Set the title of the current frame to the string TITLE."
  6093. ;; FIXME: how to name a single frame in XEmacs???
  6094. (unless (featurep 'xemacs)
  6095. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6096. ;;;; Structure editing
  6097. ;;; Inserting headlines
  6098. (defun org-previous-line-empty-p ()
  6099. (save-excursion
  6100. (and (not (bobp))
  6101. (or (beginning-of-line 0) t)
  6102. (save-match-data
  6103. (looking-at "[ \t]*$")))))
  6104. (defun org-insert-heading (&optional force-heading invisible-ok)
  6105. "Insert a new heading or item with same depth at point.
  6106. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6107. If point is at the beginning of a headline, insert a sibling before the
  6108. current headline. If point is not at the beginning, split the line,
  6109. create the new headline with the text in the current line after point
  6110. \(but see also the variable `org-M-RET-may-split-line').
  6111. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6112. This is important for non-interactive uses of the command."
  6113. (interactive "P")
  6114. (if (or (= (buffer-size) 0)
  6115. (and (not (save-excursion
  6116. (and (ignore-errors (org-back-to-heading invisible-ok))
  6117. (org-on-heading-p))))
  6118. (not (org-in-item-p))))
  6119. (progn
  6120. (insert "\n* ")
  6121. (run-hooks 'org-insert-heading-hook))
  6122. (when (or force-heading (not (org-insert-item)))
  6123. (let* ((empty-line-p nil)
  6124. (level nil)
  6125. (on-heading (org-on-heading-p))
  6126. (head (save-excursion
  6127. (condition-case nil
  6128. (progn
  6129. (org-back-to-heading invisible-ok)
  6130. (when (and (not on-heading)
  6131. (featurep 'org-inlinetask)
  6132. (integerp org-inlinetask-min-level)
  6133. (>= (length (match-string 0))
  6134. org-inlinetask-min-level))
  6135. ;; Find a heading level before the inline task
  6136. (while (and (setq level (org-up-heading-safe))
  6137. (>= level org-inlinetask-min-level)))
  6138. (if (org-on-heading-p)
  6139. (org-back-to-heading invisible-ok)
  6140. (error "This should not happen")))
  6141. (setq empty-line-p (org-previous-line-empty-p))
  6142. (match-string 0))
  6143. (error "*"))))
  6144. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6145. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6146. pos hide-previous previous-pos)
  6147. (cond
  6148. ((and (org-on-heading-p) (bolp)
  6149. (or (bobp)
  6150. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6151. ;; insert before the current line
  6152. (open-line (if blank 2 1)))
  6153. ((and (bolp)
  6154. (not org-insert-heading-respect-content)
  6155. (or (bobp)
  6156. (save-excursion
  6157. (backward-char 1) (not (outline-invisible-p)))))
  6158. ;; insert right here
  6159. nil)
  6160. (t
  6161. ;; somewhere in the line
  6162. (save-excursion
  6163. (setq previous-pos (point-at-bol))
  6164. (end-of-line)
  6165. (setq hide-previous (outline-invisible-p)))
  6166. (and org-insert-heading-respect-content (org-show-subtree))
  6167. (let ((split
  6168. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6169. (save-excursion
  6170. (let ((p (point)))
  6171. (goto-char (point-at-bol))
  6172. (and (looking-at org-complex-heading-regexp)
  6173. (> p (match-beginning 4)))))))
  6174. tags pos)
  6175. (cond
  6176. (org-insert-heading-respect-content
  6177. (org-end-of-subtree nil t)
  6178. (when (featurep 'org-inlinetask)
  6179. (while (and (not (eobp))
  6180. (looking-at "\\(\\*+\\)[ \t]+")
  6181. (>= (length (match-string 1))
  6182. org-inlinetask-min-level))
  6183. (org-end-of-subtree nil t)))
  6184. (or (bolp) (newline))
  6185. (or (org-previous-line-empty-p)
  6186. (and blank (newline)))
  6187. (open-line 1))
  6188. ((org-on-heading-p)
  6189. (when hide-previous
  6190. (show-children)
  6191. (org-show-entry))
  6192. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6193. (setq tags (and (match-end 2) (match-string 2)))
  6194. (and (match-end 1)
  6195. (delete-region (match-beginning 1) (match-end 1)))
  6196. (setq pos (point-at-bol))
  6197. (or split (end-of-line 1))
  6198. (delete-horizontal-space)
  6199. (if (string-match "\\`\\*+\\'"
  6200. (buffer-substring (point-at-bol) (point)))
  6201. (insert " "))
  6202. (newline (if blank 2 1))
  6203. (when tags
  6204. (save-excursion
  6205. (goto-char pos)
  6206. (end-of-line 1)
  6207. (insert " " tags)
  6208. (org-set-tags nil 'align))))
  6209. (t
  6210. (or split (end-of-line 1))
  6211. (newline (if blank 2 1)))))))
  6212. (insert head) (just-one-space)
  6213. (setq pos (point))
  6214. (end-of-line 1)
  6215. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6216. (when (and org-insert-heading-respect-content hide-previous)
  6217. (save-excursion
  6218. (goto-char previous-pos)
  6219. (hide-subtree)))
  6220. (run-hooks 'org-insert-heading-hook)))))
  6221. (defun org-get-heading (&optional no-tags)
  6222. "Return the heading of the current entry, without the stars."
  6223. (save-excursion
  6224. (org-back-to-heading t)
  6225. (if (looking-at
  6226. (if no-tags
  6227. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6228. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6229. (match-string 1) "")))
  6230. (defun org-heading-components ()
  6231. "Return the components of the current heading.
  6232. This is a list with the following elements:
  6233. - the level as an integer
  6234. - the reduced level, different if `org-odd-levels-only' is set.
  6235. - the TODO keyword, or nil
  6236. - the priority character, like ?A, or nil if no priority is given
  6237. - the headline text itself, or the tags string if no headline text
  6238. - the tags string, or nil."
  6239. (save-excursion
  6240. (org-back-to-heading t)
  6241. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6242. (list (length (match-string 1))
  6243. (org-reduced-level (length (match-string 1)))
  6244. (org-match-string-no-properties 2)
  6245. (and (match-end 3) (aref (match-string 3) 2))
  6246. (org-match-string-no-properties 4)
  6247. (org-match-string-no-properties 5)))))
  6248. (defun org-get-entry ()
  6249. "Get the entry text, after heading, entire subtree."
  6250. (save-excursion
  6251. (org-back-to-heading t)
  6252. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6253. (defun org-insert-heading-after-current ()
  6254. "Insert a new heading with same level as current, after current subtree."
  6255. (interactive)
  6256. (org-back-to-heading)
  6257. (org-insert-heading)
  6258. (org-move-subtree-down)
  6259. (end-of-line 1))
  6260. (defun org-insert-heading-respect-content ()
  6261. (interactive)
  6262. (let ((org-insert-heading-respect-content t))
  6263. (org-insert-heading t)))
  6264. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6265. (interactive "P")
  6266. (let ((org-insert-heading-respect-content t))
  6267. (org-insert-todo-heading force-state t)))
  6268. (defun org-insert-todo-heading (arg &optional force-heading)
  6269. "Insert a new heading with the same level and TODO state as current heading.
  6270. If the heading has no TODO state, or if the state is DONE, use the first
  6271. state (TODO by default). Also with prefix arg, force first state."
  6272. (interactive "P")
  6273. (when (or force-heading (not (org-insert-item 'checkbox)))
  6274. (org-insert-heading force-heading)
  6275. (save-excursion
  6276. (org-back-to-heading)
  6277. (outline-previous-heading)
  6278. (looking-at org-todo-line-regexp))
  6279. (let*
  6280. ((new-mark-x
  6281. (if (or arg
  6282. (not (match-beginning 2))
  6283. (member (match-string 2) org-done-keywords))
  6284. (car org-todo-keywords-1)
  6285. (match-string 2)))
  6286. (new-mark
  6287. (or
  6288. (run-hook-with-args-until-success
  6289. 'org-todo-get-default-hook new-mark-x nil)
  6290. new-mark-x)))
  6291. (beginning-of-line 1)
  6292. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6293. (if org-treat-insert-todo-heading-as-state-change
  6294. (org-todo new-mark)
  6295. (insert new-mark " "))))
  6296. (when org-provide-todo-statistics
  6297. (org-update-parent-todo-statistics))))
  6298. (defun org-insert-subheading (arg)
  6299. "Insert a new subheading and demote it.
  6300. Works for outline headings and for plain lists alike."
  6301. (interactive "P")
  6302. (org-insert-heading arg)
  6303. (cond
  6304. ((org-on-heading-p) (org-do-demote))
  6305. ((org-at-item-p) (org-indent-item))))
  6306. (defun org-insert-todo-subheading (arg)
  6307. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6308. Works for outline headings and for plain lists alike."
  6309. (interactive "P")
  6310. (org-insert-todo-heading arg)
  6311. (cond
  6312. ((org-on-heading-p) (org-do-demote))
  6313. ((org-at-item-p) (org-indent-item))))
  6314. ;;; Promotion and Demotion
  6315. (defvar org-after-demote-entry-hook nil
  6316. "Hook run after an entry has been demoted.
  6317. The cursor will be at the beginning of the entry.
  6318. When a subtree is being demoted, the hook will be called for each node.")
  6319. (defvar org-after-promote-entry-hook nil
  6320. "Hook run after an entry has been promoted.
  6321. The cursor will be at the beginning of the entry.
  6322. When a subtree is being promoted, the hook will be called for each node.")
  6323. (defun org-promote-subtree ()
  6324. "Promote the entire subtree.
  6325. See also `org-promote'."
  6326. (interactive)
  6327. (save-excursion
  6328. (org-with-limited-levels (org-map-tree 'org-promote)))
  6329. (org-fix-position-after-promote))
  6330. (defun org-demote-subtree ()
  6331. "Demote the entire subtree. See `org-demote'.
  6332. See also `org-promote'."
  6333. (interactive)
  6334. (save-excursion
  6335. (org-with-limited-levels (org-map-tree 'org-demote)))
  6336. (org-fix-position-after-promote))
  6337. (defun org-do-promote ()
  6338. "Promote the current heading higher up the tree.
  6339. If the region is active in `transient-mark-mode', promote all headings
  6340. in the region."
  6341. (interactive)
  6342. (save-excursion
  6343. (if (org-region-active-p)
  6344. (org-map-region 'org-promote (region-beginning) (region-end))
  6345. (org-promote)))
  6346. (org-fix-position-after-promote))
  6347. (defun org-do-demote ()
  6348. "Demote the current heading lower down the tree.
  6349. If the region is active in `transient-mark-mode', demote all headings
  6350. in the region."
  6351. (interactive)
  6352. (save-excursion
  6353. (if (org-region-active-p)
  6354. (org-map-region 'org-demote (region-beginning) (region-end))
  6355. (org-demote)))
  6356. (org-fix-position-after-promote))
  6357. (defun org-fix-position-after-promote ()
  6358. "Make sure that after pro/demotion cursor position is right."
  6359. (let ((pos (point)))
  6360. (when (save-excursion
  6361. (beginning-of-line 1)
  6362. (looking-at org-todo-line-regexp)
  6363. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6364. (cond ((eobp) (insert " "))
  6365. ((eolp) (insert " "))
  6366. ((equal (char-after) ?\ ) (forward-char 1))))))
  6367. (defun org-current-level ()
  6368. "Return the level of the current entry, or nil if before the first headline.
  6369. The level is the number of stars at the beginning of the headline."
  6370. (save-excursion
  6371. (org-with-limited-levels
  6372. (ignore-errors
  6373. (org-back-to-heading t)
  6374. (funcall outline-level)))))
  6375. (defun org-get-previous-line-level ()
  6376. "Return the outline depth of the last headline before the current line.
  6377. Returns 0 for the first headline in the buffer, and nil if before the
  6378. first headline."
  6379. (let ((current-level (org-current-level))
  6380. (prev-level (when (> (line-number-at-pos) 1)
  6381. (save-excursion
  6382. (beginning-of-line 0)
  6383. (org-current-level)))))
  6384. (cond ((null current-level) nil) ; Before first headline
  6385. ((null prev-level) 0) ; At first headline
  6386. (prev-level))))
  6387. (defun org-reduced-level (l)
  6388. "Compute the effective level of a heading.
  6389. This takes into account the setting of `org-odd-levels-only'."
  6390. (cond
  6391. ((zerop l) 0)
  6392. (org-odd-levels-only (1+ (floor (/ l 2))))
  6393. (t l)))
  6394. (defun org-level-increment ()
  6395. "Return the number of stars that will be added or removed at a
  6396. time to headlines when structure editing, based on the value of
  6397. `org-odd-levels-only'."
  6398. (if org-odd-levels-only 2 1))
  6399. (defun org-get-valid-level (level &optional change)
  6400. "Rectify a level change under the influence of `org-odd-levels-only'
  6401. LEVEL is a current level, CHANGE is by how much the level should be
  6402. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6403. even level numbers will become the next higher odd number."
  6404. (if org-odd-levels-only
  6405. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6406. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6407. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6408. (max 1 (+ level (or change 0)))))
  6409. (if (boundp 'define-obsolete-function-alias)
  6410. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6411. (define-obsolete-function-alias 'org-get-legal-level
  6412. 'org-get-valid-level)
  6413. (define-obsolete-function-alias 'org-get-legal-level
  6414. 'org-get-valid-level "23.1")))
  6415. (defun org-promote ()
  6416. "Promote the current heading higher up the tree.
  6417. If the region is active in `transient-mark-mode', promote all headings
  6418. in the region."
  6419. (org-back-to-heading t)
  6420. (let* ((level (save-match-data (funcall outline-level)))
  6421. (after-change-functions (remove 'flyspell-after-change-function
  6422. after-change-functions))
  6423. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6424. (diff (abs (- level (length up-head) -1))))
  6425. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6426. (replace-match up-head nil t)
  6427. ;; Fixup tag positioning
  6428. (and org-auto-align-tags (org-set-tags nil t))
  6429. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6430. (run-hooks 'org-after-promote-entry-hook)))
  6431. (defun org-demote ()
  6432. "Demote the current heading lower down the tree.
  6433. If the region is active in `transient-mark-mode', demote all headings
  6434. in the region."
  6435. (org-back-to-heading t)
  6436. (let* ((level (save-match-data (funcall outline-level)))
  6437. (after-change-functions (remove 'flyspell-after-change-function
  6438. after-change-functions))
  6439. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6440. (diff (abs (- level (length down-head) -1))))
  6441. (replace-match down-head nil t)
  6442. ;; Fixup tag positioning
  6443. (and org-auto-align-tags (org-set-tags nil t))
  6444. (if org-adapt-indentation (org-fixup-indentation diff))
  6445. (run-hooks 'org-after-demote-entry-hook)))
  6446. (defun org-cycle-level ()
  6447. "Cycle the level of an empty headline through possible states.
  6448. This goes first to child, then to parent, level, then up the hierarchy.
  6449. After top level, it switches back to sibling level."
  6450. (interactive)
  6451. (let ((org-adapt-indentation nil))
  6452. (when (org-point-at-end-of-empty-headline)
  6453. (setq this-command 'org-cycle-level) ; Only needed for caching
  6454. (let ((cur-level (org-current-level))
  6455. (prev-level (org-get-previous-line-level)))
  6456. (cond
  6457. ;; If first headline in file, promote to top-level.
  6458. ((= prev-level 0)
  6459. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6460. do (org-do-promote)))
  6461. ;; If same level as prev, demote one.
  6462. ((= prev-level cur-level)
  6463. (org-do-demote))
  6464. ;; If parent is top-level, promote to top level if not already.
  6465. ((= prev-level 1)
  6466. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6467. do (org-do-promote)))
  6468. ;; If top-level, return to prev-level.
  6469. ((= cur-level 1)
  6470. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6471. do (org-do-demote)))
  6472. ;; If less than prev-level, promote one.
  6473. ((< cur-level prev-level)
  6474. (org-do-promote))
  6475. ;; If deeper than prev-level, promote until higher than
  6476. ;; prev-level.
  6477. ((> cur-level prev-level)
  6478. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6479. do (org-do-promote))))
  6480. t))))
  6481. (defun org-map-tree (fun)
  6482. "Call FUN for every heading underneath the current one."
  6483. (org-back-to-heading)
  6484. (let ((level (funcall outline-level)))
  6485. (save-excursion
  6486. (funcall fun)
  6487. (while (and (progn
  6488. (outline-next-heading)
  6489. (> (funcall outline-level) level))
  6490. (not (eobp)))
  6491. (funcall fun)))))
  6492. (defun org-map-region (fun beg end)
  6493. "Call FUN for every heading between BEG and END."
  6494. (let ((org-ignore-region t))
  6495. (save-excursion
  6496. (setq end (copy-marker end))
  6497. (goto-char beg)
  6498. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6499. (< (point) end))
  6500. (funcall fun))
  6501. (while (and (progn
  6502. (outline-next-heading)
  6503. (< (point) end))
  6504. (not (eobp)))
  6505. (funcall fun)))))
  6506. (defun org-fixup-indentation (diff)
  6507. "Change the indentation in the current entry by DIFF.
  6508. However, if any line in the current entry has no indentation, or if it
  6509. would end up with no indentation after the change, nothing at all is done."
  6510. (save-excursion
  6511. (let ((end (save-excursion (outline-next-heading)
  6512. (point-marker)))
  6513. (prohibit (if (> diff 0)
  6514. "^\\S-"
  6515. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6516. col)
  6517. (unless (save-excursion (end-of-line 1)
  6518. (re-search-forward prohibit end t))
  6519. (while (and (< (point) end)
  6520. (re-search-forward "^[ \t]+" end t))
  6521. (goto-char (match-end 0))
  6522. (setq col (current-column))
  6523. (if (< diff 0) (replace-match ""))
  6524. (org-indent-to-column (+ diff col))))
  6525. (move-marker end nil))))
  6526. (defun org-convert-to-odd-levels ()
  6527. "Convert an org-mode file with all levels allowed to one with odd levels.
  6528. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6529. level 5 etc."
  6530. (interactive)
  6531. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6532. (let ((outline-regexp org-outline-regexp)
  6533. (outline-level 'org-outline-level)
  6534. (org-odd-levels-only nil) n)
  6535. (save-excursion
  6536. (goto-char (point-min))
  6537. (while (re-search-forward "^\\*\\*+ " nil t)
  6538. (setq n (- (length (match-string 0)) 2))
  6539. (while (>= (setq n (1- n)) 0)
  6540. (org-demote))
  6541. (end-of-line 1))))))
  6542. (defun org-convert-to-oddeven-levels ()
  6543. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6544. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6545. file contains a section with an even level, conversion would
  6546. destroy the structure of the file. An error is signaled in this
  6547. case."
  6548. (interactive)
  6549. (goto-char (point-min))
  6550. ;; First check if there are no even levels
  6551. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6552. (org-show-context t)
  6553. (error "Not all levels are odd in this file. Conversion not possible"))
  6554. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6555. (let ((outline-regexp org-outline-regexp)
  6556. (outline-level 'org-outline-level)
  6557. (org-odd-levels-only nil) n)
  6558. (save-excursion
  6559. (goto-char (point-min))
  6560. (while (re-search-forward "^\\*\\*+ " nil t)
  6561. (setq n (/ (1- (length (match-string 0))) 2))
  6562. (while (>= (setq n (1- n)) 0)
  6563. (org-promote))
  6564. (end-of-line 1))))))
  6565. (defun org-tr-level (n)
  6566. "Make N odd if required."
  6567. (if org-odd-levels-only (1+ (/ n 2)) n))
  6568. ;;; Vertical tree motion, cutting and pasting of subtrees
  6569. (defun org-move-subtree-up (&optional arg)
  6570. "Move the current subtree up past ARG headlines of the same level."
  6571. (interactive "p")
  6572. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6573. (defun org-move-subtree-down (&optional arg)
  6574. "Move the current subtree down past ARG headlines of the same level."
  6575. (interactive "p")
  6576. (setq arg (prefix-numeric-value arg))
  6577. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6578. 'org-get-last-sibling))
  6579. (ins-point (make-marker))
  6580. (cnt (abs arg))
  6581. (col (current-column))
  6582. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6583. ;; Select the tree
  6584. (org-back-to-heading)
  6585. (setq beg0 (point))
  6586. (save-excursion
  6587. (setq ne-beg (org-back-over-empty-lines))
  6588. (setq beg (point)))
  6589. (save-match-data
  6590. (save-excursion (outline-end-of-heading)
  6591. (setq folded (outline-invisible-p)))
  6592. (outline-end-of-subtree))
  6593. (outline-next-heading)
  6594. (setq ne-end (org-back-over-empty-lines))
  6595. (setq end (point))
  6596. (goto-char beg0)
  6597. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6598. ;; include less whitespace
  6599. (save-excursion
  6600. (goto-char beg)
  6601. (forward-line (- ne-beg ne-end))
  6602. (setq beg (point))))
  6603. ;; Find insertion point, with error handling
  6604. (while (> cnt 0)
  6605. (or (and (funcall movfunc) (looking-at outline-regexp))
  6606. (progn (goto-char beg0)
  6607. (error "Cannot move past superior level or buffer limit")))
  6608. (setq cnt (1- cnt)))
  6609. (if (> arg 0)
  6610. ;; Moving forward - still need to move over subtree
  6611. (progn (org-end-of-subtree t t)
  6612. (save-excursion
  6613. (org-back-over-empty-lines)
  6614. (or (bolp) (newline)))))
  6615. (setq ne-ins (org-back-over-empty-lines))
  6616. (move-marker ins-point (point))
  6617. (setq txt (buffer-substring beg end))
  6618. (org-save-markers-in-region beg end)
  6619. (delete-region beg end)
  6620. (org-remove-empty-overlays-at beg)
  6621. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6622. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6623. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6624. (let ((bbb (point)))
  6625. (insert-before-markers txt)
  6626. (org-reinstall-markers-in-region bbb)
  6627. (move-marker ins-point bbb))
  6628. (or (bolp) (insert "\n"))
  6629. (setq ins-end (point))
  6630. (goto-char ins-point)
  6631. (org-skip-whitespace)
  6632. (when (and (< arg 0)
  6633. (org-first-sibling-p)
  6634. (> ne-ins ne-beg))
  6635. ;; Move whitespace back to beginning
  6636. (save-excursion
  6637. (goto-char ins-end)
  6638. (let ((kill-whole-line t))
  6639. (kill-line (- ne-ins ne-beg)) (point)))
  6640. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6641. (move-marker ins-point nil)
  6642. (if folded
  6643. (hide-subtree)
  6644. (org-show-entry)
  6645. (show-children)
  6646. (org-cycle-hide-drawers 'children))
  6647. (org-clean-visibility-after-subtree-move)
  6648. ;; move back to the initial column we were at
  6649. (move-to-column col)))
  6650. (defvar org-subtree-clip ""
  6651. "Clipboard for cut and paste of subtrees.
  6652. This is actually only a copy of the kill, because we use the normal kill
  6653. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6654. (defvar org-subtree-clip-folded nil
  6655. "Was the last copied subtree folded?
  6656. This is used to fold the tree back after pasting.")
  6657. (defun org-cut-subtree (&optional n)
  6658. "Cut the current subtree into the clipboard.
  6659. With prefix arg N, cut this many sequential subtrees.
  6660. This is a short-hand for marking the subtree and then cutting it."
  6661. (interactive "p")
  6662. (org-copy-subtree n 'cut))
  6663. (defun org-copy-subtree (&optional n cut force-store-markers)
  6664. "Cut the current subtree into the clipboard.
  6665. With prefix arg N, cut this many sequential subtrees.
  6666. This is a short-hand for marking the subtree and then copying it.
  6667. If CUT is non-nil, actually cut the subtree.
  6668. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6669. of some markers in the region, even if CUT is non-nil. This is
  6670. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6671. (interactive "p")
  6672. (let (beg end folded (beg0 (point)))
  6673. (if (interactive-p)
  6674. (org-back-to-heading nil) ; take what looks like a subtree
  6675. (org-back-to-heading t)) ; take what is really there
  6676. (org-back-over-empty-lines)
  6677. (setq beg (point))
  6678. (skip-chars-forward " \t\r\n")
  6679. (save-match-data
  6680. (save-excursion (outline-end-of-heading)
  6681. (setq folded (outline-invisible-p)))
  6682. (condition-case nil
  6683. (org-forward-same-level (1- n) t)
  6684. (error nil))
  6685. (org-end-of-subtree t t))
  6686. (org-back-over-empty-lines)
  6687. (setq end (point))
  6688. (goto-char beg0)
  6689. (when (> end beg)
  6690. (setq org-subtree-clip-folded folded)
  6691. (when (or cut force-store-markers)
  6692. (org-save-markers-in-region beg end))
  6693. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6694. (setq org-subtree-clip (current-kill 0))
  6695. (message "%s: Subtree(s) with %d characters"
  6696. (if cut "Cut" "Copied")
  6697. (length org-subtree-clip)))))
  6698. (defun org-paste-subtree (&optional level tree for-yank)
  6699. "Paste the clipboard as a subtree, with modification of headline level.
  6700. The entire subtree is promoted or demoted in order to match a new headline
  6701. level.
  6702. If the cursor is at the beginning of a headline, the same level as
  6703. that headline is used to paste the tree
  6704. If not, the new level is derived from the *visible* headings
  6705. before and after the insertion point, and taken to be the inferior headline
  6706. level of the two. So if the previous visible heading is level 3 and the
  6707. next is level 4 (or vice versa), level 4 will be used for insertion.
  6708. This makes sure that the subtree remains an independent subtree and does
  6709. not swallow low level entries.
  6710. You can also force a different level, either by using a numeric prefix
  6711. argument, or by inserting the heading marker by hand. For example, if the
  6712. cursor is after \"*****\", then the tree will be shifted to level 5.
  6713. If optional TREE is given, use this text instead of the kill ring.
  6714. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6715. move back over whitespace before inserting, and move point to the end of
  6716. the inserted text when done."
  6717. (interactive "P")
  6718. (setq tree (or tree (and kill-ring (current-kill 0))))
  6719. (unless (org-kill-is-subtree-p tree)
  6720. (error "%s"
  6721. (substitute-command-keys
  6722. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6723. (let* ((visp (not (outline-invisible-p)))
  6724. (txt tree)
  6725. (^re (concat "^\\(" outline-regexp "\\)"))
  6726. (re (concat "\\(" outline-regexp "\\)"))
  6727. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6728. (old-level (if (string-match ^re txt)
  6729. (- (match-end 0) (match-beginning 0) 1)
  6730. -1))
  6731. (force-level (cond (level (prefix-numeric-value level))
  6732. ((and (looking-at "[ \t]*$")
  6733. (string-match
  6734. ^re_ (buffer-substring
  6735. (point-at-bol) (point))))
  6736. (- (match-end 1) (match-beginning 1)))
  6737. ((and (bolp)
  6738. (looking-at org-outline-regexp))
  6739. (- (match-end 0) (point) 1))
  6740. (t nil)))
  6741. (previous-level (save-excursion
  6742. (condition-case nil
  6743. (progn
  6744. (outline-previous-visible-heading 1)
  6745. (if (looking-at re)
  6746. (- (match-end 0) (match-beginning 0) 1)
  6747. 1))
  6748. (error 1))))
  6749. (next-level (save-excursion
  6750. (condition-case nil
  6751. (progn
  6752. (or (looking-at outline-regexp)
  6753. (outline-next-visible-heading 1))
  6754. (if (looking-at re)
  6755. (- (match-end 0) (match-beginning 0) 1)
  6756. 1))
  6757. (error 1))))
  6758. (new-level (or force-level (max previous-level next-level)))
  6759. (shift (if (or (= old-level -1)
  6760. (= new-level -1)
  6761. (= old-level new-level))
  6762. 0
  6763. (- new-level old-level)))
  6764. (delta (if (> shift 0) -1 1))
  6765. (func (if (> shift 0) 'org-demote 'org-promote))
  6766. (org-odd-levels-only nil)
  6767. beg end newend)
  6768. ;; Remove the forced level indicator
  6769. (if force-level
  6770. (delete-region (point-at-bol) (point)))
  6771. ;; Paste
  6772. (beginning-of-line 1)
  6773. (unless for-yank (org-back-over-empty-lines))
  6774. (setq beg (point))
  6775. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6776. (insert-before-markers txt)
  6777. (unless (string-match "\n\\'" txt) (insert "\n"))
  6778. (setq newend (point))
  6779. (org-reinstall-markers-in-region beg)
  6780. (setq end (point))
  6781. (goto-char beg)
  6782. (skip-chars-forward " \t\n\r")
  6783. (setq beg (point))
  6784. (if (and (outline-invisible-p) visp)
  6785. (save-excursion (outline-show-heading)))
  6786. ;; Shift if necessary
  6787. (unless (= shift 0)
  6788. (save-restriction
  6789. (narrow-to-region beg end)
  6790. (while (not (= shift 0))
  6791. (org-map-region func (point-min) (point-max))
  6792. (setq shift (+ delta shift)))
  6793. (goto-char (point-min))
  6794. (setq newend (point-max))))
  6795. (when (or (interactive-p) for-yank)
  6796. (message "Clipboard pasted as level %d subtree" new-level))
  6797. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6798. kill-ring
  6799. (eq org-subtree-clip (current-kill 0))
  6800. org-subtree-clip-folded)
  6801. ;; The tree was folded before it was killed/copied
  6802. (hide-subtree))
  6803. (and for-yank (goto-char newend))))
  6804. (defun org-kill-is-subtree-p (&optional txt)
  6805. "Check if the current kill is an outline subtree, or a set of trees.
  6806. Returns nil if kill does not start with a headline, or if the first
  6807. headline level is not the largest headline level in the tree.
  6808. So this will actually accept several entries of equal levels as well,
  6809. which is OK for `org-paste-subtree'.
  6810. If optional TXT is given, check this string instead of the current kill."
  6811. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6812. (start-level (and kill
  6813. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6814. org-outline-regexp "\\)")
  6815. kill)
  6816. (- (match-end 2) (match-beginning 2) 1)))
  6817. (re (concat "^" org-outline-regexp))
  6818. (start (1+ (or (match-beginning 2) -1))))
  6819. (if (not start-level)
  6820. (progn
  6821. nil) ;; does not even start with a heading
  6822. (catch 'exit
  6823. (while (setq start (string-match re kill (1+ start)))
  6824. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6825. (throw 'exit nil)))
  6826. t))))
  6827. (defvar org-markers-to-move nil
  6828. "Markers that should be moved with a cut-and-paste operation.
  6829. Those markers are stored together with their positions relative to
  6830. the start of the region.")
  6831. (defun org-save-markers-in-region (beg end)
  6832. "Check markers in region.
  6833. If these markers are between BEG and END, record their position relative
  6834. to BEG, so that after moving the block of text, we can put the markers back
  6835. into place.
  6836. This function gets called just before an entry or tree gets cut from the
  6837. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6838. called immediately, to move the markers with the entries."
  6839. (setq org-markers-to-move nil)
  6840. (when (featurep 'org-clock)
  6841. (org-clock-save-markers-for-cut-and-paste beg end))
  6842. (when (featurep 'org-agenda)
  6843. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6844. (defun org-check-and-save-marker (marker beg end)
  6845. "Check if MARKER is between BEG and END.
  6846. If yes, remember the marker and the distance to BEG."
  6847. (when (and (marker-buffer marker)
  6848. (equal (marker-buffer marker) (current-buffer)))
  6849. (if (and (>= marker beg) (< marker end))
  6850. (push (cons marker (- marker beg)) org-markers-to-move))))
  6851. (defun org-reinstall-markers-in-region (beg)
  6852. "Move all remembered markers to their position relative to BEG."
  6853. (mapc (lambda (x)
  6854. (move-marker (car x) (+ beg (cdr x))))
  6855. org-markers-to-move)
  6856. (setq org-markers-to-move nil))
  6857. (defun org-narrow-to-subtree ()
  6858. "Narrow buffer to the current subtree."
  6859. (interactive)
  6860. (save-excursion
  6861. (save-match-data
  6862. (org-with-limited-levels
  6863. (narrow-to-region
  6864. (progn (org-back-to-heading t) (point))
  6865. (progn (org-end-of-subtree t t)
  6866. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6867. (point)))))))
  6868. (defun org-narrow-to-block ()
  6869. "Narrow buffer to the current block."
  6870. (interactive)
  6871. (let ((bstart "^[ \t]*#\\+begin")
  6872. (bend "[ \t]*#\\+end")
  6873. (case-fold-search t) ;; allow #+BEGIN
  6874. b_start b_end)
  6875. (if (org-in-regexps-block-p bstart bend)
  6876. (progn
  6877. (save-excursion (re-search-backward bstart nil t)
  6878. (setq b_start (match-beginning 0)))
  6879. (save-excursion (re-search-forward bend nil t)
  6880. (setq b_end (match-end 0)))
  6881. (narrow-to-region b_start b_end))
  6882. (error "Not in a block"))))
  6883. (eval-when-compile
  6884. (defvar org-property-drawer-re))
  6885. (defvar org-property-start-re) ;; defined below
  6886. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6887. "Clone the task (subtree) at point N times.
  6888. The clones will be inserted as siblings.
  6889. In interactive use, the user will be prompted for the number of
  6890. clones to be produced, and for a time SHIFT, which may be a
  6891. repeater as used in time stamps, for example `+3d'.
  6892. When a valid repeater is given and the entry contains any time
  6893. stamps, the clones will become a sequence in time, with time
  6894. stamps in the subtree shifted for each clone produced. If SHIFT
  6895. is nil or the empty string, time stamps will be left alone. The
  6896. ID property of the original subtree is removed.
  6897. If the original subtree did contain time stamps with a repeater,
  6898. the following will happen:
  6899. - the repeater will be removed in each clone
  6900. - an additional clone will be produced, with the current, unshifted
  6901. date(s) in the entry.
  6902. - the original entry will be placed *after* all the clones, with
  6903. repeater intact.
  6904. - the start days in the repeater in the original entry will be shifted
  6905. to past the last clone.
  6906. I this way you can spell out a number of instances of a repeating task,
  6907. and still retain the repeater to cover future instances of the task."
  6908. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6909. (let (beg end template task idprop
  6910. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6911. (if (not (and (integerp n) (> n 0)))
  6912. (error "Invalid number of replications %s" n))
  6913. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6914. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6915. shift)))
  6916. (error "Invalid shift specification %s" shift))
  6917. (when doshift
  6918. (setq shift-n (string-to-number (match-string 1 shift))
  6919. shift-what (cdr (assoc (match-string 2 shift)
  6920. '(("d" . day) ("w" . week)
  6921. ("m" . month) ("y" . year))))))
  6922. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6923. (setq nmin 1 nmax n)
  6924. (org-back-to-heading t)
  6925. (setq beg (point))
  6926. (setq idprop (org-entry-get nil "ID"))
  6927. (org-end-of-subtree t t)
  6928. (or (bolp) (insert "\n"))
  6929. (setq end (point))
  6930. (setq template (buffer-substring beg end))
  6931. (when (and doshift
  6932. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6933. (delete-region beg end)
  6934. (setq end beg)
  6935. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6936. (goto-char end)
  6937. (loop for n from nmin to nmax do
  6938. ;; prepare clone
  6939. (with-temp-buffer
  6940. (insert template)
  6941. (org-mode)
  6942. (goto-char (point-min))
  6943. (and idprop (if org-clone-delete-id
  6944. (org-entry-delete nil "ID")
  6945. (org-id-get-create t)))
  6946. (while (re-search-forward org-property-start-re nil t)
  6947. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6948. (goto-char (point-min))
  6949. (when doshift
  6950. (while (re-search-forward org-ts-regexp-both nil t)
  6951. (org-timestamp-change (* n shift-n) shift-what))
  6952. (unless (= n n-no-remove)
  6953. (goto-char (point-min))
  6954. (while (re-search-forward org-ts-regexp nil t)
  6955. (save-excursion
  6956. (goto-char (match-beginning 0))
  6957. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6958. (delete-region (match-beginning 1) (match-end 1)))))))
  6959. (setq task (buffer-string)))
  6960. (insert task))
  6961. (goto-char beg)))
  6962. ;;; Outline Sorting
  6963. (defun org-sort (with-case)
  6964. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6965. Optional argument WITH-CASE means sort case-sensitively.
  6966. With a double prefix argument, also remove duplicate entries."
  6967. (interactive "P")
  6968. (cond
  6969. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6970. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6971. (t
  6972. (org-call-with-arg 'org-sort-entries with-case))))
  6973. (defun org-sort-remove-invisible (s)
  6974. (remove-text-properties 0 (length s) org-rm-props s)
  6975. (while (string-match org-bracket-link-regexp s)
  6976. (setq s (replace-match (if (match-end 2)
  6977. (match-string 3 s)
  6978. (match-string 1 s)) t t s)))
  6979. s)
  6980. (defvar org-priority-regexp) ; defined later in the file
  6981. (defvar org-after-sorting-entries-or-items-hook nil
  6982. "Hook that is run after a bunch of entries or items have been sorted.
  6983. When children are sorted, the cursor is in the parent line when this
  6984. hook gets called. When a region or a plain list is sorted, the cursor
  6985. will be in the first entry of the sorted region/list.")
  6986. (defun org-sort-entries
  6987. (&optional with-case sorting-type getkey-func compare-func property)
  6988. "Sort entries on a certain level of an outline tree.
  6989. If there is an active region, the entries in the region are sorted.
  6990. Else, if the cursor is before the first entry, sort the top-level items.
  6991. Else, the children of the entry at point are sorted.
  6992. Sorting can be alphabetically, numerically, by date/time as given by
  6993. a time stamp, by a property or by priority.
  6994. The command prompts for the sorting type unless it has been given to the
  6995. function through the SORTING-TYPE argument, which needs to be a character,
  6996. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6997. precise meaning of each character:
  6998. n Numerically, by converting the beginning of the entry/item to a number.
  6999. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7000. t By date/time, either the first active time stamp in the entry, or, if
  7001. none exist, by the first inactive one.
  7002. s By the scheduled date/time.
  7003. d By deadline date/time.
  7004. c By creation time, which is assumed to be the first inactive time stamp
  7005. at the beginning of a line.
  7006. p By priority according to the cookie.
  7007. r By the value of a property.
  7008. Capital letters will reverse the sort order.
  7009. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7010. called with point at the beginning of the record. It must return either
  7011. a string or a number that should serve as the sorting key for that record.
  7012. Comparing entries ignores case by default. However, with an optional argument
  7013. WITH-CASE, the sorting considers case as well."
  7014. (interactive "P")
  7015. (let ((case-func (if with-case 'identity 'downcase))
  7016. start beg end stars re re2
  7017. txt what tmp)
  7018. ;; Find beginning and end of region to sort
  7019. (cond
  7020. ((org-region-active-p)
  7021. ;; we will sort the region
  7022. (setq end (region-end)
  7023. what "region")
  7024. (goto-char (region-beginning))
  7025. (if (not (org-on-heading-p)) (outline-next-heading))
  7026. (setq start (point)))
  7027. ((or (org-on-heading-p)
  7028. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7029. ;; we will sort the children of the current headline
  7030. (org-back-to-heading)
  7031. (setq start (point)
  7032. end (progn (org-end-of-subtree t t)
  7033. (or (bolp) (insert "\n"))
  7034. (org-back-over-empty-lines)
  7035. (point))
  7036. what "children")
  7037. (goto-char start)
  7038. (show-subtree)
  7039. (outline-next-heading))
  7040. (t
  7041. ;; we will sort the top-level entries in this file
  7042. (goto-char (point-min))
  7043. (or (org-on-heading-p) (outline-next-heading))
  7044. (setq start (point))
  7045. (goto-char (point-max))
  7046. (beginning-of-line 1)
  7047. (when (looking-at ".*?\\S-")
  7048. ;; File ends in a non-white line
  7049. (end-of-line 1)
  7050. (insert "\n"))
  7051. (setq end (point-max))
  7052. (setq what "top-level")
  7053. (goto-char start)
  7054. (show-all)))
  7055. (setq beg (point))
  7056. (if (>= beg end) (error "Nothing to sort"))
  7057. (looking-at "\\(\\*+\\)")
  7058. (setq stars (match-string 1)
  7059. re (concat "^" (regexp-quote stars) " +")
  7060. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7061. txt (buffer-substring beg end))
  7062. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7063. (if (and (not (equal stars "*")) (string-match re2 txt))
  7064. (error "Region to sort contains a level above the first entry"))
  7065. (unless sorting-type
  7066. (message
  7067. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7068. [t]ime [s]cheduled [d]eadline [c]reated
  7069. A/N/T/S/D/C/P/O/F means reversed:"
  7070. what)
  7071. (setq sorting-type (read-char-exclusive))
  7072. (and (= (downcase sorting-type) ?f)
  7073. (setq getkey-func
  7074. (org-icompleting-read "Sort using function: "
  7075. obarray 'fboundp t nil nil))
  7076. (setq getkey-func (intern getkey-func)))
  7077. (and (= (downcase sorting-type) ?r)
  7078. (setq property
  7079. (org-icompleting-read "Property: "
  7080. (mapcar 'list (org-buffer-property-keys t))
  7081. nil t))))
  7082. (message "Sorting entries...")
  7083. (save-restriction
  7084. (narrow-to-region start end)
  7085. (let ((dcst (downcase sorting-type))
  7086. (case-fold-search nil)
  7087. (now (current-time)))
  7088. (sort-subr
  7089. (/= dcst sorting-type)
  7090. ;; This function moves to the beginning character of the "record" to
  7091. ;; be sorted.
  7092. (lambda nil
  7093. (if (re-search-forward re nil t)
  7094. (goto-char (match-beginning 0))
  7095. (goto-char (point-max))))
  7096. ;; This function moves to the last character of the "record" being
  7097. ;; sorted.
  7098. (lambda nil
  7099. (save-match-data
  7100. (condition-case nil
  7101. (outline-forward-same-level 1)
  7102. (error
  7103. (goto-char (point-max))))))
  7104. ;; This function returns the value that gets sorted against.
  7105. (lambda nil
  7106. (cond
  7107. ((= dcst ?n)
  7108. (if (looking-at org-complex-heading-regexp)
  7109. (string-to-number (match-string 4))
  7110. nil))
  7111. ((= dcst ?a)
  7112. (if (looking-at org-complex-heading-regexp)
  7113. (funcall case-func (match-string 4))
  7114. nil))
  7115. ((= dcst ?t)
  7116. (let ((end (save-excursion (outline-next-heading) (point))))
  7117. (if (or (re-search-forward org-ts-regexp end t)
  7118. (re-search-forward org-ts-regexp-both end t))
  7119. (org-time-string-to-seconds (match-string 0))
  7120. (org-float-time now))))
  7121. ((= dcst ?c)
  7122. (let ((end (save-excursion (outline-next-heading) (point))))
  7123. (if (re-search-forward
  7124. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7125. end t)
  7126. (org-time-string-to-seconds (match-string 0))
  7127. (org-float-time now))))
  7128. ((= dcst ?s)
  7129. (let ((end (save-excursion (outline-next-heading) (point))))
  7130. (if (re-search-forward org-scheduled-time-regexp end t)
  7131. (org-time-string-to-seconds (match-string 1))
  7132. (org-float-time now))))
  7133. ((= dcst ?d)
  7134. (let ((end (save-excursion (outline-next-heading) (point))))
  7135. (if (re-search-forward org-deadline-time-regexp end t)
  7136. (org-time-string-to-seconds (match-string 1))
  7137. (org-float-time now))))
  7138. ((= dcst ?p)
  7139. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7140. (string-to-char (match-string 2))
  7141. org-default-priority))
  7142. ((= dcst ?r)
  7143. (or (org-entry-get nil property) ""))
  7144. ((= dcst ?o)
  7145. (if (looking-at org-complex-heading-regexp)
  7146. (- 9999 (length (member (match-string 2)
  7147. org-todo-keywords-1)))))
  7148. ((= dcst ?f)
  7149. (if getkey-func
  7150. (progn
  7151. (setq tmp (funcall getkey-func))
  7152. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7153. tmp)
  7154. (error "Invalid key function `%s'" getkey-func)))
  7155. (t (error "Invalid sorting type `%c'" sorting-type))))
  7156. nil
  7157. (cond
  7158. ((= dcst ?a) 'string<)
  7159. ((= dcst ?f) compare-func)
  7160. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7161. (t nil)))))
  7162. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7163. (message "Sorting entries...done")))
  7164. (defun org-do-sort (table what &optional with-case sorting-type)
  7165. "Sort TABLE of WHAT according to SORTING-TYPE.
  7166. The user will be prompted for the SORTING-TYPE if the call to this
  7167. function does not specify it. WHAT is only for the prompt, to indicate
  7168. what is being sorted. The sorting key will be extracted from
  7169. the car of the elements of the table.
  7170. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7171. (unless sorting-type
  7172. (message
  7173. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7174. what)
  7175. (setq sorting-type (read-char-exclusive)))
  7176. (let ((dcst (downcase sorting-type))
  7177. extractfun comparefun)
  7178. ;; Define the appropriate functions
  7179. (cond
  7180. ((= dcst ?n)
  7181. (setq extractfun 'string-to-number
  7182. comparefun (if (= dcst sorting-type) '< '>)))
  7183. ((= dcst ?a)
  7184. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7185. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7186. comparefun (if (= dcst sorting-type)
  7187. 'string<
  7188. (lambda (a b) (and (not (string< a b))
  7189. (not (string= a b)))))))
  7190. ((= dcst ?t)
  7191. (setq extractfun
  7192. (lambda (x)
  7193. (if (or (string-match org-ts-regexp x)
  7194. (string-match org-ts-regexp-both x))
  7195. (org-float-time
  7196. (org-time-string-to-time (match-string 0 x)))
  7197. 0))
  7198. comparefun (if (= dcst sorting-type) '< '>)))
  7199. (t (error "Invalid sorting type `%c'" sorting-type)))
  7200. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7201. table)
  7202. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7203. ;;; The orgstruct minor mode
  7204. ;; Define a minor mode which can be used in other modes in order to
  7205. ;; integrate the org-mode structure editing commands.
  7206. ;; This is really a hack, because the org-mode structure commands use
  7207. ;; keys which normally belong to the major mode. Here is how it
  7208. ;; works: The minor mode defines all the keys necessary to operate the
  7209. ;; structure commands, but wraps the commands into a function which
  7210. ;; tests if the cursor is currently at a headline or a plain list
  7211. ;; item. If that is the case, the structure command is used,
  7212. ;; temporarily setting many Org-mode variables like regular
  7213. ;; expressions for filling etc. However, when any of those keys is
  7214. ;; used at a different location, function uses `key-binding' to look
  7215. ;; up if the key has an associated command in another currently active
  7216. ;; keymap (minor modes, major mode, global), and executes that
  7217. ;; command. There might be problems if any of the keys is otherwise
  7218. ;; used as a prefix key.
  7219. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7220. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7221. ;; addresses this by checking explicitly for both bindings.
  7222. (defvar orgstruct-mode-map (make-sparse-keymap)
  7223. "Keymap for the minor `orgstruct-mode'.")
  7224. (defvar org-local-vars nil
  7225. "List of local variables, for use by `orgstruct-mode'.")
  7226. ;;;###autoload
  7227. (define-minor-mode orgstruct-mode
  7228. "Toggle the minor mode `orgstruct-mode'.
  7229. This mode is for using Org-mode structure commands in other
  7230. modes. The following keys behave as if Org-mode were active, if
  7231. the cursor is on a headline, or on a plain list item (both as
  7232. defined by Org-mode).
  7233. M-up Move entry/item up
  7234. M-down Move entry/item down
  7235. M-left Promote
  7236. M-right Demote
  7237. M-S-up Move entry/item up
  7238. M-S-down Move entry/item down
  7239. M-S-left Promote subtree
  7240. M-S-right Demote subtree
  7241. M-q Fill paragraph and items like in Org-mode
  7242. C-c ^ Sort entries
  7243. C-c - Cycle list bullet
  7244. TAB Cycle item visibility
  7245. M-RET Insert new heading/item
  7246. S-M-RET Insert new TODO heading / Checkbox item
  7247. C-c C-c Set tags / toggle checkbox"
  7248. nil " OrgStruct" nil
  7249. (org-load-modules-maybe)
  7250. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7251. ;;;###autoload
  7252. (defun turn-on-orgstruct ()
  7253. "Unconditionally turn on `orgstruct-mode'."
  7254. (orgstruct-mode 1))
  7255. (defun orgstruct++-mode (&optional arg)
  7256. "Toggle `orgstruct-mode', the enhanced version of it.
  7257. In addition to setting orgstruct-mode, this also exports all indentation
  7258. and autofilling variables from org-mode into the buffer. It will also
  7259. recognize item context in multiline items.
  7260. Note that turning off orgstruct-mode will *not* remove the
  7261. indentation/paragraph settings. This can only be done by refreshing the
  7262. major mode, for example with \\[normal-mode]."
  7263. (interactive "P")
  7264. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7265. (if (< arg 1)
  7266. (orgstruct-mode -1)
  7267. (orgstruct-mode 1)
  7268. (let (var val)
  7269. (mapc
  7270. (lambda (x)
  7271. (when (string-match
  7272. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7273. (symbol-name (car x)))
  7274. (setq var (car x) val (nth 1 x))
  7275. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7276. org-local-vars)
  7277. (org-set-local 'orgstruct-is-++ t))))
  7278. (defvar orgstruct-is-++ nil
  7279. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7280. (make-variable-buffer-local 'orgstruct-is-++)
  7281. ;;;###autoload
  7282. (defun turn-on-orgstruct++ ()
  7283. "Unconditionally turn on `orgstruct++-mode'."
  7284. (orgstruct++-mode 1))
  7285. (defun orgstruct-error ()
  7286. "Error when there is no default binding for a structure key."
  7287. (interactive)
  7288. (error "This key has no function outside structure elements"))
  7289. (defun orgstruct-setup ()
  7290. "Setup orgstruct keymaps."
  7291. (let ((nfunc 0)
  7292. (bindings
  7293. (list
  7294. '([(meta up)] org-metaup)
  7295. '([(meta down)] org-metadown)
  7296. '([(meta left)] org-metaleft)
  7297. '([(meta right)] org-metaright)
  7298. '([(meta shift up)] org-shiftmetaup)
  7299. '([(meta shift down)] org-shiftmetadown)
  7300. '([(meta shift left)] org-shiftmetaleft)
  7301. '([(meta shift right)] org-shiftmetaright)
  7302. '([?\e (up)] org-metaup)
  7303. '([?\e (down)] org-metadown)
  7304. '([?\e (left)] org-metaleft)
  7305. '([?\e (right)] org-metaright)
  7306. '([?\e (shift up)] org-shiftmetaup)
  7307. '([?\e (shift down)] org-shiftmetadown)
  7308. '([?\e (shift left)] org-shiftmetaleft)
  7309. '([?\e (shift right)] org-shiftmetaright)
  7310. '([(shift up)] org-shiftup)
  7311. '([(shift down)] org-shiftdown)
  7312. '([(shift left)] org-shiftleft)
  7313. '([(shift right)] org-shiftright)
  7314. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7315. '("\M-q" fill-paragraph)
  7316. '("\C-c^" org-sort)
  7317. '("\C-c-" org-cycle-list-bullet)))
  7318. elt key fun cmd)
  7319. (while (setq elt (pop bindings))
  7320. (setq nfunc (1+ nfunc))
  7321. (setq key (org-key (car elt))
  7322. fun (nth 1 elt)
  7323. cmd (orgstruct-make-binding fun nfunc key))
  7324. (org-defkey orgstruct-mode-map key cmd))
  7325. ;; Special treatment needed for TAB and RET
  7326. (org-defkey orgstruct-mode-map [(tab)]
  7327. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7328. (org-defkey orgstruct-mode-map "\C-i"
  7329. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7330. (org-defkey orgstruct-mode-map "\M-\C-m"
  7331. (orgstruct-make-binding 'org-insert-heading 105
  7332. "\M-\C-m" [(meta return)]))
  7333. (org-defkey orgstruct-mode-map [(meta return)]
  7334. (orgstruct-make-binding 'org-insert-heading 106
  7335. [(meta return)] "\M-\C-m"))
  7336. (org-defkey orgstruct-mode-map [(shift meta return)]
  7337. (orgstruct-make-binding 'org-insert-todo-heading 107
  7338. [(meta return)] "\M-\C-m"))
  7339. (org-defkey orgstruct-mode-map "\e\C-m"
  7340. (orgstruct-make-binding 'org-insert-heading 108
  7341. "\e\C-m" [?\e (return)]))
  7342. (org-defkey orgstruct-mode-map [?\e (return)]
  7343. (orgstruct-make-binding 'org-insert-heading 109
  7344. [?\e (return)] "\e\C-m"))
  7345. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7346. (orgstruct-make-binding 'org-insert-todo-heading 110
  7347. [?\e (return)] "\e\C-m"))
  7348. (unless org-local-vars
  7349. (setq org-local-vars (org-get-local-variables)))
  7350. t))
  7351. (defun orgstruct-make-binding (fun n &rest keys)
  7352. "Create a function for binding in the structure minor mode.
  7353. FUN is the command to call inside a table. N is used to create a unique
  7354. command name. KEYS are keys that should be checked in for a command
  7355. to execute outside of tables."
  7356. (eval
  7357. (list 'defun
  7358. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7359. '(arg)
  7360. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7361. "Outside of structure, run the binding of `"
  7362. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7363. "'.")
  7364. '(interactive "p")
  7365. (list 'if
  7366. `(org-context-p 'headline 'item
  7367. (and orgstruct-is-++
  7368. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7369. 'item-body))
  7370. (list 'org-run-like-in-org-mode (list 'quote fun))
  7371. (list 'let '(orgstruct-mode)
  7372. (list 'call-interactively
  7373. (append '(or)
  7374. (mapcar (lambda (k)
  7375. (list 'key-binding k))
  7376. keys)
  7377. '('orgstruct-error))))))))
  7378. (defun org-context-p (&rest contexts)
  7379. "Check if local context is any of CONTEXTS.
  7380. Possible values in the list of contexts are `table', `headline', and `item'."
  7381. (let ((pos (point)))
  7382. (goto-char (point-at-bol))
  7383. (prog1 (or (and (memq 'table contexts)
  7384. (looking-at "[ \t]*|"))
  7385. (and (memq 'headline contexts)
  7386. ;;????????? (looking-at "\\*+"))
  7387. (looking-at outline-regexp))
  7388. (and (memq 'item contexts)
  7389. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7390. (and (memq 'item-body contexts)
  7391. (org-in-item-p)))
  7392. (goto-char pos))))
  7393. (defun org-get-local-variables ()
  7394. "Return a list of all local variables in an org-mode buffer."
  7395. (let (varlist)
  7396. (with-current-buffer (get-buffer-create "*Org tmp*")
  7397. (erase-buffer)
  7398. (org-mode)
  7399. (setq varlist (buffer-local-variables)))
  7400. (kill-buffer "*Org tmp*")
  7401. (delq nil
  7402. (mapcar
  7403. (lambda (x)
  7404. (setq x
  7405. (if (symbolp x)
  7406. (list x)
  7407. (list (car x) (list 'quote (cdr x)))))
  7408. (if (string-match
  7409. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7410. (symbol-name (car x)))
  7411. x nil))
  7412. varlist))))
  7413. (defun org-clone-local-variables (from-buffer &optional regexp)
  7414. "Clone local variables from FROM-BUFFER.
  7415. Optional argument REGEXP selects variables to clone."
  7416. (mapc
  7417. (lambda (pair)
  7418. (and (symbolp (car pair))
  7419. (or (null regexp)
  7420. (string-match regexp (symbol-name (car pair))))
  7421. (set (make-local-variable (car pair))
  7422. (cdr pair))))
  7423. (buffer-local-variables from-buffer)))
  7424. ;;;###autoload
  7425. (defun org-run-like-in-org-mode (cmd)
  7426. "Run a command, pretending that the current buffer is in Org-mode.
  7427. This will temporarily bind local variables that are typically bound in
  7428. Org-mode to the values they have in Org-mode, and then interactively
  7429. call CMD."
  7430. (org-load-modules-maybe)
  7431. (unless org-local-vars
  7432. (setq org-local-vars (org-get-local-variables)))
  7433. (eval (list 'let org-local-vars
  7434. (list 'call-interactively (list 'quote cmd)))))
  7435. ;;;; Archiving
  7436. (defun org-get-category (&optional pos force-refresh)
  7437. "Get the category applying to position POS."
  7438. (if force-refresh (org-refresh-category-properties))
  7439. (let ((pos (or pos (point))))
  7440. (or (get-text-property pos 'org-category)
  7441. (progn (org-refresh-category-properties)
  7442. (get-text-property pos 'org-category)))))
  7443. (defun org-refresh-category-properties ()
  7444. "Refresh category text properties in the buffer."
  7445. (let ((def-cat (cond
  7446. ((null org-category)
  7447. (if buffer-file-name
  7448. (file-name-sans-extension
  7449. (file-name-nondirectory buffer-file-name))
  7450. "???"))
  7451. ((symbolp org-category) (symbol-name org-category))
  7452. (t org-category)))
  7453. beg end cat pos optionp)
  7454. (org-unmodified
  7455. (save-excursion
  7456. (save-restriction
  7457. (widen)
  7458. (goto-char (point-min))
  7459. (put-text-property (point) (point-max) 'org-category def-cat)
  7460. (while (re-search-forward
  7461. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7462. (setq pos (match-end 0)
  7463. optionp (equal (char-after (match-beginning 0)) ?#)
  7464. cat (org-trim (match-string 2)))
  7465. (if optionp
  7466. (setq beg (point-at-bol) end (point-max))
  7467. (org-back-to-heading t)
  7468. (setq beg (point) end (org-end-of-subtree t t)))
  7469. (put-text-property beg end 'org-category cat)
  7470. (goto-char pos)))))))
  7471. ;;;; Link Stuff
  7472. ;;; Link abbreviations
  7473. (defun org-link-expand-abbrev (link)
  7474. "Apply replacements as defined in `org-link-abbrev-alist."
  7475. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7476. (let* ((key (match-string 1 link))
  7477. (as (or (assoc key org-link-abbrev-alist-local)
  7478. (assoc key org-link-abbrev-alist)))
  7479. (tag (and (match-end 2) (match-string 3 link)))
  7480. rpl)
  7481. (if (not as)
  7482. link
  7483. (setq rpl (cdr as))
  7484. (cond
  7485. ((symbolp rpl) (funcall rpl tag))
  7486. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7487. ((string-match "%h" rpl)
  7488. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7489. (t (concat rpl tag)))))
  7490. link))
  7491. ;;; Storing and inserting links
  7492. (defvar org-insert-link-history nil
  7493. "Minibuffer history for links inserted with `org-insert-link'.")
  7494. (defvar org-stored-links nil
  7495. "Contains the links stored with `org-store-link'.")
  7496. (defvar org-store-link-plist nil
  7497. "Plist with info about the most recently link created with `org-store-link'.")
  7498. (defvar org-link-protocols nil
  7499. "Link protocols added to Org-mode using `org-add-link-type'.")
  7500. (defvar org-store-link-functions nil
  7501. "List of functions that are called to create and store a link.
  7502. Each function will be called in turn until one returns a non-nil
  7503. value. Each function should check if it is responsible for creating
  7504. this link (for example by looking at the major mode).
  7505. If not, it must exit and return nil.
  7506. If yes, it should return a non-nil value after a calling
  7507. `org-store-link-props' with a list of properties and values.
  7508. Special properties are:
  7509. :type The link prefix, like \"http\". This must be given.
  7510. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7511. This is obligatory as well.
  7512. :description Optional default description for the second pair
  7513. of brackets in an Org-mode link. The user can still change
  7514. this when inserting this link into an Org-mode buffer.
  7515. In addition to these, any additional properties can be specified
  7516. and then used in remember templates.")
  7517. (defun org-add-link-type (type &optional follow export)
  7518. "Add TYPE to the list of `org-link-types'.
  7519. Re-compute all regular expressions depending on `org-link-types'
  7520. FOLLOW and EXPORT are two functions.
  7521. FOLLOW should take the link path as the single argument and do whatever
  7522. is necessary to follow the link, for example find a file or display
  7523. a mail message.
  7524. EXPORT should format the link path for export to one of the export formats.
  7525. It should be a function accepting three arguments:
  7526. path the path of the link, the text after the prefix (like \"http:\")
  7527. desc the description of the link, if any, or a description added by
  7528. org-export-normalize-links if there is none
  7529. format the export format, a symbol like `html' or `latex' or `ascii'..
  7530. The function may use the FORMAT information to return different values
  7531. depending on the format. The return value will be put literally into
  7532. the exported file. If the return value is nil, this means Org should
  7533. do what it normally does with links which do not have EXPORT defined.
  7534. Org-mode has a built-in default for exporting links. If you are happy with
  7535. this default, there is no need to define an export function for the link
  7536. type. For a simple example of an export function, see `org-bbdb.el'."
  7537. (add-to-list 'org-link-types type t)
  7538. (org-make-link-regexps)
  7539. (if (assoc type org-link-protocols)
  7540. (setcdr (assoc type org-link-protocols) (list follow export))
  7541. (push (list type follow export) org-link-protocols)))
  7542. (defvar org-agenda-buffer-name)
  7543. ;;;###autoload
  7544. (defun org-store-link (arg)
  7545. "\\<org-mode-map>Store an org-link to the current location.
  7546. This link is added to `org-stored-links' and can later be inserted
  7547. into an org-buffer with \\[org-insert-link].
  7548. For some link types, a prefix arg is interpreted:
  7549. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7550. For file links, arg negates `org-context-in-file-links'."
  7551. (interactive "P")
  7552. (org-load-modules-maybe)
  7553. (setq org-store-link-plist nil) ; reset
  7554. (org-with-limited-levels
  7555. (let (link cpltxt desc description search txt custom-id agenda-link)
  7556. (cond
  7557. ((run-hook-with-args-until-success 'org-store-link-functions)
  7558. (setq link (plist-get org-store-link-plist :link)
  7559. desc (or (plist-get org-store-link-plist :description) link)))
  7560. ((equal (buffer-name) "*Org Edit Src Example*")
  7561. (let (label gc)
  7562. (while (or (not label)
  7563. (save-excursion
  7564. (save-restriction
  7565. (widen)
  7566. (goto-char (point-min))
  7567. (re-search-forward
  7568. (regexp-quote (format org-coderef-label-format label))
  7569. nil t))))
  7570. (when label (message "Label exists already") (sit-for 2))
  7571. (setq label (read-string "Code line label: " label)))
  7572. (end-of-line 1)
  7573. (setq link (format org-coderef-label-format label))
  7574. (setq gc (- 79 (length link)))
  7575. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7576. (insert link)
  7577. (setq link (concat "(" label ")") desc nil)))
  7578. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7579. ;; We are in the agenda, link to referenced location
  7580. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7581. (get-text-property (point) 'org-marker))))
  7582. (when m
  7583. (org-with-point-at m
  7584. (setq agenda-link
  7585. (if (interactive-p)
  7586. (call-interactively 'org-store-link)
  7587. (org-store-link nil)))))))
  7588. ((eq major-mode 'calendar-mode)
  7589. (let ((cd (calendar-cursor-to-date)))
  7590. (setq link
  7591. (format-time-string
  7592. (car org-time-stamp-formats)
  7593. (apply 'encode-time
  7594. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7595. nil nil nil))))
  7596. (org-store-link-props :type "calendar" :date cd)))
  7597. ((eq major-mode 'w3-mode)
  7598. (setq cpltxt (if (and (buffer-name)
  7599. (not (string-match "Untitled" (buffer-name))))
  7600. (buffer-name)
  7601. (url-view-url t))
  7602. link (org-make-link (url-view-url t)))
  7603. (org-store-link-props :type "w3" :url (url-view-url t)))
  7604. ((eq major-mode 'w3m-mode)
  7605. (setq cpltxt (or w3m-current-title w3m-current-url)
  7606. link (org-make-link w3m-current-url))
  7607. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7608. ((setq search (run-hook-with-args-until-success
  7609. 'org-create-file-search-functions))
  7610. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7611. "::" search))
  7612. (setq cpltxt (or description link)))
  7613. ((eq major-mode 'image-mode)
  7614. (setq cpltxt (concat "file:"
  7615. (abbreviate-file-name buffer-file-name))
  7616. link (org-make-link cpltxt))
  7617. (org-store-link-props :type "image" :file buffer-file-name))
  7618. ((eq major-mode 'dired-mode)
  7619. ;; link to the file in the current line
  7620. (let ((file (dired-get-filename nil t)))
  7621. (setq file (if file
  7622. (abbreviate-file-name
  7623. (expand-file-name (dired-get-filename nil t)))
  7624. ;; otherwise, no file so use current directory.
  7625. default-directory))
  7626. (setq cpltxt (concat "file:" file)
  7627. link (org-make-link cpltxt))))
  7628. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7629. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7630. (cond
  7631. ((org-in-regexp "<<\\(.*?\\)>>")
  7632. (setq cpltxt
  7633. (concat "file:"
  7634. (abbreviate-file-name
  7635. (buffer-file-name (buffer-base-buffer)))
  7636. "::" (match-string 1))
  7637. link (org-make-link cpltxt)))
  7638. ((and (featurep 'org-id)
  7639. (or (eq org-link-to-org-use-id t)
  7640. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7641. (interactive-p))
  7642. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7643. (interactive-p)
  7644. (not custom-id))
  7645. (and org-link-to-org-use-id
  7646. (org-entry-get nil "ID"))))
  7647. ;; We can make a link using the ID.
  7648. (setq link (condition-case nil
  7649. (prog1 (org-id-store-link)
  7650. (setq desc (plist-get org-store-link-plist
  7651. :description)))
  7652. (error
  7653. ;; probably before first headline, link to file only
  7654. (concat "file:"
  7655. (abbreviate-file-name
  7656. (buffer-file-name (buffer-base-buffer))))))))
  7657. (t
  7658. ;; Just link to current headline
  7659. (setq cpltxt (concat "file:"
  7660. (abbreviate-file-name
  7661. (buffer-file-name (buffer-base-buffer)))))
  7662. ;; Add a context search string
  7663. (when (org-xor org-context-in-file-links arg)
  7664. (setq txt (cond
  7665. ((org-on-heading-p) nil)
  7666. ((org-region-active-p)
  7667. (buffer-substring (region-beginning) (region-end)))
  7668. (t nil)))
  7669. (when (or (null txt) (string-match "\\S-" txt))
  7670. (setq cpltxt
  7671. (concat cpltxt "::"
  7672. (condition-case nil
  7673. (org-make-org-heading-search-string txt)
  7674. (error "")))
  7675. desc (or (nth 4 (ignore-errors
  7676. (org-heading-components))) "NONE"))))
  7677. (if (string-match "::\\'" cpltxt)
  7678. (setq cpltxt (substring cpltxt 0 -2)))
  7679. (setq link (org-make-link cpltxt)))))
  7680. ((buffer-file-name (buffer-base-buffer))
  7681. ;; Just link to this file here.
  7682. (setq cpltxt (concat "file:"
  7683. (abbreviate-file-name
  7684. (buffer-file-name (buffer-base-buffer)))))
  7685. ;; Add a context string
  7686. (when (org-xor org-context-in-file-links arg)
  7687. (setq txt (if (org-region-active-p)
  7688. (buffer-substring (region-beginning) (region-end))
  7689. (buffer-substring (point-at-bol) (point-at-eol))))
  7690. ;; Only use search option if there is some text.
  7691. (when (string-match "\\S-" txt)
  7692. (setq cpltxt
  7693. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7694. desc "NONE")))
  7695. (setq link (org-make-link cpltxt)))
  7696. ((interactive-p)
  7697. (error "Cannot link to a buffer which is not visiting a file"))
  7698. (t (setq link nil)))
  7699. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7700. (setq link (or link cpltxt)
  7701. desc (or desc cpltxt))
  7702. (if (equal desc "NONE") (setq desc nil))
  7703. (if (and (or (interactive-p) executing-kbd-macro) link)
  7704. (progn
  7705. (setq org-stored-links
  7706. (cons (list link desc) org-stored-links))
  7707. (message "Stored: %s" (or desc link))
  7708. (when custom-id
  7709. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7710. "::#" custom-id))
  7711. (setq org-stored-links
  7712. (cons (list link desc) org-stored-links))))
  7713. (or agenda-link (and link (org-make-link-string link desc)))))))
  7714. (defun org-store-link-props (&rest plist)
  7715. "Store link properties, extract names and addresses."
  7716. (let (x adr)
  7717. (when (setq x (plist-get plist :from))
  7718. (setq adr (mail-extract-address-components x))
  7719. (setq plist (plist-put plist :fromname (car adr)))
  7720. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7721. (when (setq x (plist-get plist :to))
  7722. (setq adr (mail-extract-address-components x))
  7723. (setq plist (plist-put plist :toname (car adr)))
  7724. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7725. (let ((from (plist-get plist :from))
  7726. (to (plist-get plist :to)))
  7727. (when (and from to org-from-is-user-regexp)
  7728. (setq plist
  7729. (plist-put plist :fromto
  7730. (if (string-match org-from-is-user-regexp from)
  7731. (concat "to %t")
  7732. (concat "from %f"))))))
  7733. (setq org-store-link-plist plist))
  7734. (defun org-add-link-props (&rest plist)
  7735. "Add these properties to the link property list."
  7736. (let (key value)
  7737. (while plist
  7738. (setq key (pop plist) value (pop plist))
  7739. (setq org-store-link-plist
  7740. (plist-put org-store-link-plist key value)))))
  7741. (defun org-email-link-description (&optional fmt)
  7742. "Return the description part of an email link.
  7743. This takes information from `org-store-link-plist' and formats it
  7744. according to FMT (default from `org-email-link-description-format')."
  7745. (setq fmt (or fmt org-email-link-description-format))
  7746. (let* ((p org-store-link-plist)
  7747. (to (plist-get p :toaddress))
  7748. (from (plist-get p :fromaddress))
  7749. (table
  7750. (list
  7751. (cons "%c" (plist-get p :fromto))
  7752. (cons "%F" (plist-get p :from))
  7753. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7754. (cons "%T" (plist-get p :to))
  7755. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7756. (cons "%s" (plist-get p :subject))
  7757. (cons "%d" (plist-get p :date))
  7758. (cons "%m" (plist-get p :message-id)))))
  7759. (when (string-match "%c" fmt)
  7760. ;; Check if the user wrote this message
  7761. (if (and org-from-is-user-regexp from to
  7762. (save-match-data (string-match org-from-is-user-regexp from)))
  7763. (setq fmt (replace-match "to %t" t t fmt))
  7764. (setq fmt (replace-match "from %f" t t fmt))))
  7765. (org-replace-escapes fmt table)))
  7766. (defun org-make-org-heading-search-string (&optional string heading)
  7767. "Make search string for STRING or current headline."
  7768. (interactive)
  7769. (let ((s (or string (org-get-heading)))
  7770. (lines org-context-in-file-links))
  7771. (unless (and string (not heading))
  7772. ;; We are using a headline, clean up garbage in there.
  7773. (if (string-match org-todo-regexp s)
  7774. (setq s (replace-match "" t t s)))
  7775. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7776. (setq s (replace-match "" t t s)))
  7777. (setq s (org-trim s))
  7778. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7779. org-comment-string "\\)") s)
  7780. (setq s (replace-match "" t t s)))
  7781. (while (string-match org-ts-regexp s)
  7782. (setq s (replace-match "" t t s))))
  7783. (or string (setq s (concat "*" s))) ; Add * for headlines
  7784. (when (and string (integerp lines) (> lines 0))
  7785. (let ((slines (org-split-string s "\n")))
  7786. (when (< lines (length slines))
  7787. (setq s (mapconcat
  7788. 'identity
  7789. (reverse (nthcdr (- (length slines) lines)
  7790. (reverse slines))) "\n")))))
  7791. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7792. (defun org-make-link (&rest strings)
  7793. "Concatenate STRINGS."
  7794. (apply 'concat strings))
  7795. (defun org-make-link-string (link &optional description)
  7796. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7797. (unless (string-match "\\S-" link)
  7798. (error "Empty link"))
  7799. (when (and description
  7800. (stringp description)
  7801. (not (string-match "\\S-" description)))
  7802. (setq description nil))
  7803. (when (stringp description)
  7804. ;; Remove brackets from the description, they are fatal.
  7805. (while (string-match "\\[" description)
  7806. (setq description (replace-match "{" t t description)))
  7807. (while (string-match "\\]" description)
  7808. (setq description (replace-match "}" t t description))))
  7809. (when (equal (org-link-escape link) description)
  7810. ;; No description needed, it is identical
  7811. (setq description nil))
  7812. (when (and (not description)
  7813. (not (equal link (org-link-escape link))))
  7814. (setq description (org-extract-attributes link)))
  7815. (setq link (if (string-match org-link-types-re link)
  7816. (concat (match-string 1 link)
  7817. (org-link-escape (substring link (match-end 1))))
  7818. (org-link-escape link)))
  7819. (concat "[[" link "]"
  7820. (if description (concat "[" description "]") "")
  7821. "]"))
  7822. (defconst org-link-escape-chars
  7823. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  7824. "List of characters that should be escaped in link.
  7825. This is the list that is used for internal purposes.")
  7826. (defvar org-url-encoding-use-url-hexify nil)
  7827. (defconst org-link-escape-chars-browser
  7828. '(?\ )
  7829. "List of escapes for characters that are problematic in links.
  7830. This is the list that is used before handing over to the browser.")
  7831. (defun org-link-escape (text &optional table merge)
  7832. "Return percent escaped representation of TEXT.
  7833. TEXT is a string with the text to escape.
  7834. Optional argument TABLE is a list with characters that should be
  7835. escaped. When nil, `org-link-escape-chars' is used.
  7836. If optional argument MERGE is set, merge TABLE into
  7837. `org-link-escape-chars'."
  7838. (if (and org-url-encoding-use-url-hexify (not table))
  7839. (url-hexify-string text)
  7840. (cond
  7841. ((and table merge)
  7842. (mapc (lambda (defchr)
  7843. (unless (member defchr table)
  7844. (setq table (cons defchr table)))) org-link-escape-chars))
  7845. ((null table)
  7846. (setq table org-link-escape-chars)))
  7847. (mapconcat
  7848. (lambda (char)
  7849. (if (or (member char table)
  7850. (< char 32) (= char 37) (> char 126))
  7851. (mapconcat (lambda (sequence-element)
  7852. (format "%%%.2X" sequence-element))
  7853. (or (encode-coding-char char 'utf-8)
  7854. (error "Unable to percent escape character: %s"
  7855. (char-to-string char))) "")
  7856. (char-to-string char))) text "")))
  7857. (defun org-link-unescape (str)
  7858. "Unhex hexified unicode strings as returned from the JavaScript function
  7859. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  7860. (unless (and (null str) (string= "" str))
  7861. (let ((pos 0) (case-fold-search t) unhexed)
  7862. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  7863. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  7864. (setq str (replace-match unhexed t t str))
  7865. (setq pos (+ pos (length unhexed))))))
  7866. str)
  7867. (defun org-link-unescape-compound (hex)
  7868. "Unhexify unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  7869. Note: this function also decodes single byte encodings like
  7870. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  7871. (save-match-data
  7872. (let* ((bytes (cdr (split-string hex "%")))
  7873. (ret "")
  7874. (eat 0)
  7875. (sum 0))
  7876. (while bytes
  7877. (let* ((val (string-to-number (pop bytes) 16))
  7878. (shift-xor
  7879. (if (= 0 eat)
  7880. (cond
  7881. ((>= val 252) (cons 6 252))
  7882. ((>= val 248) (cons 5 248))
  7883. ((>= val 240) (cons 4 240))
  7884. ((>= val 224) (cons 3 224))
  7885. ((>= val 192) (cons 2 192))
  7886. (t (cons 0 0)))
  7887. (cons 6 128))))
  7888. (if (>= val 192) (setq eat (car shift-xor)))
  7889. (setq val (logxor val (cdr shift-xor)))
  7890. (setq sum (+ (lsh sum (car shift-xor)) val))
  7891. (if (> eat 0) (setq eat (- eat 1)))
  7892. (cond
  7893. ((= 0 eat) ;multi byte
  7894. (setq ret (concat ret (org-char-to-string sum)))
  7895. (setq sum 0))
  7896. ((not bytes) ; single byte(s)
  7897. (setq ret (org-link-unescape-single-byte-sequence hex))))
  7898. )) ;; end (while bytes
  7899. ret )))
  7900. (defun org-link-unescape-single-byte-sequence (hex)
  7901. "Unhexify hex-encoded single byte character sequences."
  7902. (mapconcat (lambda (byte)
  7903. (char-to-string (string-to-number byte 16)))
  7904. (cdr (split-string hex "%")) ""))
  7905. (defun org-xor (a b)
  7906. "Exclusive or."
  7907. (if a (not b) b))
  7908. (defun org-fixup-message-id-for-http (s)
  7909. "Replace special characters in a message id, so it can be used in an http query."
  7910. (when (string-match "%" s)
  7911. (setq s (mapconcat (lambda (c)
  7912. (if (eq c ?%)
  7913. "%25"
  7914. (char-to-string c)))
  7915. s "")))
  7916. (while (string-match "<" s)
  7917. (setq s (replace-match "%3C" t t s)))
  7918. (while (string-match ">" s)
  7919. (setq s (replace-match "%3E" t t s)))
  7920. (while (string-match "@" s)
  7921. (setq s (replace-match "%40" t t s)))
  7922. s)
  7923. ;;;###autoload
  7924. (defun org-insert-link-global ()
  7925. "Insert a link like Org-mode does.
  7926. This command can be called in any mode to insert a link in Org-mode syntax."
  7927. (interactive)
  7928. (org-load-modules-maybe)
  7929. (org-run-like-in-org-mode 'org-insert-link))
  7930. (defun org-insert-link (&optional complete-file link-location)
  7931. "Insert a link. At the prompt, enter the link.
  7932. Completion can be used to insert any of the link protocol prefixes like
  7933. http or ftp in use.
  7934. The history can be used to select a link previously stored with
  7935. `org-store-link'. When the empty string is entered (i.e. if you just
  7936. press RET at the prompt), the link defaults to the most recently
  7937. stored link. As SPC triggers completion in the minibuffer, you need to
  7938. use M-SPC or C-q SPC to force the insertion of a space character.
  7939. You will also be prompted for a description, and if one is given, it will
  7940. be displayed in the buffer instead of the link.
  7941. If there is already a link at point, this command will allow you to edit link
  7942. and description parts.
  7943. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7944. be selected using completion. The path to the file will be relative to the
  7945. current directory if the file is in the current directory or a subdirectory.
  7946. Otherwise, the link will be the absolute path as completed in the minibuffer
  7947. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7948. option `org-link-file-path-type'.
  7949. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7950. the current directory or below.
  7951. With three \\[universal-argument] prefixes, negate the meaning of
  7952. `org-keep-stored-link-after-insertion'.
  7953. If `org-make-link-description-function' is non-nil, this function will be
  7954. called with the link target, and the result will be the default
  7955. link description.
  7956. If the LINK-LOCATION parameter is non-nil, this value will be
  7957. used as the link location instead of reading one interactively."
  7958. (interactive "P")
  7959. (let* ((wcf (current-window-configuration))
  7960. (region (if (org-region-active-p)
  7961. (buffer-substring (region-beginning) (region-end))))
  7962. (remove (and region (list (region-beginning) (region-end))))
  7963. (desc region)
  7964. tmphist ; byte-compile incorrectly complains about this
  7965. (link link-location)
  7966. entry file all-prefixes)
  7967. (cond
  7968. (link-location) ; specified by arg, just use it.
  7969. ((org-in-regexp org-bracket-link-regexp 1)
  7970. ;; We do have a link at point, and we are going to edit it.
  7971. (setq remove (list (match-beginning 0) (match-end 0)))
  7972. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7973. (setq link (read-string "Link: "
  7974. (org-link-unescape
  7975. (org-match-string-no-properties 1)))))
  7976. ((or (org-in-regexp org-angle-link-re)
  7977. (org-in-regexp org-plain-link-re))
  7978. ;; Convert to bracket link
  7979. (setq remove (list (match-beginning 0) (match-end 0))
  7980. link (read-string "Link: "
  7981. (org-remove-angle-brackets (match-string 0)))))
  7982. ((member complete-file '((4) (16)))
  7983. ;; Completing read for file names.
  7984. (setq link (org-file-complete-link complete-file)))
  7985. (t
  7986. ;; Read link, with completion for stored links.
  7987. (with-output-to-temp-buffer "*Org Links*"
  7988. (princ "Insert a link.
  7989. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7990. (when org-stored-links
  7991. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7992. (princ (mapconcat
  7993. (lambda (x)
  7994. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7995. (reverse org-stored-links) "\n"))))
  7996. (let ((cw (selected-window)))
  7997. (select-window (get-buffer-window "*Org Links*" 'visible))
  7998. (with-current-buffer "*Org Links*" (setq truncate-lines) t)
  7999. (unless (pos-visible-in-window-p (point-max))
  8000. (org-fit-window-to-buffer))
  8001. (and (window-live-p cw) (select-window cw)))
  8002. ;; Fake a link history, containing the stored links.
  8003. (setq tmphist (append (mapcar 'car org-stored-links)
  8004. org-insert-link-history))
  8005. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8006. (mapcar 'car org-link-abbrev-alist)
  8007. org-link-types))
  8008. (unwind-protect
  8009. (progn
  8010. (setq link
  8011. (let ((org-completion-use-ido nil)
  8012. (org-completion-use-iswitchb nil))
  8013. (org-completing-read
  8014. "Link: "
  8015. (append
  8016. (mapcar (lambda (x) (list (concat x ":")))
  8017. all-prefixes)
  8018. (mapcar 'car org-stored-links))
  8019. nil nil nil
  8020. 'tmphist
  8021. (car (car org-stored-links)))))
  8022. (if (not (string-match "\\S-" link))
  8023. (error "No link selected"))
  8024. (if (or (member link all-prefixes)
  8025. (and (equal ":" (substring link -1))
  8026. (member (substring link 0 -1) all-prefixes)
  8027. (setq link (substring link 0 -1))))
  8028. (setq link (org-link-try-special-completion link))))
  8029. (set-window-configuration wcf)
  8030. (kill-buffer "*Org Links*"))
  8031. (setq entry (assoc link org-stored-links))
  8032. (or entry (push link org-insert-link-history))
  8033. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8034. (not org-keep-stored-link-after-insertion))
  8035. (setq org-stored-links (delq (assoc link org-stored-links)
  8036. org-stored-links)))
  8037. (setq desc (or desc (nth 1 entry)))))
  8038. (if (string-match org-plain-link-re link)
  8039. ;; URL-like link, normalize the use of angular brackets.
  8040. (setq link (org-make-link (org-remove-angle-brackets link))))
  8041. ;; Check if we are linking to the current file with a search option
  8042. ;; If yes, simplify the link by using only the search option.
  8043. (when (and buffer-file-name
  8044. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8045. (let* ((path (match-string 1 link))
  8046. (case-fold-search nil)
  8047. (search (match-string 2 link)))
  8048. (save-match-data
  8049. (if (equal (file-truename buffer-file-name) (file-truename path))
  8050. ;; We are linking to this same file, with a search option
  8051. (setq link search)))))
  8052. ;; Check if we can/should use a relative path. If yes, simplify the link
  8053. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8054. (let* ((type (match-string 1 link))
  8055. (path (match-string 2 link))
  8056. (origpath path)
  8057. (case-fold-search nil))
  8058. (cond
  8059. ((or (eq org-link-file-path-type 'absolute)
  8060. (equal complete-file '(16)))
  8061. (setq path (abbreviate-file-name (expand-file-name path))))
  8062. ((eq org-link-file-path-type 'noabbrev)
  8063. (setq path (expand-file-name path)))
  8064. ((eq org-link-file-path-type 'relative)
  8065. (setq path (file-relative-name path)))
  8066. (t
  8067. (save-match-data
  8068. (if (string-match (concat "^" (regexp-quote
  8069. (expand-file-name
  8070. (file-name-as-directory
  8071. default-directory))))
  8072. (expand-file-name path))
  8073. ;; We are linking a file with relative path name.
  8074. (setq path (substring (expand-file-name path)
  8075. (match-end 0)))
  8076. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8077. (setq link (concat type path))
  8078. (if (equal desc origpath)
  8079. (setq desc path))))
  8080. (if org-make-link-description-function
  8081. (setq desc (funcall org-make-link-description-function link desc)))
  8082. (setq desc (read-string "Description: " desc))
  8083. (unless (string-match "\\S-" desc) (setq desc nil))
  8084. (if remove (apply 'delete-region remove))
  8085. (insert (org-make-link-string link desc))))
  8086. (defun org-link-try-special-completion (type)
  8087. "If there is completion support for link type TYPE, offer it."
  8088. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8089. (if (functionp fun)
  8090. (funcall fun)
  8091. (read-string "Link (no completion support): " (concat type ":")))))
  8092. (defun org-file-complete-link (&optional arg)
  8093. "Create a file link using completion."
  8094. (let (file link)
  8095. (setq file (read-file-name "File: "))
  8096. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8097. (pwd1 (file-name-as-directory (abbreviate-file-name
  8098. (expand-file-name ".")))))
  8099. (cond
  8100. ((equal arg '(16))
  8101. (setq link (org-make-link
  8102. "file:"
  8103. (abbreviate-file-name (expand-file-name file)))))
  8104. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8105. (setq link (org-make-link "file:" (match-string 1 file))))
  8106. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8107. (expand-file-name file))
  8108. (setq link (org-make-link
  8109. "file:" (match-string 1 (expand-file-name file)))))
  8110. (t (setq link (org-make-link "file:" file)))))
  8111. link))
  8112. (defun org-completing-read (&rest args)
  8113. "Completing-read with SPACE being a normal character."
  8114. (let ((minibuffer-local-completion-map
  8115. (copy-keymap minibuffer-local-completion-map)))
  8116. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8117. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8118. (apply 'org-icompleting-read args)))
  8119. (defun org-completing-read-no-i (&rest args)
  8120. (let (org-completion-use-ido org-completion-use-iswitchb)
  8121. (apply 'org-completing-read args)))
  8122. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8123. "Use iswitch as a completing-read replacement to choose from choices.
  8124. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8125. from."
  8126. (let* ((iswitchb-use-virtual-buffers nil)
  8127. (iswitchb-make-buflist-hook
  8128. (lambda ()
  8129. (setq iswitchb-temp-buflist choices))))
  8130. (iswitchb-read-buffer prompt)))
  8131. (defun org-icompleting-read (&rest args)
  8132. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8133. (org-without-partial-completion
  8134. (if (and org-completion-use-ido
  8135. (fboundp 'ido-completing-read)
  8136. (boundp 'ido-mode) ido-mode
  8137. (listp (second args)))
  8138. (let ((ido-enter-matching-directory nil))
  8139. (apply 'ido-completing-read (concat (car args))
  8140. (if (consp (car (nth 1 args)))
  8141. (mapcar 'car (nth 1 args))
  8142. (nth 1 args))
  8143. (cddr args)))
  8144. (if (and org-completion-use-iswitchb
  8145. (boundp 'iswitchb-mode) iswitchb-mode
  8146. (listp (second args)))
  8147. (apply 'org-iswitchb-completing-read (concat (car args))
  8148. (if (consp (car (nth 1 args)))
  8149. (mapcar 'car (nth 1 args))
  8150. (nth 1 args))
  8151. (cddr args))
  8152. (apply 'completing-read args)))))
  8153. (defun org-extract-attributes (s)
  8154. "Extract the attributes cookie from a string and set as text property."
  8155. (let (a attr (start 0) key value)
  8156. (save-match-data
  8157. (when (string-match "{{\\([^}]+\\)}}$" s)
  8158. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8159. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8160. (setq key (match-string 1 a) value (match-string 2 a)
  8161. start (match-end 0)
  8162. attr (plist-put attr (intern key) value))))
  8163. (org-add-props s nil 'org-attr attr))
  8164. s))
  8165. (defun org-extract-attributes-from-string (tag)
  8166. (let (key value attr)
  8167. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8168. (setq key (match-string 1 tag) value (match-string 2 tag)
  8169. tag (replace-match "" t t tag)
  8170. attr (plist-put attr (intern key) value)))
  8171. (cons tag attr)))
  8172. (defun org-attributes-to-string (plist)
  8173. "Format a property list into an HTML attribute list."
  8174. (let ((s "") key value)
  8175. (while plist
  8176. (setq key (pop plist) value (pop plist))
  8177. (and value
  8178. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8179. s))
  8180. ;;; Opening/following a link
  8181. (defvar org-link-search-failed nil)
  8182. (defvar org-open-link-functions nil
  8183. "Hook for functions finding a plain text link.
  8184. These functions must take a single argument, the link content.
  8185. They will be called for links that look like [[link text][description]]
  8186. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8187. like a filename (e.g. \"./blue.png\").
  8188. These functions will be called *before* Org attempts to resolve the
  8189. link by doing text searches in the current buffer - so if you want a
  8190. link \"[[target]]\" to still find \"<<target>>\", your function should
  8191. handle this as a special case.
  8192. When the function does handle the link, it must return a non-nil value.
  8193. If it decides that it is not responsible for this link, it must return
  8194. nil to indicate that that Org-mode can continue with other options
  8195. like exact and fuzzy text search.")
  8196. (defun org-next-link ()
  8197. "Move forward to the next link.
  8198. If the link is in hidden text, expose it."
  8199. (interactive)
  8200. (when (and org-link-search-failed (eq this-command last-command))
  8201. (goto-char (point-min))
  8202. (message "Link search wrapped back to beginning of buffer"))
  8203. (setq org-link-search-failed nil)
  8204. (let* ((pos (point))
  8205. (ct (org-context))
  8206. (a (assoc :link ct)))
  8207. (if a (goto-char (nth 2 a)))
  8208. (if (re-search-forward org-any-link-re nil t)
  8209. (progn
  8210. (goto-char (match-beginning 0))
  8211. (if (outline-invisible-p) (org-show-context)))
  8212. (goto-char pos)
  8213. (setq org-link-search-failed t)
  8214. (error "No further link found"))))
  8215. (defun org-previous-link ()
  8216. "Move backward to the previous link.
  8217. If the link is in hidden text, expose it."
  8218. (interactive)
  8219. (when (and org-link-search-failed (eq this-command last-command))
  8220. (goto-char (point-max))
  8221. (message "Link search wrapped back to end of buffer"))
  8222. (setq org-link-search-failed nil)
  8223. (let* ((pos (point))
  8224. (ct (org-context))
  8225. (a (assoc :link ct)))
  8226. (if a (goto-char (nth 1 a)))
  8227. (if (re-search-backward org-any-link-re nil t)
  8228. (progn
  8229. (goto-char (match-beginning 0))
  8230. (if (outline-invisible-p) (org-show-context)))
  8231. (goto-char pos)
  8232. (setq org-link-search-failed t)
  8233. (error "No further link found"))))
  8234. (defun org-translate-link (s)
  8235. "Translate a link string if a translation function has been defined."
  8236. (if (and org-link-translation-function
  8237. (fboundp org-link-translation-function)
  8238. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8239. (progn
  8240. (setq s (funcall org-link-translation-function
  8241. (match-string 1) (match-string 2)))
  8242. (concat (car s) ":" (cdr s)))
  8243. s))
  8244. (defun org-translate-link-from-planner (type path)
  8245. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8246. This is still an experimental function, your mileage may vary."
  8247. (cond
  8248. ((member type '("http" "https" "news" "ftp"))
  8249. ;; standard Internet links are the same.
  8250. nil)
  8251. ((and (equal type "irc") (string-match "^//" path))
  8252. ;; Planner has two / at the beginning of an irc link, we have 1.
  8253. ;; We should have zero, actually....
  8254. (setq path (substring path 1)))
  8255. ((and (equal type "lisp") (string-match "^/" path))
  8256. ;; Planner has a slash, we do not.
  8257. (setq type "elisp" path (substring path 1)))
  8258. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8259. ;; A typical message link. Planner has the id after the final slash,
  8260. ;; we separate it with a hash mark
  8261. (setq path (concat (match-string 1 path) "#"
  8262. (org-remove-angle-brackets (match-string 2 path)))))
  8263. )
  8264. (cons type path))
  8265. (defun org-find-file-at-mouse (ev)
  8266. "Open file link or URL at mouse."
  8267. (interactive "e")
  8268. (mouse-set-point ev)
  8269. (org-open-at-point 'in-emacs))
  8270. (defun org-open-at-mouse (ev)
  8271. "Open file link or URL at mouse."
  8272. (interactive "e")
  8273. (mouse-set-point ev)
  8274. (if (eq major-mode 'org-agenda-mode)
  8275. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8276. (org-open-at-point))
  8277. (defvar org-window-config-before-follow-link nil
  8278. "The window configuration before following a link.
  8279. This is saved in case the need arises to restore it.")
  8280. (defvar org-open-link-marker (make-marker)
  8281. "Marker pointing to the location where `org-open-at-point; was called.")
  8282. ;;;###autoload
  8283. (defun org-open-at-point-global ()
  8284. "Follow a link like Org-mode does.
  8285. This command can be called in any mode to follow a link that has
  8286. Org-mode syntax."
  8287. (interactive)
  8288. (org-run-like-in-org-mode 'org-open-at-point))
  8289. ;;;###autoload
  8290. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8291. "Open a link in the string S, as if it was in Org-mode."
  8292. (interactive "sLink: \nP")
  8293. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8294. (with-temp-buffer
  8295. (let ((org-inhibit-startup t))
  8296. (org-mode)
  8297. (insert s)
  8298. (goto-char (point-min))
  8299. (when reference-buffer
  8300. (setq org-link-abbrev-alist-local
  8301. (with-current-buffer reference-buffer
  8302. org-link-abbrev-alist-local)))
  8303. (org-open-at-point arg reference-buffer)))))
  8304. (defvar org-open-at-point-functions nil
  8305. "Hook that is run when following a link at point.
  8306. Functions in this hook must return t if they identify and follow
  8307. a link at point. If they don't find anything interesting at point,
  8308. they must return nil.")
  8309. (defun org-open-at-point (&optional arg reference-buffer)
  8310. "Open link at or after point.
  8311. If there is no link at point, this function will search forward up to
  8312. the end of the current line.
  8313. Normally, files will be opened by an appropriate application. If the
  8314. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8315. With a double prefix argument, try to open outside of Emacs, in the
  8316. application the system uses for this file type."
  8317. (interactive "P")
  8318. ;; if in a code block, then open the block's results
  8319. (unless (call-interactively #'org-babel-open-src-block-result)
  8320. (org-load-modules-maybe)
  8321. (move-marker org-open-link-marker (point))
  8322. (setq org-window-config-before-follow-link (current-window-configuration))
  8323. (org-remove-occur-highlights nil nil t)
  8324. (cond
  8325. ((and (org-on-heading-p)
  8326. (not (org-in-regexp
  8327. (concat org-plain-link-re "\\|"
  8328. org-bracket-link-regexp "\\|"
  8329. org-angle-link-re "\\|"
  8330. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8331. (not (get-text-property (point) 'org-linked-text)))
  8332. (or (org-offer-links-in-entry arg)
  8333. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8334. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8335. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8336. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8337. (not (org-in-regexp org-bracket-link-regexp)))
  8338. (org-footnote-action))
  8339. (t
  8340. (let (type path link line search (pos (point)))
  8341. (catch 'match
  8342. (save-excursion
  8343. (skip-chars-forward "^]\n\r")
  8344. (when (org-in-regexp org-bracket-link-regexp 1)
  8345. (setq link (org-extract-attributes
  8346. (org-link-unescape (org-match-string-no-properties 1))))
  8347. (while (string-match " *\n *" link)
  8348. (setq link (replace-match " " t t link)))
  8349. (setq link (org-link-expand-abbrev link))
  8350. (cond
  8351. ((or (file-name-absolute-p link)
  8352. (string-match "^\\.\\.?/" link))
  8353. (setq type "file" path link))
  8354. ((string-match org-link-re-with-space3 link)
  8355. (setq type (match-string 1 link) path (match-string 2 link)))
  8356. (t (setq type "thisfile" path link)))
  8357. (throw 'match t)))
  8358. (when (get-text-property (point) 'org-linked-text)
  8359. (setq type "thisfile"
  8360. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8361. (1+ (point)) (point))
  8362. path (buffer-substring
  8363. (or (previous-single-property-change pos 'org-linked-text)
  8364. (point-min))
  8365. (or (next-single-property-change pos 'org-linked-text)
  8366. (point-max))))
  8367. (throw 'match t))
  8368. (save-excursion
  8369. (when (or (org-in-regexp org-angle-link-re)
  8370. (org-in-regexp org-plain-link-re))
  8371. (setq type (match-string 1) path (match-string 2))
  8372. (throw 'match t)))
  8373. (save-excursion
  8374. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8375. (setq type "tags"
  8376. path (match-string 1))
  8377. (while (string-match ":" path)
  8378. (setq path (replace-match "+" t t path)))
  8379. (throw 'match t)))
  8380. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8381. (setq type "tree-match"
  8382. path (match-string 1))
  8383. (throw 'match t)))
  8384. (unless path
  8385. (error "No link found"))
  8386. ;; switch back to reference buffer
  8387. ;; needed when if called in a temporary buffer through
  8388. ;; org-open-link-from-string
  8389. (with-current-buffer (or reference-buffer (current-buffer))
  8390. ;; Remove any trailing spaces in path
  8391. (if (string-match " +\\'" path)
  8392. (setq path (replace-match "" t t path)))
  8393. (if (and org-link-translation-function
  8394. (fboundp org-link-translation-function))
  8395. ;; Check if we need to translate the link
  8396. (let ((tmp (funcall org-link-translation-function type path)))
  8397. (setq type (car tmp) path (cdr tmp))))
  8398. (cond
  8399. ((assoc type org-link-protocols)
  8400. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8401. ((equal type "mailto")
  8402. (let ((cmd (car org-link-mailto-program))
  8403. (args (cdr org-link-mailto-program)) args1
  8404. (address path) (subject "") a)
  8405. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8406. (setq address (match-string 1 path)
  8407. subject (org-link-escape (match-string 2 path))))
  8408. (while args
  8409. (cond
  8410. ((not (stringp (car args))) (push (pop args) args1))
  8411. (t (setq a (pop args))
  8412. (if (string-match "%a" a)
  8413. (setq a (replace-match address t t a)))
  8414. (if (string-match "%s" a)
  8415. (setq a (replace-match subject t t a)))
  8416. (push a args1))))
  8417. (apply cmd (nreverse args1))))
  8418. ((member type '("http" "https" "ftp" "news"))
  8419. (browse-url (concat type ":" (org-link-escape
  8420. path org-link-escape-chars-browser))))
  8421. ((string= type "doi")
  8422. (browse-url (concat "http://dx.doi.org/"
  8423. (org-link-escape
  8424. path org-link-escape-chars-browser))))
  8425. ((member type '("message"))
  8426. (browse-url (concat type ":" path)))
  8427. ((string= type "tags")
  8428. (org-tags-view arg path))
  8429. ((string= type "tree-match")
  8430. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8431. ((string= type "file")
  8432. (if (string-match "::\\([0-9]+\\)\\'" path)
  8433. (setq line (string-to-number (match-string 1 path))
  8434. path (substring path 0 (match-beginning 0)))
  8435. (if (string-match "::\\(.+\\)\\'" path)
  8436. (setq search (match-string 1 path)
  8437. path (substring path 0 (match-beginning 0)))))
  8438. (if (string-match "[*?{]" (file-name-nondirectory path))
  8439. (dired path)
  8440. (org-open-file path arg line search)))
  8441. ((string= type "shell")
  8442. (let ((cmd path))
  8443. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8444. (string-match org-confirm-shell-link-not-regexp cmd))
  8445. (not org-confirm-shell-link-function)
  8446. (funcall org-confirm-shell-link-function
  8447. (format "Execute \"%s\" in shell? "
  8448. (org-add-props cmd nil
  8449. 'face 'org-warning))))
  8450. (progn
  8451. (message "Executing %s" cmd)
  8452. (shell-command cmd))
  8453. (error "Abort"))))
  8454. ((string= type "elisp")
  8455. (let ((cmd path))
  8456. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8457. (string-match org-confirm-elisp-link-not-regexp cmd))
  8458. (not org-confirm-elisp-link-function)
  8459. (funcall org-confirm-elisp-link-function
  8460. (format "Execute \"%s\" as elisp? "
  8461. (org-add-props cmd nil
  8462. 'face 'org-warning))))
  8463. (message "%s => %s" cmd
  8464. (if (equal (string-to-char cmd) ?\()
  8465. (eval (read cmd))
  8466. (call-interactively (read cmd))))
  8467. (error "Abort"))))
  8468. ((and (string= type "thisfile")
  8469. (run-hook-with-args-until-success
  8470. 'org-open-link-functions path)))
  8471. ((string= type "thisfile")
  8472. (if arg
  8473. (switch-to-buffer-other-window
  8474. (org-get-buffer-for-internal-link (current-buffer)))
  8475. (org-mark-ring-push))
  8476. (let ((cmd `(org-link-search
  8477. ,path
  8478. ,(cond ((equal arg '(4)) ''occur)
  8479. ((equal arg '(16)) ''org-occur)
  8480. (t nil))
  8481. ,pos)))
  8482. (condition-case nil (eval cmd)
  8483. (error (progn (widen) (eval cmd))))))
  8484. (t
  8485. (browse-url-at-point)))))))
  8486. (move-marker org-open-link-marker nil)
  8487. (run-hook-with-args 'org-follow-link-hook)))
  8488. (defun org-offer-links-in-entry (&optional nth zero)
  8489. "Offer links in the current entry and follow the selected link.
  8490. If there is only one link, follow it immediately as well.
  8491. If NTH is an integer, immediately pick the NTH link found.
  8492. If ZERO is a string, check also this string for a link, and if
  8493. there is one, offer it as link number zero."
  8494. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8495. "\\(" org-angle-link-re "\\)\\|"
  8496. "\\(" org-plain-link-re "\\)"))
  8497. (cnt ?0)
  8498. (in-emacs (if (integerp nth) nil nth))
  8499. have-zero end links link c)
  8500. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8501. (push (match-string 0 zero) links)
  8502. (setq cnt (1- cnt) have-zero t))
  8503. (save-excursion
  8504. (org-back-to-heading t)
  8505. (setq end (save-excursion (outline-next-heading) (point)))
  8506. (while (re-search-forward re end t)
  8507. (push (match-string 0) links))
  8508. (setq links (org-uniquify (reverse links))))
  8509. (cond
  8510. ((null links)
  8511. (message "No links"))
  8512. ((equal (length links) 1)
  8513. (setq link (list (car links))))
  8514. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8515. (setq link (nth (if have-zero nth (1- nth)) links)))
  8516. (t ; we have to select a link
  8517. (save-excursion
  8518. (save-window-excursion
  8519. (delete-other-windows)
  8520. (with-output-to-temp-buffer "*Select Link*"
  8521. (mapc (lambda (l)
  8522. (if (not (string-match org-bracket-link-regexp l))
  8523. (princ (format "[%c] %s\n" (incf cnt)
  8524. (org-remove-angle-brackets l)))
  8525. (if (match-end 3)
  8526. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8527. (match-string 3 l) (match-string 1 l)))
  8528. (princ (format "[%c] %s\n" (incf cnt)
  8529. (match-string 1 l))))))
  8530. links))
  8531. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8532. (message "Select link to open, RET to open all:")
  8533. (setq c (read-char-exclusive))
  8534. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8535. (when (equal c ?q) (error "Abort"))
  8536. (if (equal c ?\C-m)
  8537. (setq link links)
  8538. (setq nth (- c ?0))
  8539. (if have-zero (setq nth (1+ nth)))
  8540. (unless (and (integerp nth) (>= (length links) nth))
  8541. (error "Invalid link selection"))
  8542. (setq link (list (nth (1- nth) links))))))
  8543. (if link
  8544. (let ((buf (current-buffer)))
  8545. (dolist (l link)
  8546. (org-open-link-from-string l in-emacs buf))
  8547. t)
  8548. nil)))
  8549. ;; Add special file links that specify the way of opening
  8550. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8551. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8552. (defun org-open-file-with-system (path)
  8553. "Open file at PATH using the system way of opening it."
  8554. (org-open-file path 'system))
  8555. (defun org-open-file-with-emacs (path)
  8556. "Open file at PATH in Emacs."
  8557. (org-open-file path 'emacs))
  8558. (defun org-remove-file-link-modifiers ()
  8559. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8560. (goto-char (point-min))
  8561. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8562. (org-if-unprotected
  8563. (replace-match "file:" t t))))
  8564. (eval-after-load "org-exp"
  8565. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8566. 'org-remove-file-link-modifiers))
  8567. ;;;; Time estimates
  8568. (defun org-get-effort (&optional pom)
  8569. "Get the effort estimate for the current entry."
  8570. (org-entry-get pom org-effort-property))
  8571. ;;; File search
  8572. (defvar org-create-file-search-functions nil
  8573. "List of functions to construct the right search string for a file link.
  8574. These functions are called in turn with point at the location to
  8575. which the link should point.
  8576. A function in the hook should first test if it would like to
  8577. handle this file type, for example by checking the `major-mode'
  8578. or the file extension. If it decides not to handle this file, it
  8579. should just return nil to give other functions a chance. If it
  8580. does handle the file, it must return the search string to be used
  8581. when following the link. The search string will be part of the
  8582. file link, given after a double colon, and `org-open-at-point'
  8583. will automatically search for it. If special measures must be
  8584. taken to make the search successful, another function should be
  8585. added to the companion hook `org-execute-file-search-functions',
  8586. which see.
  8587. A function in this hook may also use `setq' to set the variable
  8588. `description' to provide a suggestion for the descriptive text to
  8589. be used for this link when it gets inserted into an Org-mode
  8590. buffer with \\[org-insert-link].")
  8591. (defvar org-execute-file-search-functions nil
  8592. "List of functions to execute a file search triggered by a link.
  8593. Functions added to this hook must accept a single argument, the
  8594. search string that was part of the file link, the part after the
  8595. double colon. The function must first check if it would like to
  8596. handle this search, for example by checking the `major-mode' or
  8597. the file extension. If it decides not to handle this search, it
  8598. should just return nil to give other functions a chance. If it
  8599. does handle the search, it must return a non-nil value to keep
  8600. other functions from trying.
  8601. Each function can access the current prefix argument through the
  8602. variable `current-prefix-argument'. Note that a single prefix is
  8603. used to force opening a link in Emacs, so it may be good to only
  8604. use a numeric or double prefix to guide the search function.
  8605. In case this is needed, a function in this hook can also restore
  8606. the window configuration before `org-open-at-point' was called using:
  8607. (set-window-configuration org-window-config-before-follow-link)")
  8608. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8609. (defun org-link-search (s &optional type avoid-pos)
  8610. "Search for a link search option.
  8611. If S is surrounded by forward slashes, it is interpreted as a
  8612. regular expression. In org-mode files, this will create an `org-occur'
  8613. sparse tree. In ordinary files, `occur' will be used to list matches.
  8614. If the current buffer is in `dired-mode', grep will be used to search
  8615. in all files. If AVOID-POS is given, ignore matches near that position."
  8616. (let ((case-fold-search t)
  8617. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8618. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8619. (append '(("") (" ") ("\t") ("\n"))
  8620. org-emphasis-alist)
  8621. "\\|") "\\)"))
  8622. (pos (point))
  8623. (pre nil) (post nil)
  8624. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8625. (cond
  8626. ;; First check if there are any special search functions
  8627. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8628. ;; Now try the builtin stuff
  8629. ((and (equal (string-to-char s0) ?#)
  8630. (> (length s0) 1)
  8631. (save-excursion
  8632. (goto-char (point-min))
  8633. (and
  8634. (re-search-forward
  8635. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8636. (setq type 'dedicated
  8637. pos (match-beginning 0))))
  8638. ;; There is an exact target for this
  8639. (goto-char pos)
  8640. (org-back-to-heading t)))
  8641. ((save-excursion
  8642. (goto-char (point-min))
  8643. (and
  8644. (re-search-forward
  8645. (concat "<<" (regexp-quote s0) ">>") nil t)
  8646. (setq type 'dedicated
  8647. pos (match-beginning 0))))
  8648. ;; There is an exact target for this
  8649. (goto-char pos))
  8650. ((and (string-match "^(\\(.*\\))$" s0)
  8651. (save-excursion
  8652. (goto-char (point-min))
  8653. (and
  8654. (re-search-forward
  8655. (concat "[^[]" (regexp-quote
  8656. (format org-coderef-label-format
  8657. (match-string 1 s0))))
  8658. nil t)
  8659. (setq type 'dedicated
  8660. pos (1+ (match-beginning 0))))))
  8661. ;; There is a coderef target for this
  8662. (goto-char pos))
  8663. ((string-match "^/\\(.*\\)/$" s)
  8664. ;; A regular expression
  8665. (cond
  8666. ((org-mode-p)
  8667. (org-occur (match-string 1 s)))
  8668. ;;((eq major-mode 'dired-mode)
  8669. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8670. (t (org-do-occur (match-string 1 s)))))
  8671. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8672. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8673. (goto-char (point-min))
  8674. (cond
  8675. ((let (case-fold-search)
  8676. (re-search-forward (format org-complex-heading-regexp-format
  8677. (regexp-quote s))
  8678. nil t))
  8679. ;; OK, found a match
  8680. (setq type 'dedicated)
  8681. (goto-char (match-beginning 0)))
  8682. ((and (not org-link-search-inhibit-query)
  8683. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8684. (y-or-n-p "No match - create this as a new heading? "))
  8685. (goto-char (point-max))
  8686. (or (bolp) (newline))
  8687. (insert "* " s "\n")
  8688. (beginning-of-line 0))
  8689. (t
  8690. (goto-char pos)
  8691. (error "No match"))))
  8692. (t
  8693. ;; A normal search string
  8694. (when (equal (string-to-char s) ?*)
  8695. ;; Anchor on headlines, post may include tags.
  8696. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8697. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8698. s (substring s 1)))
  8699. (remove-text-properties
  8700. 0 (length s)
  8701. '(face nil mouse-face nil keymap nil fontified nil) s)
  8702. ;; Make a series of regular expressions to find a match
  8703. (setq words (org-split-string s "[ \n\r\t]+")
  8704. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8705. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8706. "\\)" markers)
  8707. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8708. re2a (concat "[ \t\r\n]" re2a_)
  8709. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8710. re4 (concat "[^a-zA-Z_]" re4_)
  8711. re1 (concat pre re2 post)
  8712. re3 (concat pre (if pre re4_ re4) post)
  8713. re5 (concat pre ".*" re4)
  8714. re2 (concat pre re2)
  8715. re2a (concat pre (if pre re2a_ re2a))
  8716. re4 (concat pre (if pre re4_ re4))
  8717. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8718. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8719. re5 "\\)"
  8720. ))
  8721. (cond
  8722. ((eq type 'org-occur) (org-occur reall))
  8723. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8724. (t (goto-char (point-min))
  8725. (setq type 'fuzzy)
  8726. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8727. (org-search-not-self 1 re1 nil t)
  8728. (org-search-not-self 1 re2 nil t)
  8729. (org-search-not-self 1 re2a nil t)
  8730. (org-search-not-self 1 re3 nil t)
  8731. (org-search-not-self 1 re4 nil t)
  8732. (org-search-not-self 1 re5 nil t)
  8733. )
  8734. (goto-char (match-beginning 1))
  8735. (goto-char pos)
  8736. (error "No match"))))))
  8737. (and (org-mode-p) (org-show-context 'link-search))
  8738. type))
  8739. (defun org-search-not-self (group &rest args)
  8740. "Execute `re-search-forward', but only accept matches that do not
  8741. enclose the position of `org-open-link-marker'."
  8742. (let ((m org-open-link-marker))
  8743. (catch 'exit
  8744. (while (apply 're-search-forward args)
  8745. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8746. (goto-char (match-end group))
  8747. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8748. (> (match-beginning 0) (marker-position m))
  8749. (< (match-end 0) (marker-position m)))
  8750. (save-match-data
  8751. (or (not (org-in-regexp
  8752. org-bracket-link-analytic-regexp 1))
  8753. (not (match-end 4)) ; no description
  8754. (and (<= (match-beginning 4) (point))
  8755. (>= (match-end 4) (point))))))
  8756. (throw 'exit (point))))))))
  8757. (defun org-get-buffer-for-internal-link (buffer)
  8758. "Return a buffer to be used for displaying the link target of internal links."
  8759. (cond
  8760. ((not org-display-internal-link-with-indirect-buffer)
  8761. buffer)
  8762. ((string-match "(Clone)$" (buffer-name buffer))
  8763. (message "Buffer is already a clone, not making another one")
  8764. ;; we also do not modify visibility in this case
  8765. buffer)
  8766. (t ; make a new indirect buffer for displaying the link
  8767. (let* ((bn (buffer-name buffer))
  8768. (ibn (concat bn "(Clone)"))
  8769. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8770. (with-current-buffer ib (org-overview))
  8771. ib))))
  8772. (defun org-do-occur (regexp &optional cleanup)
  8773. "Call the Emacs command `occur'.
  8774. If CLEANUP is non-nil, remove the printout of the regular expression
  8775. in the *Occur* buffer. This is useful if the regex is long and not useful
  8776. to read."
  8777. (occur regexp)
  8778. (when cleanup
  8779. (let ((cwin (selected-window)) win beg end)
  8780. (when (setq win (get-buffer-window "*Occur*"))
  8781. (select-window win))
  8782. (goto-char (point-min))
  8783. (when (re-search-forward "match[a-z]+" nil t)
  8784. (setq beg (match-end 0))
  8785. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8786. (setq end (1- (match-beginning 0)))))
  8787. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8788. (goto-char (point-min))
  8789. (select-window cwin))))
  8790. ;;; The mark ring for links jumps
  8791. (defvar org-mark-ring nil
  8792. "Mark ring for positions before jumps in Org-mode.")
  8793. (defvar org-mark-ring-last-goto nil
  8794. "Last position in the mark ring used to go back.")
  8795. ;; Fill and close the ring
  8796. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8797. (loop for i from 1 to org-mark-ring-length do
  8798. (push (make-marker) org-mark-ring))
  8799. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8800. org-mark-ring)
  8801. (defun org-mark-ring-push (&optional pos buffer)
  8802. "Put the current position or POS into the mark ring and rotate it."
  8803. (interactive)
  8804. (setq pos (or pos (point)))
  8805. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8806. (move-marker (car org-mark-ring)
  8807. (or pos (point))
  8808. (or buffer (current-buffer)))
  8809. (message "%s"
  8810. (substitute-command-keys
  8811. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8812. (defun org-mark-ring-goto (&optional n)
  8813. "Jump to the previous position in the mark ring.
  8814. With prefix arg N, jump back that many stored positions. When
  8815. called several times in succession, walk through the entire ring.
  8816. Org-mode commands jumping to a different position in the current file,
  8817. or to another Org-mode file, automatically push the old position
  8818. onto the ring."
  8819. (interactive "p")
  8820. (let (p m)
  8821. (if (eq last-command this-command)
  8822. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8823. (setq p org-mark-ring))
  8824. (setq org-mark-ring-last-goto p)
  8825. (setq m (car p))
  8826. (switch-to-buffer (marker-buffer m))
  8827. (goto-char m)
  8828. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8829. (defun org-remove-angle-brackets (s)
  8830. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8831. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8832. s)
  8833. (defun org-add-angle-brackets (s)
  8834. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8835. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8836. s)
  8837. (defun org-remove-double-quotes (s)
  8838. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8839. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8840. s)
  8841. ;;; Following specific links
  8842. (defun org-follow-timestamp-link ()
  8843. (cond
  8844. ((org-at-date-range-p t)
  8845. (let ((org-agenda-start-on-weekday)
  8846. (t1 (match-string 1))
  8847. (t2 (match-string 2)))
  8848. (setq t1 (time-to-days (org-time-string-to-time t1))
  8849. t2 (time-to-days (org-time-string-to-time t2)))
  8850. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8851. ((org-at-timestamp-p t)
  8852. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8853. (substring (match-string 1) 0 10)))
  8854. 1))
  8855. (t (error "This should not happen"))))
  8856. ;;; Following file links
  8857. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  8858. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  8859. (declare-function mailcap-mime-info
  8860. "mailcap" (string &optional request no-decode))
  8861. (defvar org-wait nil)
  8862. (defun org-open-file (path &optional in-emacs line search)
  8863. "Open the file at PATH.
  8864. First, this expands any special file name abbreviations. Then the
  8865. configuration variable `org-file-apps' is checked if it contains an
  8866. entry for this file type, and if yes, the corresponding command is launched.
  8867. If no application is found, Emacs simply visits the file.
  8868. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8869. With a double \\[universal-argument] \\[universal-argument] \
  8870. prefix arg, Org tries to avoid opening in Emacs
  8871. and to use an external application to visit the file.
  8872. Optional LINE specifies a line to go to, optional SEARCH a string
  8873. to search for. If LINE or SEARCH is given, the file will be
  8874. opened in Emacs, unless an entry from org-file-apps that makes
  8875. use of groups in a regexp matches.
  8876. If the file does not exist, an error is thrown."
  8877. (let* ((file (if (equal path "")
  8878. buffer-file-name
  8879. (substitute-in-file-name (expand-file-name path))))
  8880. (file-apps (append org-file-apps (org-default-apps)))
  8881. (apps (org-remove-if
  8882. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8883. (apps-dlink (org-remove-if-not
  8884. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8885. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8886. (dirp (if remp nil (file-directory-p file)))
  8887. (file (if (and dirp org-open-directory-means-index-dot-org)
  8888. (concat (file-name-as-directory file) "index.org")
  8889. file))
  8890. (a-m-a-p (assq 'auto-mode apps))
  8891. (dfile (downcase file))
  8892. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8893. (link (cond ((and (eq line nil)
  8894. (eq search nil))
  8895. file)
  8896. (line
  8897. (concat file "::" (number-to-string line)))
  8898. (search
  8899. (concat file "::" search))))
  8900. (dlink (downcase link))
  8901. (old-buffer (current-buffer))
  8902. (old-pos (point))
  8903. (old-mode major-mode)
  8904. ext cmd link-match-data)
  8905. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8906. (setq ext (match-string 1 dfile))
  8907. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8908. (setq ext (match-string 1 dfile))))
  8909. (cond
  8910. ((member in-emacs '((16) system))
  8911. (setq cmd (cdr (assoc 'system apps))))
  8912. (in-emacs (setq cmd 'emacs))
  8913. (t
  8914. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8915. (and dirp (cdr (assoc 'directory apps)))
  8916. ; first, try matching against apps-dlink
  8917. ; if we get a match here, store the match data for later
  8918. (let ((match (assoc-default dlink apps-dlink
  8919. 'string-match)))
  8920. (if match
  8921. (progn (setq link-match-data (match-data))
  8922. match)
  8923. (progn (setq in-emacs (or in-emacs line search))
  8924. nil))) ; if we have no match in apps-dlink,
  8925. ; always open the file in emacs if line or search
  8926. ; is given (for backwards compatibility)
  8927. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8928. 'string-match)
  8929. (cdr (assoc ext apps))
  8930. (cdr (assoc t apps))))))
  8931. (when (eq cmd 'system)
  8932. (setq cmd (cdr (assoc 'system apps))))
  8933. (when (eq cmd 'default)
  8934. (setq cmd (cdr (assoc t apps))))
  8935. (when (eq cmd 'mailcap)
  8936. (require 'mailcap)
  8937. (mailcap-parse-mailcaps)
  8938. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8939. (command (mailcap-mime-info mime-type)))
  8940. (if (stringp command)
  8941. (setq cmd command)
  8942. (setq cmd 'emacs))))
  8943. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8944. (not (file-exists-p file))
  8945. (not org-open-non-existing-files))
  8946. (error "No such file: %s" file))
  8947. (cond
  8948. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8949. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8950. (while (string-match "['\"]%s['\"]" cmd)
  8951. (setq cmd (replace-match "%s" t t cmd)))
  8952. (while (string-match "%s" cmd)
  8953. (setq cmd (replace-match
  8954. (save-match-data
  8955. (shell-quote-argument
  8956. (convert-standard-filename file)))
  8957. t t cmd)))
  8958. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8959. (save-match-data
  8960. (let ((match-index 1)
  8961. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8962. (set-match-data link-match-data)
  8963. (while (<= match-index number-of-groups)
  8964. (let ((regex (concat "%" (number-to-string match-index)))
  8965. (replace-with (match-string match-index dlink)))
  8966. (while (string-match regex cmd)
  8967. (setq cmd (replace-match replace-with t t cmd))))
  8968. (setq match-index (+ match-index 1)))))
  8969. (save-window-excursion
  8970. (start-process-shell-command cmd nil cmd)
  8971. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8972. ))
  8973. ((or (stringp cmd)
  8974. (eq cmd 'emacs))
  8975. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8976. (widen)
  8977. (if line (org-goto-line line)
  8978. (if search (org-link-search search))))
  8979. ((consp cmd)
  8980. (let ((file (convert-standard-filename file)))
  8981. (save-match-data
  8982. (set-match-data link-match-data)
  8983. (eval cmd))))
  8984. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8985. (and (org-mode-p) (eq old-mode 'org-mode)
  8986. (or (not (equal old-buffer (current-buffer)))
  8987. (not (equal old-pos (point))))
  8988. (org-mark-ring-push old-pos old-buffer))))
  8989. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8990. "This function returns non-nil if `entry' uses a regular
  8991. expression which should be matched against the whole link by
  8992. org-open-file.
  8993. It assumes that is the case when the entry uses a regular
  8994. expression which has at least one grouping construct and the
  8995. action is either a lisp form or a command string containing
  8996. '%1', i.e. using at least one subexpression match as a
  8997. parameter."
  8998. (let ((selector (car entry))
  8999. (action (cdr entry)))
  9000. (if (stringp selector)
  9001. (and (> (regexp-opt-depth selector) 0)
  9002. (or (and (stringp action)
  9003. (string-match "%[0-9]" action))
  9004. (consp action)))
  9005. nil)))
  9006. (defun org-default-apps ()
  9007. "Return the default applications for this operating system."
  9008. (cond
  9009. ((eq system-type 'darwin)
  9010. org-file-apps-defaults-macosx)
  9011. ((eq system-type 'windows-nt)
  9012. org-file-apps-defaults-windowsnt)
  9013. (t org-file-apps-defaults-gnu)))
  9014. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9015. "Convert extensions to regular expressions in the cars of LIST.
  9016. Also, weed out any non-string entries, because the return value is used
  9017. only for regexp matching.
  9018. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9019. point to the symbol `emacs', indicating that the file should
  9020. be opened in Emacs."
  9021. (append
  9022. (delq nil
  9023. (mapcar (lambda (x)
  9024. (if (not (stringp (car x)))
  9025. nil
  9026. (if (string-match "\\W" (car x))
  9027. x
  9028. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9029. list))
  9030. (if add-auto-mode
  9031. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9032. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9033. (defun org-file-remote-p (file)
  9034. "Test whether FILE specifies a location on a remote system.
  9035. Return non-nil if the location is indeed remote.
  9036. For example, the filename \"/user@host:/foo\" specifies a location
  9037. on the system \"/user@host:\"."
  9038. (cond ((fboundp 'file-remote-p)
  9039. (file-remote-p file))
  9040. ((fboundp 'tramp-handle-file-remote-p)
  9041. (tramp-handle-file-remote-p file))
  9042. ((and (boundp 'ange-ftp-name-format)
  9043. (string-match (car ange-ftp-name-format) file))
  9044. t)
  9045. (t nil)))
  9046. ;;;; Refiling
  9047. (defun org-get-org-file ()
  9048. "Read a filename, with default directory `org-directory'."
  9049. (let ((default (or org-default-notes-file remember-data-file)))
  9050. (read-file-name (format "File name [%s]: " default)
  9051. (file-name-as-directory org-directory)
  9052. default)))
  9053. (defun org-notes-order-reversed-p ()
  9054. "Check if the current file should receive notes in reversed order."
  9055. (cond
  9056. ((not org-reverse-note-order) nil)
  9057. ((eq t org-reverse-note-order) t)
  9058. ((not (listp org-reverse-note-order)) nil)
  9059. (t (catch 'exit
  9060. (let ((all org-reverse-note-order)
  9061. entry)
  9062. (while (setq entry (pop all))
  9063. (if (string-match (car entry) buffer-file-name)
  9064. (throw 'exit (cdr entry))))
  9065. nil)))))
  9066. (defvar org-refile-target-table nil
  9067. "The list of refile targets, created by `org-refile'.")
  9068. (defvar org-agenda-new-buffers nil
  9069. "Buffers created to visit agenda files.")
  9070. (defvar org-refile-cache nil
  9071. "Cache for refile targets.")
  9072. (defvar org-refile-markers nil
  9073. "All the markers used for caching refile locations.")
  9074. (defun org-refile-marker (pos)
  9075. "Get a new refile marker, but only if caching is in use."
  9076. (if (not org-refile-use-cache)
  9077. pos
  9078. (let ((m (make-marker)))
  9079. (move-marker m pos)
  9080. (push m org-refile-markers)
  9081. m)))
  9082. (defun org-refile-cache-clear ()
  9083. "Clear the refile cache and disable all the markers."
  9084. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9085. (setq org-refile-markers nil)
  9086. (setq org-refile-cache nil)
  9087. (message "Refile cache has been cleared"))
  9088. (defun org-refile-cache-check-set (set)
  9089. "Check if all the markers in the cache still have live buffers."
  9090. (let (marker)
  9091. (catch 'exit
  9092. (while (and set (setq marker (nth 3 (pop set))))
  9093. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9094. (when (and marker (null (marker-buffer marker)))
  9095. (message "not found") (sit-for 3)
  9096. (throw 'exit nil)))
  9097. t)))
  9098. (defun org-refile-cache-put (set &rest identifiers)
  9099. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9100. (let* ((key (sha1 (prin1-to-string identifiers)))
  9101. (entry (assoc key org-refile-cache)))
  9102. (if entry
  9103. (setcdr entry set)
  9104. (push (cons key set) org-refile-cache))))
  9105. (defun org-refile-cache-get (&rest identifiers)
  9106. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9107. (cond
  9108. ((not org-refile-cache) nil)
  9109. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9110. (t
  9111. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9112. org-refile-cache))))
  9113. (and set (org-refile-cache-check-set set) set)))))
  9114. (defun org-refile-get-targets (&optional default-buffer)
  9115. "Produce a table with refile targets."
  9116. (let ((case-fold-search nil)
  9117. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9118. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9119. targets tgs txt re files f desc descre fast-path-p level pos0)
  9120. (message "Getting targets...")
  9121. (with-current-buffer (or default-buffer (current-buffer))
  9122. (while (setq entry (pop entries))
  9123. (setq files (car entry) desc (cdr entry))
  9124. (setq fast-path-p nil)
  9125. (cond
  9126. ((null files) (setq files (list (current-buffer))))
  9127. ((eq files 'org-agenda-files)
  9128. (setq files (org-agenda-files 'unrestricted)))
  9129. ((and (symbolp files) (fboundp files))
  9130. (setq files (funcall files)))
  9131. ((and (symbolp files) (boundp files))
  9132. (setq files (symbol-value files))))
  9133. (if (stringp files) (setq files (list files)))
  9134. (cond
  9135. ((eq (car desc) :tag)
  9136. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9137. ((eq (car desc) :todo)
  9138. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9139. ((eq (car desc) :regexp)
  9140. (setq descre (cdr desc)))
  9141. ((eq (car desc) :level)
  9142. (setq descre (concat "^\\*\\{" (number-to-string
  9143. (if org-odd-levels-only
  9144. (1- (* 2 (cdr desc)))
  9145. (cdr desc)))
  9146. "\\}[ \t]")))
  9147. ((eq (car desc) :maxlevel)
  9148. (setq fast-path-p t)
  9149. (setq descre (concat "^\\*\\{1," (number-to-string
  9150. (if org-odd-levels-only
  9151. (1- (* 2 (cdr desc)))
  9152. (cdr desc)))
  9153. "\\}[ \t]")))
  9154. (t (error "Bad refiling target description %s" desc)))
  9155. (while (setq f (pop files))
  9156. (with-current-buffer
  9157. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9158. (or
  9159. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9160. (progn
  9161. (if (bufferp f) (setq f (buffer-file-name
  9162. (buffer-base-buffer f))))
  9163. (setq f (and f (expand-file-name f)))
  9164. (if (eq org-refile-use-outline-path 'file)
  9165. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9166. (save-excursion
  9167. (save-restriction
  9168. (widen)
  9169. (goto-char (point-min))
  9170. (while (re-search-forward descre nil t)
  9171. (goto-char (setq pos0 (point-at-bol)))
  9172. (catch 'next
  9173. (when org-refile-target-verify-function
  9174. (save-match-data
  9175. (or (funcall org-refile-target-verify-function)
  9176. (throw 'next t))))
  9177. (when (looking-at org-complex-heading-regexp)
  9178. (setq level (org-reduced-level
  9179. (- (match-end 1) (match-beginning 1)))
  9180. txt (org-link-display-format (match-string 4))
  9181. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9182. re (format org-complex-heading-regexp-format
  9183. (regexp-quote (match-string 4))))
  9184. (when org-refile-use-outline-path
  9185. (setq txt (mapconcat
  9186. 'org-protect-slash
  9187. (append
  9188. (if (eq org-refile-use-outline-path
  9189. 'file)
  9190. (list (file-name-nondirectory
  9191. (buffer-file-name
  9192. (buffer-base-buffer))))
  9193. (if (eq org-refile-use-outline-path
  9194. 'full-file-path)
  9195. (list (buffer-file-name
  9196. (buffer-base-buffer)))))
  9197. (org-get-outline-path fast-path-p
  9198. level txt)
  9199. (list txt))
  9200. "/")))
  9201. (push (list txt f re (org-refile-marker (point)))
  9202. tgs)))
  9203. (when (= (point) pos0)
  9204. ;; verification function has not moved point
  9205. (goto-char (point-at-eol))))))))
  9206. (when org-refile-use-cache
  9207. (org-refile-cache-put tgs (buffer-file-name) descre))
  9208. (setq targets (append tgs targets))
  9209. ))))
  9210. (message "Getting targets...done")
  9211. (nreverse targets)))
  9212. (defun org-protect-slash (s)
  9213. (while (string-match "/" s)
  9214. (setq s (replace-match "\\" t t s)))
  9215. s)
  9216. (defvar org-olpa (make-vector 20 nil))
  9217. (defun org-get-outline-path (&optional fastp level heading)
  9218. "Return the outline path to the current entry, as a list.
  9219. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9220. routine which makes outline path derivations for an entire file,
  9221. avoiding backtracing. Refile target collection makes use of that."
  9222. (if fastp
  9223. (progn
  9224. (if (> level 19)
  9225. (error "Outline path failure, more than 19 levels"))
  9226. (loop for i from level upto 19 do
  9227. (aset org-olpa i nil))
  9228. (prog1
  9229. (delq nil (append org-olpa nil))
  9230. (aset org-olpa level heading)))
  9231. (let (rtn case-fold-search)
  9232. (save-excursion
  9233. (save-restriction
  9234. (widen)
  9235. (while (org-up-heading-safe)
  9236. (when (looking-at org-complex-heading-regexp)
  9237. (push (org-match-string-no-properties 4) rtn)))
  9238. rtn)))))
  9239. (defun org-format-outline-path (path &optional width prefix)
  9240. "Format the outline path PATH for display.
  9241. Width is the maximum number of characters that is available.
  9242. Prefix is a prefix to be included in the returned string,
  9243. such as the file name."
  9244. (setq width (or width 79))
  9245. (if prefix (setq width (- width (length prefix))))
  9246. (if (not path)
  9247. (or prefix "")
  9248. (let* ((nsteps (length path))
  9249. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9250. (maxwidth (if (<= total-width width)
  9251. 10000 ;; everything fits
  9252. ;; we need to shorten the level headings
  9253. (/ (- width nsteps) nsteps)))
  9254. (org-odd-levels-only nil)
  9255. (n 0)
  9256. (total (1+ (length prefix))))
  9257. (setq maxwidth (max maxwidth 10))
  9258. (concat prefix
  9259. (mapconcat
  9260. (lambda (h)
  9261. (setq n (1+ n))
  9262. (if (and (= n nsteps) (< maxwidth 10000))
  9263. (setq maxwidth (- total-width total)))
  9264. (if (< (length h) maxwidth)
  9265. (progn (setq total (+ total (length h) 1)) h)
  9266. (setq h (substring h 0 (- maxwidth 2))
  9267. total (+ total maxwidth 1))
  9268. (if (string-match "[ \t]+\\'" h)
  9269. (setq h (substring h 0 (match-beginning 0))))
  9270. (setq h (concat h "..")))
  9271. (org-add-props h nil 'face
  9272. (nth (% (1- n) org-n-level-faces)
  9273. org-level-faces))
  9274. h)
  9275. path "/")))))
  9276. (defun org-display-outline-path (&optional file current)
  9277. "Display the current outline path in the echo area."
  9278. (interactive "P")
  9279. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9280. (case-fold-search nil)
  9281. (path (and (org-mode-p) (org-get-outline-path))))
  9282. (if current (setq path (append path
  9283. (save-excursion
  9284. (org-back-to-heading t)
  9285. (if (looking-at org-complex-heading-regexp)
  9286. (list (match-string 4)))))))
  9287. (message "%s"
  9288. (org-format-outline-path
  9289. path
  9290. (1- (frame-width))
  9291. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9292. (defvar org-refile-history nil
  9293. "History for refiling operations.")
  9294. (defvar org-after-refile-insert-hook nil
  9295. "Hook run after `org-refile' has inserted its stuff at the new location.
  9296. Note that this is still *before* the stuff will be removed from
  9297. the *old* location.")
  9298. (defvar org-capture-last-stored-marker)
  9299. (defun org-refile (&optional goto default-buffer rfloc)
  9300. "Move the entry at point to another heading.
  9301. The list of target headings is compiled using the information in
  9302. `org-refile-targets', which see. This list is created before each use
  9303. and will therefore always be up-to-date.
  9304. At the target location, the entry is filed as a subitem of the target heading.
  9305. Depending on `org-reverse-note-order', the new subitem will either be the
  9306. first or the last subitem.
  9307. If there is an active region, all entries in that region will be moved.
  9308. However, the region must fulfill the requirement that the first heading
  9309. is the first one sets the top-level of the moved text - at most siblings
  9310. below it are allowed.
  9311. With prefix arg GOTO, the command will only visit the target location,
  9312. not actually move anything.
  9313. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9314. go to the location where the last refiling
  9315. operation has put the subtree.
  9316. With a prefix argument of `2', refile to the running clock.
  9317. RFLOC can be a refile location obtained in a different way.
  9318. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9319. If you are using target caching (see `org-refile-use-cache'),
  9320. You have to clear the target cache in order to find new targets.
  9321. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9322. (interactive "P")
  9323. (if (member goto '(0 (64)))
  9324. (org-refile-cache-clear)
  9325. (let* ((cbuf (current-buffer))
  9326. (regionp (org-region-active-p))
  9327. (region-start (and regionp (region-beginning)))
  9328. (region-end (and regionp (region-end)))
  9329. (region-length (and regionp (- region-end region-start)))
  9330. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9331. pos it nbuf file re level reversed)
  9332. (setq last-command nil)
  9333. (when regionp
  9334. (goto-char region-start)
  9335. (or (bolp) (goto-char (point-at-bol)))
  9336. (setq region-start (point))
  9337. (unless (org-kill-is-subtree-p
  9338. (buffer-substring region-start region-end))
  9339. (error "The region is not a (sequence of) subtree(s)")))
  9340. (if (equal goto '(16))
  9341. (org-refile-goto-last-stored)
  9342. (when (or
  9343. (and (equal goto 2)
  9344. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9345. (prog1
  9346. (setq it (list (or org-clock-heading "running clock")
  9347. (buffer-file-name
  9348. (marker-buffer org-clock-hd-marker))
  9349. ""
  9350. (marker-position org-clock-hd-marker)))
  9351. (setq goto nil)))
  9352. (setq it (or rfloc
  9353. (save-excursion
  9354. (org-refile-get-location
  9355. (if goto "Goto" "Refile to") default-buffer
  9356. org-refile-allow-creating-parent-nodes)))))
  9357. (setq file (nth 1 it)
  9358. re (nth 2 it)
  9359. pos (nth 3 it))
  9360. (if (and (not goto)
  9361. pos
  9362. (equal (buffer-file-name) file)
  9363. (if regionp
  9364. (and (>= pos region-start)
  9365. (<= pos region-end))
  9366. (and (>= pos (point))
  9367. (< pos (save-excursion
  9368. (org-end-of-subtree t t))))))
  9369. (error "Cannot refile to position inside the tree or region"))
  9370. (setq nbuf (or (find-buffer-visiting file)
  9371. (find-file-noselect file)))
  9372. (if goto
  9373. (progn
  9374. (switch-to-buffer nbuf)
  9375. (goto-char pos)
  9376. (org-show-context 'org-goto))
  9377. (if regionp
  9378. (progn
  9379. (org-kill-new (buffer-substring region-start region-end))
  9380. (org-save-markers-in-region region-start region-end))
  9381. (org-copy-subtree 1 nil t))
  9382. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9383. (find-file-noselect file)))
  9384. (setq reversed (org-notes-order-reversed-p))
  9385. (save-excursion
  9386. (save-restriction
  9387. (widen)
  9388. (if pos
  9389. (progn
  9390. (goto-char pos)
  9391. (looking-at outline-regexp)
  9392. (setq level (org-get-valid-level (funcall outline-level) 1))
  9393. (goto-char
  9394. (if reversed
  9395. (or (outline-next-heading) (point-max))
  9396. (or (save-excursion (org-get-next-sibling))
  9397. (org-end-of-subtree t t)
  9398. (point-max)))))
  9399. (setq level 1)
  9400. (if (not reversed)
  9401. (goto-char (point-max))
  9402. (goto-char (point-min))
  9403. (or (outline-next-heading) (goto-char (point-max)))))
  9404. (if (not (bolp)) (newline))
  9405. (org-paste-subtree level)
  9406. (when org-log-refile
  9407. (org-add-log-setup 'refile nil nil 'findpos
  9408. org-log-refile)
  9409. (unless (eq org-log-refile 'note)
  9410. (save-excursion (org-add-log-note))))
  9411. (and org-auto-align-tags (org-set-tags nil t))
  9412. (bookmark-set "org-refile-last-stored")
  9413. ;; If we are refiling for capture, make sure that the
  9414. ;; last-capture pointers point here
  9415. (when (org-bound-and-true-p org-refile-for-capture)
  9416. (bookmark-set "org-capture-last-stored-marker")
  9417. (move-marker org-capture-last-stored-marker (point)))
  9418. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9419. (run-hooks 'org-after-refile-insert-hook))))
  9420. (if regionp
  9421. (delete-region (point) (+ (point) region-length))
  9422. (org-cut-subtree))
  9423. (when (featurep 'org-inlinetask)
  9424. (org-inlinetask-remove-END-maybe))
  9425. (setq org-markers-to-move nil)
  9426. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9427. (defun org-refile-goto-last-stored ()
  9428. "Go to the location where the last refile was stored."
  9429. (interactive)
  9430. (bookmark-jump "org-refile-last-stored")
  9431. (message "This is the location of the last refile"))
  9432. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9433. "Prompt the user for a refile location, using PROMPT.
  9434. PROMPT should not be suffixed with a colon and a space, because
  9435. this function appends the default value from
  9436. `org-refile-history' automatically, if that is not empty."
  9437. (let ((org-refile-targets org-refile-targets)
  9438. (org-refile-use-outline-path org-refile-use-outline-path))
  9439. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  9440. (unless org-refile-target-table
  9441. (error "No refile targets"))
  9442. (let* ((prompt (concat prompt
  9443. (and (car org-refile-history)
  9444. (concat " (default " (car org-refile-history) ")"))
  9445. ": "))
  9446. (cbuf (current-buffer))
  9447. (partial-completion-mode nil)
  9448. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9449. (cfunc (if (and org-refile-use-outline-path
  9450. org-outline-path-complete-in-steps)
  9451. 'org-olpath-completing-read
  9452. 'org-icompleting-read))
  9453. (extra (if org-refile-use-outline-path "/" ""))
  9454. (filename (and cfn (expand-file-name cfn)))
  9455. (tbl (mapcar
  9456. (lambda (x)
  9457. (if (and (not (member org-refile-use-outline-path
  9458. '(file full-file-path)))
  9459. (not (equal filename (nth 1 x))))
  9460. (cons (concat (car x) extra " ("
  9461. (file-name-nondirectory (nth 1 x)) ")")
  9462. (cdr x))
  9463. (cons (concat (car x) extra) (cdr x))))
  9464. org-refile-target-table))
  9465. (completion-ignore-case t)
  9466. pa answ parent-target child parent old-hist)
  9467. (setq old-hist org-refile-history)
  9468. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9469. nil 'org-refile-history (car org-refile-history)))
  9470. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9471. (org-refile-check-position pa)
  9472. (if pa
  9473. (progn
  9474. (when (or (not org-refile-history)
  9475. (not (eq old-hist org-refile-history))
  9476. (not (equal (car pa) (car org-refile-history))))
  9477. (setq org-refile-history
  9478. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9479. org-refile-history
  9480. (cdr org-refile-history))))
  9481. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9482. (pop org-refile-history)))
  9483. pa)
  9484. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9485. (progn
  9486. (setq parent (match-string 1 answ)
  9487. child (match-string 2 answ))
  9488. (setq parent-target (or (assoc parent tbl)
  9489. (assoc (concat parent "/") tbl)))
  9490. (when (and parent-target
  9491. (or (eq new-nodes t)
  9492. (and (eq new-nodes 'confirm)
  9493. (y-or-n-p (format "Create new node \"%s\"? "
  9494. child)))))
  9495. (org-refile-new-child parent-target child)))
  9496. (error "Invalid target location")))))
  9497. (defun org-refile-check-position (refile-pointer)
  9498. "Check if the refile pointer matches the readline to which it points."
  9499. (let* ((file (nth 1 refile-pointer))
  9500. (re (nth 2 refile-pointer))
  9501. (pos (nth 3 refile-pointer))
  9502. buffer)
  9503. (when (org-string-nw-p re)
  9504. (setq buffer (if (markerp pos)
  9505. (marker-buffer pos)
  9506. (or (find-buffer-visiting file)
  9507. (find-file-noselect file))))
  9508. (with-current-buffer buffer
  9509. (save-excursion
  9510. (save-restriction
  9511. (widen)
  9512. (goto-char pos)
  9513. (beginning-of-line 1)
  9514. (unless (org-looking-at-p re)
  9515. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9516. (defun org-refile-new-child (parent-target child)
  9517. "Use refile target PARENT-TARGET to add new CHILD below it."
  9518. (unless parent-target
  9519. (error "Cannot find parent for new node"))
  9520. (let ((file (nth 1 parent-target))
  9521. (pos (nth 3 parent-target))
  9522. level)
  9523. (with-current-buffer (or (find-buffer-visiting file)
  9524. (find-file-noselect file))
  9525. (save-excursion
  9526. (save-restriction
  9527. (widen)
  9528. (if pos
  9529. (goto-char pos)
  9530. (goto-char (point-max))
  9531. (if (not (bolp)) (newline)))
  9532. (when (looking-at outline-regexp)
  9533. (setq level (funcall outline-level))
  9534. (org-end-of-subtree t t))
  9535. (org-back-over-empty-lines)
  9536. (insert "\n" (make-string
  9537. (if pos (org-get-valid-level level 1) 1) ?*)
  9538. " " child "\n")
  9539. (beginning-of-line 0)
  9540. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9541. (defun org-olpath-completing-read (prompt collection &rest args)
  9542. "Read an outline path like a file name."
  9543. (let ((thetable collection)
  9544. (org-completion-use-ido nil) ; does not work with ido.
  9545. (org-completion-use-iswitchb nil)) ; or iswitchb
  9546. (apply
  9547. 'org-icompleting-read prompt
  9548. (lambda (string predicate &optional flag)
  9549. (let (rtn r f (l (length string)))
  9550. (cond
  9551. ((eq flag nil)
  9552. ;; try completion
  9553. (try-completion string thetable))
  9554. ((eq flag t)
  9555. ;; all-completions
  9556. (setq rtn (all-completions string thetable predicate))
  9557. (mapcar
  9558. (lambda (x)
  9559. (setq r (substring x l))
  9560. (if (string-match " ([^)]*)$" x)
  9561. (setq f (match-string 0 x))
  9562. (setq f ""))
  9563. (if (string-match "/" r)
  9564. (concat string (substring r 0 (match-end 0)) f)
  9565. x))
  9566. rtn))
  9567. ((eq flag 'lambda)
  9568. ;; exact match?
  9569. (assoc string thetable)))
  9570. ))
  9571. args)))
  9572. ;;;; Dynamic blocks
  9573. (defun org-find-dblock (name)
  9574. "Find the first dynamic block with name NAME in the buffer.
  9575. If not found, stay at current position and return nil."
  9576. (let (pos)
  9577. (save-excursion
  9578. (goto-char (point-min))
  9579. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9580. nil t)
  9581. (match-beginning 0))))
  9582. (if pos (goto-char pos))
  9583. pos))
  9584. (defconst org-dblock-start-re
  9585. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9586. "Matches the start line of a dynamic block, with parameters.")
  9587. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9588. "Matches the end of a dynamic block.")
  9589. (defun org-create-dblock (plist)
  9590. "Create a dynamic block section, with parameters taken from PLIST.
  9591. PLIST must contain a :name entry which is used as name of the block."
  9592. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9593. (end-of-line 1)
  9594. (newline))
  9595. (let ((col (current-column))
  9596. (name (plist-get plist :name)))
  9597. (insert "#+BEGIN: " name)
  9598. (while plist
  9599. (if (eq (car plist) :name)
  9600. (setq plist (cddr plist))
  9601. (insert " " (prin1-to-string (pop plist)))))
  9602. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9603. (beginning-of-line -2)))
  9604. (defun org-prepare-dblock ()
  9605. "Prepare dynamic block for refresh.
  9606. This empties the block, puts the cursor at the insert position and returns
  9607. the property list including an extra property :name with the block name."
  9608. (unless (looking-at org-dblock-start-re)
  9609. (error "Not at a dynamic block"))
  9610. (let* ((begdel (1+ (match-end 0)))
  9611. (name (org-no-properties (match-string 1)))
  9612. (params (append (list :name name)
  9613. (read (concat "(" (match-string 3) ")")))))
  9614. (save-excursion
  9615. (beginning-of-line 1)
  9616. (skip-chars-forward " \t")
  9617. (setq params (plist-put params :indentation-column (current-column))))
  9618. (unless (re-search-forward org-dblock-end-re nil t)
  9619. (error "Dynamic block not terminated"))
  9620. (setq params
  9621. (append params
  9622. (list :content (buffer-substring
  9623. begdel (match-beginning 0)))))
  9624. (delete-region begdel (match-beginning 0))
  9625. (goto-char begdel)
  9626. (open-line 1)
  9627. params))
  9628. (defun org-map-dblocks (&optional command)
  9629. "Apply COMMAND to all dynamic blocks in the current buffer.
  9630. If COMMAND is not given, use `org-update-dblock'."
  9631. (let ((cmd (or command 'org-update-dblock)))
  9632. (save-excursion
  9633. (goto-char (point-min))
  9634. (while (re-search-forward org-dblock-start-re nil t)
  9635. (goto-char (match-beginning 0))
  9636. (save-excursion
  9637. (condition-case nil
  9638. (funcall cmd)
  9639. (error (message "Error during update of dynamic block"))))
  9640. (unless (re-search-forward org-dblock-end-re nil t)
  9641. (error "Dynamic block not terminated"))))))
  9642. (defun org-dblock-update (&optional arg)
  9643. "User command for updating dynamic blocks.
  9644. Update the dynamic block at point. With prefix ARG, update all dynamic
  9645. blocks in the buffer."
  9646. (interactive "P")
  9647. (if arg
  9648. (org-update-all-dblocks)
  9649. (or (looking-at org-dblock-start-re)
  9650. (org-beginning-of-dblock))
  9651. (org-update-dblock)))
  9652. (defun org-update-dblock ()
  9653. "Update the dynamic block at point.
  9654. This means to empty the block, parse for parameters and then call
  9655. the correct writing function."
  9656. (interactive)
  9657. (save-window-excursion
  9658. (let* ((pos (point))
  9659. (line (org-current-line))
  9660. (params (org-prepare-dblock))
  9661. (name (plist-get params :name))
  9662. (indent (plist-get params :indentation-column))
  9663. (cmd (intern (concat "org-dblock-write:" name))))
  9664. (message "Updating dynamic block `%s' at line %d..." name line)
  9665. (funcall cmd params)
  9666. (message "Updating dynamic block `%s' at line %d...done" name line)
  9667. (goto-char pos)
  9668. (when (and indent (> indent 0))
  9669. (setq indent (make-string indent ?\ ))
  9670. (save-excursion
  9671. (org-beginning-of-dblock)
  9672. (forward-line 1)
  9673. (while (not (looking-at org-dblock-end-re))
  9674. (insert indent)
  9675. (beginning-of-line 2))
  9676. (when (looking-at org-dblock-end-re)
  9677. (and (looking-at "[ \t]+")
  9678. (replace-match ""))
  9679. (insert indent)))))))
  9680. (defun org-beginning-of-dblock ()
  9681. "Find the beginning of the dynamic block at point.
  9682. Error if there is no such block at point."
  9683. (let ((pos (point))
  9684. beg)
  9685. (end-of-line 1)
  9686. (if (and (re-search-backward org-dblock-start-re nil t)
  9687. (setq beg (match-beginning 0))
  9688. (re-search-forward org-dblock-end-re nil t)
  9689. (> (match-end 0) pos))
  9690. (goto-char beg)
  9691. (goto-char pos)
  9692. (error "Not in a dynamic block"))))
  9693. (defun org-update-all-dblocks ()
  9694. "Update all dynamic blocks in the buffer.
  9695. This function can be used in a hook."
  9696. (interactive)
  9697. (when (org-mode-p)
  9698. (org-map-dblocks 'org-update-dblock)))
  9699. ;;;; Completion
  9700. (defconst org-additional-option-like-keywords
  9701. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9702. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9703. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9704. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9705. "BEGIN:" "END:"
  9706. "ORGTBL" "TBLFM:" "TBLNAME:"
  9707. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9708. "BEGIN_QUOTE" "END_QUOTE"
  9709. "BEGIN_VERSE" "END_VERSE"
  9710. "BEGIN_CENTER" "END_CENTER"
  9711. "BEGIN_SRC" "END_SRC"
  9712. "BEGIN_RESULT" "END_RESULT"
  9713. "SOURCE:" "SRCNAME:" "FUNCTION:"
  9714. "RESULTS:"
  9715. "HEADER:" "HEADERS:"
  9716. "BABEL:"
  9717. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9718. "CAPTION:" "LABEL:"
  9719. "SETUPFILE:"
  9720. "INCLUDE:"
  9721. "BIND:"
  9722. "MACRO:"))
  9723. (defcustom org-structure-template-alist
  9724. '(
  9725. ("s" "#+begin_src ?\n\n#+end_src"
  9726. "<src lang=\"?\">\n\n</src>")
  9727. ("e" "#+begin_example\n?\n#+end_example"
  9728. "<example>\n?\n</example>")
  9729. ("q" "#+begin_quote\n?\n#+end_quote"
  9730. "<quote>\n?\n</quote>")
  9731. ("v" "#+begin_verse\n?\n#+end_verse"
  9732. "<verse>\n?\n/verse>")
  9733. ("c" "#+begin_center\n?\n#+end_center"
  9734. "<center>\n?\n/center>")
  9735. ("l" "#+begin_latex\n?\n#+end_latex"
  9736. "<literal style=\"latex\">\n?\n</literal>")
  9737. ("L" "#+latex: "
  9738. "<literal style=\"latex\">?</literal>")
  9739. ("h" "#+begin_html\n?\n#+end_html"
  9740. "<literal style=\"html\">\n?\n</literal>")
  9741. ("H" "#+html: "
  9742. "<literal style=\"html\">?</literal>")
  9743. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9744. ("A" "#+ascii: ")
  9745. ("i" "#+index: ?"
  9746. "#+index: ?")
  9747. ("I" "#+include %file ?"
  9748. "<include file=%file markup=\"?\">")
  9749. )
  9750. "Structure completion elements.
  9751. This is a list of abbreviation keys and values. The value gets inserted
  9752. if you type `<' followed by the key and then press the completion key,
  9753. usually `M-TAB'. %file will be replaced by a file name after prompting
  9754. for the file using completion. The cursor will be placed at the position
  9755. of the `?` in the template.
  9756. There are two templates for each key, the first uses the original Org syntax,
  9757. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9758. the default when the /org-mtags.el/ module has been loaded. See also the
  9759. variable `org-mtags-prefer-muse-templates'.
  9760. This is an experimental feature, it is undecided if it is going to stay in."
  9761. :group 'org-completion
  9762. :type '(repeat
  9763. (string :tag "Key")
  9764. (string :tag "Template")
  9765. (string :tag "Muse Template")))
  9766. (defun org-try-structure-completion ()
  9767. "Try to complete a structure template before point.
  9768. This looks for strings like \"<e\" on an otherwise empty line and
  9769. expands them."
  9770. (let ((l (buffer-substring (point-at-bol) (point)))
  9771. a)
  9772. (when (and (looking-at "[ \t]*$")
  9773. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  9774. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9775. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9776. (match-beginning 1)) a)
  9777. t)))
  9778. (defun org-complete-expand-structure-template (start cell)
  9779. "Expand a structure template."
  9780. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9781. (rpl (nth (if musep 2 1) cell))
  9782. (ind ""))
  9783. (delete-region start (point))
  9784. (when (string-match "\\`#\\+" rpl)
  9785. (cond
  9786. ((bolp))
  9787. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9788. (setq ind (buffer-substring (point-at-bol) (point))))
  9789. (t (newline))))
  9790. (setq start (point))
  9791. (if (string-match "%file" rpl)
  9792. (setq rpl (replace-match
  9793. (concat
  9794. "\""
  9795. (save-match-data
  9796. (abbreviate-file-name (read-file-name "Include file: ")))
  9797. "\"")
  9798. t t rpl)))
  9799. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9800. (concat "\n" ind)))
  9801. (insert rpl)
  9802. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9803. ;;;; TODO, DEADLINE, Comments
  9804. (defun org-toggle-comment ()
  9805. "Change the COMMENT state of an entry."
  9806. (interactive)
  9807. (save-excursion
  9808. (org-back-to-heading)
  9809. (let (case-fold-search)
  9810. (if (looking-at (concat outline-regexp
  9811. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9812. (replace-match "" t t nil 1)
  9813. (if (looking-at outline-regexp)
  9814. (progn
  9815. (goto-char (match-end 0))
  9816. (insert org-comment-string " ")))))))
  9817. (defvar org-last-todo-state-is-todo nil
  9818. "This is non-nil when the last TODO state change led to a TODO state.
  9819. If the last change removed the TODO tag or switched to DONE, then
  9820. this is nil.")
  9821. (defvar org-setting-tags nil) ; dynamically skipped
  9822. (defvar org-todo-setup-filter-hook nil
  9823. "Hook for functions that pre-filter todo specs.
  9824. Each function takes a todo spec and returns either nil or the spec
  9825. transformed into canonical form." )
  9826. (defvar org-todo-get-default-hook nil
  9827. "Hook for functions that get a default item for todo.
  9828. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9829. nil or a string to be used for the todo mark." )
  9830. (defvar org-agenda-headline-snapshot-before-repeat)
  9831. (defun org-todo (&optional arg)
  9832. "Change the TODO state of an item.
  9833. The state of an item is given by a keyword at the start of the heading,
  9834. like
  9835. *** TODO Write paper
  9836. *** DONE Call mom
  9837. The different keywords are specified in the variable `org-todo-keywords'.
  9838. By default the available states are \"TODO\" and \"DONE\".
  9839. So for this example: when the item starts with TODO, it is changed to DONE.
  9840. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9841. DONE are present, add TODO at the beginning of the heading.
  9842. With \\[universal-argument] prefix arg, use completion to determine the new \
  9843. state.
  9844. With numeric prefix arg, switch to that state.
  9845. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9846. keywords (nextset).
  9847. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9848. For calling through lisp, arg is also interpreted in the following way:
  9849. 'none -> empty state
  9850. \"\"(empty string) -> switch to empty state
  9851. 'done -> switch to DONE
  9852. 'nextset -> switch to the next set of keywords
  9853. 'previousset -> switch to the previous set of keywords
  9854. \"WAITING\" -> switch to the specified keyword, but only if it
  9855. really is a member of `org-todo-keywords'."
  9856. (interactive "P")
  9857. (if (equal arg '(16)) (setq arg 'nextset))
  9858. (let ((org-blocker-hook org-blocker-hook)
  9859. (case-fold-search nil))
  9860. (when (equal arg '(64))
  9861. (setq arg nil org-blocker-hook nil))
  9862. (when (and org-blocker-hook
  9863. (or org-inhibit-blocking
  9864. (org-entry-get nil "NOBLOCKING")))
  9865. (setq org-blocker-hook nil))
  9866. (save-excursion
  9867. (catch 'exit
  9868. (org-back-to-heading t)
  9869. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9870. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9871. (looking-at " *"))
  9872. (let* ((match-data (match-data))
  9873. (startpos (point-at-bol))
  9874. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9875. (org-log-done org-log-done)
  9876. (org-log-repeat org-log-repeat)
  9877. (org-todo-log-states org-todo-log-states)
  9878. (this (match-string 1))
  9879. (hl-pos (match-beginning 0))
  9880. (head (org-get-todo-sequence-head this))
  9881. (ass (assoc head org-todo-kwd-alist))
  9882. (interpret (nth 1 ass))
  9883. (done-word (nth 3 ass))
  9884. (final-done-word (nth 4 ass))
  9885. (last-state (or this ""))
  9886. (completion-ignore-case t)
  9887. (member (member this org-todo-keywords-1))
  9888. (tail (cdr member))
  9889. (state (cond
  9890. ((and org-todo-key-trigger
  9891. (or (and (equal arg '(4))
  9892. (eq org-use-fast-todo-selection 'prefix))
  9893. (and (not arg) org-use-fast-todo-selection
  9894. (not (eq org-use-fast-todo-selection
  9895. 'prefix)))))
  9896. ;; Use fast selection
  9897. (org-fast-todo-selection))
  9898. ((and (equal arg '(4))
  9899. (or (not org-use-fast-todo-selection)
  9900. (not org-todo-key-trigger)))
  9901. ;; Read a state with completion
  9902. (org-icompleting-read
  9903. "State: " (mapcar (lambda(x) (list x))
  9904. org-todo-keywords-1)
  9905. nil t))
  9906. ((eq arg 'right)
  9907. (if this
  9908. (if tail (car tail) nil)
  9909. (car org-todo-keywords-1)))
  9910. ((eq arg 'left)
  9911. (if (equal member org-todo-keywords-1)
  9912. nil
  9913. (if this
  9914. (nth (- (length org-todo-keywords-1)
  9915. (length tail) 2)
  9916. org-todo-keywords-1)
  9917. (org-last org-todo-keywords-1))))
  9918. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9919. (setq arg nil))) ; hack to fall back to cycling
  9920. (arg
  9921. ;; user or caller requests a specific state
  9922. (cond
  9923. ((equal arg "") nil)
  9924. ((eq arg 'none) nil)
  9925. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9926. ((eq arg 'nextset)
  9927. (or (car (cdr (member head org-todo-heads)))
  9928. (car org-todo-heads)))
  9929. ((eq arg 'previousset)
  9930. (let ((org-todo-heads (reverse org-todo-heads)))
  9931. (or (car (cdr (member head org-todo-heads)))
  9932. (car org-todo-heads))))
  9933. ((car (member arg org-todo-keywords-1)))
  9934. ((stringp arg)
  9935. (error "State `%s' not valid in this file" arg))
  9936. ((nth (1- (prefix-numeric-value arg))
  9937. org-todo-keywords-1))))
  9938. ((null member) (or head (car org-todo-keywords-1)))
  9939. ((equal this final-done-word) nil) ;; -> make empty
  9940. ((null tail) nil) ;; -> first entry
  9941. ((memq interpret '(type priority))
  9942. (if (eq this-command last-command)
  9943. (car tail)
  9944. (if (> (length tail) 0)
  9945. (or done-word (car org-done-keywords))
  9946. nil)))
  9947. (t
  9948. (car tail))))
  9949. (state (or
  9950. (run-hook-with-args-until-success
  9951. 'org-todo-get-default-hook state last-state)
  9952. state))
  9953. (next (if state (concat " " state " ") " "))
  9954. (change-plist (list :type 'todo-state-change :from this :to state
  9955. :position startpos))
  9956. dolog now-done-p)
  9957. (when org-blocker-hook
  9958. (setq org-last-todo-state-is-todo
  9959. (not (member this org-done-keywords)))
  9960. (unless (save-excursion
  9961. (save-match-data
  9962. (org-with-wide-buffer
  9963. (run-hook-with-args-until-failure
  9964. 'org-blocker-hook change-plist))))
  9965. (if (interactive-p)
  9966. (error "TODO state change from %s to %s blocked" this state)
  9967. ;; fail silently
  9968. (message "TODO state change from %s to %s blocked" this state)
  9969. (throw 'exit nil))))
  9970. (store-match-data match-data)
  9971. (replace-match next t t)
  9972. (unless (pos-visible-in-window-p hl-pos)
  9973. (message "TODO state changed to %s" (org-trim next)))
  9974. (unless head
  9975. (setq head (org-get-todo-sequence-head state)
  9976. ass (assoc head org-todo-kwd-alist)
  9977. interpret (nth 1 ass)
  9978. done-word (nth 3 ass)
  9979. final-done-word (nth 4 ass)))
  9980. (when (memq arg '(nextset previousset))
  9981. (message "Keyword-Set %d/%d: %s"
  9982. (- (length org-todo-sets) -1
  9983. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9984. (length org-todo-sets)
  9985. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9986. (setq org-last-todo-state-is-todo
  9987. (not (member state org-done-keywords)))
  9988. (setq now-done-p (and (member state org-done-keywords)
  9989. (not (member this org-done-keywords))))
  9990. (and logging (org-local-logging logging))
  9991. (when (and (or org-todo-log-states org-log-done)
  9992. (not (eq org-inhibit-logging t))
  9993. (not (memq arg '(nextset previousset))))
  9994. ;; we need to look at recording a time and note
  9995. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9996. (nth 2 (assoc this org-todo-log-states))))
  9997. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9998. (setq dolog 'time))
  9999. (when (and state
  10000. (member state org-not-done-keywords)
  10001. (not (member this org-not-done-keywords)))
  10002. ;; This is now a todo state and was not one before
  10003. ;; If there was a CLOSED time stamp, get rid of it.
  10004. (org-add-planning-info nil nil 'closed))
  10005. (when (and now-done-p org-log-done)
  10006. ;; It is now done, and it was not done before
  10007. (org-add-planning-info 'closed (org-current-time))
  10008. (if (and (not dolog) (eq 'note org-log-done))
  10009. (org-add-log-setup 'done state this 'findpos 'note)))
  10010. (when (and state dolog)
  10011. ;; This is a non-nil state, and we need to log it
  10012. (org-add-log-setup 'state state this 'findpos dolog)))
  10013. ;; Fixup tag positioning
  10014. (org-todo-trigger-tag-changes state)
  10015. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10016. (when org-provide-todo-statistics
  10017. (org-update-parent-todo-statistics))
  10018. (run-hooks 'org-after-todo-state-change-hook)
  10019. (if (and arg (not (member state org-done-keywords)))
  10020. (setq head (org-get-todo-sequence-head state)))
  10021. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10022. ;; Do we need to trigger a repeat?
  10023. (when now-done-p
  10024. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10025. ;; This is for the agenda, take a snapshot of the headline.
  10026. (save-match-data
  10027. (setq org-agenda-headline-snapshot-before-repeat
  10028. (org-get-heading))))
  10029. (org-auto-repeat-maybe state))
  10030. ;; Fixup cursor location if close to the keyword
  10031. (if (and (outline-on-heading-p)
  10032. (not (bolp))
  10033. (save-excursion (beginning-of-line 1)
  10034. (looking-at org-todo-line-regexp))
  10035. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10036. (progn
  10037. (goto-char (or (match-end 2) (match-end 1)))
  10038. (and (looking-at " ") (just-one-space))))
  10039. (when org-trigger-hook
  10040. (save-excursion
  10041. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10042. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10043. "Block turning an entry into a TODO, using the hierarchy.
  10044. This checks whether the current task should be blocked from state
  10045. changes. Such blocking occurs when:
  10046. 1. The task has children which are not all in a completed state.
  10047. 2. A task has a parent with the property :ORDERED:, and there
  10048. are siblings prior to the current task with incomplete
  10049. status.
  10050. 3. The parent of the task is blocked because it has siblings that should
  10051. be done first, or is child of a block grandparent TODO entry."
  10052. (if (not org-enforce-todo-dependencies)
  10053. t ; if locally turned off don't block
  10054. (catch 'dont-block
  10055. ;; If this is not a todo state change, or if this entry is already DONE,
  10056. ;; do not block
  10057. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10058. (member (plist-get change-plist :from)
  10059. (cons 'done org-done-keywords))
  10060. (member (plist-get change-plist :to)
  10061. (cons 'todo org-not-done-keywords))
  10062. (not (plist-get change-plist :to)))
  10063. (throw 'dont-block t))
  10064. ;; If this task has children, and any are undone, it's blocked
  10065. (save-excursion
  10066. (org-back-to-heading t)
  10067. (let ((this-level (funcall outline-level)))
  10068. (outline-next-heading)
  10069. (let ((child-level (funcall outline-level)))
  10070. (while (and (not (eobp))
  10071. (> child-level this-level))
  10072. ;; this todo has children, check whether they are all
  10073. ;; completed
  10074. (if (and (not (org-entry-is-done-p))
  10075. (org-entry-is-todo-p))
  10076. (throw 'dont-block nil))
  10077. (outline-next-heading)
  10078. (setq child-level (funcall outline-level))))))
  10079. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10080. ;; any previous siblings are undone, it's blocked
  10081. (save-excursion
  10082. (org-back-to-heading t)
  10083. (let* ((pos (point))
  10084. (parent-pos (and (org-up-heading-safe) (point))))
  10085. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10086. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10087. (forward-line 1)
  10088. (re-search-forward org-not-done-heading-regexp pos t))
  10089. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10090. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10091. (while t
  10092. (goto-char parent-pos)
  10093. (if (not (looking-at org-not-done-heading-regexp))
  10094. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10095. (setq pos (point))
  10096. (setq parent-pos (and (org-up-heading-safe) (point)))
  10097. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10098. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10099. (forward-line 1)
  10100. (re-search-forward org-not-done-heading-regexp pos t))
  10101. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10102. (defcustom org-track-ordered-property-with-tag nil
  10103. "Should the ORDERED property also be shown as a tag?
  10104. The ORDERED property decides if an entry should require subtasks to be
  10105. completed in sequence. Since a property is not very visible, setting
  10106. this option means that toggling the ORDERED property with the command
  10107. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10108. not relevant for the behavior, but it makes things more visible.
  10109. Note that toggling the tag with tags commands will not change the property
  10110. and therefore not influence behavior!
  10111. This can be t, meaning the tag ORDERED should be used, It can also be a
  10112. string to select a different tag for this task."
  10113. :group 'org-todo
  10114. :type '(choice
  10115. (const :tag "No tracking" nil)
  10116. (const :tag "Track with ORDERED tag" t)
  10117. (string :tag "Use other tag")))
  10118. (defun org-toggle-ordered-property ()
  10119. "Toggle the ORDERED property of the current entry.
  10120. For better visibility, you can track the value of this property with a tag.
  10121. See variable `org-track-ordered-property-with-tag'."
  10122. (interactive)
  10123. (let* ((t1 org-track-ordered-property-with-tag)
  10124. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10125. (save-excursion
  10126. (org-back-to-heading)
  10127. (if (org-entry-get nil "ORDERED")
  10128. (progn
  10129. (org-delete-property "ORDERED")
  10130. (and tag (org-toggle-tag tag 'off))
  10131. (message "Subtasks can be completed in arbitrary order"))
  10132. (org-entry-put nil "ORDERED" "t")
  10133. (and tag (org-toggle-tag tag 'on))
  10134. (message "Subtasks must be completed in sequence")))))
  10135. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10136. (defun org-block-todo-from-checkboxes (change-plist)
  10137. "Block turning an entry into a TODO, using checkboxes.
  10138. This checks whether the current task should be blocked from state
  10139. changes because there are unchecked boxes in this entry."
  10140. (if (not org-enforce-todo-checkbox-dependencies)
  10141. t ; if locally turned off don't block
  10142. (catch 'dont-block
  10143. ;; If this is not a todo state change, or if this entry is already DONE,
  10144. ;; do not block
  10145. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10146. (member (plist-get change-plist :from)
  10147. (cons 'done org-done-keywords))
  10148. (member (plist-get change-plist :to)
  10149. (cons 'todo org-not-done-keywords))
  10150. (not (plist-get change-plist :to)))
  10151. (throw 'dont-block t))
  10152. ;; If this task has checkboxes that are not checked, it's blocked
  10153. (save-excursion
  10154. (org-back-to-heading t)
  10155. (let ((beg (point)) end)
  10156. (outline-next-heading)
  10157. (setq end (point))
  10158. (goto-char beg)
  10159. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10160. end t)
  10161. (progn
  10162. (if (boundp 'org-blocked-by-checkboxes)
  10163. (setq org-blocked-by-checkboxes t))
  10164. (throw 'dont-block nil)))))
  10165. t))) ; do not block
  10166. (defun org-entry-blocked-p ()
  10167. "Is the current entry blocked?"
  10168. (if (org-entry-get nil "NOBLOCKING")
  10169. nil ;; Never block this entry
  10170. (not
  10171. (run-hook-with-args-until-failure
  10172. 'org-blocker-hook
  10173. (list :type 'todo-state-change
  10174. :position (point)
  10175. :from 'todo
  10176. :to 'done)))))
  10177. (defun org-update-statistics-cookies (all)
  10178. "Update the statistics cookie, either from TODO or from checkboxes.
  10179. This should be called with the cursor in a line with a statistics cookie."
  10180. (interactive "P")
  10181. (if all
  10182. (progn
  10183. (org-update-checkbox-count 'all)
  10184. (org-map-entries 'org-update-parent-todo-statistics))
  10185. (if (not (org-on-heading-p))
  10186. (org-update-checkbox-count)
  10187. (let ((pos (move-marker (make-marker) (point)))
  10188. end l1 l2)
  10189. (ignore-errors (org-back-to-heading t))
  10190. (if (not (org-on-heading-p))
  10191. (org-update-checkbox-count)
  10192. (setq l1 (org-outline-level))
  10193. (setq end (save-excursion
  10194. (outline-next-heading)
  10195. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10196. (point)))
  10197. (if (and (save-excursion
  10198. (re-search-forward
  10199. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10200. (not (save-excursion (re-search-forward
  10201. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10202. (org-update-checkbox-count)
  10203. (if (and l2 (> l2 l1))
  10204. (progn
  10205. (goto-char end)
  10206. (org-update-parent-todo-statistics))
  10207. (goto-char pos)
  10208. (beginning-of-line 1)
  10209. (while (re-search-forward
  10210. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10211. (point-at-eol) t)
  10212. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10213. (goto-char pos)
  10214. (move-marker pos nil)))))
  10215. (defvar org-entry-property-inherited-from) ;; defined below
  10216. (defun org-update-parent-todo-statistics ()
  10217. "Update any statistics cookie in the parent of the current headline.
  10218. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10219. the entire subtree and this will travel up the hierarchy and update
  10220. statistics everywhere."
  10221. (interactive)
  10222. (let* ((lim 0) prop
  10223. (recursive (or (not org-hierarchical-todo-statistics)
  10224. (string-match
  10225. "\\<recursive\\>"
  10226. (or (setq prop (org-entry-get
  10227. nil "COOKIE_DATA" 'inherit)) ""))))
  10228. (lim (or (and prop (marker-position
  10229. org-entry-property-inherited-from))
  10230. lim))
  10231. (first t)
  10232. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10233. level ltoggle l1 new ndel
  10234. (cnt-all 0) (cnt-done 0) is-percent kwd
  10235. checkbox-beg ov ovs ove cookie-present)
  10236. (catch 'exit
  10237. (save-excursion
  10238. (beginning-of-line 1)
  10239. (if (org-at-heading-p)
  10240. (setq ltoggle (funcall outline-level))
  10241. (error "This should not happen"))
  10242. (while (and (setq level (org-up-heading-safe))
  10243. (or recursive first)
  10244. (>= (point) lim))
  10245. (setq first nil cookie-present nil)
  10246. (unless (and level
  10247. (not (string-match
  10248. "\\<checkbox\\>"
  10249. (downcase
  10250. (or (org-entry-get
  10251. nil "COOKIE_DATA")
  10252. "")))))
  10253. (throw 'exit nil))
  10254. (while (re-search-forward box-re (point-at-eol) t)
  10255. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10256. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10257. (save-match-data
  10258. (unless (outline-next-heading) (throw 'exit nil))
  10259. (while (and (looking-at org-complex-heading-regexp)
  10260. (> (setq l1 (length (match-string 1))) level))
  10261. (setq kwd (and (or recursive (= l1 ltoggle))
  10262. (match-string 2)))
  10263. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10264. (and (listp org-provide-todo-statistics)
  10265. (or (member kwd org-provide-todo-statistics)
  10266. (member kwd org-done-keywords))))
  10267. (setq cnt-all (1+ cnt-all))
  10268. (if (eq org-provide-todo-statistics t)
  10269. (and kwd (setq cnt-all (1+ cnt-all)))))
  10270. (and (member kwd org-done-keywords)
  10271. (setq cnt-done (1+ cnt-done)))
  10272. (outline-next-heading)))
  10273. (setq new
  10274. (if is-percent
  10275. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10276. (format "[%d/%d]" cnt-done cnt-all))
  10277. ndel (- (match-end 0) checkbox-beg))
  10278. ;; handle overlays when updating cookie from column view
  10279. (when (setq ov (car (overlays-at checkbox-beg)))
  10280. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10281. (delete-overlay ov))
  10282. (goto-char checkbox-beg)
  10283. (insert new)
  10284. (delete-region (point) (+ (point) ndel))
  10285. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10286. (when ov (move-overlay ov ovs ove)))
  10287. (when cookie-present
  10288. (run-hook-with-args 'org-after-todo-statistics-hook
  10289. cnt-done (- cnt-all cnt-done))))))
  10290. (run-hooks 'org-todo-statistics-hook)))
  10291. (defvar org-after-todo-statistics-hook nil
  10292. "Hook that is called after a TODO statistics cookie has been updated.
  10293. Each function is called with two arguments: the number of not-done entries
  10294. and the number of done entries.
  10295. For example, the following function, when added to this hook, will switch
  10296. an entry to DONE when all children are done, and back to TODO when new
  10297. entries are set to a TODO status. Note that this hook is only called
  10298. when there is a statistics cookie in the headline!
  10299. (defun org-summary-todo (n-done n-not-done)
  10300. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10301. (let (org-log-done org-log-states) ; turn off logging
  10302. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10303. ")
  10304. (defvar org-todo-statistics-hook nil
  10305. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10306. This hook runs even if there is no statistics cookie present, in which case
  10307. `org-after-todo-statistics-hook' would not run.")
  10308. (defun org-todo-trigger-tag-changes (state)
  10309. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10310. (let ((l org-todo-state-tags-triggers)
  10311. changes)
  10312. (when (or (not state) (equal state ""))
  10313. (setq changes (append changes (cdr (assoc "" l)))))
  10314. (when (and (stringp state) (> (length state) 0))
  10315. (setq changes (append changes (cdr (assoc state l)))))
  10316. (when (member state org-not-done-keywords)
  10317. (setq changes (append changes (cdr (assoc 'todo l)))))
  10318. (when (member state org-done-keywords)
  10319. (setq changes (append changes (cdr (assoc 'done l)))))
  10320. (dolist (c changes)
  10321. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10322. (defun org-local-logging (value)
  10323. "Get logging settings from a property VALUE."
  10324. (let* (words w a)
  10325. ;; directly set the variables, they are already local.
  10326. (setq org-log-done nil
  10327. org-log-repeat nil
  10328. org-todo-log-states nil)
  10329. (setq words (org-split-string value))
  10330. (while (setq w (pop words))
  10331. (cond
  10332. ((setq a (assoc w org-startup-options))
  10333. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10334. (set (nth 1 a) (nth 2 a))))
  10335. ((setq a (org-extract-log-state-settings w))
  10336. (and (member (car a) org-todo-keywords-1)
  10337. (push a org-todo-log-states)))))))
  10338. (defun org-get-todo-sequence-head (kwd)
  10339. "Return the head of the TODO sequence to which KWD belongs.
  10340. If KWD is not set, check if there is a text property remembering the
  10341. right sequence."
  10342. (let (p)
  10343. (cond
  10344. ((not kwd)
  10345. (or (get-text-property (point-at-bol) 'org-todo-head)
  10346. (progn
  10347. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10348. nil (point-at-eol)))
  10349. (get-text-property p 'org-todo-head))))
  10350. ((not (member kwd org-todo-keywords-1))
  10351. (car org-todo-keywords-1))
  10352. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10353. (defun org-fast-todo-selection ()
  10354. "Fast TODO keyword selection with single keys.
  10355. Returns the new TODO keyword, or nil if no state change should occur."
  10356. (let* ((fulltable org-todo-key-alist)
  10357. (done-keywords org-done-keywords) ;; needed for the faces.
  10358. (maxlen (apply 'max (mapcar
  10359. (lambda (x)
  10360. (if (stringp (car x)) (string-width (car x)) 0))
  10361. fulltable)))
  10362. (expert nil)
  10363. (fwidth (+ maxlen 3 1 3))
  10364. (ncol (/ (- (window-width) 4) fwidth))
  10365. tg cnt e c tbl
  10366. groups ingroup)
  10367. (save-excursion
  10368. (save-window-excursion
  10369. (if expert
  10370. (set-buffer (get-buffer-create " *Org todo*"))
  10371. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10372. (erase-buffer)
  10373. (org-set-local 'org-done-keywords done-keywords)
  10374. (setq tbl fulltable cnt 0)
  10375. (while (setq e (pop tbl))
  10376. (cond
  10377. ((equal e '(:startgroup))
  10378. (push '() groups) (setq ingroup t)
  10379. (when (not (= cnt 0))
  10380. (setq cnt 0)
  10381. (insert "\n"))
  10382. (insert "{ "))
  10383. ((equal e '(:endgroup))
  10384. (setq ingroup nil cnt 0)
  10385. (insert "}\n"))
  10386. ((equal e '(:newline))
  10387. (when (not (= cnt 0))
  10388. (setq cnt 0)
  10389. (insert "\n")
  10390. (setq e (car tbl))
  10391. (while (equal (car tbl) '(:newline))
  10392. (insert "\n")
  10393. (setq tbl (cdr tbl)))))
  10394. (t
  10395. (setq tg (car e) c (cdr e))
  10396. (if ingroup (push tg (car groups)))
  10397. (setq tg (org-add-props tg nil 'face
  10398. (org-get-todo-face tg)))
  10399. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10400. (insert "[" c "] " tg (make-string
  10401. (- fwidth 4 (length tg)) ?\ ))
  10402. (when (= (setq cnt (1+ cnt)) ncol)
  10403. (insert "\n")
  10404. (if ingroup (insert " "))
  10405. (setq cnt 0)))))
  10406. (insert "\n")
  10407. (goto-char (point-min))
  10408. (if (not expert) (org-fit-window-to-buffer))
  10409. (message "[a-z..]:Set [SPC]:clear")
  10410. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10411. (cond
  10412. ((or (= c ?\C-g)
  10413. (and (= c ?q) (not (rassoc c fulltable))))
  10414. (setq quit-flag t))
  10415. ((= c ?\ ) nil)
  10416. ((setq e (rassoc c fulltable) tg (car e))
  10417. tg)
  10418. (t (setq quit-flag t)))))))
  10419. (defun org-entry-is-todo-p ()
  10420. (member (org-get-todo-state) org-not-done-keywords))
  10421. (defun org-entry-is-done-p ()
  10422. (member (org-get-todo-state) org-done-keywords))
  10423. (defun org-get-todo-state ()
  10424. (save-excursion
  10425. (org-back-to-heading t)
  10426. (and (looking-at org-todo-line-regexp)
  10427. (match-end 2)
  10428. (match-string 2))))
  10429. (defun org-at-date-range-p (&optional inactive-ok)
  10430. "Is the cursor inside a date range?"
  10431. (interactive)
  10432. (save-excursion
  10433. (catch 'exit
  10434. (let ((pos (point)))
  10435. (skip-chars-backward "^[<\r\n")
  10436. (skip-chars-backward "<[")
  10437. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10438. (>= (match-end 0) pos)
  10439. (throw 'exit t))
  10440. (skip-chars-backward "^<[\r\n")
  10441. (skip-chars-backward "<[")
  10442. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10443. (>= (match-end 0) pos)
  10444. (throw 'exit t)))
  10445. nil)))
  10446. (defun org-get-repeat (&optional tagline)
  10447. "Check if there is a deadline/schedule with repeater in this entry."
  10448. (save-match-data
  10449. (save-excursion
  10450. (org-back-to-heading t)
  10451. (and (re-search-forward (if tagline
  10452. (concat tagline "\\s-*" org-repeat-re)
  10453. org-repeat-re)
  10454. (org-entry-end-position) t)
  10455. (match-string-no-properties 1)))))
  10456. (defvar org-last-changed-timestamp)
  10457. (defvar org-last-inserted-timestamp)
  10458. (defvar org-log-post-message)
  10459. (defvar org-log-note-purpose)
  10460. (defvar org-log-note-how)
  10461. (defvar org-log-note-extra)
  10462. (defun org-auto-repeat-maybe (done-word)
  10463. "Check if the current headline contains a repeated deadline/schedule.
  10464. If yes, set TODO state back to what it was and change the base date
  10465. of repeating deadline/scheduled time stamps to new date.
  10466. This function is run automatically after each state change to a DONE state."
  10467. ;; last-state is dynamically scoped into this function
  10468. (let* ((repeat (org-get-repeat))
  10469. (aa (assoc last-state org-todo-kwd-alist))
  10470. (interpret (nth 1 aa))
  10471. (head (nth 2 aa))
  10472. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10473. (msg "Entry repeats: ")
  10474. (org-log-done nil)
  10475. (org-todo-log-states nil)
  10476. re type n what ts time to-state)
  10477. (when repeat
  10478. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10479. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10480. org-todo-repeat-to-state))
  10481. (unless (and to-state (member to-state org-todo-keywords-1))
  10482. (setq to-state (if (eq interpret 'type) last-state head)))
  10483. (org-todo to-state)
  10484. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10485. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10486. (org-time-stamp-format t t))))
  10487. (when org-log-repeat
  10488. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10489. (memq 'org-add-log-note post-command-hook))
  10490. ;; OK, we are already setup for some record
  10491. (if (eq org-log-repeat 'note)
  10492. ;; make sure we take a note, not only a time stamp
  10493. (setq org-log-note-how 'note))
  10494. ;; Set up for taking a record
  10495. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10496. last-state
  10497. 'findpos org-log-repeat)))
  10498. (org-back-to-heading t)
  10499. (org-add-planning-info nil nil 'closed)
  10500. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10501. org-deadline-time-regexp "\\)\\|\\("
  10502. org-ts-regexp "\\)"))
  10503. (while (re-search-forward
  10504. re (save-excursion (outline-next-heading) (point)) t)
  10505. (setq type (if (match-end 1) org-scheduled-string
  10506. (if (match-end 3) org-deadline-string "Plain:"))
  10507. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10508. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10509. (setq n (string-to-number (match-string 2 ts))
  10510. what (match-string 3 ts))
  10511. (if (equal what "w") (setq n (* n 7) what "d"))
  10512. ;; Preparation, see if we need to modify the start date for the change
  10513. (when (match-end 1)
  10514. (setq time (save-match-data (org-time-string-to-time ts)))
  10515. (cond
  10516. ((equal (match-string 1 ts) ".")
  10517. ;; Shift starting date to today
  10518. (org-timestamp-change
  10519. (- (time-to-days (current-time)) (time-to-days time))
  10520. 'day))
  10521. ((equal (match-string 1 ts) "+")
  10522. (let ((nshiftmax 10) (nshift 0))
  10523. (while (or (= nshift 0)
  10524. (<= (time-to-days time)
  10525. (time-to-days (current-time))))
  10526. (when (= (incf nshift) nshiftmax)
  10527. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10528. (error "Abort")))
  10529. (org-timestamp-change n (cdr (assoc what whata)))
  10530. (org-at-timestamp-p t)
  10531. (setq ts (match-string 1))
  10532. (setq time (save-match-data (org-time-string-to-time ts)))))
  10533. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10534. ;; rematch, so that we have everything in place for the real shift
  10535. (org-at-timestamp-p t)
  10536. (setq ts (match-string 1))
  10537. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10538. (org-timestamp-change n (cdr (assoc what whata)))
  10539. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10540. (setq org-log-post-message msg)
  10541. (message "%s" msg))))
  10542. (defun org-show-todo-tree (arg)
  10543. "Make a compact tree which shows all headlines marked with TODO.
  10544. The tree will show the lines where the regexp matches, and all higher
  10545. headlines above the match.
  10546. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10547. With a numeric prefix N, construct a sparse tree for the Nth element
  10548. of `org-todo-keywords-1'."
  10549. (interactive "P")
  10550. (let ((case-fold-search nil)
  10551. (kwd-re
  10552. (cond ((null arg) org-not-done-regexp)
  10553. ((equal arg '(4))
  10554. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10555. (mapcar 'list org-todo-keywords-1))))
  10556. (concat "\\("
  10557. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10558. "\\)\\>")))
  10559. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10560. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10561. org-todo-keywords-1)))
  10562. (t (error "Invalid prefix argument: %s" arg)))))
  10563. (message "%d TODO entries found"
  10564. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10565. (defun org-deadline (&optional remove time)
  10566. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10567. With argument REMOVE, remove any deadline from the item.
  10568. When TIME is set, it should be an internal time specification, and the
  10569. scheduling will use the corresponding date."
  10570. (interactive "P")
  10571. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10572. (repeater (and old-date
  10573. (string-match
  10574. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10575. old-date)
  10576. (match-string 1 old-date))))
  10577. (if remove
  10578. (progn
  10579. (when (and old-date org-log-redeadline)
  10580. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10581. org-log-redeadline))
  10582. (org-remove-timestamp-with-keyword org-deadline-string)
  10583. (message "Item no longer has a deadline."))
  10584. (org-add-planning-info 'deadline time 'closed)
  10585. (when (and old-date org-log-redeadline
  10586. (not (equal old-date
  10587. (substring org-last-inserted-timestamp 1 -1))))
  10588. (org-add-log-setup 'redeadline nil old-date 'findpos
  10589. org-log-redeadline))
  10590. (when repeater
  10591. (save-excursion
  10592. (org-back-to-heading t)
  10593. (when (re-search-forward (concat org-deadline-string " "
  10594. org-last-inserted-timestamp)
  10595. (save-excursion
  10596. (outline-next-heading) (point)) t)
  10597. (goto-char (1- (match-end 0)))
  10598. (insert " " repeater)
  10599. (setq org-last-inserted-timestamp
  10600. (concat (substring org-last-inserted-timestamp 0 -1)
  10601. " " repeater
  10602. (substring org-last-inserted-timestamp -1))))))
  10603. (message "Deadline on %s" org-last-inserted-timestamp))))
  10604. (defun org-schedule (&optional remove time)
  10605. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10606. With argument REMOVE, remove any scheduling date from the item.
  10607. When TIME is set, it should be an internal time specification, and the
  10608. scheduling will use the corresponding date."
  10609. (interactive "P")
  10610. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10611. (repeater (and old-date
  10612. (string-match
  10613. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10614. old-date)
  10615. (match-string 1 old-date))))
  10616. (if remove
  10617. (progn
  10618. (when (and old-date org-log-reschedule)
  10619. (org-add-log-setup 'delschedule nil old-date 'findpos
  10620. org-log-reschedule))
  10621. (org-remove-timestamp-with-keyword org-scheduled-string)
  10622. (message "Item is no longer scheduled."))
  10623. (org-add-planning-info 'scheduled time 'closed)
  10624. (when (and old-date org-log-reschedule
  10625. (not (equal old-date
  10626. (substring org-last-inserted-timestamp 1 -1))))
  10627. (org-add-log-setup 'reschedule nil old-date 'findpos
  10628. org-log-reschedule))
  10629. (when repeater
  10630. (save-excursion
  10631. (org-back-to-heading t)
  10632. (when (re-search-forward (concat org-scheduled-string " "
  10633. org-last-inserted-timestamp)
  10634. (save-excursion
  10635. (outline-next-heading) (point)) t)
  10636. (goto-char (1- (match-end 0)))
  10637. (insert " " repeater)
  10638. (setq org-last-inserted-timestamp
  10639. (concat (substring org-last-inserted-timestamp 0 -1)
  10640. " " repeater
  10641. (substring org-last-inserted-timestamp -1))))))
  10642. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10643. (defun org-get-scheduled-time (pom &optional inherit)
  10644. "Get the scheduled time as a time tuple, of a format suitable
  10645. for calling org-schedule with, or if there is no scheduling,
  10646. returns nil."
  10647. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10648. (when time
  10649. (apply 'encode-time (org-parse-time-string time)))))
  10650. (defun org-get-deadline-time (pom &optional inherit)
  10651. "Get the deadline as a time tuple, of a format suitable for
  10652. calling org-deadline with, or if there is no scheduling, returns
  10653. nil."
  10654. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10655. (when time
  10656. (apply 'encode-time (org-parse-time-string time)))))
  10657. (defun org-remove-timestamp-with-keyword (keyword)
  10658. "Remove all time stamps with KEYWORD in the current entry."
  10659. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10660. beg)
  10661. (save-excursion
  10662. (org-back-to-heading t)
  10663. (setq beg (point))
  10664. (outline-next-heading)
  10665. (while (re-search-backward re beg t)
  10666. (replace-match "")
  10667. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10668. (equal (char-before) ?\ ))
  10669. (backward-delete-char 1)
  10670. (if (string-match "^[ \t]*$" (buffer-substring
  10671. (point-at-bol) (point-at-eol)))
  10672. (delete-region (point-at-bol)
  10673. (min (point-max) (1+ (point-at-eol))))))))))
  10674. (defun org-add-planning-info (what &optional time &rest remove)
  10675. "Insert new timestamp with keyword in the line directly after the headline.
  10676. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10677. If non is given, the user is prompted for a date.
  10678. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10679. be removed."
  10680. (interactive)
  10681. (let (org-time-was-given org-end-time-was-given ts
  10682. end default-time default-input)
  10683. (catch 'exit
  10684. (when (and (not time) (memq what '(scheduled deadline)))
  10685. ;; Try to get a default date/time from existing timestamp
  10686. (save-excursion
  10687. (org-back-to-heading t)
  10688. (setq end (save-excursion (outline-next-heading) (point)))
  10689. (when (re-search-forward (if (eq what 'scheduled)
  10690. org-scheduled-time-regexp
  10691. org-deadline-time-regexp)
  10692. end t)
  10693. (setq ts (match-string 1)
  10694. default-time
  10695. (apply 'encode-time (org-parse-time-string ts))
  10696. default-input (and ts (org-get-compact-tod ts))))))
  10697. (when what
  10698. ;; If necessary, get the time from the user
  10699. (setq time (or time (org-read-date nil 'to-time nil nil
  10700. default-time default-input))))
  10701. (when (and org-insert-labeled-timestamps-at-point
  10702. (member what '(scheduled deadline)))
  10703. (insert
  10704. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10705. (org-insert-time-stamp time org-time-was-given
  10706. nil nil nil (list org-end-time-was-given))
  10707. (setq what nil))
  10708. (save-excursion
  10709. (save-restriction
  10710. (let (col list elt ts buffer-invisibility-spec)
  10711. (org-back-to-heading t)
  10712. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10713. (goto-char (match-end 1))
  10714. (setq col (current-column))
  10715. (goto-char (match-end 0))
  10716. (if (eobp) (insert "\n") (forward-char 1))
  10717. (when (and (not what)
  10718. (not (looking-at
  10719. (concat "[ \t]*"
  10720. org-keyword-time-not-clock-regexp))))
  10721. ;; Nothing to add, nothing to remove...... :-)
  10722. (throw 'exit nil))
  10723. (if (and (not (looking-at outline-regexp))
  10724. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10725. "[^\r\n]*"))
  10726. (not (equal (match-string 1) org-clock-string)))
  10727. (narrow-to-region (match-beginning 0) (match-end 0))
  10728. (insert-before-markers "\n")
  10729. (backward-char 1)
  10730. (narrow-to-region (point) (point))
  10731. (and org-adapt-indentation (org-indent-to-column col)))
  10732. ;; Check if we have to remove something.
  10733. (setq list (cons what remove))
  10734. (while list
  10735. (setq elt (pop list))
  10736. (goto-char (point-min))
  10737. (when (or (and (eq elt 'scheduled)
  10738. (re-search-forward org-scheduled-time-regexp nil t))
  10739. (and (eq elt 'deadline)
  10740. (re-search-forward org-deadline-time-regexp nil t))
  10741. (and (eq elt 'closed)
  10742. (re-search-forward org-closed-time-regexp nil t)))
  10743. (replace-match "")
  10744. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10745. (skip-chars-backward " ")
  10746. (if (looking-at " +") (replace-match ""))))
  10747. (goto-char (point-max))
  10748. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10749. (when what
  10750. (insert
  10751. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10752. (cond ((eq what 'scheduled) org-scheduled-string)
  10753. ((eq what 'deadline) org-deadline-string)
  10754. ((eq what 'closed) org-closed-string))
  10755. " ")
  10756. (setq ts (org-insert-time-stamp
  10757. time
  10758. (or org-time-was-given
  10759. (and (eq what 'closed) org-log-done-with-time))
  10760. (eq what 'closed)
  10761. nil nil (list org-end-time-was-given)))
  10762. (end-of-line 1))
  10763. (goto-char (point-min))
  10764. (widen)
  10765. (if (and (looking-at "[ \t]*\n")
  10766. (equal (char-before) ?\n))
  10767. (delete-region (1- (point)) (point-at-eol)))
  10768. ts))))))
  10769. (defvar org-log-note-marker (make-marker))
  10770. (defvar org-log-note-purpose nil)
  10771. (defvar org-log-note-state nil)
  10772. (defvar org-log-note-previous-state nil)
  10773. (defvar org-log-note-how nil)
  10774. (defvar org-log-note-extra nil)
  10775. (defvar org-log-note-window-configuration nil)
  10776. (defvar org-log-note-return-to (make-marker))
  10777. (defvar org-log-post-message nil
  10778. "Message to be displayed after a log note has been stored.
  10779. The auto-repeater uses this.")
  10780. (defun org-add-note ()
  10781. "Add a note to the current entry.
  10782. This is done in the same way as adding a state change note."
  10783. (interactive)
  10784. (org-add-log-setup 'note nil nil 'findpos nil))
  10785. (defvar org-property-end-re)
  10786. (defun org-add-log-setup (&optional purpose state prev-state
  10787. findpos how extra)
  10788. "Set up the post command hook to take a note.
  10789. If this is about to TODO state change, the new state is expected in STATE.
  10790. When FINDPOS is non-nil, find the correct position for the note in
  10791. the current entry. If not, assume that it can be inserted at point.
  10792. HOW is an indicator what kind of note should be created.
  10793. EXTRA is additional text that will be inserted into the notes buffer."
  10794. (let* ((org-log-into-drawer (org-log-into-drawer))
  10795. (drawer (cond ((stringp org-log-into-drawer)
  10796. org-log-into-drawer)
  10797. (org-log-into-drawer "LOGBOOK")
  10798. (t nil))))
  10799. (save-restriction
  10800. (save-excursion
  10801. (when findpos
  10802. (org-back-to-heading t)
  10803. (narrow-to-region (point) (save-excursion
  10804. (outline-next-heading) (point)))
  10805. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10806. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10807. "[^\r\n]*\\)?"))
  10808. (goto-char (match-end 0))
  10809. (cond
  10810. (drawer
  10811. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10812. nil t)
  10813. (progn
  10814. (goto-char (match-end 0))
  10815. (or org-log-states-order-reversed
  10816. (and (re-search-forward org-property-end-re nil t)
  10817. (goto-char (1- (match-beginning 0))))))
  10818. (insert "\n:" drawer ":\n:END:")
  10819. (beginning-of-line 0)
  10820. (org-indent-line-function)
  10821. (beginning-of-line 2)
  10822. (org-indent-line-function)
  10823. (end-of-line 0)))
  10824. ((and org-log-state-notes-insert-after-drawers
  10825. (save-excursion
  10826. (forward-line) (looking-at org-drawer-regexp)))
  10827. (forward-line)
  10828. (while (looking-at org-drawer-regexp)
  10829. (goto-char (match-end 0))
  10830. (re-search-forward org-property-end-re (point-max) t)
  10831. (forward-line))
  10832. (forward-line -1)))
  10833. (unless org-log-states-order-reversed
  10834. (and (= (char-after) ?\n) (forward-char 1))
  10835. (org-skip-over-state-notes)
  10836. (skip-chars-backward " \t\n\r")))
  10837. (move-marker org-log-note-marker (point))
  10838. (setq org-log-note-purpose purpose
  10839. org-log-note-state state
  10840. org-log-note-previous-state prev-state
  10841. org-log-note-how how
  10842. org-log-note-extra extra)
  10843. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10844. (defun org-skip-over-state-notes ()
  10845. "Skip past the list of State notes in an entry."
  10846. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10847. (when (ignore-errors (goto-char (org-in-item-p)))
  10848. (let* ((struct (org-list-struct))
  10849. (prevs (org-list-prevs-alist struct)))
  10850. (while (looking-at "[ \t]*- State")
  10851. (goto-char (or (org-list-get-next-item (point) struct prevs)
  10852. (org-list-get-item-end (point) struct)))))))
  10853. (defun org-add-log-note (&optional purpose)
  10854. "Pop up a window for taking a note, and add this note later at point."
  10855. (remove-hook 'post-command-hook 'org-add-log-note)
  10856. (setq org-log-note-window-configuration (current-window-configuration))
  10857. (delete-other-windows)
  10858. (move-marker org-log-note-return-to (point))
  10859. (switch-to-buffer (marker-buffer org-log-note-marker))
  10860. (goto-char org-log-note-marker)
  10861. (org-switch-to-buffer-other-window "*Org Note*")
  10862. (erase-buffer)
  10863. (if (memq org-log-note-how '(time state))
  10864. (let (current-prefix-arg) (org-store-log-note))
  10865. (let ((org-inhibit-startup t)) (org-mode))
  10866. (insert (format "# Insert note for %s.
  10867. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10868. (cond
  10869. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10870. ((eq org-log-note-purpose 'done) "closed todo item")
  10871. ((eq org-log-note-purpose 'state)
  10872. (format "state change from \"%s\" to \"%s\""
  10873. (or org-log-note-previous-state "")
  10874. (or org-log-note-state "")))
  10875. ((eq org-log-note-purpose 'reschedule)
  10876. "rescheduling")
  10877. ((eq org-log-note-purpose 'delschedule)
  10878. "no longer scheduled")
  10879. ((eq org-log-note-purpose 'redeadline)
  10880. "changing deadline")
  10881. ((eq org-log-note-purpose 'deldeadline)
  10882. "removing deadline")
  10883. ((eq org-log-note-purpose 'refile)
  10884. "refiling")
  10885. ((eq org-log-note-purpose 'note)
  10886. "this entry")
  10887. (t (error "This should not happen")))))
  10888. (if org-log-note-extra (insert org-log-note-extra))
  10889. (org-set-local 'org-finish-function 'org-store-log-note)))
  10890. (defvar org-note-abort nil) ; dynamically scoped
  10891. (defun org-store-log-note ()
  10892. "Finish taking a log note, and insert it to where it belongs."
  10893. (let ((txt (buffer-string))
  10894. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10895. lines ind bul)
  10896. (kill-buffer (current-buffer))
  10897. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10898. (setq txt (replace-match "" t t txt)))
  10899. (if (string-match "\\s-+\\'" txt)
  10900. (setq txt (replace-match "" t t txt)))
  10901. (setq lines (org-split-string txt "\n"))
  10902. (when (and note (string-match "\\S-" note))
  10903. (setq note
  10904. (org-replace-escapes
  10905. note
  10906. (list (cons "%u" (user-login-name))
  10907. (cons "%U" user-full-name)
  10908. (cons "%t" (format-time-string
  10909. (org-time-stamp-format 'long 'inactive)
  10910. (current-time)))
  10911. (cons "%T" (format-time-string
  10912. (org-time-stamp-format 'long nil)
  10913. (current-time)))
  10914. (cons "%s" (if org-log-note-state
  10915. (concat "\"" org-log-note-state "\"")
  10916. ""))
  10917. (cons "%S" (if org-log-note-previous-state
  10918. (concat "\"" org-log-note-previous-state "\"")
  10919. "\"\"")))))
  10920. (if lines (setq note (concat note " \\\\")))
  10921. (push note lines))
  10922. (when (or current-prefix-arg org-note-abort)
  10923. (when org-log-into-drawer
  10924. (org-remove-empty-drawer-at
  10925. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10926. org-log-note-marker))
  10927. (setq lines nil))
  10928. (when lines
  10929. (with-current-buffer (marker-buffer org-log-note-marker)
  10930. (save-excursion
  10931. (goto-char org-log-note-marker)
  10932. (move-marker org-log-note-marker nil)
  10933. (end-of-line 1)
  10934. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10935. (setq ind (save-excursion
  10936. (if (ignore-errors (goto-char (org-in-item-p)))
  10937. (let ((struct (org-list-struct)))
  10938. (org-list-get-ind
  10939. (org-list-get-top-point struct) struct))
  10940. (skip-chars-backward " \r\t\n")
  10941. (cond
  10942. ((and (org-at-heading-p)
  10943. org-adapt-indentation)
  10944. (1+ (org-current-level)))
  10945. ((org-at-heading-p) 0)
  10946. (t (org-get-indentation))))))
  10947. (setq bul (org-list-bullet-string "-"))
  10948. (org-indent-line-to ind)
  10949. (insert bul (pop lines))
  10950. (let ((ind-body (+ (length bul) ind)))
  10951. (while lines
  10952. (insert "\n")
  10953. (org-indent-line-to ind-body)
  10954. (insert (pop lines))))
  10955. (message "Note stored")
  10956. (org-back-to-heading t)
  10957. (org-cycle-hide-drawers 'children)))))
  10958. (set-window-configuration org-log-note-window-configuration)
  10959. (with-current-buffer (marker-buffer org-log-note-return-to)
  10960. (goto-char org-log-note-return-to))
  10961. (move-marker org-log-note-return-to nil)
  10962. (and org-log-post-message (message "%s" org-log-post-message)))
  10963. (defun org-remove-empty-drawer-at (drawer pos)
  10964. "Remove an empty drawer DRAWER at position POS.
  10965. POS may also be a marker."
  10966. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10967. (save-excursion
  10968. (save-restriction
  10969. (widen)
  10970. (goto-char pos)
  10971. (if (org-in-regexp
  10972. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10973. (replace-match ""))))))
  10974. (defun org-sparse-tree (&optional arg)
  10975. "Create a sparse tree, prompt for the details.
  10976. This command can create sparse trees. You first need to select the type
  10977. of match used to create the tree:
  10978. t Show all TODO entries.
  10979. T Show entries with a specific TODO keyword.
  10980. m Show entries selected by a tags/property match.
  10981. p Enter a property name and its value (both with completion on existing
  10982. names/values) and show entries with that property.
  10983. r Show entries matching a regular expression (`/' can be used as well)
  10984. d Show deadlines due within `org-deadline-warning-days'.
  10985. b Show deadlines and scheduled items before a date.
  10986. a Show deadlines and scheduled items after a date."
  10987. (interactive "P")
  10988. (let (ans kwd value)
  10989. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  10990. (setq ans (read-char-exclusive))
  10991. (cond
  10992. ((equal ans ?d)
  10993. (call-interactively 'org-check-deadlines))
  10994. ((equal ans ?b)
  10995. (call-interactively 'org-check-before-date))
  10996. ((equal ans ?a)
  10997. (call-interactively 'org-check-after-date))
  10998. ((equal ans ?t)
  10999. (org-show-todo-tree nil))
  11000. ((equal ans ?T)
  11001. (org-show-todo-tree '(4)))
  11002. ((member ans '(?T ?m))
  11003. (call-interactively 'org-match-sparse-tree))
  11004. ((member ans '(?p ?P))
  11005. (setq kwd (org-icompleting-read "Property: "
  11006. (mapcar 'list (org-buffer-property-keys))))
  11007. (setq value (org-icompleting-read "Value: "
  11008. (mapcar 'list (org-property-values kwd))))
  11009. (unless (string-match "\\`{.*}\\'" value)
  11010. (setq value (concat "\"" value "\"")))
  11011. (org-match-sparse-tree arg (concat kwd "=" value)))
  11012. ((member ans '(?r ?R ?/))
  11013. (call-interactively 'org-occur))
  11014. (t (error "No such sparse tree command \"%c\"" ans)))))
  11015. (defvar org-occur-highlights nil
  11016. "List of overlays used for occur matches.")
  11017. (make-variable-buffer-local 'org-occur-highlights)
  11018. (defvar org-occur-parameters nil
  11019. "Parameters of the active org-occur calls.
  11020. This is a list, each call to org-occur pushes as cons cell,
  11021. containing the regular expression and the callback, onto the list.
  11022. The list can contain several entries if `org-occur' has been called
  11023. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11024. will only contain one set of parameters. When the highlights are
  11025. removed (for example with `C-c C-c', or with the next edit (depending
  11026. on `org-remove-highlights-with-change'), this variable is emptied
  11027. as well.")
  11028. (make-variable-buffer-local 'org-occur-parameters)
  11029. (defun org-occur (regexp &optional keep-previous callback)
  11030. "Make a compact tree which shows all matches of REGEXP.
  11031. The tree will show the lines where the regexp matches, and all higher
  11032. headlines above the match. It will also show the heading after the match,
  11033. to make sure editing the matching entry is easy.
  11034. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11035. call to `org-occur' will be kept, to allow stacking of calls to this
  11036. command.
  11037. If CALLBACK is non-nil, it is a function which is called to confirm
  11038. that the match should indeed be shown."
  11039. (interactive "sRegexp: \nP")
  11040. (when (equal regexp "")
  11041. (error "Regexp cannot be empty"))
  11042. (unless keep-previous
  11043. (org-remove-occur-highlights nil nil t))
  11044. (push (cons regexp callback) org-occur-parameters)
  11045. (let ((cnt 0))
  11046. (save-excursion
  11047. (goto-char (point-min))
  11048. (if (or (not keep-previous) ; do not want to keep
  11049. (not org-occur-highlights)) ; no previous matches
  11050. ;; hide everything
  11051. (org-overview))
  11052. (while (re-search-forward regexp nil t)
  11053. (when (or (not callback)
  11054. (save-match-data (funcall callback)))
  11055. (setq cnt (1+ cnt))
  11056. (when org-highlight-sparse-tree-matches
  11057. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11058. (org-show-context 'occur-tree))))
  11059. (when org-remove-highlights-with-change
  11060. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11061. nil 'local))
  11062. (unless org-sparse-tree-open-archived-trees
  11063. (org-hide-archived-subtrees (point-min) (point-max)))
  11064. (run-hooks 'org-occur-hook)
  11065. (if (interactive-p)
  11066. (message "%d match(es) for regexp %s" cnt regexp))
  11067. cnt))
  11068. (defun org-occur-next-match (&optional n reset)
  11069. "Function for `next-error-function' to find sparse tree matches.
  11070. N is the number of matches to move, when negative move backwards.
  11071. RESET is entirely ignored - this function always goes back to the
  11072. starting point when no match is found."
  11073. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11074. (search-func (if (< n 0)
  11075. 'previous-single-char-property-change
  11076. 'next-single-char-property-change))
  11077. (n (abs n))
  11078. (pos (point))
  11079. p1)
  11080. (catch 'exit
  11081. (while (setq p1 (funcall search-func (point) 'org-type))
  11082. (when (equal p1 limit)
  11083. (goto-char pos)
  11084. (error "No more matches"))
  11085. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11086. (setq n (1- n))
  11087. (when (= n 0)
  11088. (goto-char p1)
  11089. (throw 'exit (point))))
  11090. (goto-char p1))
  11091. (goto-char p1)
  11092. (error "No more matches"))))
  11093. (defun org-show-context (&optional key)
  11094. "Make sure point and context are visible.
  11095. How much context is shown depends upon the variables
  11096. `org-show-hierarchy-above', `org-show-following-heading'. and
  11097. `org-show-siblings'."
  11098. (let ((heading-p (org-on-heading-p t))
  11099. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11100. (following-p (org-get-alist-option org-show-following-heading key))
  11101. (entry-p (org-get-alist-option org-show-entry-below key))
  11102. (siblings-p (org-get-alist-option org-show-siblings key)))
  11103. (catch 'exit
  11104. ;; Show heading or entry text
  11105. (if (and heading-p (not entry-p))
  11106. (org-flag-heading nil) ; only show the heading
  11107. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11108. (org-show-hidden-entry))) ; show entire entry
  11109. (when following-p
  11110. ;; Show next sibling, or heading below text
  11111. (save-excursion
  11112. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11113. (org-flag-heading nil))))
  11114. (when siblings-p (org-show-siblings))
  11115. (when hierarchy-p
  11116. ;; show all higher headings, possibly with siblings
  11117. (save-excursion
  11118. (while (and (condition-case nil
  11119. (progn (org-up-heading-all 1) t)
  11120. (error nil))
  11121. (not (bobp)))
  11122. (org-flag-heading nil)
  11123. (when siblings-p (org-show-siblings))))))))
  11124. (defvar org-reveal-start-hook nil
  11125. "Hook run before revealing a location.")
  11126. (defun org-reveal (&optional siblings)
  11127. "Show current entry, hierarchy above it, and the following headline.
  11128. This can be used to show a consistent set of context around locations
  11129. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11130. not t for the search context.
  11131. With optional argument SIBLINGS, on each level of the hierarchy all
  11132. siblings are shown. This repairs the tree structure to what it would
  11133. look like when opened with hierarchical calls to `org-cycle'.
  11134. With double optional argument \\[universal-argument] \\[universal-argument], \
  11135. go to the parent and show the
  11136. entire tree."
  11137. (interactive "P")
  11138. (run-hooks 'org-reveal-start-hook)
  11139. (let ((org-show-hierarchy-above t)
  11140. (org-show-following-heading t)
  11141. (org-show-siblings (if siblings t org-show-siblings)))
  11142. (org-show-context nil))
  11143. (when (equal siblings '(16))
  11144. (save-excursion
  11145. (when (org-up-heading-safe)
  11146. (org-show-subtree)
  11147. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11148. (defun org-highlight-new-match (beg end)
  11149. "Highlight from BEG to END and mark the highlight is an occur headline."
  11150. (let ((ov (make-overlay beg end)))
  11151. (overlay-put ov 'face 'secondary-selection)
  11152. (overlay-put ov 'org-type 'org-occur)
  11153. (push ov org-occur-highlights)))
  11154. (defun org-remove-occur-highlights (&optional beg end noremove)
  11155. "Remove the occur highlights from the buffer.
  11156. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11157. from the `before-change-functions' in the current buffer."
  11158. (interactive)
  11159. (unless org-inhibit-highlight-removal
  11160. (mapc 'delete-overlay org-occur-highlights)
  11161. (setq org-occur-highlights nil)
  11162. (setq org-occur-parameters nil)
  11163. (unless noremove
  11164. (remove-hook 'before-change-functions
  11165. 'org-remove-occur-highlights 'local))))
  11166. ;;;; Priorities
  11167. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11168. "Regular expression matching the priority indicator.")
  11169. (defvar org-remove-priority-next-time nil)
  11170. (defun org-priority-up ()
  11171. "Increase the priority of the current item."
  11172. (interactive)
  11173. (org-priority 'up))
  11174. (defun org-priority-down ()
  11175. "Decrease the priority of the current item."
  11176. (interactive)
  11177. (org-priority 'down))
  11178. (defun org-priority (&optional action)
  11179. "Change the priority of an item by ARG.
  11180. ACTION can be `set', `up', `down', or a character."
  11181. (interactive)
  11182. (unless org-enable-priority-commands
  11183. (error "Priority commands are disabled"))
  11184. (setq action (or action 'set))
  11185. (let (current new news have remove)
  11186. (save-excursion
  11187. (org-back-to-heading t)
  11188. (if (looking-at org-priority-regexp)
  11189. (setq current (string-to-char (match-string 2))
  11190. have t))
  11191. (cond
  11192. ((eq action 'remove)
  11193. (setq remove t new ?\ ))
  11194. ((or (eq action 'set)
  11195. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11196. (if (not (eq action 'set))
  11197. (setq new action)
  11198. (message "Priority %c-%c, SPC to remove: "
  11199. org-highest-priority org-lowest-priority)
  11200. (save-match-data
  11201. (setq new (read-char-exclusive))))
  11202. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11203. (= (upcase org-lowest-priority) org-lowest-priority))
  11204. (setq new (upcase new)))
  11205. (cond ((equal new ?\ ) (setq remove t))
  11206. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11207. (error "Priority must be between `%c' and `%c'"
  11208. org-highest-priority org-lowest-priority))))
  11209. ((eq action 'up)
  11210. (setq new (if have
  11211. (1- current) ; normal cycling
  11212. ;; last priority was empty
  11213. (if (eq last-command this-command)
  11214. org-lowest-priority ; wrap around empty to lowest
  11215. ;; default
  11216. (if org-priority-start-cycle-with-default
  11217. org-default-priority
  11218. (1- org-default-priority))))))
  11219. ((eq action 'down)
  11220. (setq new (if have
  11221. (1+ current) ; normal cycling
  11222. ;; last priority was empty
  11223. (if (eq last-command this-command)
  11224. org-highest-priority ; wrap around empty to highest
  11225. ;; default
  11226. (if org-priority-start-cycle-with-default
  11227. org-default-priority
  11228. (1+ org-default-priority))))))
  11229. (t (error "Invalid action")))
  11230. (if (or (< (upcase new) org-highest-priority)
  11231. (> (upcase new) org-lowest-priority))
  11232. (if (and (memq action '(up down))
  11233. (not have) (not (eq last-command this-command)))
  11234. ;; `new' is from default priority
  11235. (error
  11236. "The default can not be set, see `org-default-priority' why")
  11237. ;; normal cycling: `new' is beyond highest/lowest priority
  11238. ;; and is wrapped around to the empty priority
  11239. (setq remove t)))
  11240. (setq news (format "%c" new))
  11241. (if have
  11242. (if remove
  11243. (replace-match "" t t nil 1)
  11244. (replace-match news t t nil 2))
  11245. (if remove
  11246. (error "No priority cookie found in line")
  11247. (let ((case-fold-search nil))
  11248. (looking-at org-todo-line-regexp))
  11249. (if (match-end 2)
  11250. (progn
  11251. (goto-char (match-end 2))
  11252. (insert " [#" news "]"))
  11253. (goto-char (match-beginning 3))
  11254. (insert "[#" news "] "))))
  11255. (org-preserve-lc (org-set-tags nil 'align)))
  11256. (if remove
  11257. (message "Priority removed")
  11258. (message "Priority of current item set to %s" news))))
  11259. (defun org-get-priority (s)
  11260. "Find priority cookie and return priority."
  11261. (if (functionp org-get-priority-function)
  11262. (funcall org-get-priority-function)
  11263. (save-match-data
  11264. (if (not (string-match org-priority-regexp s))
  11265. (* 1000 (- org-lowest-priority org-default-priority))
  11266. (* 1000 (- org-lowest-priority
  11267. (string-to-char (match-string 2 s))))))))
  11268. ;;;; Tags
  11269. (defvar org-agenda-archives-mode)
  11270. (defvar org-map-continue-from nil
  11271. "Position from where mapping should continue.
  11272. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11273. (defvar org-scanner-tags nil
  11274. "The current tag list while the tags scanner is running.")
  11275. (defvar org-trust-scanner-tags nil
  11276. "Should `org-get-tags-at' use the tags for the scanner.
  11277. This is for internal dynamical scoping only.
  11278. When this is non-nil, the function `org-get-tags-at' will return the value
  11279. of `org-scanner-tags' instead of building the list by itself. This
  11280. can lead to large speed-ups when the tags scanner is used in a file with
  11281. many entries, and when the list of tags is retrieved, for example to
  11282. obtain a list of properties. Building the tags list for each entry in such
  11283. a file becomes an N^2 operation - but with this variable set, it scales
  11284. as N.")
  11285. (defun org-scan-tags (action matcher &optional todo-only)
  11286. "Scan headline tags with inheritance and produce output ACTION.
  11287. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11288. or `agenda' to produce an entry list for an agenda view. It can also be
  11289. a Lisp form or a function that should be called at each matched headline, in
  11290. this case the return value is a list of all return values from these calls.
  11291. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11292. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11293. only lines with a TODO keyword are included in the output."
  11294. (require 'org-agenda)
  11295. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11296. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11297. (org-re
  11298. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11299. (props (list 'face 'default
  11300. 'done-face 'org-agenda-done
  11301. 'undone-face 'default
  11302. 'mouse-face 'highlight
  11303. 'org-not-done-regexp org-not-done-regexp
  11304. 'org-todo-regexp org-todo-regexp
  11305. 'help-echo
  11306. (format "mouse-2 or RET jump to org file %s"
  11307. (abbreviate-file-name
  11308. (or (buffer-file-name (buffer-base-buffer))
  11309. (buffer-name (buffer-base-buffer)))))))
  11310. (case-fold-search nil)
  11311. (org-map-continue-from nil)
  11312. lspos tags tags-list
  11313. (tags-alist (list (cons 0 org-file-tags)))
  11314. (llast 0) rtn rtn1 level category i txt
  11315. todo marker entry priority)
  11316. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11317. (setq action (list 'lambda nil action)))
  11318. (save-excursion
  11319. (goto-char (point-min))
  11320. (when (eq action 'sparse-tree)
  11321. (org-overview)
  11322. (org-remove-occur-highlights))
  11323. (while (re-search-forward re nil t)
  11324. (catch :skip
  11325. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11326. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11327. (goto-char (setq lspos (match-beginning 0)))
  11328. (setq level (org-reduced-level (funcall outline-level))
  11329. category (org-get-category))
  11330. (setq i llast llast level)
  11331. ;; remove tag lists from same and sublevels
  11332. (while (>= i level)
  11333. (when (setq entry (assoc i tags-alist))
  11334. (setq tags-alist (delete entry tags-alist)))
  11335. (setq i (1- i)))
  11336. ;; add the next tags
  11337. (when tags
  11338. (setq tags (org-split-string tags ":")
  11339. tags-alist
  11340. (cons (cons level tags) tags-alist)))
  11341. ;; compile tags for current headline
  11342. (setq tags-list
  11343. (if org-use-tag-inheritance
  11344. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11345. tags)
  11346. org-scanner-tags tags-list)
  11347. (when org-use-tag-inheritance
  11348. (setcdr (car tags-alist)
  11349. (mapcar (lambda (x)
  11350. (setq x (copy-sequence x))
  11351. (org-add-prop-inherited x))
  11352. (cdar tags-alist))))
  11353. (when (and tags org-use-tag-inheritance
  11354. (or (not (eq t org-use-tag-inheritance))
  11355. org-tags-exclude-from-inheritance))
  11356. ;; selective inheritance, remove uninherited ones
  11357. (setcdr (car tags-alist)
  11358. (org-remove-uninherited-tags (cdar tags-alist))))
  11359. (when (and
  11360. ;; eval matcher only when the todo condition is OK
  11361. (and (or (not todo-only) (member todo org-not-done-keywords))
  11362. (let ((case-fold-search t)) (eval matcher)))
  11363. ;; Call the skipper, but return t if it does not skip,
  11364. ;; so that the `and' form continues evaluating
  11365. (progn
  11366. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11367. t)
  11368. ;; Check if timestamps are deselecting this entry
  11369. (or (not todo-only)
  11370. (and (member todo org-not-done-keywords)
  11371. (or (not org-agenda-tags-todo-honor-ignore-options)
  11372. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11373. ;; Extra check for the archive tag
  11374. ;; FIXME: Does the skipper already do this????
  11375. (or
  11376. (not (member org-archive-tag tags-list))
  11377. ;; we have an archive tag, should we use this anyway?
  11378. (or (not org-agenda-skip-archived-trees)
  11379. (and (eq action 'agenda) org-agenda-archives-mode))))
  11380. ;; select this headline
  11381. (cond
  11382. ((eq action 'sparse-tree)
  11383. (and org-highlight-sparse-tree-matches
  11384. (org-get-heading) (match-end 0)
  11385. (org-highlight-new-match
  11386. (match-beginning 0) (match-beginning 1)))
  11387. (org-show-context 'tags-tree))
  11388. ((eq action 'agenda)
  11389. (setq txt (org-format-agenda-item
  11390. ""
  11391. (concat
  11392. (if (eq org-tags-match-list-sublevels 'indented)
  11393. (make-string (1- level) ?.) "")
  11394. (org-get-heading))
  11395. category
  11396. tags-list
  11397. )
  11398. priority (org-get-priority txt))
  11399. (goto-char lspos)
  11400. (setq marker (org-agenda-new-marker))
  11401. (org-add-props txt props
  11402. 'org-marker marker 'org-hd-marker marker 'org-category category
  11403. 'todo-state todo
  11404. 'priority priority 'type "tagsmatch")
  11405. (push txt rtn))
  11406. ((functionp action)
  11407. (setq org-map-continue-from nil)
  11408. (save-excursion
  11409. (setq rtn1 (funcall action))
  11410. (push rtn1 rtn)))
  11411. (t (error "Invalid action")))
  11412. ;; if we are to skip sublevels, jump to end of subtree
  11413. (unless org-tags-match-list-sublevels
  11414. (org-end-of-subtree t)
  11415. (backward-char 1))))
  11416. ;; Get the correct position from where to continue
  11417. (if org-map-continue-from
  11418. (goto-char org-map-continue-from)
  11419. (and (= (point) lspos) (end-of-line 1)))))
  11420. (when (and (eq action 'sparse-tree)
  11421. (not org-sparse-tree-open-archived-trees))
  11422. (org-hide-archived-subtrees (point-min) (point-max)))
  11423. (nreverse rtn)))
  11424. (defun org-remove-uninherited-tags (tags)
  11425. "Remove all tags that are not inherited from the list TAGS."
  11426. (cond
  11427. ((eq org-use-tag-inheritance t)
  11428. (if org-tags-exclude-from-inheritance
  11429. (org-delete-all org-tags-exclude-from-inheritance tags)
  11430. tags))
  11431. ((not org-use-tag-inheritance) nil)
  11432. ((stringp org-use-tag-inheritance)
  11433. (delq nil (mapcar
  11434. (lambda (x)
  11435. (if (and (string-match org-use-tag-inheritance x)
  11436. (not (member x org-tags-exclude-from-inheritance)))
  11437. x nil))
  11438. tags)))
  11439. ((listp org-use-tag-inheritance)
  11440. (delq nil (mapcar
  11441. (lambda (x)
  11442. (if (member x org-use-tag-inheritance) x nil))
  11443. tags)))))
  11444. (defvar todo-only) ;; dynamically scoped
  11445. (defun org-match-sparse-tree (&optional todo-only match)
  11446. "Create a sparse tree according to tags string MATCH.
  11447. MATCH can contain positive and negative selection of tags, like
  11448. \"+WORK+URGENT-WITHBOSS\".
  11449. If optional argument TODO-ONLY is non-nil, only select lines that are
  11450. also TODO lines."
  11451. (interactive "P")
  11452. (org-prepare-agenda-buffers (list (current-buffer)))
  11453. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11454. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11455. (defvar org-cached-props nil)
  11456. (defun org-cached-entry-get (pom property)
  11457. (if (or (eq t org-use-property-inheritance)
  11458. (and (stringp org-use-property-inheritance)
  11459. (string-match org-use-property-inheritance property))
  11460. (and (listp org-use-property-inheritance)
  11461. (member property org-use-property-inheritance)))
  11462. ;; Caching is not possible, check it directly
  11463. (org-entry-get pom property 'inherit)
  11464. ;; Get all properties, so that we can do complicated checks easily
  11465. (cdr (assoc property (or org-cached-props
  11466. (setq org-cached-props
  11467. (org-entry-properties pom)))))))
  11468. (defun org-global-tags-completion-table (&optional files)
  11469. "Return the list of all tags in all agenda buffer/files.
  11470. Optional FILES argument is a list of files to which can be used
  11471. instead of the agenda files."
  11472. (save-excursion
  11473. (org-uniquify
  11474. (delq nil
  11475. (apply 'append
  11476. (mapcar
  11477. (lambda (file)
  11478. (set-buffer (find-file-noselect file))
  11479. (append (org-get-buffer-tags)
  11480. (mapcar (lambda (x) (if (stringp (car-safe x))
  11481. (list (car-safe x)) nil))
  11482. org-tag-alist)))
  11483. (if (and files (car files))
  11484. files
  11485. (org-agenda-files))))))))
  11486. (defun org-make-tags-matcher (match)
  11487. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11488. ;; todo-only is scoped dynamically into this function, and the function
  11489. ;; may change it if the matcher asks for it.
  11490. (unless match
  11491. ;; Get a new match request, with completion
  11492. (let ((org-last-tags-completion-table
  11493. (org-global-tags-completion-table)))
  11494. (setq match (org-completing-read-no-i
  11495. "Match: " 'org-tags-completion-function nil nil nil
  11496. 'org-tags-history))))
  11497. ;; Parse the string and create a lisp form
  11498. (let ((match0 match)
  11499. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11500. minus tag mm
  11501. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11502. orterms term orlist re-p str-p level-p level-op time-p
  11503. prop-p pn pv po gv rest)
  11504. (if (string-match "/+" match)
  11505. ;; match contains also a todo-matching request
  11506. (progn
  11507. (setq tagsmatch (substring match 0 (match-beginning 0))
  11508. todomatch (substring match (match-end 0)))
  11509. (if (string-match "^!" todomatch)
  11510. (setq todo-only t todomatch (substring todomatch 1)))
  11511. (if (string-match "^\\s-*$" todomatch)
  11512. (setq todomatch nil)))
  11513. ;; only matching tags
  11514. (setq tagsmatch match todomatch nil))
  11515. ;; Make the tags matcher
  11516. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11517. (setq tagsmatcher t)
  11518. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11519. (while (setq term (pop orterms))
  11520. (while (and (equal (substring term -1) "\\") orterms)
  11521. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11522. (while (string-match re term)
  11523. (setq rest (substring term (match-end 0))
  11524. minus (and (match-end 1)
  11525. (equal (match-string 1 term) "-"))
  11526. tag (save-match-data (replace-regexp-in-string
  11527. "\\\\-" "-"
  11528. (match-string 2 term)))
  11529. re-p (equal (string-to-char tag) ?{)
  11530. level-p (match-end 4)
  11531. prop-p (match-end 5)
  11532. mm (cond
  11533. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11534. (level-p
  11535. (setq level-op (org-op-to-function (match-string 3 term)))
  11536. `(,level-op level ,(string-to-number
  11537. (match-string 4 term))))
  11538. (prop-p
  11539. (setq pn (match-string 5 term)
  11540. po (match-string 6 term)
  11541. pv (match-string 7 term)
  11542. re-p (equal (string-to-char pv) ?{)
  11543. str-p (equal (string-to-char pv) ?\")
  11544. time-p (save-match-data
  11545. (string-match "^\"[[<].*[]>]\"$" pv))
  11546. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11547. (if time-p (setq pv (org-matcher-time pv)))
  11548. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11549. (cond
  11550. ((equal pn "CATEGORY")
  11551. (setq gv '(get-text-property (point) 'org-category)))
  11552. ((equal pn "TODO")
  11553. (setq gv 'todo))
  11554. (t
  11555. (setq gv `(org-cached-entry-get nil ,pn))))
  11556. (if re-p
  11557. (if (eq po 'org<>)
  11558. `(not (string-match ,pv (or ,gv "")))
  11559. `(string-match ,pv (or ,gv "")))
  11560. (if str-p
  11561. `(,po (or ,gv "") ,pv)
  11562. `(,po (string-to-number (or ,gv ""))
  11563. ,(string-to-number pv) ))))
  11564. (t `(member ,tag tags-list)))
  11565. mm (if minus (list 'not mm) mm)
  11566. term rest)
  11567. (push mm tagsmatcher))
  11568. (push (if (> (length tagsmatcher) 1)
  11569. (cons 'and tagsmatcher)
  11570. (car tagsmatcher))
  11571. orlist)
  11572. (setq tagsmatcher nil))
  11573. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11574. (setq tagsmatcher
  11575. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11576. ;; Make the todo matcher
  11577. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11578. (setq todomatcher t)
  11579. (setq orterms (org-split-string todomatch "|") orlist nil)
  11580. (while (setq term (pop orterms))
  11581. (while (string-match re term)
  11582. (setq minus (and (match-end 1)
  11583. (equal (match-string 1 term) "-"))
  11584. kwd (match-string 2 term)
  11585. re-p (equal (string-to-char kwd) ?{)
  11586. term (substring term (match-end 0))
  11587. mm (if re-p
  11588. `(string-match ,(substring kwd 1 -1) todo)
  11589. (list 'equal 'todo kwd))
  11590. mm (if minus (list 'not mm) mm))
  11591. (push mm todomatcher))
  11592. (push (if (> (length todomatcher) 1)
  11593. (cons 'and todomatcher)
  11594. (car todomatcher))
  11595. orlist)
  11596. (setq todomatcher nil))
  11597. (setq todomatcher (if (> (length orlist) 1)
  11598. (cons 'or orlist) (car orlist))))
  11599. ;; Return the string and lisp forms of the matcher
  11600. (setq matcher (if todomatcher
  11601. (list 'and tagsmatcher todomatcher)
  11602. tagsmatcher))
  11603. (cons match0 matcher)))
  11604. (defun org-op-to-function (op &optional stringp)
  11605. "Turn an operator into the appropriate function."
  11606. (setq op
  11607. (cond
  11608. ((equal op "<" ) '(< string< org-time<))
  11609. ((equal op ">" ) '(> org-string> org-time>))
  11610. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11611. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11612. ((member op '("=" "==")) '(= string= org-time=))
  11613. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11614. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11615. (defun org<> (a b) (not (= a b)))
  11616. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11617. (defun org-string>= (a b) (not (string< a b)))
  11618. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11619. (defun org-string<> (a b) (not (string= a b)))
  11620. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11621. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11622. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11623. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11624. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11625. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11626. (defun org-2ft (s)
  11627. "Convert S to a floating point time.
  11628. If S is already a number, just return it. If it is a string, parse
  11629. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11630. (cond
  11631. ((numberp s) s)
  11632. ((stringp s)
  11633. (condition-case nil
  11634. (float-time (apply 'encode-time (org-parse-time-string s)))
  11635. (error 0.)))
  11636. (t 0.)))
  11637. (defun org-time-today ()
  11638. "Time in seconds today at 0:00.
  11639. Returns the float number of seconds since the beginning of the
  11640. epoch to the beginning of today (00:00)."
  11641. (float-time (apply 'encode-time
  11642. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11643. (defun org-matcher-time (s)
  11644. "Interpret a time comparison value."
  11645. (save-match-data
  11646. (cond
  11647. ((string= s "<now>") (float-time))
  11648. ((string= s "<today>") (org-time-today))
  11649. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11650. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11651. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11652. (+ (org-time-today)
  11653. (* (string-to-number (match-string 1 s))
  11654. (cdr (assoc (match-string 2 s)
  11655. '(("d" . 86400.0) ("w" . 604800.0)
  11656. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11657. (t (org-2ft s)))))
  11658. (defun org-match-any-p (re list)
  11659. "Does re match any element of list?"
  11660. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11661. (delq nil list))
  11662. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11663. (defvar org-tags-overlay (make-overlay 1 1))
  11664. (org-detach-overlay org-tags-overlay)
  11665. (defun org-get-local-tags-at (&optional pos)
  11666. "Get a list of tags defined in the current headline."
  11667. (org-get-tags-at pos 'local))
  11668. (defun org-get-local-tags ()
  11669. "Get a list of tags defined in the current headline."
  11670. (org-get-tags-at nil 'local))
  11671. (defun org-get-tags-at (&optional pos local)
  11672. "Get a list of all headline tags applicable at POS.
  11673. POS defaults to point. If tags are inherited, the list contains
  11674. the targets in the same sequence as the headlines appear, i.e.
  11675. the tags of the current headline come last.
  11676. When LOCAL is non-nil, only return tags from the current headline,
  11677. ignore inherited ones."
  11678. (interactive)
  11679. (if (and org-trust-scanner-tags
  11680. (or (not pos) (equal pos (point)))
  11681. (not local))
  11682. org-scanner-tags
  11683. (let (tags ltags lastpos parent)
  11684. (save-excursion
  11685. (save-restriction
  11686. (widen)
  11687. (goto-char (or pos (point)))
  11688. (save-match-data
  11689. (catch 'done
  11690. (condition-case nil
  11691. (progn
  11692. (org-back-to-heading t)
  11693. (while (not (equal lastpos (point)))
  11694. (setq lastpos (point))
  11695. (when (looking-at
  11696. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11697. (setq ltags (org-split-string
  11698. (org-match-string-no-properties 1) ":"))
  11699. (when parent
  11700. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11701. (setq tags (append
  11702. (if parent
  11703. (org-remove-uninherited-tags ltags)
  11704. ltags)
  11705. tags)))
  11706. (or org-use-tag-inheritance (throw 'done t))
  11707. (if local (throw 'done t))
  11708. (or (org-up-heading-safe) (error nil))
  11709. (setq parent t)))
  11710. (error nil)))))
  11711. (if local
  11712. tags
  11713. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  11714. (defun org-add-prop-inherited (s)
  11715. (add-text-properties 0 (length s) '(inherited t) s)
  11716. s)
  11717. (defun org-toggle-tag (tag &optional onoff)
  11718. "Toggle the tag TAG for the current line.
  11719. If ONOFF is `on' or `off', don't toggle but set to this state."
  11720. (let (res current)
  11721. (save-excursion
  11722. (org-back-to-heading t)
  11723. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11724. (point-at-eol) t)
  11725. (progn
  11726. (setq current (match-string 1))
  11727. (replace-match ""))
  11728. (setq current ""))
  11729. (setq current (nreverse (org-split-string current ":")))
  11730. (cond
  11731. ((eq onoff 'on)
  11732. (setq res t)
  11733. (or (member tag current) (push tag current)))
  11734. ((eq onoff 'off)
  11735. (or (not (member tag current)) (setq current (delete tag current))))
  11736. (t (if (member tag current)
  11737. (setq current (delete tag current))
  11738. (setq res t)
  11739. (push tag current))))
  11740. (end-of-line 1)
  11741. (if current
  11742. (progn
  11743. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11744. (org-set-tags nil t))
  11745. (delete-horizontal-space))
  11746. (run-hooks 'org-after-tags-change-hook))
  11747. res))
  11748. (defun org-align-tags-here (to-col)
  11749. ;; Assumes that this is a headline
  11750. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11751. (beginning-of-line 1)
  11752. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11753. (< pos (match-beginning 2)))
  11754. (progn
  11755. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11756. (goto-char (match-beginning 1))
  11757. (insert " ")
  11758. (delete-region (point) (1+ (match-beginning 2)))
  11759. (setq ncol (max (1+ (current-column))
  11760. (1+ col)
  11761. (if (> to-col 0)
  11762. to-col
  11763. (- (abs to-col) tags-l))))
  11764. (setq p (point))
  11765. (insert (make-string (- ncol (current-column)) ?\ ))
  11766. (setq ncol (current-column))
  11767. (when indent-tabs-mode (tabify p (point-at-eol)))
  11768. (org-move-to-column (min ncol col) t))
  11769. (goto-char pos))))
  11770. (defun org-set-tags-command (&optional arg just-align)
  11771. "Call the set-tags command for the current entry."
  11772. (interactive "P")
  11773. (if (org-on-heading-p)
  11774. (org-set-tags arg just-align)
  11775. (save-excursion
  11776. (org-back-to-heading t)
  11777. (org-set-tags arg just-align))))
  11778. (defun org-set-tags-to (data)
  11779. "Set the tags of the current entry to DATA, replacing the current tags.
  11780. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11781. If DATA is nil or the empty string, any tags will be removed."
  11782. (interactive "sTags: ")
  11783. (setq data
  11784. (cond
  11785. ((eq data nil) "")
  11786. ((equal data "") "")
  11787. ((stringp data)
  11788. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11789. ":"))
  11790. ((listp data)
  11791. (concat ":" (mapconcat 'identity data ":") ":"))
  11792. (t nil)))
  11793. (when data
  11794. (save-excursion
  11795. (org-back-to-heading t)
  11796. (when (looking-at org-complex-heading-regexp)
  11797. (if (match-end 5)
  11798. (progn
  11799. (goto-char (match-beginning 5))
  11800. (insert data)
  11801. (delete-region (point) (point-at-eol))
  11802. (org-set-tags nil 'align))
  11803. (goto-char (point-at-eol))
  11804. (insert " " data)
  11805. (org-set-tags nil 'align)))
  11806. (beginning-of-line 1)
  11807. (if (looking-at ".*?\\([ \t]+\\)$")
  11808. (delete-region (match-beginning 1) (match-end 1))))))
  11809. (defun org-align-all-tags ()
  11810. "Align the tags i all headings."
  11811. (interactive)
  11812. (save-excursion
  11813. (or (ignore-errors (org-back-to-heading t))
  11814. (outline-next-heading))
  11815. (if (org-on-heading-p)
  11816. (org-set-tags t)
  11817. (message "No headings"))))
  11818. (defvar org-indent-indentation-per-level)
  11819. (defun org-set-tags (&optional arg just-align)
  11820. "Set the tags for the current headline.
  11821. With prefix ARG, realign all tags in headings in the current buffer."
  11822. (interactive "P")
  11823. (let* ((re (concat "^" outline-regexp))
  11824. (current (org-get-tags-string))
  11825. (col (current-column))
  11826. (org-setting-tags t)
  11827. table current-tags inherited-tags ; computed below when needed
  11828. tags p0 c0 c1 rpl di tc level)
  11829. (if arg
  11830. (save-excursion
  11831. (goto-char (point-min))
  11832. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11833. (while (re-search-forward re nil t)
  11834. (org-set-tags nil t)
  11835. (end-of-line 1)))
  11836. (message "All tags realigned to column %d" org-tags-column))
  11837. (if just-align
  11838. (setq tags current)
  11839. ;; Get a new set of tags from the user
  11840. (save-excursion
  11841. (setq table (append org-tag-persistent-alist
  11842. (or org-tag-alist (org-get-buffer-tags))
  11843. (and
  11844. org-complete-tags-always-offer-all-agenda-tags
  11845. (org-global-tags-completion-table
  11846. (org-agenda-files))))
  11847. org-last-tags-completion-table table
  11848. current-tags (org-split-string current ":")
  11849. inherited-tags (nreverse
  11850. (nthcdr (length current-tags)
  11851. (nreverse (org-get-tags-at))))
  11852. tags
  11853. (if (or (eq t org-use-fast-tag-selection)
  11854. (and org-use-fast-tag-selection
  11855. (delq nil (mapcar 'cdr table))))
  11856. (org-fast-tag-selection
  11857. current-tags inherited-tags table
  11858. (if org-fast-tag-selection-include-todo
  11859. org-todo-key-alist))
  11860. (let ((org-add-colon-after-tag-completion t))
  11861. (org-trim
  11862. (org-without-partial-completion
  11863. (org-icompleting-read "Tags: "
  11864. 'org-tags-completion-function
  11865. nil nil current 'org-tags-history)))))))
  11866. (while (string-match "[-+&]+" tags)
  11867. ;; No boolean logic, just a list
  11868. (setq tags (replace-match ":" t t tags))))
  11869. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  11870. (if org-tags-sort-function
  11871. (setq tags (mapconcat 'identity
  11872. (sort (org-split-string
  11873. tags (org-re "[^[:alnum:]_@#%]+"))
  11874. org-tags-sort-function) ":")))
  11875. (if (string-match "\\`[\t ]*\\'" tags)
  11876. (setq tags "")
  11877. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11878. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11879. ;; Insert new tags at the correct column
  11880. (beginning-of-line 1)
  11881. (setq level (or (and (looking-at org-outline-regexp)
  11882. (- (match-end 0) (point) 1))
  11883. 1))
  11884. (cond
  11885. ((and (equal current "") (equal tags "")))
  11886. ((re-search-forward
  11887. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11888. (point-at-eol) t)
  11889. (if (equal tags "")
  11890. (setq rpl "")
  11891. (goto-char (match-beginning 0))
  11892. (setq c0 (current-column)
  11893. ;; compute offset for the case of org-indent-mode active
  11894. di (if org-indent-mode
  11895. (* (1- org-indent-indentation-per-level) (1- level))
  11896. 0)
  11897. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11898. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11899. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11900. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11901. (replace-match rpl t t)
  11902. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11903. tags)
  11904. (t (error "Tags alignment failed")))
  11905. (org-move-to-column col)
  11906. (unless just-align
  11907. (run-hooks 'org-after-tags-change-hook)))))
  11908. (defun org-change-tag-in-region (beg end tag off)
  11909. "Add or remove TAG for each entry in the region.
  11910. This works in the agenda, and also in an org-mode buffer."
  11911. (interactive
  11912. (list (region-beginning) (region-end)
  11913. (let ((org-last-tags-completion-table
  11914. (if (org-mode-p)
  11915. (org-get-buffer-tags)
  11916. (org-global-tags-completion-table))))
  11917. (org-icompleting-read
  11918. "Tag: " 'org-tags-completion-function nil nil nil
  11919. 'org-tags-history))
  11920. (progn
  11921. (message "[s]et or [r]emove? ")
  11922. (equal (read-char-exclusive) ?r))))
  11923. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11924. (let ((agendap (equal major-mode 'org-agenda-mode))
  11925. l1 l2 m buf pos newhead (cnt 0))
  11926. (goto-char end)
  11927. (setq l2 (1- (org-current-line)))
  11928. (goto-char beg)
  11929. (setq l1 (org-current-line))
  11930. (loop for l from l1 to l2 do
  11931. (org-goto-line l)
  11932. (setq m (get-text-property (point) 'org-hd-marker))
  11933. (when (or (and (org-mode-p) (org-on-heading-p))
  11934. (and agendap m))
  11935. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11936. pos (if agendap m (point)))
  11937. (with-current-buffer buf
  11938. (save-excursion
  11939. (save-restriction
  11940. (goto-char pos)
  11941. (setq cnt (1+ cnt))
  11942. (org-toggle-tag tag (if off 'off 'on))
  11943. (setq newhead (org-get-heading)))))
  11944. (and agendap (org-agenda-change-all-lines newhead m))))
  11945. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11946. (defun org-tags-completion-function (string predicate &optional flag)
  11947. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11948. (confirm (lambda (x) (stringp (car x)))))
  11949. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11950. (setq s1 (match-string 1 string)
  11951. s2 (match-string 2 string))
  11952. (setq s1 "" s2 string))
  11953. (cond
  11954. ((eq flag nil)
  11955. ;; try completion
  11956. (setq rtn (try-completion s2 ctable confirm))
  11957. (if (stringp rtn)
  11958. (setq rtn
  11959. (concat s1 s2 (substring rtn (length s2))
  11960. (if (and org-add-colon-after-tag-completion
  11961. (assoc rtn ctable))
  11962. ":" ""))))
  11963. rtn)
  11964. ((eq flag t)
  11965. ;; all-completions
  11966. (all-completions s2 ctable confirm)
  11967. )
  11968. ((eq flag 'lambda)
  11969. ;; exact match?
  11970. (assoc s2 ctable)))
  11971. ))
  11972. (defun org-fast-tag-insert (kwd tags face &optional end)
  11973. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11974. (insert (format "%-12s" (concat kwd ":"))
  11975. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11976. (or end "")))
  11977. (defun org-fast-tag-show-exit (flag)
  11978. (save-excursion
  11979. (org-goto-line 3)
  11980. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11981. (replace-match ""))
  11982. (when flag
  11983. (end-of-line 1)
  11984. (org-move-to-column (- (window-width) 19) t)
  11985. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11986. (defun org-set-current-tags-overlay (current prefix)
  11987. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11988. (if (featurep 'xemacs)
  11989. (org-overlay-display org-tags-overlay (concat prefix s)
  11990. 'secondary-selection)
  11991. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11992. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11993. (defvar org-last-tag-selection-key nil)
  11994. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11995. "Fast tag selection with single keys.
  11996. CURRENT is the current list of tags in the headline, INHERITED is the
  11997. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11998. possibly with grouping information. TODO-TABLE is a similar table with
  11999. TODO keywords, should these have keys assigned to them.
  12000. If the keys are nil, a-z are automatically assigned.
  12001. Returns the new tags string, or nil to not change the current settings."
  12002. (let* ((fulltable (append table todo-table))
  12003. (maxlen (apply 'max (mapcar
  12004. (lambda (x)
  12005. (if (stringp (car x)) (string-width (car x)) 0))
  12006. fulltable)))
  12007. (buf (current-buffer))
  12008. (expert (eq org-fast-tag-selection-single-key 'expert))
  12009. (buffer-tags nil)
  12010. (fwidth (+ maxlen 3 1 3))
  12011. (ncol (/ (- (window-width) 4) fwidth))
  12012. (i-face 'org-done)
  12013. (c-face 'org-todo)
  12014. tg cnt e c char c1 c2 ntable tbl rtn
  12015. ov-start ov-end ov-prefix
  12016. (exit-after-next org-fast-tag-selection-single-key)
  12017. (done-keywords org-done-keywords)
  12018. groups ingroup)
  12019. (save-excursion
  12020. (beginning-of-line 1)
  12021. (if (looking-at
  12022. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12023. (setq ov-start (match-beginning 1)
  12024. ov-end (match-end 1)
  12025. ov-prefix "")
  12026. (setq ov-start (1- (point-at-eol))
  12027. ov-end (1+ ov-start))
  12028. (skip-chars-forward "^\n\r")
  12029. (setq ov-prefix
  12030. (concat
  12031. (buffer-substring (1- (point)) (point))
  12032. (if (> (current-column) org-tags-column)
  12033. " "
  12034. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12035. (move-overlay org-tags-overlay ov-start ov-end)
  12036. (save-window-excursion
  12037. (if expert
  12038. (set-buffer (get-buffer-create " *Org tags*"))
  12039. (delete-other-windows)
  12040. (split-window-vertically)
  12041. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12042. (erase-buffer)
  12043. (org-set-local 'org-done-keywords done-keywords)
  12044. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12045. (org-fast-tag-insert "Current" current c-face "\n\n")
  12046. (org-fast-tag-show-exit exit-after-next)
  12047. (org-set-current-tags-overlay current ov-prefix)
  12048. (setq tbl fulltable char ?a cnt 0)
  12049. (while (setq e (pop tbl))
  12050. (cond
  12051. ((equal (car e) :startgroup)
  12052. (push '() groups) (setq ingroup t)
  12053. (when (not (= cnt 0))
  12054. (setq cnt 0)
  12055. (insert "\n"))
  12056. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12057. ((equal (car e) :endgroup)
  12058. (setq ingroup nil cnt 0)
  12059. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12060. ((equal e '(:newline))
  12061. (when (not (= cnt 0))
  12062. (setq cnt 0)
  12063. (insert "\n")
  12064. (setq e (car tbl))
  12065. (while (equal (car tbl) '(:newline))
  12066. (insert "\n")
  12067. (setq tbl (cdr tbl)))))
  12068. (t
  12069. (setq tg (copy-sequence (car e)) c2 nil)
  12070. (if (cdr e)
  12071. (setq c (cdr e))
  12072. ;; automatically assign a character.
  12073. (setq c1 (string-to-char
  12074. (downcase (substring
  12075. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12076. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12077. (while (or (rassoc char ntable) (rassoc char table))
  12078. (setq char (1+ char)))
  12079. (setq c2 c1))
  12080. (setq c (or c2 char)))
  12081. (if ingroup (push tg (car groups)))
  12082. (setq tg (org-add-props tg nil 'face
  12083. (cond
  12084. ((not (assoc tg table))
  12085. (org-get-todo-face tg))
  12086. ((member tg current) c-face)
  12087. ((member tg inherited) i-face)
  12088. (t nil))))
  12089. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12090. (insert "[" c "] " tg (make-string
  12091. (- fwidth 4 (length tg)) ?\ ))
  12092. (push (cons tg c) ntable)
  12093. (when (= (setq cnt (1+ cnt)) ncol)
  12094. (insert "\n")
  12095. (if ingroup (insert " "))
  12096. (setq cnt 0)))))
  12097. (setq ntable (nreverse ntable))
  12098. (insert "\n")
  12099. (goto-char (point-min))
  12100. (if (not expert) (org-fit-window-to-buffer))
  12101. (setq rtn
  12102. (catch 'exit
  12103. (while t
  12104. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12105. (if (not groups) "no " "")
  12106. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12107. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12108. (setq org-last-tag-selection-key c)
  12109. (cond
  12110. ((= c ?\r) (throw 'exit t))
  12111. ((= c ?!)
  12112. (setq groups (not groups))
  12113. (goto-char (point-min))
  12114. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12115. ((= c ?\C-c)
  12116. (if (not expert)
  12117. (org-fast-tag-show-exit
  12118. (setq exit-after-next (not exit-after-next)))
  12119. (setq expert nil)
  12120. (delete-other-windows)
  12121. (set-window-buffer (split-window-vertically) " *Org tags*")
  12122. (org-switch-to-buffer-other-window " *Org tags*")
  12123. (org-fit-window-to-buffer)))
  12124. ((or (= c ?\C-g)
  12125. (and (= c ?q) (not (rassoc c ntable))))
  12126. (org-detach-overlay org-tags-overlay)
  12127. (setq quit-flag t))
  12128. ((= c ?\ )
  12129. (setq current nil)
  12130. (if exit-after-next (setq exit-after-next 'now)))
  12131. ((= c ?\t)
  12132. (condition-case nil
  12133. (setq tg (org-icompleting-read
  12134. "Tag: "
  12135. (or buffer-tags
  12136. (with-current-buffer buf
  12137. (org-get-buffer-tags)))))
  12138. (quit (setq tg "")))
  12139. (when (string-match "\\S-" tg)
  12140. (add-to-list 'buffer-tags (list tg))
  12141. (if (member tg current)
  12142. (setq current (delete tg current))
  12143. (push tg current)))
  12144. (if exit-after-next (setq exit-after-next 'now)))
  12145. ((setq e (rassoc c todo-table) tg (car e))
  12146. (with-current-buffer buf
  12147. (save-excursion (org-todo tg)))
  12148. (if exit-after-next (setq exit-after-next 'now)))
  12149. ((setq e (rassoc c ntable) tg (car e))
  12150. (if (member tg current)
  12151. (setq current (delete tg current))
  12152. (loop for g in groups do
  12153. (if (member tg g)
  12154. (mapc (lambda (x)
  12155. (setq current (delete x current)))
  12156. g)))
  12157. (push tg current))
  12158. (if exit-after-next (setq exit-after-next 'now))))
  12159. ;; Create a sorted list
  12160. (setq current
  12161. (sort current
  12162. (lambda (a b)
  12163. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12164. (if (eq exit-after-next 'now) (throw 'exit t))
  12165. (goto-char (point-min))
  12166. (beginning-of-line 2)
  12167. (delete-region (point) (point-at-eol))
  12168. (org-fast-tag-insert "Current" current c-face)
  12169. (org-set-current-tags-overlay current ov-prefix)
  12170. (while (re-search-forward
  12171. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12172. (setq tg (match-string 1))
  12173. (add-text-properties
  12174. (match-beginning 1) (match-end 1)
  12175. (list 'face
  12176. (cond
  12177. ((member tg current) c-face)
  12178. ((member tg inherited) i-face)
  12179. (t (get-text-property (match-beginning 1) 'face))))))
  12180. (goto-char (point-min)))))
  12181. (org-detach-overlay org-tags-overlay)
  12182. (if rtn
  12183. (mapconcat 'identity current ":")
  12184. nil))))
  12185. (defun org-get-tags-string ()
  12186. "Get the TAGS string in the current headline."
  12187. (unless (org-on-heading-p t)
  12188. (error "Not on a heading"))
  12189. (save-excursion
  12190. (beginning-of-line 1)
  12191. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12192. (org-match-string-no-properties 1)
  12193. "")))
  12194. (defun org-get-tags ()
  12195. "Get the list of tags specified in the current headline."
  12196. (org-split-string (org-get-tags-string) ":"))
  12197. (defun org-get-buffer-tags ()
  12198. "Get a table of all tags used in the buffer, for completion."
  12199. (let (tags)
  12200. (save-excursion
  12201. (goto-char (point-min))
  12202. (while (re-search-forward
  12203. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12204. (when (equal (char-after (point-at-bol 0)) ?*)
  12205. (mapc (lambda (x) (add-to-list 'tags x))
  12206. (org-split-string (org-match-string-no-properties 1) ":")))))
  12207. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12208. (mapcar 'list tags)))
  12209. ;;;; The mapping API
  12210. ;;;###autoload
  12211. (defun org-map-entries (func &optional match scope &rest skip)
  12212. "Call FUNC at each headline selected by MATCH in SCOPE.
  12213. FUNC is a function or a lisp form. The function will be called without
  12214. arguments, with the cursor positioned at the beginning of the headline.
  12215. The return values of all calls to the function will be collected and
  12216. returned as a list.
  12217. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12218. does not need to preserve point. After evaluation, the cursor will be
  12219. moved to the end of the line (presumably of the headline of the
  12220. processed entry) and search continues from there. Under some
  12221. circumstances, this may not produce the wanted results. For example,
  12222. if you have removed (e.g. archived) the current (sub)tree it could
  12223. mean that the next entry will be skipped entirely. In such cases, you
  12224. can specify the position from where search should continue by making
  12225. FUNC set the variable `org-map-continue-from' to the desired buffer
  12226. position.
  12227. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12228. Only headlines that are matched by this query will be considered during
  12229. the iteration. When MATCH is nil or t, all headlines will be
  12230. visited by the iteration.
  12231. SCOPE determines the scope of this command. It can be any of:
  12232. nil The current buffer, respecting the restriction if any
  12233. tree The subtree started with the entry at point
  12234. file The current buffer, without restriction
  12235. file-with-archives
  12236. The current buffer, and any archives associated with it
  12237. agenda All agenda files
  12238. agenda-with-archives
  12239. All agenda files with any archive files associated with them
  12240. \(file1 file2 ...)
  12241. If this is a list, all files in the list will be scanned
  12242. The remaining args are treated as settings for the skipping facilities of
  12243. the scanner. The following items can be given here:
  12244. archive skip trees with the archive tag.
  12245. comment skip trees with the COMMENT keyword
  12246. function or Emacs Lisp form:
  12247. will be used as value for `org-agenda-skip-function', so whenever
  12248. the function returns t, FUNC will not be called for that
  12249. entry and search will continue from the point where the
  12250. function leaves it.
  12251. If your function needs to retrieve the tags including inherited tags
  12252. at the *current* entry, you can use the value of the variable
  12253. `org-scanner-tags' which will be much faster than getting the value
  12254. with `org-get-tags-at'. If your function gets properties with
  12255. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12256. to t around the call to `org-entry-properties' to get the same speedup.
  12257. Note that if your function moves around to retrieve tags and properties at
  12258. a *different* entry, you cannot use these techniques."
  12259. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12260. (org-agenda-skip-archived-trees (memq 'archive skip))
  12261. (org-agenda-skip-comment-trees (memq 'comment skip))
  12262. (org-agenda-skip-function
  12263. (car (org-delete-all '(comment archive) skip)))
  12264. (org-tags-match-list-sublevels t)
  12265. matcher file res
  12266. org-todo-keywords-for-agenda
  12267. org-done-keywords-for-agenda
  12268. org-todo-keyword-alist-for-agenda
  12269. org-drawers-for-agenda
  12270. org-tag-alist-for-agenda)
  12271. (cond
  12272. ((eq match t) (setq matcher t))
  12273. ((eq match nil) (setq matcher t))
  12274. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12275. (save-excursion
  12276. (save-restriction
  12277. (when (eq scope 'tree)
  12278. (org-back-to-heading t)
  12279. (org-narrow-to-subtree)
  12280. (setq scope nil))
  12281. (if (not scope)
  12282. (progn
  12283. (org-prepare-agenda-buffers
  12284. (list (buffer-file-name (current-buffer))))
  12285. (setq res (org-scan-tags func matcher)))
  12286. ;; Get the right scope
  12287. (cond
  12288. ((and scope (listp scope) (symbolp (car scope)))
  12289. (setq scope (eval scope)))
  12290. ((eq scope 'agenda)
  12291. (setq scope (org-agenda-files t)))
  12292. ((eq scope 'agenda-with-archives)
  12293. (setq scope (org-agenda-files t))
  12294. (setq scope (org-add-archive-files scope)))
  12295. ((eq scope 'file)
  12296. (setq scope (list (buffer-file-name))))
  12297. ((eq scope 'file-with-archives)
  12298. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12299. (org-prepare-agenda-buffers scope)
  12300. (while (setq file (pop scope))
  12301. (with-current-buffer (org-find-base-buffer-visiting file)
  12302. (save-excursion
  12303. (save-restriction
  12304. (widen)
  12305. (goto-char (point-min))
  12306. (setq res (append res (org-scan-tags func matcher))))))))))
  12307. res))
  12308. ;;;; Properties
  12309. ;;; Setting and retrieving properties
  12310. (defconst org-special-properties
  12311. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12312. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12313. "The special properties valid in Org-mode.
  12314. These are properties that are not defined in the property drawer,
  12315. but in some other way.")
  12316. (defconst org-default-properties
  12317. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12318. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12319. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12320. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12321. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12322. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12323. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12324. "Some properties that are used by Org-mode for various purposes.
  12325. Being in this list makes sure that they are offered for completion.")
  12326. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12327. "Regular expression matching the first line of a property drawer.")
  12328. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12329. "Regular expression matching the last line of a property drawer.")
  12330. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12331. "Regular expression matching the first line of a property drawer.")
  12332. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12333. "Regular expression matching the first line of a property drawer.")
  12334. (defconst org-property-drawer-re
  12335. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12336. org-property-end-re "\\)\n?")
  12337. "Matches an entire property drawer.")
  12338. (defconst org-clock-drawer-re
  12339. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12340. org-property-end-re "\\)\n?")
  12341. "Matches an entire clock drawer.")
  12342. (defsubst org-re-property (property)
  12343. "Return a regexp matching PROPERTY.
  12344. Match group 1 will be set to the value "
  12345. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12346. (defun org-property-action ()
  12347. "Do an action on properties."
  12348. (interactive)
  12349. (let (c)
  12350. (org-at-property-p)
  12351. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12352. (setq c (read-char-exclusive))
  12353. (cond
  12354. ((equal c ?s)
  12355. (call-interactively 'org-set-property))
  12356. ((equal c ?d)
  12357. (call-interactively 'org-delete-property))
  12358. ((equal c ?D)
  12359. (call-interactively 'org-delete-property-globally))
  12360. ((equal c ?c)
  12361. (call-interactively 'org-compute-property-at-point))
  12362. (t (error "No such property action %c" c)))))
  12363. (defun org-set-effort (&optional value)
  12364. "Set the effort property of the current entry.
  12365. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12366. allowed value."
  12367. (interactive "P")
  12368. (if (equal value 0) (setq value 10))
  12369. (let* ((completion-ignore-case t)
  12370. (prop org-effort-property)
  12371. (cur (org-entry-get nil prop))
  12372. (allowed (org-property-get-allowed-values nil prop 'table))
  12373. (existing (mapcar 'list (org-property-values prop)))
  12374. rpl
  12375. (val (cond
  12376. ((stringp value) value)
  12377. ((and allowed (integerp value))
  12378. (or (car (nth (1- value) allowed))
  12379. (car (org-last allowed))))
  12380. (allowed
  12381. (message "Select 1-9,0, [RET%s]: %s"
  12382. (if cur (concat "=" cur) "")
  12383. (mapconcat 'car allowed " "))
  12384. (setq rpl (read-char-exclusive))
  12385. (if (equal rpl ?\r)
  12386. cur
  12387. (setq rpl (- rpl ?0))
  12388. (if (equal rpl 0) (setq rpl 10))
  12389. (if (and (> rpl 0) (<= rpl (length allowed)))
  12390. (car (nth (1- rpl) allowed))
  12391. (org-completing-read "Effort: " allowed nil))))
  12392. (t
  12393. (let (org-completion-use-ido org-completion-use-iswitchb)
  12394. (org-completing-read
  12395. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12396. (concat "[" cur "]") "")
  12397. ": ")
  12398. existing nil nil "" nil cur))))))
  12399. (unless (equal (org-entry-get nil prop) val)
  12400. (org-entry-put nil prop val))
  12401. (message "%s is now %s" prop val)))
  12402. (defun org-at-property-p ()
  12403. "Is cursor inside a property drawer?"
  12404. (save-excursion
  12405. (beginning-of-line 1)
  12406. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12407. (save-match-data ;; Used by calling procedures
  12408. (let ((p (point))
  12409. (range (unless (org-before-first-heading-p)
  12410. (org-get-property-block))))
  12411. (and range (<= (car range) p) (< p (cdr range))))))))
  12412. (defun org-get-property-block (&optional beg end force)
  12413. "Return the (beg . end) range of the body of the property drawer.
  12414. BEG and END can be beginning and end of subtree, if not given
  12415. they will be found.
  12416. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12417. (catch 'exit
  12418. (save-excursion
  12419. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12420. (end (or end (progn (outline-next-heading) (point)))))
  12421. (goto-char beg)
  12422. (if (re-search-forward org-property-start-re end t)
  12423. (setq beg (1+ (match-end 0)))
  12424. (if force
  12425. (save-excursion
  12426. (org-insert-property-drawer)
  12427. (setq end (progn (outline-next-heading) (point))))
  12428. (throw 'exit nil))
  12429. (goto-char beg)
  12430. (if (re-search-forward org-property-start-re end t)
  12431. (setq beg (1+ (match-end 0)))))
  12432. (if (re-search-forward org-property-end-re end t)
  12433. (setq end (match-beginning 0))
  12434. (or force (throw 'exit nil))
  12435. (goto-char beg)
  12436. (setq end beg)
  12437. (org-indent-line-function)
  12438. (insert ":END:\n"))
  12439. (cons beg end)))))
  12440. (defun org-entry-properties (&optional pom which specific)
  12441. "Get all properties of the entry at point-or-marker POM.
  12442. This includes the TODO keyword, the tags, time strings for deadline,
  12443. scheduled, and clocking, and any additional properties defined in the
  12444. entry. The return value is an alist, keys may occur multiple times
  12445. if the property key was used several times.
  12446. POM may also be nil, in which case the current entry is used.
  12447. If WHICH is nil or `all', get all properties. If WHICH is
  12448. `special' or `standard', only get that subclass. If WHICH
  12449. is a string only get exactly this property. SPECIFIC can be a string, the
  12450. specific property we are interested in. Specifying it can speed
  12451. things up because then unnecessary parsing is avoided."
  12452. (setq which (or which 'all))
  12453. (org-with-point-at pom
  12454. (let ((clockstr (substring org-clock-string 0 -1))
  12455. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12456. (case-fold-search nil)
  12457. beg end range props sum-props key key1 value string clocksum)
  12458. (save-excursion
  12459. (when (condition-case nil
  12460. (and (org-mode-p) (org-back-to-heading t))
  12461. (error nil))
  12462. (setq beg (point))
  12463. (setq sum-props (get-text-property (point) 'org-summaries))
  12464. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12465. (outline-next-heading)
  12466. (setq end (point))
  12467. (when (memq which '(all special))
  12468. ;; Get the special properties, like TODO and tags
  12469. (goto-char beg)
  12470. (when (and (or (not specific) (string= specific "TODO"))
  12471. (looking-at org-todo-line-regexp) (match-end 2))
  12472. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12473. (when (and (or (not specific) (string= specific "PRIORITY"))
  12474. (looking-at org-priority-regexp))
  12475. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12476. (when (or (not specific) (string= specific "FILE"))
  12477. (push (cons "FILE" buffer-file-name) props))
  12478. (when (and (or (not specific) (string= specific "TAGS"))
  12479. (setq value (org-get-tags-string))
  12480. (string-match "\\S-" value))
  12481. (push (cons "TAGS" value) props))
  12482. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12483. (setq value (org-get-tags-at)))
  12484. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12485. ":"))
  12486. props))
  12487. (when (or (not specific) (string= specific "BLOCKED"))
  12488. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12489. (when (or (not specific)
  12490. (member specific
  12491. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12492. "TIMESTAMP" "TIMESTAMP_IA")))
  12493. (catch 'match
  12494. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12495. (setq key (if (match-end 1)
  12496. (substring (org-match-string-no-properties 1)
  12497. 0 -1))
  12498. string (if (equal key clockstr)
  12499. (org-no-properties
  12500. (org-trim
  12501. (buffer-substring
  12502. (match-beginning 3) (goto-char
  12503. (point-at-eol)))))
  12504. (substring (org-match-string-no-properties 3)
  12505. 1 -1)))
  12506. ;; Get the correct property name from the key. This is
  12507. ;; necessary if the user has configured time keywords.
  12508. (setq key1 (concat key ":"))
  12509. (cond
  12510. ((not key)
  12511. (setq key
  12512. (if (= (char-after (match-beginning 3)) ?\[)
  12513. "TIMESTAMP_IA" "TIMESTAMP")))
  12514. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12515. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12516. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12517. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12518. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12519. (progn
  12520. (push (cons key string) props)
  12521. ;; no need to search further if match is found
  12522. (throw 'match t))
  12523. (when (or (equal key "CLOCK") (not (assoc key props)))
  12524. (push (cons key string) props))))))
  12525. )
  12526. (when (memq which '(all standard))
  12527. ;; Get the standard properties, like :PROP: ...
  12528. (setq range (org-get-property-block beg end))
  12529. (when range
  12530. (goto-char (car range))
  12531. (while (re-search-forward
  12532. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12533. (cdr range) t)
  12534. (setq key (org-match-string-no-properties 1)
  12535. value (org-trim (or (org-match-string-no-properties 2) "")))
  12536. (unless (member key excluded)
  12537. (push (cons key (or value "")) props)))))
  12538. (if clocksum
  12539. (push (cons "CLOCKSUM"
  12540. (org-columns-number-to-string (/ (float clocksum) 60.)
  12541. 'add_times))
  12542. props))
  12543. (unless (assoc "CATEGORY" props)
  12544. (push (cons "CATEGORY" (org-get-category)) props))
  12545. (append sum-props (nreverse props)))))))
  12546. (defun org-entry-get (pom property &optional inherit literal-nil)
  12547. "Get value of PROPERTY for entry at point-or-marker POM.
  12548. If INHERIT is non-nil and the entry does not have the property,
  12549. then also check higher levels of the hierarchy.
  12550. If INHERIT is the symbol `selective', use inheritance only if the setting
  12551. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12552. If the property is present but empty, the return value is the empty string.
  12553. If the property is not present at all, nil is returned.
  12554. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12555. do not interpret it as the list atom nil. This is used for inheritance
  12556. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12557. (org-with-point-at pom
  12558. (if (and inherit (if (eq inherit 'selective)
  12559. (org-property-inherit-p property)
  12560. t))
  12561. (org-entry-get-with-inheritance property literal-nil)
  12562. (if (member property org-special-properties)
  12563. ;; We need a special property. Use `org-entry-properties' to
  12564. ;; retrieve it, but specify the wanted property
  12565. (cdr (assoc property (org-entry-properties nil 'special property)))
  12566. (let ((range (unless (org-before-first-heading-p)
  12567. (org-get-property-block))))
  12568. (if (and range
  12569. (goto-char (car range))
  12570. (re-search-forward
  12571. (org-re-property property)
  12572. (cdr range) t))
  12573. ;; Found the property, return it.
  12574. (if (match-end 1)
  12575. (if literal-nil
  12576. (org-match-string-no-properties 1)
  12577. (org-not-nil (org-match-string-no-properties 1)))
  12578. "")))))))
  12579. (defun org-property-or-variable-value (var &optional inherit)
  12580. "Check if there is a property fixing the value of VAR.
  12581. If yes, return this value. If not, return the current value of the variable."
  12582. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12583. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12584. (read prop)
  12585. (symbol-value var))))
  12586. (defun org-entry-delete (pom property)
  12587. "Delete the property PROPERTY from entry at point-or-marker POM."
  12588. (org-with-point-at pom
  12589. (if (member property org-special-properties)
  12590. nil ; cannot delete these properties.
  12591. (let ((range (org-get-property-block)))
  12592. (if (and range
  12593. (goto-char (car range))
  12594. (re-search-forward
  12595. (org-re-property property)
  12596. (cdr range) t))
  12597. (progn
  12598. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12599. t)
  12600. nil)))))
  12601. ;; Multi-values properties are properties that contain multiple values
  12602. ;; These values are assumed to be single words, separated by whitespace.
  12603. (defun org-entry-add-to-multivalued-property (pom property value)
  12604. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12605. (let* ((old (org-entry-get pom property))
  12606. (values (and old (org-split-string old "[ \t]"))))
  12607. (setq value (org-entry-protect-space value))
  12608. (unless (member value values)
  12609. (setq values (cons value values))
  12610. (org-entry-put pom property
  12611. (mapconcat 'identity values " ")))))
  12612. (defun org-entry-remove-from-multivalued-property (pom property value)
  12613. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12614. (let* ((old (org-entry-get pom property))
  12615. (values (and old (org-split-string old "[ \t]"))))
  12616. (setq value (org-entry-protect-space value))
  12617. (when (member value values)
  12618. (setq values (delete value values))
  12619. (org-entry-put pom property
  12620. (mapconcat 'identity values " ")))))
  12621. (defun org-entry-member-in-multivalued-property (pom property value)
  12622. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12623. (let* ((old (org-entry-get pom property))
  12624. (values (and old (org-split-string old "[ \t]"))))
  12625. (setq value (org-entry-protect-space value))
  12626. (member value values)))
  12627. (defun org-entry-get-multivalued-property (pom property)
  12628. "Return a list of values in a multivalued property."
  12629. (let* ((value (org-entry-get pom property))
  12630. (values (and value (org-split-string value "[ \t]"))))
  12631. (mapcar 'org-entry-restore-space values)))
  12632. (defun org-entry-put-multivalued-property (pom property &rest values)
  12633. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12634. VALUES should be a list of strings. Spaces will be protected."
  12635. (org-entry-put pom property
  12636. (mapconcat 'org-entry-protect-space values " "))
  12637. (let* ((value (org-entry-get pom property))
  12638. (values (and value (org-split-string value "[ \t]"))))
  12639. (mapcar 'org-entry-restore-space values)))
  12640. (defun org-entry-protect-space (s)
  12641. "Protect spaces and newline in string S."
  12642. (while (string-match " " s)
  12643. (setq s (replace-match "%20" t t s)))
  12644. (while (string-match "\n" s)
  12645. (setq s (replace-match "%0A" t t s)))
  12646. s)
  12647. (defun org-entry-restore-space (s)
  12648. "Restore spaces and newline in string S."
  12649. (while (string-match "%20" s)
  12650. (setq s (replace-match " " t t s)))
  12651. (while (string-match "%0A" s)
  12652. (setq s (replace-match "\n" t t s)))
  12653. s)
  12654. (defvar org-entry-property-inherited-from (make-marker)
  12655. "Marker pointing to the entry from where a property was inherited.
  12656. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12657. location of the entry where the inheritance search matched. If there was
  12658. no match, the marker will point nowhere.
  12659. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12660. is set.")
  12661. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12662. "Get entry property, and search higher levels if not present.
  12663. The search will stop at the first ancestor which has the property defined.
  12664. If the value found is \"nil\", return nil to show that the property
  12665. should be considered as undefined (this is the meaning of nil here).
  12666. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12667. (move-marker org-entry-property-inherited-from nil)
  12668. (let (tmp)
  12669. (unless (org-before-first-heading-p)
  12670. (save-excursion
  12671. (save-restriction
  12672. (widen)
  12673. (catch 'ex
  12674. (while t
  12675. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12676. (org-back-to-heading t)
  12677. (move-marker org-entry-property-inherited-from (point))
  12678. (throw 'ex tmp))
  12679. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12680. (setq tmp (or tmp
  12681. (cdr (assoc property org-file-properties))
  12682. (cdr (assoc property org-global-properties))
  12683. (cdr (assoc property org-global-properties-fixed))))
  12684. (if literal-nil tmp (org-not-nil tmp))))
  12685. (defvar org-property-changed-functions nil
  12686. "Hook called when the value of a property has changed.
  12687. Each hook function should accept two arguments, the name of the property
  12688. and the new value.")
  12689. (defun org-entry-put (pom property value)
  12690. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12691. (org-with-point-at pom
  12692. (org-back-to-heading t)
  12693. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12694. range)
  12695. (cond
  12696. ((equal property "TODO")
  12697. (when (and (stringp value) (string-match "\\S-" value)
  12698. (not (member value org-todo-keywords-1)))
  12699. (error "\"%s\" is not a valid TODO state" value))
  12700. (if (or (not value)
  12701. (not (string-match "\\S-" value)))
  12702. (setq value 'none))
  12703. (org-todo value)
  12704. (org-set-tags nil 'align))
  12705. ((equal property "PRIORITY")
  12706. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12707. (string-to-char value) ?\ ))
  12708. (org-set-tags nil 'align))
  12709. ((equal property "SCHEDULED")
  12710. (if (re-search-forward org-scheduled-time-regexp end t)
  12711. (cond
  12712. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12713. ((eq value 'later) (org-timestamp-change 1 'day))
  12714. (t (call-interactively 'org-schedule)))
  12715. (call-interactively 'org-schedule)))
  12716. ((equal property "DEADLINE")
  12717. (if (re-search-forward org-deadline-time-regexp end t)
  12718. (cond
  12719. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12720. ((eq value 'later) (org-timestamp-change 1 'day))
  12721. (t (call-interactively 'org-deadline)))
  12722. (call-interactively 'org-deadline)))
  12723. ((member property org-special-properties)
  12724. (error "The %s property can not yet be set with `org-entry-put'"
  12725. property))
  12726. (t ; a non-special property
  12727. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12728. (setq range (org-get-property-block beg end 'force))
  12729. (goto-char (car range))
  12730. (if (re-search-forward
  12731. (org-re-property property) (cdr range) t)
  12732. (progn
  12733. (delete-region (match-beginning 0) (match-end 0))
  12734. (goto-char (match-beginning 0)))
  12735. (goto-char (cdr range))
  12736. (insert "\n")
  12737. (backward-char 1)
  12738. (org-indent-line-function))
  12739. (insert ":" property ":")
  12740. (and value (insert " " value))
  12741. (org-indent-line-function)))))
  12742. (run-hook-with-args 'org-property-changed-functions property value)))
  12743. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12744. "Get all property keys in the current buffer.
  12745. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12746. like tags and TODO state.
  12747. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12748. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  12749. and others.
  12750. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12751. formats in the current buffer."
  12752. (let (rtn range cfmt s p)
  12753. (save-excursion
  12754. (save-restriction
  12755. (widen)
  12756. (goto-char (point-min))
  12757. (while (re-search-forward org-property-start-re nil t)
  12758. (setq range (org-get-property-block))
  12759. (goto-char (car range))
  12760. (while (re-search-forward
  12761. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12762. (cdr range) t)
  12763. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12764. (outline-next-heading))))
  12765. (when include-specials
  12766. (setq rtn (append org-special-properties rtn)))
  12767. (when include-defaults
  12768. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12769. (add-to-list 'rtn org-effort-property))
  12770. (when include-columns
  12771. (save-excursion
  12772. (save-restriction
  12773. (widen)
  12774. (goto-char (point-min))
  12775. (while (re-search-forward
  12776. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12777. nil t)
  12778. (setq cfmt (match-string 2) s 0)
  12779. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12780. cfmt s)
  12781. (setq s (match-end 0)
  12782. p (match-string 1 cfmt))
  12783. (unless (or (equal p "ITEM")
  12784. (member p org-special-properties))
  12785. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12786. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12787. (defun org-property-values (key)
  12788. "Return a list of all values of property KEY in the current buffer."
  12789. (save-excursion
  12790. (save-restriction
  12791. (widen)
  12792. (goto-char (point-min))
  12793. (let ((re (org-re-property key))
  12794. values)
  12795. (while (re-search-forward re nil t)
  12796. (add-to-list 'values (org-trim (match-string 1))))
  12797. (delete "" values)))))
  12798. (defun org-insert-property-drawer ()
  12799. "Insert a property drawer into the current entry."
  12800. (interactive)
  12801. (org-back-to-heading t)
  12802. (looking-at outline-regexp)
  12803. (let ((indent (if org-adapt-indentation
  12804. (- (match-end 0)(match-beginning 0))
  12805. 0))
  12806. (beg (point))
  12807. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12808. end hiddenp)
  12809. (outline-next-heading)
  12810. (setq end (point))
  12811. (goto-char beg)
  12812. (while (re-search-forward re end t))
  12813. (setq hiddenp (outline-invisible-p))
  12814. (end-of-line 1)
  12815. (and (equal (char-after) ?\n) (forward-char 1))
  12816. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12817. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12818. ;; just skip this line
  12819. (beginning-of-line 2)
  12820. ;; Drawer start, find the end
  12821. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12822. (beginning-of-line 1)))
  12823. (org-skip-over-state-notes)
  12824. (skip-chars-backward " \t\n\r")
  12825. (if (eq (char-before) ?*) (forward-char 1))
  12826. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12827. (beginning-of-line 0)
  12828. (org-indent-to-column indent)
  12829. (beginning-of-line 2)
  12830. (org-indent-to-column indent)
  12831. (beginning-of-line 0)
  12832. (if hiddenp
  12833. (save-excursion
  12834. (org-back-to-heading t)
  12835. (hide-entry))
  12836. (org-flag-drawer t))))
  12837. (defvar org-property-set-functions-alist nil
  12838. "Property set function alist.
  12839. Each entry should have the following format:
  12840. (PROPERTY . READ-FUNCTION)
  12841. The read function will be called with the same argument as
  12842. `org-completing-read'.")
  12843. (defun org-set-property-function (property)
  12844. "Get the function that should be used to set PROPERTY.
  12845. This is computed according to `org-property-set-functions-alist'."
  12846. (or (cdr (assoc property org-property-set-functions-alist))
  12847. 'org-completing-read))
  12848. (defun org-read-property-value (property)
  12849. "Read PROPERTY value from user."
  12850. (let* ((completion-ignore-case t)
  12851. (allowed (org-property-get-allowed-values nil property 'table))
  12852. (cur (org-entry-get nil property))
  12853. (prompt (concat property " value"
  12854. (if (and cur (string-match "\\S-" cur))
  12855. (concat " [" cur "]") "") ": "))
  12856. (set-function (org-set-property-function property))
  12857. (val (if allowed
  12858. (funcall set-function prompt allowed nil
  12859. (not (get-text-property 0 'org-unrestricted
  12860. (caar allowed))))
  12861. (let (org-completion-use-ido org-completion-use-iswitchb)
  12862. (funcall set-function prompt
  12863. (mapcar 'list (org-property-values property))
  12864. nil nil "" nil cur)))))
  12865. (if (equal val "")
  12866. cur
  12867. val)))
  12868. (defun org-read-property-name ()
  12869. "Read a property name."
  12870. (let* ((completion-ignore-case t)
  12871. (keys (org-buffer-property-keys nil t t))
  12872. (default-prop (save-excursion
  12873. (save-match-data
  12874. (beginning-of-line)
  12875. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  12876. (null (string= (match-string 1) "END"))
  12877. (match-string 1)))))
  12878. (property (org-icompleting-read
  12879. (concat "Property"
  12880. (if default-prop (concat " [" default-prop "]") "")
  12881. ": ")
  12882. (mapcar 'list keys)
  12883. nil nil nil nil
  12884. default-prop
  12885. )))
  12886. (if (member property keys)
  12887. property
  12888. (or (cdr (assoc (downcase property)
  12889. (mapcar (lambda (x) (cons (downcase x) x))
  12890. keys)))
  12891. property))))
  12892. (defun org-set-property (property value)
  12893. "In the current entry, set PROPERTY to VALUE.
  12894. When called interactively, this will prompt for a property name, offering
  12895. completion on existing and default properties. And then it will prompt
  12896. for a value, offering completion either on allowed values (via an inherited
  12897. xxx_ALL property) or on existing values in other instances of this property
  12898. in the current file."
  12899. (interactive (list nil nil))
  12900. (let* ((property (or property (org-read-property-name)))
  12901. (value (or value (org-read-property-value property))))
  12902. (unless (equal (org-entry-get nil property) value)
  12903. (org-entry-put nil property value))))
  12904. (defun org-delete-property (property)
  12905. "In the current entry, delete PROPERTY."
  12906. (interactive
  12907. (let* ((completion-ignore-case t)
  12908. (prop (org-icompleting-read "Property: "
  12909. (org-entry-properties nil 'standard))))
  12910. (list prop)))
  12911. (message "Property %s %s" property
  12912. (if (org-entry-delete nil property)
  12913. "deleted"
  12914. "was not present in the entry")))
  12915. (defun org-delete-property-globally (property)
  12916. "Remove PROPERTY globally, from all entries."
  12917. (interactive
  12918. (let* ((completion-ignore-case t)
  12919. (prop (org-icompleting-read
  12920. "Globally remove property: "
  12921. (mapcar 'list (org-buffer-property-keys)))))
  12922. (list prop)))
  12923. (save-excursion
  12924. (save-restriction
  12925. (widen)
  12926. (goto-char (point-min))
  12927. (let ((cnt 0))
  12928. (while (re-search-forward
  12929. (org-re-property property)
  12930. nil t)
  12931. (setq cnt (1+ cnt))
  12932. (replace-match ""))
  12933. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12934. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12935. (defun org-compute-property-at-point ()
  12936. "Compute the property at point.
  12937. This looks for an enclosing column format, extracts the operator and
  12938. then applies it to the property in the column format's scope."
  12939. (interactive)
  12940. (unless (org-at-property-p)
  12941. (error "Not at a property"))
  12942. (let ((prop (org-match-string-no-properties 2)))
  12943. (org-columns-get-format-and-top-level)
  12944. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12945. (error "No operator defined for property %s" prop))
  12946. (org-columns-compute prop)))
  12947. (defvar org-property-allowed-value-functions nil
  12948. "Hook for functions supplying allowed values for a specific property.
  12949. The functions must take a single argument, the name of the property, and
  12950. return a flat list of allowed values. If \":ETC\" is one of
  12951. the values, this means that these values are intended as defaults for
  12952. completion, but that other values should be allowed too.
  12953. The functions must return nil if they are not responsible for this
  12954. property.")
  12955. (defun org-property-get-allowed-values (pom property &optional table)
  12956. "Get allowed values for the property PROPERTY.
  12957. When TABLE is non-nil, return an alist that can directly be used for
  12958. completion."
  12959. (let (vals)
  12960. (cond
  12961. ((equal property "TODO")
  12962. (setq vals (org-with-point-at pom
  12963. (append org-todo-keywords-1 '("")))))
  12964. ((equal property "PRIORITY")
  12965. (let ((n org-lowest-priority))
  12966. (while (>= n org-highest-priority)
  12967. (push (char-to-string n) vals)
  12968. (setq n (1- n)))))
  12969. ((member property org-special-properties))
  12970. ((setq vals (run-hook-with-args-until-success
  12971. 'org-property-allowed-value-functions property)))
  12972. (t
  12973. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12974. (when (and vals (string-match "\\S-" vals))
  12975. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12976. (setq vals (mapcar (lambda (x)
  12977. (cond ((stringp x) x)
  12978. ((numberp x) (number-to-string x))
  12979. ((symbolp x) (symbol-name x))
  12980. (t "???")))
  12981. vals)))))
  12982. (when (member ":ETC" vals)
  12983. (setq vals (remove ":ETC" vals))
  12984. (org-add-props (car vals) '(org-unrestricted t)))
  12985. (if table (mapcar 'list vals) vals)))
  12986. (defun org-property-previous-allowed-value (&optional previous)
  12987. "Switch to the next allowed value for this property."
  12988. (interactive)
  12989. (org-property-next-allowed-value t))
  12990. (defun org-property-next-allowed-value (&optional previous)
  12991. "Switch to the next allowed value for this property."
  12992. (interactive)
  12993. (unless (org-at-property-p)
  12994. (error "Not at a property"))
  12995. (let* ((key (match-string 2))
  12996. (value (match-string 3))
  12997. (allowed (or (org-property-get-allowed-values (point) key)
  12998. (and (member value '("[ ]" "[-]" "[X]"))
  12999. '("[ ]" "[X]"))))
  13000. nval)
  13001. (unless allowed
  13002. (error "Allowed values for this property have not been defined"))
  13003. (if previous (setq allowed (reverse allowed)))
  13004. (if (member value allowed)
  13005. (setq nval (car (cdr (member value allowed)))))
  13006. (setq nval (or nval (car allowed)))
  13007. (if (equal nval value)
  13008. (error "Only one allowed value for this property"))
  13009. (org-at-property-p)
  13010. (replace-match (concat " :" key ": " nval) t t)
  13011. (org-indent-line-function)
  13012. (beginning-of-line 1)
  13013. (skip-chars-forward " \t")
  13014. (run-hook-with-args 'org-property-changed-functions key nval)))
  13015. (defun org-find-olp (path &optional this-buffer)
  13016. "Return a marker pointing to the entry at outline path OLP.
  13017. If anything goes wrong, throw an error.
  13018. You can wrap this call to catch the error like this:
  13019. (condition-case msg
  13020. (org-mobile-locate-entry (match-string 4))
  13021. (error (nth 1 msg)))
  13022. The return value will then be either a string with the error message,
  13023. or a marker if everything is OK.
  13024. If THIS-BUFFER is set, the outline path does not contain a file,
  13025. only headings."
  13026. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13027. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13028. (level 1)
  13029. (lmin 1)
  13030. (lmax 1)
  13031. limit re end found pos heading cnt flevel)
  13032. (unless buffer (error "File not found :%s" file))
  13033. (with-current-buffer buffer
  13034. (save-excursion
  13035. (save-restriction
  13036. (widen)
  13037. (setq limit (point-max))
  13038. (goto-char (point-min))
  13039. (while (setq heading (pop path))
  13040. (setq re (format org-complex-heading-regexp-format
  13041. (regexp-quote heading)))
  13042. (setq cnt 0 pos (point))
  13043. (while (re-search-forward re end t)
  13044. (setq level (- (match-end 1) (match-beginning 1)))
  13045. (if (and (>= level lmin) (<= level lmax))
  13046. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13047. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13048. lmax heading))
  13049. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13050. lmax heading))
  13051. (goto-char found)
  13052. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13053. (setq end (save-excursion (org-end-of-subtree t t))))
  13054. (when (org-on-heading-p)
  13055. (move-marker (make-marker) (point))))))))
  13056. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13057. "Find node HEADING in BUFFER.
  13058. Return a marker to the heading if it was found, or nil if not.
  13059. If POS-ONLY is set, return just the position instead of a marker.
  13060. The heading text must match exact, but it may have a TODO keyword,
  13061. a priority cookie and tags in the standard locations."
  13062. (with-current-buffer (or buffer (current-buffer))
  13063. (save-excursion
  13064. (save-restriction
  13065. (widen)
  13066. (goto-char (point-min))
  13067. (let (case-fold-search)
  13068. (if (re-search-forward
  13069. (format org-complex-heading-regexp-format
  13070. (regexp-quote heading)) nil t)
  13071. (if pos-only
  13072. (match-beginning 0)
  13073. (move-marker (make-marker) (match-beginning 0)))))))))
  13074. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13075. "Find Org node headline HEADING in all .org files in directory DIR.
  13076. When the target headline is found, return a marker to this location."
  13077. (let ((files (directory-files (or dir default-directory)
  13078. nil "\\`[^.#].*\\.org\\'"))
  13079. file visiting m buffer)
  13080. (catch 'found
  13081. (while (setq file (pop files))
  13082. (message "trying %s" file)
  13083. (setq visiting (org-find-base-buffer-visiting file))
  13084. (setq buffer (or visiting (find-file-noselect file)))
  13085. (setq m (org-find-exact-headline-in-buffer
  13086. heading buffer))
  13087. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13088. (and m (throw 'found m))))))
  13089. (defun org-find-entry-with-id (ident)
  13090. "Locate the entry that contains the ID property with exact value IDENT.
  13091. IDENT can be a string, a symbol or a number, this function will search for
  13092. the string representation of it.
  13093. Return the position where this entry starts, or nil if there is no such entry."
  13094. (interactive "sID: ")
  13095. (let ((id (cond
  13096. ((stringp ident) ident)
  13097. ((symbol-name ident) (symbol-name ident))
  13098. ((numberp ident) (number-to-string ident))
  13099. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13100. (case-fold-search nil))
  13101. (save-excursion
  13102. (save-restriction
  13103. (widen)
  13104. (goto-char (point-min))
  13105. (when (re-search-forward
  13106. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13107. nil t)
  13108. (org-back-to-heading t)
  13109. (point))))))
  13110. ;;;; Timestamps
  13111. (defvar org-last-changed-timestamp nil)
  13112. (defvar org-last-inserted-timestamp nil
  13113. "The last time stamp inserted with `org-insert-time-stamp'.")
  13114. (defvar org-time-was-given) ; dynamically scoped parameter
  13115. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13116. (defvar org-ts-what) ; dynamically scoped parameter
  13117. (defun org-time-stamp (arg &optional inactive)
  13118. "Prompt for a date/time and insert a time stamp.
  13119. If the user specifies a time like HH:MM, or if this command is called
  13120. with a prefix argument, the time stamp will contain date and time.
  13121. Otherwise, only the date will be included. All parts of a date not
  13122. specified by the user will be filled in from the current date/time.
  13123. So if you press just return without typing anything, the time stamp
  13124. will represent the current date/time. If there is already a timestamp
  13125. at the cursor, it will be modified."
  13126. (interactive "P")
  13127. (let* ((ts nil)
  13128. (default-time
  13129. ;; Default time is either today, or, when entering a range,
  13130. ;; the range start.
  13131. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13132. (save-excursion
  13133. (re-search-backward
  13134. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13135. (- (point) 20) t)))
  13136. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13137. (current-time)))
  13138. (default-input (and ts (org-get-compact-tod ts)))
  13139. (repeater (save-excursion
  13140. (save-match-data
  13141. (beginning-of-line)
  13142. (when (re-search-forward
  13143. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13144. (save-excursion (progn (end-of-line) (point))) t)
  13145. (match-string 0)))))
  13146. org-time-was-given org-end-time-was-given time)
  13147. (cond
  13148. ((and (org-at-timestamp-p t)
  13149. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13150. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13151. (insert "--")
  13152. (setq time (let ((this-command this-command))
  13153. (org-read-date arg 'totime nil nil
  13154. default-time default-input)))
  13155. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13156. ((org-at-timestamp-p t)
  13157. (setq time (let ((this-command this-command))
  13158. (org-read-date arg 'totime nil nil default-time default-input)))
  13159. (when (org-at-timestamp-p t) ; just to get the match data
  13160. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13161. (replace-match "")
  13162. (setq org-last-changed-timestamp
  13163. (org-insert-time-stamp
  13164. time (or org-time-was-given arg)
  13165. inactive nil nil (list org-end-time-was-given)))
  13166. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13167. (setq org-last-changed-timestamp
  13168. (concat (substring org-last-inserted-timestamp 0 -1)
  13169. " " repeater ">"))))
  13170. (message "Timestamp updated"))
  13171. (t
  13172. (setq time (let ((this-command this-command))
  13173. (org-read-date arg 'totime nil nil default-time default-input)))
  13174. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13175. nil nil (list org-end-time-was-given))))))
  13176. ;; FIXME: can we use this for something else, like computing time differences?
  13177. (defun org-get-compact-tod (s)
  13178. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13179. (let* ((t1 (match-string 1 s))
  13180. (h1 (string-to-number (match-string 2 s)))
  13181. (m1 (string-to-number (match-string 3 s)))
  13182. (t2 (and (match-end 4) (match-string 5 s)))
  13183. (h2 (and t2 (string-to-number (match-string 6 s))))
  13184. (m2 (and t2 (string-to-number (match-string 7 s))))
  13185. dh dm)
  13186. (if (not t2)
  13187. t1
  13188. (setq dh (- h2 h1) dm (- m2 m1))
  13189. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13190. (concat t1 "+" (number-to-string dh)
  13191. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13192. (defun org-time-stamp-inactive (&optional arg)
  13193. "Insert an inactive time stamp.
  13194. An inactive time stamp is enclosed in square brackets instead of angle
  13195. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13196. does not link to the calendar and cannot be changed with the S-cursor keys.
  13197. So these are more for recording a certain time/date."
  13198. (interactive "P")
  13199. (org-time-stamp arg 'inactive))
  13200. (defvar org-date-ovl (make-overlay 1 1))
  13201. (overlay-put org-date-ovl 'face 'org-warning)
  13202. (org-detach-overlay org-date-ovl)
  13203. (defvar org-ans1) ; dynamically scoped parameter
  13204. (defvar org-ans2) ; dynamically scoped parameter
  13205. (defvar org-plain-time-of-day-regexp) ; defined below
  13206. (defvar org-overriding-default-time nil) ; dynamically scoped
  13207. (defvar org-read-date-overlay nil)
  13208. (defvar org-dcst nil) ; dynamically scoped
  13209. (defvar org-read-date-history nil)
  13210. (defvar org-read-date-final-answer nil)
  13211. (defvar org-read-date-analyze-futurep nil)
  13212. (defvar org-read-date-analyze-forced-year nil)
  13213. (defun org-read-date (&optional with-time to-time from-string prompt
  13214. default-time default-input)
  13215. "Read a date, possibly a time, and make things smooth for the user.
  13216. The prompt will suggest to enter an ISO date, but you can also enter anything
  13217. which will at least partially be understood by `parse-time-string'.
  13218. Unrecognized parts of the date will default to the current day, month, year,
  13219. hour and minute. If this command is called to replace a timestamp at point,
  13220. of to enter the second timestamp of a range, the default time is taken
  13221. from the existing stamp. Furthermore, the command prefers the future,
  13222. so if you are giving a date where the year is not given, and the day-month
  13223. combination is already past in the current year, it will assume you
  13224. mean next year. For details, see the manual. A few examples:
  13225. 3-2-5 --> 2003-02-05
  13226. feb 15 --> currentyear-02-15
  13227. 2/15 --> currentyear-02-15
  13228. sep 12 9 --> 2009-09-12
  13229. 12:45 --> today 12:45
  13230. 22 sept 0:34 --> currentyear-09-22 0:34
  13231. 12 --> currentyear-currentmonth-12
  13232. Fri --> nearest Friday (today or later)
  13233. etc.
  13234. Furthermore you can specify a relative date by giving, as the *first* thing
  13235. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13236. change in days weeks, months, years.
  13237. With a single plus or minus, the date is relative to today. With a double
  13238. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13239. +4d --> four days from today
  13240. +4 --> same as above
  13241. +2w --> two weeks from today
  13242. ++5 --> five days from default date
  13243. The function understands only English month and weekday abbreviations,
  13244. but this can be configured with the variables `parse-time-months' and
  13245. `parse-time-weekdays'.
  13246. While prompting, a calendar is popped up - you can also select the
  13247. date with the mouse (button 1). The calendar shows a period of three
  13248. months. To scroll it to other months, use the keys `>' and `<'.
  13249. If you don't like the calendar, turn it off with
  13250. \(setq org-read-date-popup-calendar nil)
  13251. With optional argument TO-TIME, the date will immediately be converted
  13252. to an internal time.
  13253. With an optional argument WITH-TIME, the prompt will suggest to also
  13254. insert a time. Note that when WITH-TIME is not set, you can still
  13255. enter a time, and this function will inform the calling routine about
  13256. this change. The calling routine may then choose to change the format
  13257. used to insert the time stamp into the buffer to include the time.
  13258. With optional argument FROM-STRING, read from this string instead from
  13259. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13260. the time/date that is used for everything that is not specified by the
  13261. user."
  13262. (require 'parse-time)
  13263. (let* ((org-time-stamp-rounding-minutes
  13264. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13265. (org-dcst org-display-custom-times)
  13266. (ct (org-current-time))
  13267. (def (or org-overriding-default-time default-time ct))
  13268. (defdecode (decode-time def))
  13269. (dummy (progn
  13270. (when (< (nth 2 defdecode) org-extend-today-until)
  13271. (setcar (nthcdr 2 defdecode) -1)
  13272. (setcar (nthcdr 1 defdecode) 59)
  13273. (setq def (apply 'encode-time defdecode)
  13274. defdecode (decode-time def)))))
  13275. (calendar-frame-setup nil)
  13276. (calendar-setup nil)
  13277. (calendar-move-hook nil)
  13278. (calendar-view-diary-initially-flag nil)
  13279. (calendar-view-holidays-initially-flag nil)
  13280. (timestr (format-time-string
  13281. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13282. (prompt (concat (if prompt (concat prompt " ") "")
  13283. (format "Date+time [%s]: " timestr)))
  13284. ans (org-ans0 "") org-ans1 org-ans2 final)
  13285. (cond
  13286. (from-string (setq ans from-string))
  13287. (org-read-date-popup-calendar
  13288. (save-excursion
  13289. (save-window-excursion
  13290. (calendar)
  13291. (unwind-protect
  13292. (progn
  13293. (calendar-forward-day (- (time-to-days def)
  13294. (calendar-absolute-from-gregorian
  13295. (calendar-current-date))))
  13296. (org-eval-in-calendar nil t)
  13297. (let* ((old-map (current-local-map))
  13298. (map (copy-keymap calendar-mode-map))
  13299. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13300. (org-defkey map (kbd "RET") 'org-calendar-select)
  13301. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13302. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13303. (org-defkey minibuffer-local-map [(meta shift left)]
  13304. (lambda () (interactive)
  13305. (org-eval-in-calendar '(calendar-backward-month 1))))
  13306. (org-defkey minibuffer-local-map [(meta shift right)]
  13307. (lambda () (interactive)
  13308. (org-eval-in-calendar '(calendar-forward-month 1))))
  13309. (org-defkey minibuffer-local-map [(meta shift up)]
  13310. (lambda () (interactive)
  13311. (org-eval-in-calendar '(calendar-backward-year 1))))
  13312. (org-defkey minibuffer-local-map [(meta shift down)]
  13313. (lambda () (interactive)
  13314. (org-eval-in-calendar '(calendar-forward-year 1))))
  13315. (org-defkey minibuffer-local-map [?\e (shift left)]
  13316. (lambda () (interactive)
  13317. (org-eval-in-calendar '(calendar-backward-month 1))))
  13318. (org-defkey minibuffer-local-map [?\e (shift right)]
  13319. (lambda () (interactive)
  13320. (org-eval-in-calendar '(calendar-forward-month 1))))
  13321. (org-defkey minibuffer-local-map [?\e (shift up)]
  13322. (lambda () (interactive)
  13323. (org-eval-in-calendar '(calendar-backward-year 1))))
  13324. (org-defkey minibuffer-local-map [?\e (shift down)]
  13325. (lambda () (interactive)
  13326. (org-eval-in-calendar '(calendar-forward-year 1))))
  13327. (org-defkey minibuffer-local-map [(shift up)]
  13328. (lambda () (interactive)
  13329. (org-eval-in-calendar '(calendar-backward-week 1))))
  13330. (org-defkey minibuffer-local-map [(shift down)]
  13331. (lambda () (interactive)
  13332. (org-eval-in-calendar '(calendar-forward-week 1))))
  13333. (org-defkey minibuffer-local-map [(shift left)]
  13334. (lambda () (interactive)
  13335. (org-eval-in-calendar '(calendar-backward-day 1))))
  13336. (org-defkey minibuffer-local-map [(shift right)]
  13337. (lambda () (interactive)
  13338. (org-eval-in-calendar '(calendar-forward-day 1))))
  13339. (org-defkey minibuffer-local-map ">"
  13340. (lambda () (interactive)
  13341. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13342. (org-defkey minibuffer-local-map "<"
  13343. (lambda () (interactive)
  13344. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13345. (org-defkey minibuffer-local-map "\C-v"
  13346. (lambda () (interactive)
  13347. (org-eval-in-calendar
  13348. '(calendar-scroll-left-three-months 1))))
  13349. (org-defkey minibuffer-local-map "\M-v"
  13350. (lambda () (interactive)
  13351. (org-eval-in-calendar
  13352. '(calendar-scroll-right-three-months 1))))
  13353. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13354. (unwind-protect
  13355. (progn
  13356. (use-local-map map)
  13357. (add-hook 'post-command-hook 'org-read-date-display)
  13358. (setq org-ans0 (read-string prompt default-input
  13359. 'org-read-date-history nil))
  13360. ;; org-ans0: from prompt
  13361. ;; org-ans1: from mouse click
  13362. ;; org-ans2: from calendar motion
  13363. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13364. (remove-hook 'post-command-hook 'org-read-date-display)
  13365. (use-local-map old-map)
  13366. (when org-read-date-overlay
  13367. (delete-overlay org-read-date-overlay)
  13368. (setq org-read-date-overlay nil)))))
  13369. (bury-buffer "*Calendar*")))))
  13370. (t ; Naked prompt only
  13371. (unwind-protect
  13372. (setq ans (read-string prompt default-input
  13373. 'org-read-date-history timestr))
  13374. (when org-read-date-overlay
  13375. (delete-overlay org-read-date-overlay)
  13376. (setq org-read-date-overlay nil)))))
  13377. (setq final (org-read-date-analyze ans def defdecode))
  13378. (when org-read-date-analyze-forced-year
  13379. (message "Year was forced into %s"
  13380. (if org-read-date-force-compatible-dates
  13381. "compatible range (1970-2037)"
  13382. "range representable on this machine"))
  13383. (ding))
  13384. ;; One round trip to get rid of 34th of August and stuff like that....
  13385. (setq final (decode-time (apply 'encode-time final)))
  13386. (setq org-read-date-final-answer ans)
  13387. (if to-time
  13388. (apply 'encode-time final)
  13389. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13390. (format "%04d-%02d-%02d %02d:%02d"
  13391. (nth 5 final) (nth 4 final) (nth 3 final)
  13392. (nth 2 final) (nth 1 final))
  13393. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13394. (defvar def)
  13395. (defvar defdecode)
  13396. (defvar with-time)
  13397. (defun org-read-date-display ()
  13398. "Display the current date prompt interpretation in the minibuffer."
  13399. (when org-read-date-display-live
  13400. (when org-read-date-overlay
  13401. (delete-overlay org-read-date-overlay))
  13402. (let ((p (point)))
  13403. (end-of-line 1)
  13404. (while (not (equal (buffer-substring
  13405. (max (point-min) (- (point) 4)) (point))
  13406. " "))
  13407. (insert " "))
  13408. (goto-char p))
  13409. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13410. " " (or org-ans1 org-ans2)))
  13411. (org-end-time-was-given nil)
  13412. (f (org-read-date-analyze ans def defdecode))
  13413. (fmts (if org-dcst
  13414. org-time-stamp-custom-formats
  13415. org-time-stamp-formats))
  13416. (fmt (if (or with-time
  13417. (and (boundp 'org-time-was-given) org-time-was-given))
  13418. (cdr fmts)
  13419. (car fmts)))
  13420. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13421. (when (and org-end-time-was-given
  13422. (string-match org-plain-time-of-day-regexp txt))
  13423. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13424. org-end-time-was-given
  13425. (substring txt (match-end 0)))))
  13426. (when org-read-date-analyze-futurep
  13427. (setq txt (concat txt " (=>F)")))
  13428. (setq org-read-date-overlay
  13429. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13430. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13431. (defun org-read-date-analyze (ans def defdecode)
  13432. "Analyze the combined answer of the date prompt."
  13433. ;; FIXME: cleanup and comment
  13434. (let ((nowdecode (decode-time (current-time)))
  13435. delta deltan deltaw deltadef year month day
  13436. hour minute second wday pm h2 m2 tl wday1
  13437. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13438. (setq org-read-date-analyze-futurep nil
  13439. org-read-date-analyze-forced-year nil)
  13440. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13441. (setq ans "+0"))
  13442. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13443. (setq ans (replace-match "" t t ans)
  13444. deltan (car delta)
  13445. deltaw (nth 1 delta)
  13446. deltadef (nth 2 delta)))
  13447. ;; Check if there is an iso week date in there
  13448. ;; If yes, store the info and postpone interpreting it until the rest
  13449. ;; of the parsing is done
  13450. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13451. (setq iso-year (if (match-end 1)
  13452. (org-small-year-to-year
  13453. (string-to-number (match-string 1 ans))))
  13454. iso-weekday (if (match-end 3)
  13455. (string-to-number (match-string 3 ans)))
  13456. iso-week (string-to-number (match-string 2 ans)))
  13457. (setq ans (replace-match "" t t ans)))
  13458. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13459. (when (string-match
  13460. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13461. (setq year (if (match-end 2)
  13462. (string-to-number (match-string 2 ans))
  13463. (progn (setq kill-year t)
  13464. (string-to-number (format-time-string "%Y"))))
  13465. month (string-to-number (match-string 3 ans))
  13466. day (string-to-number (match-string 4 ans)))
  13467. (if (< year 100) (setq year (+ 2000 year)))
  13468. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13469. t nil ans)))
  13470. ;; Help matching dottet european dates
  13471. (when (string-match
  13472. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13473. (setq year (if (match-end 3)
  13474. (string-to-number (match-string 3 ans))
  13475. (progn (setq kill-year t)
  13476. (string-to-number (format-time-string "%Y"))))
  13477. day (string-to-number (match-string 1 ans))
  13478. month (string-to-number (match-string 2 ans))
  13479. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13480. t nil ans)))
  13481. ;; Help matching american dates, like 5/30 or 5/30/7
  13482. (when (string-match
  13483. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13484. (setq year (if (match-end 4)
  13485. (string-to-number (match-string 4 ans))
  13486. (progn (setq kill-year t)
  13487. (string-to-number (format-time-string "%Y"))))
  13488. month (string-to-number (match-string 1 ans))
  13489. day (string-to-number (match-string 2 ans)))
  13490. (if (< year 100) (setq year (+ 2000 year)))
  13491. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13492. t nil ans)))
  13493. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13494. ;; If there is a time with am/pm, and *no* time without it, we convert
  13495. ;; so that matching will be successful.
  13496. (loop for i from 1 to 2 do ; twice, for end time as well
  13497. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13498. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13499. (setq hour (string-to-number (match-string 1 ans))
  13500. minute (if (match-end 3)
  13501. (string-to-number (match-string 3 ans))
  13502. 0)
  13503. pm (equal ?p
  13504. (string-to-char (downcase (match-string 4 ans)))))
  13505. (if (and (= hour 12) (not pm))
  13506. (setq hour 0)
  13507. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13508. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13509. t t ans))))
  13510. ;; Check if a time range is given as a duration
  13511. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13512. (setq hour (string-to-number (match-string 1 ans))
  13513. h2 (+ hour (string-to-number (match-string 3 ans)))
  13514. minute (string-to-number (match-string 2 ans))
  13515. m2 (+ minute (if (match-end 5) (string-to-number
  13516. (match-string 5 ans))0)))
  13517. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13518. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13519. t t ans)))
  13520. ;; Check if there is a time range
  13521. (when (boundp 'org-end-time-was-given)
  13522. (setq org-time-was-given nil)
  13523. (when (and (string-match org-plain-time-of-day-regexp ans)
  13524. (match-end 8))
  13525. (setq org-end-time-was-given (match-string 8 ans))
  13526. (setq ans (concat (substring ans 0 (match-beginning 7))
  13527. (substring ans (match-end 7))))))
  13528. (setq tl (parse-time-string ans)
  13529. day (or (nth 3 tl) (nth 3 defdecode))
  13530. month (or (nth 4 tl)
  13531. (if (and org-read-date-prefer-future
  13532. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13533. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13534. (nth 4 defdecode)))
  13535. year (or (and (not kill-year) (nth 5 tl))
  13536. (if (and org-read-date-prefer-future
  13537. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13538. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13539. (nth 5 defdecode)))
  13540. hour (or (nth 2 tl) (nth 2 defdecode))
  13541. minute (or (nth 1 tl) (nth 1 defdecode))
  13542. second (or (nth 0 tl) 0)
  13543. wday (nth 6 tl))
  13544. (when (and (eq org-read-date-prefer-future 'time)
  13545. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13546. (equal day (nth 3 nowdecode))
  13547. (equal month (nth 4 nowdecode))
  13548. (equal year (nth 5 nowdecode))
  13549. (nth 2 tl)
  13550. (or (< (nth 2 tl) (nth 2 nowdecode))
  13551. (and (= (nth 2 tl) (nth 2 nowdecode))
  13552. (nth 1 tl)
  13553. (< (nth 1 tl) (nth 1 nowdecode)))))
  13554. (setq day (1+ day)
  13555. futurep t))
  13556. ;; Special date definitions below
  13557. (cond
  13558. (iso-week
  13559. ;; There was an iso week
  13560. (require 'cal-iso)
  13561. (setq futurep nil)
  13562. (setq year (or iso-year year)
  13563. day (or iso-weekday wday 1)
  13564. wday nil ; to make sure that the trigger below does not match
  13565. iso-date (calendar-gregorian-from-absolute
  13566. (calendar-absolute-from-iso
  13567. (list iso-week day year))))
  13568. ; FIXME: Should we also push ISO weeks into the future?
  13569. ; (when (and org-read-date-prefer-future
  13570. ; (not iso-year)
  13571. ; (< (calendar-absolute-from-gregorian iso-date)
  13572. ; (time-to-days (current-time))))
  13573. ; (setq year (1+ year)
  13574. ; iso-date (calendar-gregorian-from-absolute
  13575. ; (calendar-absolute-from-iso
  13576. ; (list iso-week day year)))))
  13577. (setq month (car iso-date)
  13578. year (nth 2 iso-date)
  13579. day (nth 1 iso-date)))
  13580. (deltan
  13581. (setq futurep nil)
  13582. (unless deltadef
  13583. (let ((now (decode-time (current-time))))
  13584. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13585. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13586. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13587. ((equal deltaw "m") (setq month (+ month deltan)))
  13588. ((equal deltaw "y") (setq year (+ year deltan)))))
  13589. ((and wday (not (nth 3 tl)))
  13590. (setq futurep nil)
  13591. ;; Weekday was given, but no day, so pick that day in the week
  13592. ;; on or after the derived date.
  13593. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13594. (unless (equal wday wday1)
  13595. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13596. (if (and (boundp 'org-time-was-given)
  13597. (nth 2 tl))
  13598. (setq org-time-was-given t))
  13599. (if (< year 100) (setq year (+ 2000 year)))
  13600. ;; Check of the date is representable
  13601. (if org-read-date-force-compatible-dates
  13602. (progn
  13603. (if (< year 1970)
  13604. (setq year 1970 org-read-date-analyze-forced-year t))
  13605. (if (> year 2037)
  13606. (setq year 2037 org-read-date-analyze-forced-year t)))
  13607. (condition-case nil
  13608. (ignore (encode-time second minute hour day month year))
  13609. (error
  13610. (setq year (nth 5 defdecode))
  13611. (setq org-read-date-analyze-forced-year t))))
  13612. (setq org-read-date-analyze-futurep futurep)
  13613. (list second minute hour day month year)))
  13614. (defvar parse-time-weekdays)
  13615. (defun org-read-date-get-relative (s today default)
  13616. "Check string S for special relative date string.
  13617. TODAY and DEFAULT are internal times, for today and for a default.
  13618. Return shift list (N what def-flag)
  13619. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13620. N is the number of WHATs to shift.
  13621. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13622. the DEFAULT date rather than TODAY."
  13623. (when (and
  13624. (string-match
  13625. (concat
  13626. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13627. "\\([0-9]+\\)?"
  13628. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13629. "\\([ \t]\\|$\\)") s)
  13630. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13631. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13632. (string-to-char (substring (match-string 1 s) -1))
  13633. ?+))
  13634. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13635. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13636. (what (if (match-end 3) (match-string 3 s) "d"))
  13637. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13638. (date (if rel default today))
  13639. (wday (nth 6 (decode-time date)))
  13640. delta)
  13641. (if wday1
  13642. (progn
  13643. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13644. (if (= dir ?-) (setq delta (- delta 7)))
  13645. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13646. (list delta "d" rel))
  13647. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13648. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13649. "Turn a user-specified date into the internal representation.
  13650. The internal representation needed by the calendar is (month day year).
  13651. This is a wrapper to handle the brain-dead convention in calendar that
  13652. user function argument order change dependent on argument order."
  13653. (if (boundp 'calendar-date-style)
  13654. (cond
  13655. ((eq calendar-date-style 'american)
  13656. (list arg1 arg2 arg3))
  13657. ((eq calendar-date-style 'european)
  13658. (list arg2 arg1 arg3))
  13659. ((eq calendar-date-style 'iso)
  13660. (list arg2 arg3 arg1)))
  13661. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13662. (if (org-bound-and-true-p european-calendar-style)
  13663. (list arg2 arg1 arg3)
  13664. (list arg1 arg2 arg3)))))
  13665. (defun org-eval-in-calendar (form &optional keepdate)
  13666. "Eval FORM in the calendar window and return to current window.
  13667. Also, store the cursor date in variable org-ans2."
  13668. (let ((sf (selected-frame))
  13669. (sw (selected-window)))
  13670. (select-window (get-buffer-window "*Calendar*" t))
  13671. (eval form)
  13672. (when (and (not keepdate) (calendar-cursor-to-date))
  13673. (let* ((date (calendar-cursor-to-date))
  13674. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13675. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13676. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13677. (select-window sw)
  13678. (org-select-frame-set-input-focus sf)))
  13679. (defun org-calendar-select ()
  13680. "Return to `org-read-date' with the date currently selected.
  13681. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13682. (interactive)
  13683. (when (calendar-cursor-to-date)
  13684. (let* ((date (calendar-cursor-to-date))
  13685. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13686. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13687. (if (active-minibuffer-window) (exit-minibuffer))))
  13688. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13689. "Insert a date stamp for the date given by the internal TIME.
  13690. WITH-HM means use the stamp format that includes the time of the day.
  13691. INACTIVE means use square brackets instead of angular ones, so that the
  13692. stamp will not contribute to the agenda.
  13693. PRE and POST are optional strings to be inserted before and after the
  13694. stamp.
  13695. The command returns the inserted time stamp."
  13696. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13697. stamp)
  13698. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13699. (insert-before-markers (or pre ""))
  13700. (when (listp extra)
  13701. (setq extra (car extra))
  13702. (if (and (stringp extra)
  13703. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13704. (setq extra (format "-%02d:%02d"
  13705. (string-to-number (match-string 1 extra))
  13706. (string-to-number (match-string 2 extra))))
  13707. (setq extra nil)))
  13708. (when extra
  13709. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13710. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13711. (insert-before-markers (or post ""))
  13712. (setq org-last-inserted-timestamp stamp)))
  13713. (defun org-toggle-time-stamp-overlays ()
  13714. "Toggle the use of custom time stamp formats."
  13715. (interactive)
  13716. (setq org-display-custom-times (not org-display-custom-times))
  13717. (unless org-display-custom-times
  13718. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13719. (while (setq p (next-single-property-change p 'display))
  13720. (if (and (get-text-property p 'display)
  13721. (eq (get-text-property p 'face) 'org-date))
  13722. (remove-text-properties
  13723. p (setq p (next-single-property-change p 'display))
  13724. '(display t))))
  13725. (set-buffer-modified-p bmp)))
  13726. (if (featurep 'xemacs)
  13727. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13728. (org-restart-font-lock)
  13729. (setq org-table-may-need-update t)
  13730. (if org-display-custom-times
  13731. (message "Time stamps are overlayed with custom format")
  13732. (message "Time stamp overlays removed")))
  13733. (defun org-display-custom-time (beg end)
  13734. "Overlay modified time stamp format over timestamp between BEG and END."
  13735. (let* ((ts (buffer-substring beg end))
  13736. t1 w1 with-hm tf time str w2 (off 0))
  13737. (save-match-data
  13738. (setq t1 (org-parse-time-string ts t))
  13739. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13740. (setq off (- (match-end 0) (match-beginning 0)))))
  13741. (setq end (- end off))
  13742. (setq w1 (- end beg)
  13743. with-hm (and (nth 1 t1) (nth 2 t1))
  13744. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13745. time (org-fix-decoded-time t1)
  13746. str (org-add-props
  13747. (format-time-string
  13748. (substring tf 1 -1) (apply 'encode-time time))
  13749. nil 'mouse-face 'highlight)
  13750. w2 (length str))
  13751. (if (not (= w2 w1))
  13752. (add-text-properties (1+ beg) (+ 2 beg)
  13753. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13754. (if (featurep 'xemacs)
  13755. (progn
  13756. (put-text-property beg end 'invisible t)
  13757. (put-text-property beg end 'end-glyph (make-glyph str)))
  13758. (put-text-property beg end 'display str))))
  13759. (defun org-translate-time (string)
  13760. "Translate all timestamps in STRING to custom format.
  13761. But do this only if the variable `org-display-custom-times' is set."
  13762. (when org-display-custom-times
  13763. (save-match-data
  13764. (let* ((start 0)
  13765. (re org-ts-regexp-both)
  13766. t1 with-hm inactive tf time str beg end)
  13767. (while (setq start (string-match re string start))
  13768. (setq beg (match-beginning 0)
  13769. end (match-end 0)
  13770. t1 (save-match-data
  13771. (org-parse-time-string (substring string beg end) t))
  13772. with-hm (and (nth 1 t1) (nth 2 t1))
  13773. inactive (equal (substring string beg (1+ beg)) "[")
  13774. tf (funcall (if with-hm 'cdr 'car)
  13775. org-time-stamp-custom-formats)
  13776. time (org-fix-decoded-time t1)
  13777. str (format-time-string
  13778. (concat
  13779. (if inactive "[" "<") (substring tf 1 -1)
  13780. (if inactive "]" ">"))
  13781. (apply 'encode-time time))
  13782. string (replace-match str t t string)
  13783. start (+ start (length str)))))))
  13784. string)
  13785. (defun org-fix-decoded-time (time)
  13786. "Set 0 instead of nil for the first 6 elements of time.
  13787. Don't touch the rest."
  13788. (let ((n 0))
  13789. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13790. (defun org-days-to-time (timestamp-string)
  13791. "Difference between TIMESTAMP-STRING and now in days."
  13792. (- (time-to-days (org-time-string-to-time timestamp-string))
  13793. (time-to-days (current-time))))
  13794. (defun org-deadline-close (timestamp-string &optional ndays)
  13795. "Is the time in TIMESTAMP-STRING close to the current date?"
  13796. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13797. (and (< (org-days-to-time timestamp-string) ndays)
  13798. (not (org-entry-is-done-p))))
  13799. (defun org-get-wdays (ts)
  13800. "Get the deadline lead time appropriate for timestring TS."
  13801. (cond
  13802. ((<= org-deadline-warning-days 0)
  13803. ;; 0 or negative, enforce this value no matter what
  13804. (- org-deadline-warning-days))
  13805. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13806. ;; lead time is specified.
  13807. (floor (* (string-to-number (match-string 1 ts))
  13808. (cdr (assoc (match-string 2 ts)
  13809. '(("d" . 1) ("w" . 7)
  13810. ("m" . 30.4) ("y" . 365.25)))))))
  13811. ;; go for the default.
  13812. (t org-deadline-warning-days)))
  13813. (defun org-calendar-select-mouse (ev)
  13814. "Return to `org-read-date' with the date currently selected.
  13815. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13816. (interactive "e")
  13817. (mouse-set-point ev)
  13818. (when (calendar-cursor-to-date)
  13819. (let* ((date (calendar-cursor-to-date))
  13820. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13821. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13822. (if (active-minibuffer-window) (exit-minibuffer))))
  13823. (defun org-check-deadlines (ndays)
  13824. "Check if there are any deadlines due or past due.
  13825. A deadline is considered due if it happens within `org-deadline-warning-days'
  13826. days from today's date. If the deadline appears in an entry marked DONE,
  13827. it is not shown. The prefix arg NDAYS can be used to test that many
  13828. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13829. (interactive "P")
  13830. (let* ((org-warn-days
  13831. (cond
  13832. ((equal ndays '(4)) 100000)
  13833. (ndays (prefix-numeric-value ndays))
  13834. (t (abs org-deadline-warning-days))))
  13835. (case-fold-search nil)
  13836. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13837. (callback
  13838. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13839. (message "%d deadlines past-due or due within %d days"
  13840. (org-occur regexp nil callback)
  13841. org-warn-days)))
  13842. (defun org-check-before-date (date)
  13843. "Check if there are deadlines or scheduled entries before DATE."
  13844. (interactive (list (org-read-date)))
  13845. (let ((case-fold-search nil)
  13846. (regexp (concat "\\<\\(" org-deadline-string
  13847. "\\|" org-scheduled-string
  13848. "\\) *<\\([^>]+\\)>"))
  13849. (callback
  13850. (lambda () (time-less-p
  13851. (org-time-string-to-time (match-string 2))
  13852. (org-time-string-to-time date)))))
  13853. (message "%d entries before %s"
  13854. (org-occur regexp nil callback) date)))
  13855. (defun org-check-after-date (date)
  13856. "Check if there are deadlines or scheduled entries after DATE."
  13857. (interactive (list (org-read-date)))
  13858. (let ((case-fold-search nil)
  13859. (regexp (concat "\\<\\(" org-deadline-string
  13860. "\\|" org-scheduled-string
  13861. "\\) *<\\([^>]+\\)>"))
  13862. (callback
  13863. (lambda () (not
  13864. (time-less-p
  13865. (org-time-string-to-time (match-string 2))
  13866. (org-time-string-to-time date))))))
  13867. (message "%d entries after %s"
  13868. (org-occur regexp nil callback) date)))
  13869. (defun org-evaluate-time-range (&optional to-buffer)
  13870. "Evaluate a time range by computing the difference between start and end.
  13871. Normally the result is just printed in the echo area, but with prefix arg
  13872. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13873. If the time range is actually in a table, the result is inserted into the
  13874. next column.
  13875. For time difference computation, a year is assumed to be exactly 365
  13876. days in order to avoid rounding problems."
  13877. (interactive "P")
  13878. (or
  13879. (org-clock-update-time-maybe)
  13880. (save-excursion
  13881. (unless (org-at-date-range-p t)
  13882. (goto-char (point-at-bol))
  13883. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13884. (if (not (org-at-date-range-p t))
  13885. (error "Not at a time-stamp range, and none found in current line")))
  13886. (let* ((ts1 (match-string 1))
  13887. (ts2 (match-string 2))
  13888. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13889. (match-end (match-end 0))
  13890. (time1 (org-time-string-to-time ts1))
  13891. (time2 (org-time-string-to-time ts2))
  13892. (t1 (org-float-time time1))
  13893. (t2 (org-float-time time2))
  13894. (diff (abs (- t2 t1)))
  13895. (negative (< (- t2 t1) 0))
  13896. ;; (ys (floor (* 365 24 60 60)))
  13897. (ds (* 24 60 60))
  13898. (hs (* 60 60))
  13899. (fy "%dy %dd %02d:%02d")
  13900. (fy1 "%dy %dd")
  13901. (fd "%dd %02d:%02d")
  13902. (fd1 "%dd")
  13903. (fh "%02d:%02d")
  13904. y d h m align)
  13905. (if havetime
  13906. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13907. y 0
  13908. d (floor (/ diff ds)) diff (mod diff ds)
  13909. h (floor (/ diff hs)) diff (mod diff hs)
  13910. m (floor (/ diff 60)))
  13911. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13912. y 0
  13913. d (floor (+ (/ diff ds) 0.5))
  13914. h 0 m 0))
  13915. (if (not to-buffer)
  13916. (message "%s" (org-make-tdiff-string y d h m))
  13917. (if (org-at-table-p)
  13918. (progn
  13919. (goto-char match-end)
  13920. (setq align t)
  13921. (and (looking-at " *|") (goto-char (match-end 0))))
  13922. (goto-char match-end))
  13923. (if (looking-at
  13924. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13925. (replace-match ""))
  13926. (if negative (insert " -"))
  13927. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13928. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13929. (insert " " (format fh h m))))
  13930. (if align (org-table-align))
  13931. (message "Time difference inserted")))))
  13932. (defun org-make-tdiff-string (y d h m)
  13933. (let ((fmt "")
  13934. (l nil))
  13935. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13936. l (push y l)))
  13937. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13938. l (push d l)))
  13939. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13940. l (push h l)))
  13941. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13942. l (push m l)))
  13943. (apply 'format fmt (nreverse l))))
  13944. (defun org-time-string-to-time (s)
  13945. (apply 'encode-time (org-parse-time-string s)))
  13946. (defun org-time-string-to-seconds (s)
  13947. (org-float-time (org-time-string-to-time s)))
  13948. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13949. "Convert a time stamp to an absolute day number.
  13950. If there is a specifier for a cyclic time stamp, get the closest date to
  13951. DAYNR.
  13952. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13953. The variable date is bound by the calendar when this is called."
  13954. (cond
  13955. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13956. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13957. daynr
  13958. (+ daynr 1000)))
  13959. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13960. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13961. (time-to-days (current-time))) (match-string 0 s)
  13962. prefer show-all))
  13963. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13964. (defun org-days-to-iso-week (days)
  13965. "Return the iso week number."
  13966. (require 'cal-iso)
  13967. (car (calendar-iso-from-absolute days)))
  13968. (defun org-small-year-to-year (year)
  13969. "Convert 2-digit years into 4-digit years.
  13970. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13971. The year 2000 cannot be abbreviated. Any year larger than 99
  13972. is returned unchanged."
  13973. (if (< year 38)
  13974. (setq year (+ 2000 year))
  13975. (if (< year 100)
  13976. (setq year (+ 1900 year))))
  13977. year)
  13978. (defun org-time-from-absolute (d)
  13979. "Return the time corresponding to date D.
  13980. D may be an absolute day number, or a calendar-type list (month day year)."
  13981. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13982. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13983. (defun org-calendar-holiday ()
  13984. "List of holidays, for Diary display in Org-mode."
  13985. (require 'holidays)
  13986. (let ((hl (funcall
  13987. (if (fboundp 'calendar-check-holidays)
  13988. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13989. (if hl (mapconcat 'identity hl "; "))))
  13990. (defun org-diary-sexp-entry (sexp entry date)
  13991. "Process a SEXP diary ENTRY for DATE."
  13992. (require 'diary-lib)
  13993. (let ((result (if calendar-debug-sexp
  13994. (let ((stack-trace-on-error t))
  13995. (eval (car (read-from-string sexp))))
  13996. (condition-case nil
  13997. (eval (car (read-from-string sexp)))
  13998. (error
  13999. (beep)
  14000. (message "Bad sexp at line %d in %s: %s"
  14001. (org-current-line)
  14002. (buffer-file-name) sexp)
  14003. (sleep-for 2))))))
  14004. (cond ((stringp result) (split-string result "; "))
  14005. ((and (consp result)
  14006. (not (consp (cdr result)))
  14007. (stringp (cdr result))) (cdr result))
  14008. ((and (consp result)
  14009. (stringp (car result))) result)
  14010. (result entry)
  14011. (t nil))))
  14012. (defun org-diary-to-ical-string (frombuf)
  14013. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14014. This uses the icalendar.el library."
  14015. (let* ((tmpdir (if (featurep 'xemacs)
  14016. (temp-directory)
  14017. temporary-file-directory))
  14018. (tmpfile (make-temp-name
  14019. (expand-file-name "orgics" tmpdir)))
  14020. buf rtn b e)
  14021. (with-current-buffer frombuf
  14022. (icalendar-export-region (point-min) (point-max) tmpfile)
  14023. (setq buf (find-buffer-visiting tmpfile))
  14024. (set-buffer buf)
  14025. (goto-char (point-min))
  14026. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14027. (setq b (match-beginning 0)))
  14028. (goto-char (point-max))
  14029. (if (re-search-backward "^END:VEVENT" nil t)
  14030. (setq e (match-end 0)))
  14031. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14032. (kill-buffer buf)
  14033. (delete-file tmpfile)
  14034. rtn))
  14035. (defun org-closest-date (start current change prefer show-all)
  14036. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14037. When PREFER is `past', return a date that is either CURRENT or past.
  14038. When PREFER is `future', return a date that is either CURRENT or future.
  14039. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14040. ;; Make the proper lists from the dates
  14041. (catch 'exit
  14042. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14043. dn dw sday cday n1 n2 n0
  14044. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14045. (setq start (org-date-to-gregorian start)
  14046. current (org-date-to-gregorian
  14047. (if show-all
  14048. current
  14049. (time-to-days (current-time))))
  14050. sday (calendar-absolute-from-gregorian start)
  14051. cday (calendar-absolute-from-gregorian current))
  14052. (if (<= cday sday) (throw 'exit sday))
  14053. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14054. (setq dn (string-to-number (match-string 1 change))
  14055. dw (cdr (assoc (match-string 2 change) a1)))
  14056. (error "Invalid change specifier: %s" change))
  14057. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14058. (cond
  14059. ((eq dw 'day)
  14060. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14061. n2 (+ n1 dn)))
  14062. ((eq dw 'year)
  14063. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14064. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14065. (setq date1 (list m d y1)
  14066. n1 (calendar-absolute-from-gregorian date1)
  14067. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14068. n2 (calendar-absolute-from-gregorian date2)))
  14069. ((eq dw 'month)
  14070. ;; approx number of month between the two dates
  14071. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14072. ;; How often does dn fit in there?
  14073. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14074. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14075. m (+ m nm)
  14076. ny (floor (/ m 12))
  14077. y (+ y ny)
  14078. m (- m (* ny 12)))
  14079. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14080. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14081. (setq m2 (+ m dn) y2 y)
  14082. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14083. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14084. (while (<= n2 cday)
  14085. (setq n1 n2 m m2 y y2)
  14086. (setq m2 (+ m dn) y2 y)
  14087. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14088. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14089. ;; Make sure n1 is the earlier date
  14090. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14091. (if show-all
  14092. (cond
  14093. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14094. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14095. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14096. (cond
  14097. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14098. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14099. (t (if (= cday n1) n1 n2)))))))
  14100. (defun org-date-to-gregorian (date)
  14101. "Turn any specification of DATE into a Gregorian date for the calendar."
  14102. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14103. ((and (listp date) (= (length date) 3)) date)
  14104. ((stringp date)
  14105. (setq date (org-parse-time-string date))
  14106. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14107. ((listp date)
  14108. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14109. (defun org-parse-time-string (s &optional nodefault)
  14110. "Parse the standard Org-mode time string.
  14111. This should be a lot faster than the normal `parse-time-string'.
  14112. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14113. hour and minute fields will be nil if not given."
  14114. (if (string-match org-ts-regexp0 s)
  14115. (list 0
  14116. (if (or (match-beginning 8) (not nodefault))
  14117. (string-to-number (or (match-string 8 s) "0")))
  14118. (if (or (match-beginning 7) (not nodefault))
  14119. (string-to-number (or (match-string 7 s) "0")))
  14120. (string-to-number (match-string 4 s))
  14121. (string-to-number (match-string 3 s))
  14122. (string-to-number (match-string 2 s))
  14123. nil nil nil)
  14124. (error "Not a standard Org-mode time string: %s" s)))
  14125. (defun org-timestamp-up (&optional arg)
  14126. "Increase the date item at the cursor by one.
  14127. If the cursor is on the year, change the year. If it is on the month or
  14128. the day, change that.
  14129. With prefix ARG, change by that many units."
  14130. (interactive "p")
  14131. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14132. (defun org-timestamp-down (&optional arg)
  14133. "Decrease the date item at the cursor by one.
  14134. If the cursor is on the year, change the year. If it is on the month or
  14135. the day, change that.
  14136. With prefix ARG, change by that many units."
  14137. (interactive "p")
  14138. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14139. (defun org-timestamp-up-day (&optional arg)
  14140. "Increase the date in the time stamp by one day.
  14141. With prefix ARG, change that many days."
  14142. (interactive "p")
  14143. (if (and (not (org-at-timestamp-p t))
  14144. (org-on-heading-p))
  14145. (org-todo 'up)
  14146. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14147. (defun org-timestamp-down-day (&optional arg)
  14148. "Decrease the date in the time stamp by one day.
  14149. With prefix ARG, change that many days."
  14150. (interactive "p")
  14151. (if (and (not (org-at-timestamp-p t))
  14152. (org-on-heading-p))
  14153. (org-todo 'down)
  14154. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14155. (defun org-at-timestamp-p (&optional inactive-ok)
  14156. "Determine if the cursor is in or at a timestamp."
  14157. (interactive)
  14158. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14159. (pos (point))
  14160. (ans (or (looking-at tsr)
  14161. (save-excursion
  14162. (skip-chars-backward "^[<\n\r\t")
  14163. (if (> (point) (point-min)) (backward-char 1))
  14164. (and (looking-at tsr)
  14165. (> (- (match-end 0) pos) -1))))))
  14166. (and ans
  14167. (boundp 'org-ts-what)
  14168. (setq org-ts-what
  14169. (cond
  14170. ((= pos (match-beginning 0)) 'bracket)
  14171. ((= pos (1- (match-end 0))) 'bracket)
  14172. ((org-pos-in-match-range pos 2) 'year)
  14173. ((org-pos-in-match-range pos 3) 'month)
  14174. ((org-pos-in-match-range pos 7) 'hour)
  14175. ((org-pos-in-match-range pos 8) 'minute)
  14176. ((or (org-pos-in-match-range pos 4)
  14177. (org-pos-in-match-range pos 5)) 'day)
  14178. ((and (> pos (or (match-end 8) (match-end 5)))
  14179. (< pos (match-end 0)))
  14180. (- pos (or (match-end 8) (match-end 5))))
  14181. (t 'day))))
  14182. ans))
  14183. (defun org-toggle-timestamp-type ()
  14184. "Toggle the type (<active> or [inactive]) of a time stamp."
  14185. (interactive)
  14186. (when (org-at-timestamp-p t)
  14187. (let ((beg (match-beginning 0)) (end (match-end 0))
  14188. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14189. (save-excursion
  14190. (goto-char beg)
  14191. (while (re-search-forward "[][<>]" end t)
  14192. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14193. t t)))
  14194. (message "Timestamp is now %sactive"
  14195. (if (equal (char-after beg) ?<) "" "in")))))
  14196. (defun org-timestamp-change (n &optional what updown)
  14197. "Change the date in the time stamp at point.
  14198. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14199. `year', `minute', `second'. If WHAT is not given, the cursor position
  14200. in the timestamp determines what will be changed."
  14201. (let ((pos (point))
  14202. with-hm inactive
  14203. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14204. org-ts-what
  14205. extra rem
  14206. ts time time0)
  14207. (if (not (org-at-timestamp-p t))
  14208. (error "Not at a timestamp"))
  14209. (if (and (not what) (eq org-ts-what 'bracket))
  14210. (org-toggle-timestamp-type)
  14211. (if (and (not what) (not (eq org-ts-what 'day))
  14212. org-display-custom-times
  14213. (get-text-property (point) 'display)
  14214. (not (get-text-property (1- (point)) 'display)))
  14215. (setq org-ts-what 'day))
  14216. (setq org-ts-what (or what org-ts-what)
  14217. inactive (= (char-after (match-beginning 0)) ?\[)
  14218. ts (match-string 0))
  14219. (replace-match "")
  14220. (if (string-match
  14221. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14222. ts)
  14223. (setq extra (match-string 1 ts)))
  14224. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14225. (setq with-hm t))
  14226. (setq time0 (org-parse-time-string ts))
  14227. (when (and updown
  14228. (eq org-ts-what 'minute)
  14229. (not current-prefix-arg))
  14230. ;; This looks like s-up and s-down. Change by one rounding step.
  14231. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14232. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14233. (setcar (cdr time0) (+ (nth 1 time0)
  14234. (if (> n 0) (- rem) (- dm rem))))))
  14235. (setq time
  14236. (encode-time (or (car time0) 0)
  14237. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14238. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14239. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14240. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14241. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14242. (nthcdr 6 time0)))
  14243. (when (and (member org-ts-what '(hour minute))
  14244. extra
  14245. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14246. (setq extra (org-modify-ts-extra
  14247. extra
  14248. (if (eq org-ts-what 'hour) 2 5)
  14249. n dm)))
  14250. (when (integerp org-ts-what)
  14251. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14252. (if (eq what 'calendar)
  14253. (let ((cal-date (org-get-date-from-calendar)))
  14254. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14255. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14256. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14257. (setcar time0 (or (car time0) 0))
  14258. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14259. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14260. (setq time (apply 'encode-time time0))))
  14261. (setq org-last-changed-timestamp
  14262. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14263. (org-clock-update-time-maybe)
  14264. (goto-char pos)
  14265. ;; Try to recenter the calendar window, if any
  14266. (if (and org-calendar-follow-timestamp-change
  14267. (get-buffer-window "*Calendar*" t)
  14268. (memq org-ts-what '(day month year)))
  14269. (org-recenter-calendar (time-to-days time))))))
  14270. (defun org-modify-ts-extra (s pos n dm)
  14271. "Change the different parts of the lead-time and repeat fields in timestamp."
  14272. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14273. ng h m new rem)
  14274. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14275. (cond
  14276. ((or (org-pos-in-match-range pos 2)
  14277. (org-pos-in-match-range pos 3))
  14278. (setq m (string-to-number (match-string 3 s))
  14279. h (string-to-number (match-string 2 s)))
  14280. (if (org-pos-in-match-range pos 2)
  14281. (setq h (+ h n))
  14282. (setq n (* dm (org-no-warnings (signum n))))
  14283. (when (not (= 0 (setq rem (% m dm))))
  14284. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14285. (setq m (+ m n)))
  14286. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14287. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14288. (setq h (min 24 (max 0 h)))
  14289. (setq ng 1 new (format "-%02d:%02d" h m)))
  14290. ((org-pos-in-match-range pos 6)
  14291. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14292. ((org-pos-in-match-range pos 5)
  14293. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14294. ((org-pos-in-match-range pos 9)
  14295. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14296. ((org-pos-in-match-range pos 8)
  14297. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14298. (when ng
  14299. (setq s (concat
  14300. (substring s 0 (match-beginning ng))
  14301. new
  14302. (substring s (match-end ng))))))
  14303. s))
  14304. (defun org-recenter-calendar (date)
  14305. "If the calendar is visible, recenter it to DATE."
  14306. (let* ((win (selected-window))
  14307. (cwin (get-buffer-window "*Calendar*" t))
  14308. (calendar-move-hook nil))
  14309. (when cwin
  14310. (select-window cwin)
  14311. (calendar-goto-date (if (listp date) date
  14312. (calendar-gregorian-from-absolute date)))
  14313. (select-window win))))
  14314. (defun org-goto-calendar (&optional arg)
  14315. "Go to the Emacs calendar at the current date.
  14316. If there is a time stamp in the current line, go to that date.
  14317. A prefix ARG can be used to force the current date."
  14318. (interactive "P")
  14319. (let ((tsr org-ts-regexp) diff
  14320. (calendar-move-hook nil)
  14321. (calendar-view-holidays-initially-flag nil)
  14322. (calendar-view-diary-initially-flag nil))
  14323. (if (or (org-at-timestamp-p)
  14324. (save-excursion
  14325. (beginning-of-line 1)
  14326. (looking-at (concat ".*" tsr))))
  14327. (let ((d1 (time-to-days (current-time)))
  14328. (d2 (time-to-days
  14329. (org-time-string-to-time (match-string 1)))))
  14330. (setq diff (- d2 d1))))
  14331. (calendar)
  14332. (calendar-goto-today)
  14333. (if (and diff (not arg)) (calendar-forward-day diff))))
  14334. (defun org-get-date-from-calendar ()
  14335. "Return a list (month day year) of date at point in calendar."
  14336. (with-current-buffer "*Calendar*"
  14337. (save-match-data
  14338. (calendar-cursor-to-date))))
  14339. (defun org-date-from-calendar ()
  14340. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14341. If there is already a time stamp at the cursor position, update it."
  14342. (interactive)
  14343. (if (org-at-timestamp-p t)
  14344. (org-timestamp-change 0 'calendar)
  14345. (let ((cal-date (org-get-date-from-calendar)))
  14346. (org-insert-time-stamp
  14347. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14348. (defun org-minutes-to-hh:mm-string (m)
  14349. "Compute H:MM from a number of minutes."
  14350. (let ((h (/ m 60)))
  14351. (setq m (- m (* 60 h)))
  14352. (format org-time-clocksum-format h m)))
  14353. (defun org-hh:mm-string-to-minutes (s)
  14354. "Convert a string H:MM to a number of minutes.
  14355. If the string is just a number, interpret it as minutes.
  14356. In fact, the first hh:mm or number in the string will be taken,
  14357. there can be extra stuff in the string.
  14358. If no number is found, the return value is 0."
  14359. (cond
  14360. ((integerp s) s)
  14361. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14362. (+ (* (string-to-number (match-string 1 s)) 60)
  14363. (string-to-number (match-string 2 s))))
  14364. ((string-match "\\([0-9]+\\)" s)
  14365. (string-to-number (match-string 1 s)))
  14366. (t 0)))
  14367. (defcustom org-effort-durations
  14368. `(("h" . 60)
  14369. ("d" . ,(* 60 8))
  14370. ("w" . ,(* 60 8 5))
  14371. ("m" . ,(* 60 8 5 4))
  14372. ("y" . ,(* 60 8 5 40)))
  14373. "Conversion factor to minutes for an effort modifier.
  14374. Each entry has the form (MODIFIER . MINUTES).
  14375. In an effort string, a number followed by MODIFIER is multiplied
  14376. by the specified number of MINUTES to obtain an effort in
  14377. minutes.
  14378. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14379. effort string \"2hours\" is equivalent to 120 minutes."
  14380. :group 'org-agenda
  14381. :type '(alist :key-type (string :tag "Modifier")
  14382. :value-type (number :tag "Minutes")))
  14383. (defun org-duration-string-to-minutes (s)
  14384. "Convert a duration string S to minutes.
  14385. A bare number is interpreted as minutes, modifiers can be set by
  14386. customizing `org-effort-durations' (which see).
  14387. Entries containing a colon are interpreted as H:MM by
  14388. `org-hh:mm-string-to-minutes'."
  14389. (let ((result 0)
  14390. (re (concat "\\([0-9]+\\) *\\("
  14391. (regexp-opt (mapcar 'car org-effort-durations))
  14392. "\\)")))
  14393. (while (string-match re s)
  14394. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14395. (string-to-number (match-string 1 s))))
  14396. (setq s (replace-match "" nil t s)))
  14397. (incf result (org-hh:mm-string-to-minutes s))
  14398. result))
  14399. ;;;; Files
  14400. (defun org-save-all-org-buffers ()
  14401. "Save all Org-mode buffers without user confirmation."
  14402. (interactive)
  14403. (message "Saving all Org-mode buffers...")
  14404. (save-some-buffers t 'org-mode-p)
  14405. (when (featurep 'org-id) (org-id-locations-save))
  14406. (message "Saving all Org-mode buffers... done"))
  14407. (defun org-revert-all-org-buffers ()
  14408. "Revert all Org-mode buffers.
  14409. Prompt for confirmation when there are unsaved changes.
  14410. Be sure you know what you are doing before letting this function
  14411. overwrite your changes.
  14412. This function is useful in a setup where one tracks org files
  14413. with a version control system, to revert on one machine after pulling
  14414. changes from another. I believe the procedure must be like this:
  14415. 1. M-x org-save-all-org-buffers
  14416. 2. Pull changes from the other machine, resolve conflicts
  14417. 3. M-x org-revert-all-org-buffers"
  14418. (interactive)
  14419. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14420. (error "Abort"))
  14421. (save-excursion
  14422. (save-window-excursion
  14423. (mapc
  14424. (lambda (b)
  14425. (when (and (with-current-buffer b (org-mode-p))
  14426. (with-current-buffer b buffer-file-name))
  14427. (switch-to-buffer b)
  14428. (revert-buffer t 'no-confirm)))
  14429. (buffer-list))
  14430. (when (and (featurep 'org-id) org-id-track-globally)
  14431. (org-id-locations-load)))))
  14432. ;;;; Agenda files
  14433. ;;;###autoload
  14434. (defun org-switchb (&optional arg)
  14435. "Switch between Org buffers.
  14436. With a prefix argument, restrict available to files.
  14437. With two prefix arguments, restrict available buffers to agenda files.
  14438. Defaults to `iswitchb' for buffer name completion.
  14439. Set `org-completion-use-ido' to make it use ido instead."
  14440. (interactive "P")
  14441. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14442. ((equal arg '(16)) (org-buffer-list 'agenda))
  14443. (t (org-buffer-list))))
  14444. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14445. (org-completion-use-ido org-completion-use-ido))
  14446. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14447. (setq org-completion-use-iswitchb t))
  14448. (switch-to-buffer
  14449. (org-icompleting-read "Org buffer: "
  14450. (mapcar 'list (mapcar 'buffer-name blist))
  14451. nil t))))
  14452. ;;; Define some older names previously used for this functionality
  14453. ;;;###autoload
  14454. (defalias 'org-ido-switchb 'org-switchb)
  14455. ;;;###autoload
  14456. (defalias 'org-iswitchb 'org-switchb)
  14457. (defun org-buffer-list (&optional predicate exclude-tmp)
  14458. "Return a list of Org buffers.
  14459. PREDICATE can be `export', `files' or `agenda'.
  14460. export restrict the list to Export buffers.
  14461. files restrict the list to buffers visiting Org files.
  14462. agenda restrict the list to buffers visiting agenda files.
  14463. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14464. (let* ((bfn nil)
  14465. (agenda-files (and (eq predicate 'agenda)
  14466. (mapcar 'file-truename (org-agenda-files t))))
  14467. (filter
  14468. (cond
  14469. ((eq predicate 'files)
  14470. (lambda (b) (with-current-buffer b (org-mode-p))))
  14471. ((eq predicate 'export)
  14472. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14473. ((eq predicate 'agenda)
  14474. (lambda (b)
  14475. (with-current-buffer b
  14476. (and (org-mode-p)
  14477. (setq bfn (buffer-file-name b))
  14478. (member (file-truename bfn) agenda-files)))))
  14479. (t (lambda (b) (with-current-buffer b
  14480. (or (org-mode-p)
  14481. (string-match "\*Org .*Export"
  14482. (buffer-name b)))))))))
  14483. (delq nil
  14484. (mapcar
  14485. (lambda(b)
  14486. (if (and (funcall filter b)
  14487. (or (not exclude-tmp)
  14488. (not (string-match "tmp" (buffer-name b)))))
  14489. b
  14490. nil))
  14491. (buffer-list)))))
  14492. (defun org-agenda-files (&optional unrestricted archives)
  14493. "Get the list of agenda files.
  14494. Optional UNRESTRICTED means return the full list even if a restriction
  14495. is currently in place.
  14496. When ARCHIVES is t, include all archive files that are really being
  14497. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14498. `org-agenda-archives-mode' is t."
  14499. (let ((files
  14500. (cond
  14501. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14502. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14503. ((listp org-agenda-files) org-agenda-files)
  14504. (t (error "Invalid value of `org-agenda-files'")))))
  14505. (setq files (apply 'append
  14506. (mapcar (lambda (f)
  14507. (if (file-directory-p f)
  14508. (directory-files
  14509. f t org-agenda-file-regexp)
  14510. (list f)))
  14511. files)))
  14512. (when org-agenda-skip-unavailable-files
  14513. (setq files (delq nil
  14514. (mapcar (function
  14515. (lambda (file)
  14516. (and (file-readable-p file) file)))
  14517. files))))
  14518. (when (or (eq archives t)
  14519. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14520. (setq files (org-add-archive-files files)))
  14521. files))
  14522. (defun org-agenda-file-p (&optional file)
  14523. "Return non-nil, if FILE is an agenda file.
  14524. If FILE is omitted, use the file associated with the current
  14525. buffer."
  14526. (member (or file (buffer-file-name))
  14527. (org-agenda-files t)))
  14528. (defun org-edit-agenda-file-list ()
  14529. "Edit the list of agenda files.
  14530. Depending on setup, this either uses customize to edit the variable
  14531. `org-agenda-files', or it visits the file that is holding the list. In the
  14532. latter case, the buffer is set up in a way that saving it automatically kills
  14533. the buffer and restores the previous window configuration."
  14534. (interactive)
  14535. (if (stringp org-agenda-files)
  14536. (let ((cw (current-window-configuration)))
  14537. (find-file org-agenda-files)
  14538. (org-set-local 'org-window-configuration cw)
  14539. (org-add-hook 'after-save-hook
  14540. (lambda ()
  14541. (set-window-configuration
  14542. (prog1 org-window-configuration
  14543. (kill-buffer (current-buffer))))
  14544. (org-install-agenda-files-menu)
  14545. (message "New agenda file list installed"))
  14546. nil 'local)
  14547. (message "%s" (substitute-command-keys
  14548. "Edit list and finish with \\[save-buffer]")))
  14549. (customize-variable 'org-agenda-files)))
  14550. (defun org-store-new-agenda-file-list (list)
  14551. "Set new value for the agenda file list and save it correctly."
  14552. (if (stringp org-agenda-files)
  14553. (let ((fe (org-read-agenda-file-list t)) b u)
  14554. (while (setq b (find-buffer-visiting org-agenda-files))
  14555. (kill-buffer b))
  14556. (with-temp-file org-agenda-files
  14557. (insert
  14558. (mapconcat
  14559. (lambda (f) ;; Keep un-expanded entries.
  14560. (if (setq u (assoc f fe))
  14561. (cdr u)
  14562. f))
  14563. list "\n")
  14564. "\n")))
  14565. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14566. (org-insert-mode-line-in-empty-file nil))
  14567. (setq org-agenda-files list)
  14568. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14569. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14570. "Read the list of agenda files from a file.
  14571. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14572. filenames, used by `org-store-new-agenda-file-list' to write back
  14573. un-expanded file names."
  14574. (when (file-directory-p org-agenda-files)
  14575. (error "`org-agenda-files' cannot be a single directory"))
  14576. (when (stringp org-agenda-files)
  14577. (with-temp-buffer
  14578. (insert-file-contents org-agenda-files)
  14579. (mapcar
  14580. (lambda (f)
  14581. (let ((e (expand-file-name (substitute-in-file-name f)
  14582. org-directory)))
  14583. (if pair-with-expansion
  14584. (cons e f)
  14585. e)))
  14586. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14587. ;;;###autoload
  14588. (defun org-cycle-agenda-files ()
  14589. "Cycle through the files in `org-agenda-files'.
  14590. If the current buffer visits an agenda file, find the next one in the list.
  14591. If the current buffer does not, find the first agenda file."
  14592. (interactive)
  14593. (let* ((fs (org-agenda-files t))
  14594. (files (append fs (list (car fs))))
  14595. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14596. file)
  14597. (unless files (error "No agenda files"))
  14598. (catch 'exit
  14599. (while (setq file (pop files))
  14600. (if (equal (file-truename file) tcf)
  14601. (when (car files)
  14602. (find-file (car files))
  14603. (throw 'exit t))))
  14604. (find-file (car fs)))
  14605. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14606. (defun org-agenda-file-to-front (&optional to-end)
  14607. "Move/add the current file to the top of the agenda file list.
  14608. If the file is not present in the list, it is added to the front. If it is
  14609. present, it is moved there. With optional argument TO-END, add/move to the
  14610. end of the list."
  14611. (interactive "P")
  14612. (let ((org-agenda-skip-unavailable-files nil)
  14613. (file-alist (mapcar (lambda (x)
  14614. (cons (file-truename x) x))
  14615. (org-agenda-files t)))
  14616. (ctf (file-truename buffer-file-name))
  14617. x had)
  14618. (setq x (assoc ctf file-alist) had x)
  14619. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14620. (if to-end
  14621. (setq file-alist (append (delq x file-alist) (list x)))
  14622. (setq file-alist (cons x (delq x file-alist))))
  14623. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14624. (org-install-agenda-files-menu)
  14625. (message "File %s to %s of agenda file list"
  14626. (if had "moved" "added") (if to-end "end" "front"))))
  14627. (defun org-remove-file (&optional file)
  14628. "Remove current file from the list of files in variable `org-agenda-files'.
  14629. These are the files which are being checked for agenda entries.
  14630. Optional argument FILE means use this file instead of the current."
  14631. (interactive)
  14632. (let* ((org-agenda-skip-unavailable-files nil)
  14633. (file (or file buffer-file-name))
  14634. (true-file (file-truename file))
  14635. (afile (abbreviate-file-name file))
  14636. (files (delq nil (mapcar
  14637. (lambda (x)
  14638. (if (equal true-file
  14639. (file-truename x))
  14640. nil x))
  14641. (org-agenda-files t)))))
  14642. (if (not (= (length files) (length (org-agenda-files t))))
  14643. (progn
  14644. (org-store-new-agenda-file-list files)
  14645. (org-install-agenda-files-menu)
  14646. (message "Removed file: %s" afile))
  14647. (message "File was not in list: %s (not removed)" afile))))
  14648. (defun org-file-menu-entry (file)
  14649. (vector file (list 'find-file file) t))
  14650. (defun org-check-agenda-file (file)
  14651. "Make sure FILE exists. If not, ask user what to do."
  14652. (when (not (file-exists-p file))
  14653. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14654. (abbreviate-file-name file))
  14655. (let ((r (downcase (read-char-exclusive))))
  14656. (cond
  14657. ((equal r ?r)
  14658. (org-remove-file file)
  14659. (throw 'nextfile t))
  14660. (t (error "Abort"))))))
  14661. (defun org-get-agenda-file-buffer (file)
  14662. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14663. it to the list of buffers which might be released later."
  14664. (let ((buf (org-find-base-buffer-visiting file)))
  14665. (if buf
  14666. buf ; just return it
  14667. ;; Make a new buffer and remember it
  14668. (setq buf (find-file-noselect file))
  14669. (if buf (push buf org-agenda-new-buffers))
  14670. buf)))
  14671. (defun org-release-buffers (blist)
  14672. "Release all buffers in list, asking the user for confirmation when needed.
  14673. When a buffer is unmodified, it is just killed. When modified, it is saved
  14674. \(if the user agrees) and then killed."
  14675. (let (buf file)
  14676. (while (setq buf (pop blist))
  14677. (setq file (buffer-file-name buf))
  14678. (when (and (buffer-modified-p buf)
  14679. file
  14680. (y-or-n-p (format "Save file %s? " file)))
  14681. (with-current-buffer buf (save-buffer)))
  14682. (kill-buffer buf))))
  14683. (defun org-prepare-agenda-buffers (files)
  14684. "Create buffers for all agenda files, protect archived trees and comments."
  14685. (interactive)
  14686. (let ((pa '(:org-archived t))
  14687. (pc '(:org-comment t))
  14688. (pall '(:org-archived t :org-comment t))
  14689. (inhibit-read-only t)
  14690. (rea (concat ":" org-archive-tag ":"))
  14691. bmp file re)
  14692. (save-excursion
  14693. (save-restriction
  14694. (while (setq file (pop files))
  14695. (catch 'nextfile
  14696. (if (bufferp file)
  14697. (set-buffer file)
  14698. (org-check-agenda-file file)
  14699. (set-buffer (org-get-agenda-file-buffer file)))
  14700. (widen)
  14701. (setq bmp (buffer-modified-p))
  14702. (org-refresh-category-properties)
  14703. (setq org-todo-keywords-for-agenda
  14704. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14705. (setq org-done-keywords-for-agenda
  14706. (append org-done-keywords-for-agenda org-done-keywords))
  14707. (setq org-todo-keyword-alist-for-agenda
  14708. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14709. (setq org-drawers-for-agenda
  14710. (append org-drawers-for-agenda org-drawers))
  14711. (setq org-tag-alist-for-agenda
  14712. (append org-tag-alist-for-agenda org-tag-alist))
  14713. (save-excursion
  14714. (remove-text-properties (point-min) (point-max) pall)
  14715. (when org-agenda-skip-archived-trees
  14716. (goto-char (point-min))
  14717. (while (re-search-forward rea nil t)
  14718. (if (org-on-heading-p t)
  14719. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14720. (goto-char (point-min))
  14721. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14722. (while (re-search-forward re nil t)
  14723. (add-text-properties
  14724. (match-beginning 0) (org-end-of-subtree t) pc)))
  14725. (set-buffer-modified-p bmp)))))
  14726. (setq org-todo-keywords-for-agenda
  14727. (org-uniquify org-todo-keywords-for-agenda))
  14728. (setq org-todo-keyword-alist-for-agenda
  14729. (org-uniquify org-todo-keyword-alist-for-agenda)
  14730. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14731. ;;;; Embedded LaTeX
  14732. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14733. "Keymap for the minor `org-cdlatex-mode'.")
  14734. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14735. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14736. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14737. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14738. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14739. (defvar org-cdlatex-texmathp-advice-is-done nil
  14740. "Flag remembering if we have applied the advice to texmathp already.")
  14741. (define-minor-mode org-cdlatex-mode
  14742. "Toggle the minor `org-cdlatex-mode'.
  14743. This mode supports entering LaTeX environment and math in LaTeX fragments
  14744. in Org-mode.
  14745. \\{org-cdlatex-mode-map}"
  14746. nil " OCDL" nil
  14747. (when org-cdlatex-mode (require 'cdlatex))
  14748. (unless org-cdlatex-texmathp-advice-is-done
  14749. (setq org-cdlatex-texmathp-advice-is-done t)
  14750. (defadvice texmathp (around org-math-always-on activate)
  14751. "Always return t in org-mode buffers.
  14752. This is because we want to insert math symbols without dollars even outside
  14753. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14754. an embedded LaTeX fragment, let texmathp do its job.
  14755. \\[org-cdlatex-mode-map]"
  14756. (interactive)
  14757. (let (p)
  14758. (cond
  14759. ((not (org-mode-p)) ad-do-it)
  14760. ((eq this-command 'cdlatex-math-symbol)
  14761. (setq ad-return-value t
  14762. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14763. (t
  14764. (let ((p (org-inside-LaTeX-fragment-p)))
  14765. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14766. (setq ad-return-value t
  14767. texmathp-why '("Org-mode embedded math" . 0))
  14768. (if p ad-do-it)))))))))
  14769. (defun turn-on-org-cdlatex ()
  14770. "Unconditionally turn on `org-cdlatex-mode'."
  14771. (org-cdlatex-mode 1))
  14772. (defun org-inside-LaTeX-fragment-p ()
  14773. "Test if point is inside a LaTeX fragment.
  14774. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14775. sequence appearing also before point.
  14776. Even though the matchers for math are configurable, this function assumes
  14777. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14778. delimiters are skipped when they have been removed by customization.
  14779. The return value is nil, or a cons cell with the delimiter and the
  14780. position of this delimiter.
  14781. This function does a reasonably good job, but can locally be fooled by
  14782. for example currency specifications. For example it will assume being in
  14783. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14784. fragments that are properly closed, but during editing, we have to live
  14785. with the uncertainty caused by missing closing delimiters. This function
  14786. looks only before point, not after."
  14787. (catch 'exit
  14788. (let ((pos (point))
  14789. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14790. (lim (progn
  14791. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14792. (point)))
  14793. dd-on str (start 0) m re)
  14794. (goto-char pos)
  14795. (when dodollar
  14796. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14797. re (nth 1 (assoc "$" org-latex-regexps)))
  14798. (while (string-match re str start)
  14799. (cond
  14800. ((= (match-end 0) (length str))
  14801. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14802. ((= (match-end 0) (- (length str) 5))
  14803. (throw 'exit nil))
  14804. (t (setq start (match-end 0))))))
  14805. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14806. (goto-char pos)
  14807. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14808. (and (match-beginning 2) (throw 'exit nil))
  14809. ;; count $$
  14810. (while (re-search-backward "\\$\\$" lim t)
  14811. (setq dd-on (not dd-on)))
  14812. (goto-char pos)
  14813. (if dd-on (cons "$$" m))))))
  14814. (defun org-inside-latex-macro-p ()
  14815. "Is point inside a LaTeX macro or its arguments?"
  14816. (save-match-data
  14817. (org-in-regexp
  14818. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14819. (defun org-try-cdlatex-tab ()
  14820. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14821. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14822. - inside a LaTeX fragment, or
  14823. - after the first word in a line, where an abbreviation expansion could
  14824. insert a LaTeX environment."
  14825. (when org-cdlatex-mode
  14826. (cond
  14827. ((save-excursion
  14828. (skip-chars-backward "a-zA-Z0-9*")
  14829. (skip-chars-backward " \t")
  14830. (bolp))
  14831. (cdlatex-tab) t)
  14832. ((org-inside-LaTeX-fragment-p)
  14833. (cdlatex-tab) t)
  14834. (t nil))))
  14835. (defun org-cdlatex-underscore-caret (&optional arg)
  14836. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14837. Revert to the normal definition outside of these fragments."
  14838. (interactive "P")
  14839. (if (org-inside-LaTeX-fragment-p)
  14840. (call-interactively 'cdlatex-sub-superscript)
  14841. (let (org-cdlatex-mode)
  14842. (call-interactively (key-binding (vector last-input-event))))))
  14843. (defun org-cdlatex-math-modify (&optional arg)
  14844. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14845. Revert to the normal definition outside of these fragments."
  14846. (interactive "P")
  14847. (if (org-inside-LaTeX-fragment-p)
  14848. (call-interactively 'cdlatex-math-modify)
  14849. (let (org-cdlatex-mode)
  14850. (call-interactively (key-binding (vector last-input-event))))))
  14851. (defvar org-latex-fragment-image-overlays nil
  14852. "List of overlays carrying the images of latex fragments.")
  14853. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14854. (defun org-remove-latex-fragment-image-overlays ()
  14855. "Remove all overlays with LaTeX fragment images in current buffer."
  14856. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14857. (setq org-latex-fragment-image-overlays nil))
  14858. (defun org-preview-latex-fragment (&optional subtree)
  14859. "Preview the LaTeX fragment at point, or all locally or globally.
  14860. If the cursor is in a LaTeX fragment, create the image and overlay
  14861. it over the source code. If there is no fragment at point, display
  14862. all fragments in the current text, from one headline to the next. With
  14863. prefix SUBTREE, display all fragments in the current subtree. With a
  14864. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14865. the cursor is before the first headline,
  14866. display all fragments in the buffer.
  14867. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14868. (interactive "P")
  14869. (org-remove-latex-fragment-image-overlays)
  14870. (save-excursion
  14871. (save-restriction
  14872. (let (beg end at msg)
  14873. (cond
  14874. ((or (equal subtree '(16))
  14875. (not (save-excursion
  14876. (re-search-backward (concat "^" outline-regexp) nil t))))
  14877. (setq beg (point-min) end (point-max)
  14878. msg "Creating images for buffer...%s"))
  14879. ((equal subtree '(4))
  14880. (org-back-to-heading)
  14881. (setq beg (point) end (org-end-of-subtree t)
  14882. msg "Creating images for subtree...%s"))
  14883. (t
  14884. (if (setq at (org-inside-LaTeX-fragment-p))
  14885. (goto-char (max (point-min) (- (cdr at) 2)))
  14886. (org-back-to-heading))
  14887. (setq beg (point) end (progn (outline-next-heading) (point))
  14888. msg (if at "Creating image...%s"
  14889. "Creating images for entry...%s"))))
  14890. (message msg "")
  14891. (narrow-to-region beg end)
  14892. (goto-char beg)
  14893. (org-format-latex
  14894. (concat "ltxpng/" (file-name-sans-extension
  14895. (file-name-nondirectory
  14896. buffer-file-name)))
  14897. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14898. (message msg "done. Use `C-c C-c' to remove images.")))))
  14899. (defvar org-latex-regexps
  14900. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14901. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14902. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14903. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14904. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14905. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14906. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14907. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14908. "Regular expressions for matching embedded LaTeX.")
  14909. (defvar org-export-have-math nil) ;; dynamic scoping
  14910. (defun org-format-latex (prefix &optional dir overlays msg at
  14911. forbuffer processing-type)
  14912. "Replace LaTeX fragments with links to an image, and produce images.
  14913. Some of the options can be changed using the variable
  14914. `org-format-latex-options'."
  14915. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14916. (let* ((prefixnodir (file-name-nondirectory prefix))
  14917. (absprefix (expand-file-name prefix dir))
  14918. (todir (file-name-directory absprefix))
  14919. (opt org-format-latex-options)
  14920. (matchers (plist-get opt :matchers))
  14921. (re-list org-latex-regexps)
  14922. (org-format-latex-header-extra
  14923. (plist-get (org-infile-export-plist) :latex-header-extra))
  14924. (cnt 0) txt hash link beg end re e checkdir
  14925. executables-checked string
  14926. m n block linkfile movefile ov)
  14927. ;; Check the different regular expressions
  14928. (while (setq e (pop re-list))
  14929. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14930. block (if (nth 3 e) "\n\n" ""))
  14931. (when (member m matchers)
  14932. (goto-char (point-min))
  14933. (while (re-search-forward re nil t)
  14934. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14935. (not (get-text-property (match-beginning n)
  14936. 'org-protected))
  14937. (or (not overlays)
  14938. (not (eq (get-char-property (match-beginning n)
  14939. 'org-overlay-type)
  14940. 'org-latex-overlay))))
  14941. (setq org-export-have-math t)
  14942. (cond
  14943. ((eq processing-type 'verbatim)
  14944. ;; Leave the text verbatim, just protect it
  14945. (add-text-properties (match-beginning n) (match-end n)
  14946. '(org-protected t)))
  14947. ((eq processing-type 'mathjax)
  14948. ;; Prepare for MathJax processing
  14949. (setq string (match-string n))
  14950. (if (member m '("$" "$1"))
  14951. (save-excursion
  14952. (delete-region (match-beginning n) (match-end n))
  14953. (goto-char (match-beginning n))
  14954. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14955. "\\)")
  14956. '(org-protected t))))
  14957. (add-text-properties (match-beginning n) (match-end n)
  14958. '(org-protected t))))
  14959. ((or (eq processing-type 'dvipng) t)
  14960. ;; Process to an image
  14961. (setq txt (match-string n)
  14962. beg (match-beginning n) end (match-end n)
  14963. cnt (1+ cnt))
  14964. (let (print-length print-level) ; make sure full list is printed
  14965. (setq hash (sha1 (prin1-to-string
  14966. (list org-format-latex-header
  14967. org-format-latex-header-extra
  14968. org-export-latex-default-packages-alist
  14969. org-export-latex-packages-alist
  14970. org-format-latex-options
  14971. forbuffer txt)))
  14972. linkfile (format "%s_%s.png" prefix hash)
  14973. movefile (format "%s_%s.png" absprefix hash)))
  14974. (setq link (concat block "[[file:" linkfile "]]" block))
  14975. (if msg (message msg cnt))
  14976. (goto-char beg)
  14977. (unless checkdir ; make sure the directory exists
  14978. (setq checkdir t)
  14979. (or (file-directory-p todir) (make-directory todir t)))
  14980. (unless executables-checked
  14981. (org-check-external-command
  14982. "latex" "needed to convert LaTeX fragments to images")
  14983. (org-check-external-command
  14984. "dvipng" "needed to convert LaTeX fragments to images")
  14985. (setq executables-checked t))
  14986. (unless (file-exists-p movefile)
  14987. (org-create-formula-image
  14988. txt movefile opt forbuffer))
  14989. (if overlays
  14990. (progn
  14991. (mapc (lambda (o)
  14992. (if (eq (overlay-get o 'org-overlay-type)
  14993. 'org-latex-overlay)
  14994. (delete-overlay o)))
  14995. (overlays-in beg end))
  14996. (setq ov (make-overlay beg end))
  14997. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14998. (if (featurep 'xemacs)
  14999. (progn
  15000. (overlay-put ov 'invisible t)
  15001. (overlay-put
  15002. ov 'end-glyph
  15003. (make-glyph (vector 'png :file movefile))))
  15004. (overlay-put
  15005. ov 'display
  15006. (list 'image :type 'png :file movefile :ascent 'center)))
  15007. (push ov org-latex-fragment-image-overlays)
  15008. (goto-char end))
  15009. (delete-region beg end)
  15010. (insert (org-add-props link
  15011. (list 'org-latex-src
  15012. (replace-regexp-in-string
  15013. "\"" "" txt)))))))))))))
  15014. ;; This function borrows from Ganesh Swami's latex2png.el
  15015. (defun org-create-formula-image (string tofile options buffer)
  15016. "This calls dvipng."
  15017. (require 'org-latex)
  15018. (let* ((tmpdir (if (featurep 'xemacs)
  15019. (temp-directory)
  15020. temporary-file-directory))
  15021. (texfilebase (make-temp-name
  15022. (expand-file-name "orgtex" tmpdir)))
  15023. (texfile (concat texfilebase ".tex"))
  15024. (dvifile (concat texfilebase ".dvi"))
  15025. (pngfile (concat texfilebase ".png"))
  15026. (fnh (if (featurep 'xemacs)
  15027. (font-height (get-face-font 'default))
  15028. (face-attribute 'default :height nil)))
  15029. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15030. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15031. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15032. "Black"))
  15033. (bg (or (plist-get options (if buffer :background :html-background))
  15034. "Transparent")))
  15035. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15036. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15037. (with-temp-file texfile
  15038. (insert (org-splice-latex-header
  15039. org-format-latex-header
  15040. org-export-latex-default-packages-alist
  15041. org-export-latex-packages-alist t
  15042. org-format-latex-header-extra))
  15043. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15044. (require 'org-latex)
  15045. (org-export-latex-fix-inputenc))
  15046. (let ((dir default-directory))
  15047. (condition-case nil
  15048. (progn
  15049. (cd tmpdir)
  15050. (call-process "latex" nil nil nil texfile))
  15051. (error nil))
  15052. (cd dir))
  15053. (if (not (file-exists-p dvifile))
  15054. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15055. (condition-case nil
  15056. (call-process "dvipng" nil nil nil
  15057. "-fg" fg "-bg" bg
  15058. "-D" dpi
  15059. ;;"-x" scale "-y" scale
  15060. "-T" "tight"
  15061. "-o" pngfile
  15062. dvifile)
  15063. (error nil))
  15064. (if (not (file-exists-p pngfile))
  15065. (if org-format-latex-signal-error
  15066. (error "Failed to create png file from %s" texfile)
  15067. (message "Failed to create png file from %s" texfile)
  15068. nil)
  15069. ;; Use the requested file name and clean up
  15070. (copy-file pngfile tofile 'replace)
  15071. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15072. (delete-file (concat texfilebase e)))
  15073. pngfile))))
  15074. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15075. "Fill a LaTeX header template TPL.
  15076. In the template, the following place holders will be recognized:
  15077. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15078. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15079. [PACKAGES] \\usepackage statements for PKG
  15080. [NO-PACKAGES] do not include PKG
  15081. [EXTRA] the string EXTRA
  15082. [NO-EXTRA] do not include EXTRA
  15083. For backward compatibility, if both the positive and the negative place
  15084. holder is missing, the positive one (without the \"NO-\") will be
  15085. assumed to be present at the end of the template.
  15086. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15087. EXTRA is a string.
  15088. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15089. (let (rpl (end ""))
  15090. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15091. (setq rpl (if (or (match-end 1) (not def-pkg))
  15092. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15093. tpl (replace-match rpl t t tpl))
  15094. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15095. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15096. (setq rpl (if (or (match-end 1) (not pkg))
  15097. "" (org-latex-packages-to-string pkg snippets-p t))
  15098. tpl (replace-match rpl t t tpl))
  15099. (if pkg (setq end
  15100. (concat end "\n"
  15101. (org-latex-packages-to-string pkg snippets-p)))))
  15102. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15103. (setq rpl (if (or (match-end 1) (not extra))
  15104. "" (concat extra "\n"))
  15105. tpl (replace-match rpl t t tpl))
  15106. (if (and extra (string-match "\\S-" extra))
  15107. (setq end (concat end "\n" extra))))
  15108. (if (string-match "\\S-" end)
  15109. (concat tpl "\n" end)
  15110. tpl)))
  15111. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15112. "Turn an alist of packages into a string with the \\usepackage macros."
  15113. (setq pkg (mapconcat (lambda(p)
  15114. (cond
  15115. ((stringp p) p)
  15116. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15117. (format "%% Package %s omitted" (cadr p)))
  15118. ((equal "" (car p))
  15119. (format "\\usepackage{%s}" (cadr p)))
  15120. (t
  15121. (format "\\usepackage[%s]{%s}"
  15122. (car p) (cadr p)))))
  15123. pkg
  15124. "\n"))
  15125. (if newline (concat pkg "\n") pkg))
  15126. (defun org-dvipng-color (attr)
  15127. "Return an rgb color specification for dvipng."
  15128. (apply 'format "rgb %s %s %s"
  15129. (mapcar 'org-normalize-color
  15130. (color-values (face-attribute 'default attr nil)))))
  15131. (defun org-normalize-color (value)
  15132. "Return string to be used as color value for an RGB component."
  15133. (format "%g" (/ value 65535.0)))
  15134. ;; Image display
  15135. (defvar org-inline-image-overlays nil)
  15136. (make-variable-buffer-local 'org-inline-image-overlays)
  15137. (defun org-toggle-inline-images (&optional include-linked)
  15138. "Toggle the display of inline images.
  15139. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15140. (interactive "P")
  15141. (if org-inline-image-overlays
  15142. (progn
  15143. (org-remove-inline-images)
  15144. (message "Inline image display turned off"))
  15145. (org-display-inline-images include-linked)
  15146. (if org-inline-image-overlays
  15147. (message "%d images displayed inline"
  15148. (length org-inline-image-overlays))
  15149. (message "No images to display inline"))))
  15150. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15151. "Display inline images.
  15152. Normally only links without a description part are inlined, because this
  15153. is how it will work for export. When INCLUDE-LINKED is set, also links
  15154. with a description part will be inlined. This can be nice for a quick
  15155. look at those images, but it does not reflect what exported files will look
  15156. like.
  15157. When REFRESH is set, refresh existing images between BEG and END.
  15158. This will create new image displays only if necessary.
  15159. BEG and END default to the buffer boundaries."
  15160. (interactive "P")
  15161. (unless refresh
  15162. (org-remove-inline-images)
  15163. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15164. (save-excursion
  15165. (save-restriction
  15166. (widen)
  15167. (setq beg (or beg (point-min)) end (or end (point-max)))
  15168. (goto-char (point-min))
  15169. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15170. (substring (org-image-file-name-regexp) 0 -2)
  15171. "\\)\\]" (if include-linked "" "\\]")))
  15172. old file ov img)
  15173. (while (re-search-forward re end t)
  15174. (setq old (get-char-property-and-overlay (match-beginning 1)
  15175. 'org-image-overlay))
  15176. (setq file (expand-file-name
  15177. (concat (or (match-string 3) "") (match-string 4))))
  15178. (when (file-exists-p file)
  15179. (if (and (car-safe old) refresh)
  15180. (image-refresh (overlay-get (cdr old) 'display))
  15181. (setq img (save-match-data (create-image file)))
  15182. (when img
  15183. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15184. (overlay-put ov 'display img)
  15185. (overlay-put ov 'face 'default)
  15186. (overlay-put ov 'org-image-overlay t)
  15187. (overlay-put ov 'modification-hooks
  15188. (list 'org-display-inline-modification-hook))
  15189. (push ov org-inline-image-overlays)))))))))
  15190. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15191. "Remove inline-display overlay if a corresponding region is modified."
  15192. (let ((inhibit-modification-hooks t))
  15193. (when (and ov after)
  15194. (delete ov org-inline-image-overlays)
  15195. (delete-overlay ov))))
  15196. (defun org-remove-inline-images ()
  15197. "Remove inline display of images."
  15198. (interactive)
  15199. (mapc 'delete-overlay org-inline-image-overlays)
  15200. (setq org-inline-image-overlays nil))
  15201. ;;;; Key bindings
  15202. ;; Make `C-c C-x' a prefix key
  15203. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15204. ;; TAB key with modifiers
  15205. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15206. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15207. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15208. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15209. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15210. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15211. ;; The following line is necessary under Suse GNU/Linux
  15212. (unless (featurep 'xemacs)
  15213. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15214. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15215. (define-key org-mode-map [backtab] 'org-shifttab)
  15216. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15217. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15218. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15219. ;; Cursor keys with modifiers
  15220. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15221. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15222. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15223. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15224. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15225. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15226. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15227. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15228. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15229. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15230. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15231. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15232. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15233. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15234. ;; Babel keys
  15235. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15236. (mapc (lambda (pair)
  15237. (define-key org-babel-map (car pair) (cdr pair)))
  15238. org-babel-key-bindings)
  15239. ;;; Extra keys for tty access.
  15240. ;; We only set them when really needed because otherwise the
  15241. ;; menus don't show the simple keys
  15242. (when (or org-use-extra-keys
  15243. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15244. (not window-system))
  15245. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15246. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15247. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15248. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15249. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15250. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15251. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15252. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15253. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15254. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15255. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15256. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15257. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15258. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15259. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15260. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15261. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15262. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15263. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15264. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15265. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15266. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15267. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15268. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15269. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15270. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15271. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15272. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15273. ;; All the other keys
  15274. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15275. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15276. (if (boundp 'narrow-map)
  15277. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15278. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15279. (if (boundp 'narrow-map)
  15280. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15281. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15282. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15283. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15284. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15285. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15286. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15287. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15288. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15289. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15290. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15291. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15292. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15293. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15294. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15295. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15296. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15297. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15298. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15299. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15300. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15301. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15302. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15303. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15304. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15305. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15306. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15307. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15308. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15309. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15310. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15311. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15312. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15313. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15314. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15315. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15316. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15317. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15318. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15319. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15320. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15321. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15322. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15323. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15324. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15325. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15326. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15327. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15328. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15329. (org-defkey org-mode-map "\C-m" 'org-return)
  15330. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15331. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15332. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15333. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15334. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15335. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15336. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15337. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15338. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15339. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15340. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15341. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15342. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15343. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15344. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15345. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15346. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15347. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15348. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15349. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15350. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15351. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15352. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15353. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15354. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15355. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15356. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15357. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15358. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15359. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15360. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15361. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15362. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15363. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15364. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15365. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15366. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15367. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15368. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15369. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15370. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15371. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15372. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15373. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15374. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15375. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15376. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15377. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15378. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15379. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15380. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15381. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15382. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15383. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15384. (when (featurep 'xemacs)
  15385. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15386. (defconst org-speed-commands-default
  15387. '(
  15388. ("Outline Navigation")
  15389. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15390. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15391. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15392. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15393. ("u" . (org-speed-move-safe 'outline-up-heading))
  15394. ("j" . org-goto)
  15395. ("g" . (org-refile t))
  15396. ("Outline Visibility")
  15397. ("c" . org-cycle)
  15398. ("C" . org-shifttab)
  15399. (" " . org-display-outline-path)
  15400. ("Outline Structure Editing")
  15401. ("U" . org-shiftmetaup)
  15402. ("D" . org-shiftmetadown)
  15403. ("r" . org-metaright)
  15404. ("l" . org-metaleft)
  15405. ("R" . org-shiftmetaright)
  15406. ("L" . org-shiftmetaleft)
  15407. ("i" . (progn (forward-char 1) (call-interactively
  15408. 'org-insert-heading-respect-content)))
  15409. ("^" . org-sort)
  15410. ("w" . org-refile)
  15411. ("a" . org-archive-subtree-default-with-confirmation)
  15412. ("." . org-mark-subtree)
  15413. ("Clock Commands")
  15414. ("I" . org-clock-in)
  15415. ("O" . org-clock-out)
  15416. ("Meta Data Editing")
  15417. ("t" . org-todo)
  15418. ("0" . (org-priority ?\ ))
  15419. ("1" . (org-priority ?A))
  15420. ("2" . (org-priority ?B))
  15421. ("3" . (org-priority ?C))
  15422. (";" . org-set-tags-command)
  15423. ("e" . org-set-effort)
  15424. ("Agenda Views etc")
  15425. ("v" . org-agenda)
  15426. ("/" . org-sparse-tree)
  15427. ("Misc")
  15428. ("o" . org-open-at-point)
  15429. ("?" . org-speed-command-help)
  15430. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15431. (">" . (org-agenda-remove-restriction-lock))
  15432. )
  15433. "The default speed commands.")
  15434. (defun org-print-speed-command (e)
  15435. (if (> (length (car e)) 1)
  15436. (progn
  15437. (princ "\n")
  15438. (princ (car e))
  15439. (princ "\n")
  15440. (princ (make-string (length (car e)) ?-))
  15441. (princ "\n"))
  15442. (princ (car e))
  15443. (princ " ")
  15444. (if (symbolp (cdr e))
  15445. (princ (symbol-name (cdr e)))
  15446. (prin1 (cdr e)))
  15447. (princ "\n")))
  15448. (defun org-speed-command-help ()
  15449. "Show the available speed commands."
  15450. (interactive)
  15451. (if (not org-use-speed-commands)
  15452. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15453. (with-output-to-temp-buffer "*Help*"
  15454. (princ "User-defined Speed commands\n===========================\n")
  15455. (mapc 'org-print-speed-command org-speed-commands-user)
  15456. (princ "\n")
  15457. (princ "Built-in Speed commands\n=======================\n")
  15458. (mapc 'org-print-speed-command org-speed-commands-default))
  15459. (with-current-buffer "*Help*"
  15460. (setq truncate-lines t))))
  15461. (defun org-speed-move-safe (cmd)
  15462. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15463. If not, return to the original position and throw an error."
  15464. (interactive)
  15465. (let ((pos (point)))
  15466. (call-interactively cmd)
  15467. (unless (and (bolp) (org-on-heading-p))
  15468. (goto-char pos)
  15469. (error "Boundary reached while executing %s" cmd))))
  15470. (defvar org-self-insert-command-undo-counter 0)
  15471. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15472. (defvar org-speed-command nil)
  15473. (defun org-speed-command-default-hook (keys)
  15474. "Hook for activating single-letter speed commands.
  15475. `org-speed-commands-default' specifies a minimal command set. Use
  15476. `org-speed-commands-user' for further customization."
  15477. (when (or (and (bolp) (looking-at outline-regexp))
  15478. (and (functionp org-use-speed-commands)
  15479. (funcall org-use-speed-commands)))
  15480. (cdr (assoc keys (append org-speed-commands-user
  15481. org-speed-commands-default)))))
  15482. (defun org-babel-speed-command-hook (keys)
  15483. "Hook for activating single-letter code block commands."
  15484. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15485. (cdr (assoc keys org-babel-key-bindings))))
  15486. (defcustom org-speed-command-hook
  15487. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15488. "Hook for activating speed commands at strategic locations.
  15489. Hook functions are called in sequence until a valid handler is
  15490. found.
  15491. Each hook takes a single argument, a user-pressed command key
  15492. which is also a `self-insert-command' from the global map.
  15493. Within the hook, examine the cursor position and the command key
  15494. and return nil or a valid handler as appropriate. Handler could
  15495. be one of an interactive command, a function, or a form.
  15496. Set `org-use-speed-commands' to non-nil value to enable this
  15497. hook. The default setting is `org-speed-command-default-hook'."
  15498. :group 'org-structure
  15499. :type 'hook)
  15500. (defun org-self-insert-command (N)
  15501. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15502. If the cursor is in a table looking at whitespace, the whitespace is
  15503. overwritten, and the table is not marked as requiring realignment."
  15504. (interactive "p")
  15505. (cond
  15506. ((and org-use-speed-commands
  15507. (setq org-speed-command
  15508. (run-hook-with-args-until-success
  15509. 'org-speed-command-hook (this-command-keys))))
  15510. (cond
  15511. ((commandp org-speed-command)
  15512. (setq this-command org-speed-command)
  15513. (call-interactively org-speed-command))
  15514. ((functionp org-speed-command)
  15515. (funcall org-speed-command))
  15516. ((and org-speed-command (listp org-speed-command))
  15517. (eval org-speed-command))
  15518. (t (let (org-use-speed-commands)
  15519. (call-interactively 'org-self-insert-command)))))
  15520. ((and
  15521. (org-table-p)
  15522. (progn
  15523. ;; check if we blank the field, and if that triggers align
  15524. (and (featurep 'org-table) org-table-auto-blank-field
  15525. (member last-command
  15526. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15527. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15528. ;; got extra space, this field does not determine column width
  15529. (let (org-table-may-need-update) (org-table-blank-field))
  15530. ;; no extra space, this field may determine column width
  15531. (org-table-blank-field)))
  15532. t)
  15533. (eq N 1)
  15534. (looking-at "[^|\n]* |"))
  15535. (let (org-table-may-need-update)
  15536. (goto-char (1- (match-end 0)))
  15537. (delete-char -1)
  15538. (goto-char (match-beginning 0))
  15539. (self-insert-command N)))
  15540. (t
  15541. (setq org-table-may-need-update t)
  15542. (self-insert-command N)
  15543. (org-fix-tags-on-the-fly)
  15544. (if org-self-insert-cluster-for-undo
  15545. (if (not (eq last-command 'org-self-insert-command))
  15546. (setq org-self-insert-command-undo-counter 1)
  15547. (if (>= org-self-insert-command-undo-counter 20)
  15548. (setq org-self-insert-command-undo-counter 1)
  15549. (and (> org-self-insert-command-undo-counter 0)
  15550. buffer-undo-list (listp buffer-undo-list)
  15551. (not (cadr buffer-undo-list)) ; remove nil entry
  15552. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15553. (setq org-self-insert-command-undo-counter
  15554. (1+ org-self-insert-command-undo-counter))))))))
  15555. (defun org-fix-tags-on-the-fly ()
  15556. (when (and (equal (char-after (point-at-bol)) ?*)
  15557. (org-on-heading-p))
  15558. (org-align-tags-here org-tags-column)))
  15559. (defun org-delete-backward-char (N)
  15560. "Like `delete-backward-char', insert whitespace at field end in tables.
  15561. When deleting backwards, in tables this function will insert whitespace in
  15562. front of the next \"|\" separator, to keep the table aligned. The table will
  15563. still be marked for re-alignment if the field did fill the entire column,
  15564. because, in this case the deletion might narrow the column."
  15565. (interactive "p")
  15566. (if (and (org-table-p)
  15567. (eq N 1)
  15568. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15569. (looking-at ".*?|"))
  15570. (let ((pos (point))
  15571. (noalign (looking-at "[^|\n\r]* |"))
  15572. (c org-table-may-need-update))
  15573. (backward-delete-char N)
  15574. (if (not overwrite-mode)
  15575. (progn
  15576. (skip-chars-forward "^|")
  15577. (insert " ")
  15578. (goto-char (1- pos))))
  15579. ;; noalign: if there were two spaces at the end, this field
  15580. ;; does not determine the width of the column.
  15581. (if noalign (setq org-table-may-need-update c)))
  15582. (backward-delete-char N)
  15583. (org-fix-tags-on-the-fly)))
  15584. (defun org-delete-char (N)
  15585. "Like `delete-char', but insert whitespace at field end in tables.
  15586. When deleting characters, in tables this function will insert whitespace in
  15587. front of the next \"|\" separator, to keep the table aligned. The table will
  15588. still be marked for re-alignment if the field did fill the entire column,
  15589. because, in this case the deletion might narrow the column."
  15590. (interactive "p")
  15591. (if (and (org-table-p)
  15592. (not (bolp))
  15593. (not (= (char-after) ?|))
  15594. (eq N 1))
  15595. (if (looking-at ".*?|")
  15596. (let ((pos (point))
  15597. (noalign (looking-at "[^|\n\r]* |"))
  15598. (c org-table-may-need-update))
  15599. (replace-match (concat
  15600. (substring (match-string 0) 1 -1)
  15601. " |"))
  15602. (goto-char pos)
  15603. ;; noalign: if there were two spaces at the end, this field
  15604. ;; does not determine the width of the column.
  15605. (if noalign (setq org-table-may-need-update c)))
  15606. (delete-char N))
  15607. (delete-char N)
  15608. (org-fix-tags-on-the-fly)))
  15609. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15610. (put 'org-self-insert-command 'delete-selection t)
  15611. (put 'orgtbl-self-insert-command 'delete-selection t)
  15612. (put 'org-delete-char 'delete-selection 'supersede)
  15613. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15614. (put 'org-yank 'delete-selection 'yank)
  15615. ;; Make `flyspell-mode' delay after some commands
  15616. (put 'org-self-insert-command 'flyspell-delayed t)
  15617. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15618. (put 'org-delete-char 'flyspell-delayed t)
  15619. (put 'org-delete-backward-char 'flyspell-delayed t)
  15620. ;; Make pabbrev-mode expand after org-mode commands
  15621. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15622. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15623. ;; How to do this: Measure non-white length of current string
  15624. ;; If equal to column width, we should realign.
  15625. (defun org-remap (map &rest commands)
  15626. "In MAP, remap the functions given in COMMANDS.
  15627. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15628. (let (new old)
  15629. (while commands
  15630. (setq old (pop commands) new (pop commands))
  15631. (if (fboundp 'command-remapping)
  15632. (org-defkey map (vector 'remap old) new)
  15633. (substitute-key-definition old new map global-map)))))
  15634. (when (eq org-enable-table-editor 'optimized)
  15635. ;; If the user wants maximum table support, we need to hijack
  15636. ;; some standard editing functions
  15637. (org-remap org-mode-map
  15638. 'self-insert-command 'org-self-insert-command
  15639. 'delete-char 'org-delete-char
  15640. 'delete-backward-char 'org-delete-backward-char)
  15641. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15642. (defvar org-ctrl-c-ctrl-c-hook nil
  15643. "Hook for functions attaching themselves to `C-c C-c'.
  15644. This can be used to add additional functionality to the C-c C-c key which
  15645. executes context-dependent commands.
  15646. Each function will be called with no arguments. The function must check
  15647. if the context is appropriate for it to act. If yes, it should do its
  15648. thing and then return a non-nil value. If the context is wrong,
  15649. just do nothing and return nil.")
  15650. (defvar org-tab-first-hook nil
  15651. "Hook for functions to attach themselves to TAB.
  15652. See `org-ctrl-c-ctrl-c-hook' for more information.
  15653. This hook runs as the first action when TAB is pressed, even before
  15654. `org-cycle' messes around with the `outline-regexp' to cater for
  15655. inline tasks and plain list item folding.
  15656. If any function in this hook returns t, any other actions that
  15657. would have been caused by TAB (such as table field motion or visibility
  15658. cycling) will not occur.")
  15659. (defvar org-tab-after-check-for-table-hook nil
  15660. "Hook for functions to attach themselves to TAB.
  15661. See `org-ctrl-c-ctrl-c-hook' for more information.
  15662. This hook runs after it has been established that the cursor is not in a
  15663. table, but before checking if the cursor is in a headline or if global cycling
  15664. should be done.
  15665. If any function in this hook returns t, not other actions like visibility
  15666. cycling will be done.")
  15667. (defvar org-tab-after-check-for-cycling-hook nil
  15668. "Hook for functions to attach themselves to TAB.
  15669. See `org-ctrl-c-ctrl-c-hook' for more information.
  15670. This hook runs after it has been established that not table field motion and
  15671. not visibility should be done because of current context. This is probably
  15672. the place where a package like yasnippets can hook in.")
  15673. (defvar org-tab-before-tab-emulation-hook nil
  15674. "Hook for functions to attach themselves to TAB.
  15675. See `org-ctrl-c-ctrl-c-hook' for more information.
  15676. This hook runs after every other options for TAB have been exhausted, but
  15677. before indentation and \t insertion takes place.")
  15678. (defvar org-metaleft-hook nil
  15679. "Hook for functions attaching themselves to `M-left'.
  15680. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15681. (defvar org-metaright-hook nil
  15682. "Hook for functions attaching themselves to `M-right'.
  15683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15684. (defvar org-metaup-hook nil
  15685. "Hook for functions attaching themselves to `M-up'.
  15686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15687. (defvar org-metadown-hook nil
  15688. "Hook for functions attaching themselves to `M-down'.
  15689. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15690. (defvar org-shiftmetaleft-hook nil
  15691. "Hook for functions attaching themselves to `M-S-left'.
  15692. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15693. (defvar org-shiftmetaright-hook nil
  15694. "Hook for functions attaching themselves to `M-S-right'.
  15695. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15696. (defvar org-shiftmetaup-hook nil
  15697. "Hook for functions attaching themselves to `M-S-up'.
  15698. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15699. (defvar org-shiftmetadown-hook nil
  15700. "Hook for functions attaching themselves to `M-S-down'.
  15701. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15702. (defvar org-metareturn-hook nil
  15703. "Hook for functions attaching themselves to `M-RET'.
  15704. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15705. (defvar org-shiftup-hook nil
  15706. "Hook for functions attaching themselves to `S-up'.
  15707. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15708. (defvar org-shiftup-final-hook nil
  15709. "Hook for functions attaching themselves to `S-up'.
  15710. This one runs after all other options except shift-select have been excluded.
  15711. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15712. (defvar org-shiftdown-hook nil
  15713. "Hook for functions attaching themselves to `S-down'.
  15714. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15715. (defvar org-shiftdown-final-hook nil
  15716. "Hook for functions attaching themselves to `S-down'.
  15717. This one runs after all other options except shift-select have been excluded.
  15718. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15719. (defvar org-shiftleft-hook nil
  15720. "Hook for functions attaching themselves to `S-left'.
  15721. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15722. (defvar org-shiftleft-final-hook nil
  15723. "Hook for functions attaching themselves to `S-left'.
  15724. This one runs after all other options except shift-select have been excluded.
  15725. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15726. (defvar org-shiftright-hook nil
  15727. "Hook for functions attaching themselves to `S-right'.
  15728. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15729. (defvar org-shiftright-final-hook nil
  15730. "Hook for functions attaching themselves to `S-right'.
  15731. This one runs after all other options except shift-select have been excluded.
  15732. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15733. (defun org-modifier-cursor-error ()
  15734. "Throw an error, a modified cursor command was applied in wrong context."
  15735. (error "This command is active in special context like tables, headlines or items"))
  15736. (defun org-shiftselect-error ()
  15737. "Throw an error because Shift-Cursor command was applied in wrong context."
  15738. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15739. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15740. (error "This command works only in special context like headlines or timestamps")))
  15741. (defun org-call-for-shift-select (cmd)
  15742. (let ((this-command-keys-shift-translated t))
  15743. (call-interactively cmd)))
  15744. (defun org-shifttab (&optional arg)
  15745. "Global visibility cycling or move to previous table field.
  15746. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15747. on context.
  15748. See the individual commands for more information."
  15749. (interactive "P")
  15750. (cond
  15751. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15752. ((integerp arg)
  15753. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15754. (message "Content view to level: %d" arg)
  15755. (org-content (prefix-numeric-value arg2))
  15756. (setq org-cycle-global-status 'overview)))
  15757. (t (call-interactively 'org-global-cycle))))
  15758. (defun org-shiftmetaleft ()
  15759. "Promote subtree or delete table column.
  15760. Calls `org-promote-subtree', `org-outdent-item',
  15761. or `org-table-delete-column', depending on context.
  15762. See the individual commands for more information."
  15763. (interactive)
  15764. (cond
  15765. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15766. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15767. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15768. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15769. (t (org-modifier-cursor-error))))
  15770. (defun org-shiftmetaright ()
  15771. "Demote subtree or insert table column.
  15772. Calls `org-demote-subtree', `org-indent-item',
  15773. or `org-table-insert-column', depending on context.
  15774. See the individual commands for more information."
  15775. (interactive)
  15776. (cond
  15777. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15778. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15779. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15780. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15781. (t (org-modifier-cursor-error))))
  15782. (defun org-shiftmetaup (&optional arg)
  15783. "Move subtree up or kill table row.
  15784. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15785. `org-move-item-up' depending on context. See the individual commands
  15786. for more information."
  15787. (interactive "P")
  15788. (cond
  15789. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15790. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15791. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15792. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15793. (t (org-modifier-cursor-error))))
  15794. (defun org-shiftmetadown (&optional arg)
  15795. "Move subtree down or insert table row.
  15796. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15797. `org-move-item-down', depending on context. See the individual
  15798. commands for more information."
  15799. (interactive "P")
  15800. (cond
  15801. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15802. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15803. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15804. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15805. (t (org-modifier-cursor-error))))
  15806. (defsubst org-hidden-tree-error ()
  15807. (error
  15808. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15809. (defun org-metaleft (&optional arg)
  15810. "Promote heading or move table column to left.
  15811. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15812. With no specific context, calls the Emacs default `backward-word'.
  15813. See the individual commands for more information."
  15814. (interactive "P")
  15815. (cond
  15816. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15817. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15818. ((org-with-limited-levels
  15819. (or (org-on-heading-p)
  15820. (and (org-region-active-p)
  15821. (save-excursion
  15822. (goto-char (region-beginning))
  15823. (org-on-heading-p)))))
  15824. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15825. (call-interactively 'org-do-promote))
  15826. ;; At an inline task.
  15827. ((org-on-heading-p)
  15828. (call-interactively 'org-inlinetask-promote))
  15829. ((or (org-at-item-p)
  15830. (and (org-region-active-p)
  15831. (save-excursion
  15832. (goto-char (region-beginning))
  15833. (org-at-item-p))))
  15834. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15835. (call-interactively 'org-outdent-item))
  15836. (t (call-interactively 'backward-word))))
  15837. (defun org-metaright (&optional arg)
  15838. "Demote subtree or move table column to right.
  15839. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15840. With no specific context, calls the Emacs default `forward-word'.
  15841. See the individual commands for more information."
  15842. (interactive "P")
  15843. (cond
  15844. ((run-hook-with-args-until-success 'org-metaright-hook))
  15845. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15846. ((org-with-limited-levels
  15847. (or (org-on-heading-p)
  15848. (and (org-region-active-p)
  15849. (save-excursion
  15850. (goto-char (region-beginning))
  15851. (org-on-heading-p)))))
  15852. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15853. (call-interactively 'org-do-demote))
  15854. ;; At an inline task.
  15855. ((org-on-heading-p)
  15856. (call-interactively 'org-inlinetask-demote))
  15857. ((or (org-at-item-p)
  15858. (and (org-region-active-p)
  15859. (save-excursion
  15860. (goto-char (region-beginning))
  15861. (org-at-item-p))))
  15862. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15863. (call-interactively 'org-indent-item))
  15864. (t (call-interactively 'forward-word))))
  15865. (defun org-check-for-hidden (what)
  15866. "Check if there are hidden headlines/items in the current visual line.
  15867. WHAT can be either `headlines' or `items'. If the current line is
  15868. an outline or item heading and it has a folded subtree below it,
  15869. this function returns t, nil otherwise."
  15870. (let ((re (cond
  15871. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15872. ((eq what 'items) (org-item-beginning-re))
  15873. (t (error "This should not happen"))))
  15874. beg end)
  15875. (save-excursion
  15876. (catch 'exit
  15877. (unless (org-region-active-p)
  15878. (setq beg (point-at-bol))
  15879. (beginning-of-line 2)
  15880. (while (and (not (eobp)) ;; this is like `next-line'
  15881. (get-char-property (1- (point)) 'invisible))
  15882. (beginning-of-line 2))
  15883. (setq end (point))
  15884. (goto-char beg)
  15885. (goto-char (point-at-eol))
  15886. (setq end (max end (point)))
  15887. (while (re-search-forward re end t)
  15888. (if (get-char-property (match-beginning 0) 'invisible)
  15889. (throw 'exit t))))
  15890. nil))))
  15891. (defun org-metaup (&optional arg)
  15892. "Move subtree up or move table row up.
  15893. Calls `org-move-subtree-up' or `org-table-move-row' or
  15894. `org-move-item-up', depending on context. See the individual commands
  15895. for more information."
  15896. (interactive "P")
  15897. (cond
  15898. ((run-hook-with-args-until-success 'org-metaup-hook))
  15899. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15900. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15901. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15902. (t (transpose-lines 1) (beginning-of-line -1))))
  15903. (defun org-metadown (&optional arg)
  15904. "Move subtree down or move table row down.
  15905. Calls `org-move-subtree-down' or `org-table-move-row' or
  15906. `org-move-item-down', depending on context. See the individual
  15907. commands for more information."
  15908. (interactive "P")
  15909. (cond
  15910. ((run-hook-with-args-until-success 'org-metadown-hook))
  15911. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15912. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15913. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15914. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15915. (defun org-shiftup (&optional arg)
  15916. "Increase item in timestamp or increase priority of current headline.
  15917. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15918. depending on context. See the individual commands for more information."
  15919. (interactive "P")
  15920. (cond
  15921. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15922. ((and org-support-shift-select (org-region-active-p))
  15923. (org-call-for-shift-select 'previous-line))
  15924. ((org-at-timestamp-p t)
  15925. (call-interactively (if org-edit-timestamp-down-means-later
  15926. 'org-timestamp-down 'org-timestamp-up)))
  15927. ((and (not (eq org-support-shift-select 'always))
  15928. org-enable-priority-commands
  15929. (org-on-heading-p))
  15930. (call-interactively 'org-priority-up))
  15931. ((and (not org-support-shift-select) (org-at-item-p))
  15932. (call-interactively 'org-previous-item))
  15933. ((org-clocktable-try-shift 'up arg))
  15934. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15935. (org-support-shift-select
  15936. (org-call-for-shift-select 'previous-line))
  15937. (t (org-shiftselect-error))))
  15938. (defun org-shiftdown (&optional arg)
  15939. "Decrease item in timestamp or decrease priority of current headline.
  15940. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15941. depending on context. See the individual commands for more information."
  15942. (interactive "P")
  15943. (cond
  15944. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15945. ((and org-support-shift-select (org-region-active-p))
  15946. (org-call-for-shift-select 'next-line))
  15947. ((org-at-timestamp-p t)
  15948. (call-interactively (if org-edit-timestamp-down-means-later
  15949. 'org-timestamp-up 'org-timestamp-down)))
  15950. ((and (not (eq org-support-shift-select 'always))
  15951. org-enable-priority-commands
  15952. (org-on-heading-p))
  15953. (call-interactively 'org-priority-down))
  15954. ((and (not org-support-shift-select) (org-at-item-p))
  15955. (call-interactively 'org-next-item))
  15956. ((org-clocktable-try-shift 'down arg))
  15957. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15958. (org-support-shift-select
  15959. (org-call-for-shift-select 'next-line))
  15960. (t (org-shiftselect-error))))
  15961. (defun org-shiftright (&optional arg)
  15962. "Cycle the thing at point or in the current line, depending on context.
  15963. Depending on context, this does one of the following:
  15964. - switch a timestamp at point one day into the future
  15965. - on a headline, switch to the next TODO keyword.
  15966. - on an item, switch entire list to the next bullet type
  15967. - on a property line, switch to the next allowed value
  15968. - on a clocktable definition line, move time block into the future"
  15969. (interactive "P")
  15970. (cond
  15971. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15972. ((and org-support-shift-select (org-region-active-p))
  15973. (org-call-for-shift-select 'forward-char))
  15974. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15975. ((and (not (eq org-support-shift-select 'always))
  15976. (org-on-heading-p))
  15977. (let ((org-inhibit-logging
  15978. (not org-treat-S-cursor-todo-selection-as-state-change))
  15979. (org-inhibit-blocking
  15980. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15981. (org-call-with-arg 'org-todo 'right)))
  15982. ((or (and org-support-shift-select
  15983. (not (eq org-support-shift-select 'always))
  15984. (org-at-item-bullet-p))
  15985. (and (not org-support-shift-select) (org-at-item-p)))
  15986. (org-call-with-arg 'org-cycle-list-bullet nil))
  15987. ((and (not (eq org-support-shift-select 'always))
  15988. (org-at-property-p))
  15989. (call-interactively 'org-property-next-allowed-value))
  15990. ((org-clocktable-try-shift 'right arg))
  15991. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15992. (org-support-shift-select
  15993. (org-call-for-shift-select 'forward-char))
  15994. (t (org-shiftselect-error))))
  15995. (defun org-shiftleft (&optional arg)
  15996. "Cycle the thing at point or in the current line, depending on context.
  15997. Depending on context, this does one of the following:
  15998. - switch a timestamp at point one day into the past
  15999. - on a headline, switch to the previous TODO keyword.
  16000. - on an item, switch entire list to the previous bullet type
  16001. - on a property line, switch to the previous allowed value
  16002. - on a clocktable definition line, move time block into the past"
  16003. (interactive "P")
  16004. (cond
  16005. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16006. ((and org-support-shift-select (org-region-active-p))
  16007. (org-call-for-shift-select 'backward-char))
  16008. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16009. ((and (not (eq org-support-shift-select 'always))
  16010. (org-on-heading-p))
  16011. (let ((org-inhibit-logging
  16012. (not org-treat-S-cursor-todo-selection-as-state-change))
  16013. (org-inhibit-blocking
  16014. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16015. (org-call-with-arg 'org-todo 'left)))
  16016. ((or (and org-support-shift-select
  16017. (not (eq org-support-shift-select 'always))
  16018. (org-at-item-bullet-p))
  16019. (and (not org-support-shift-select) (org-at-item-p)))
  16020. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16021. ((and (not (eq org-support-shift-select 'always))
  16022. (org-at-property-p))
  16023. (call-interactively 'org-property-previous-allowed-value))
  16024. ((org-clocktable-try-shift 'left arg))
  16025. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16026. (org-support-shift-select
  16027. (org-call-for-shift-select 'backward-char))
  16028. (t (org-shiftselect-error))))
  16029. (defun org-shiftcontrolright ()
  16030. "Switch to next TODO set."
  16031. (interactive)
  16032. (cond
  16033. ((and org-support-shift-select (org-region-active-p))
  16034. (org-call-for-shift-select 'forward-word))
  16035. ((and (not (eq org-support-shift-select 'always))
  16036. (org-on-heading-p))
  16037. (org-call-with-arg 'org-todo 'nextset))
  16038. (org-support-shift-select
  16039. (org-call-for-shift-select 'forward-word))
  16040. (t (org-shiftselect-error))))
  16041. (defun org-shiftcontrolleft ()
  16042. "Switch to previous TODO set."
  16043. (interactive)
  16044. (cond
  16045. ((and org-support-shift-select (org-region-active-p))
  16046. (org-call-for-shift-select 'backward-word))
  16047. ((and (not (eq org-support-shift-select 'always))
  16048. (org-on-heading-p))
  16049. (org-call-with-arg 'org-todo 'previousset))
  16050. (org-support-shift-select
  16051. (org-call-for-shift-select 'backward-word))
  16052. (t (org-shiftselect-error))))
  16053. (defun org-ctrl-c-ret ()
  16054. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16055. (interactive)
  16056. (cond
  16057. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16058. (t (call-interactively 'org-insert-heading))))
  16059. (defun org-copy-special ()
  16060. "Copy region in table or copy current subtree.
  16061. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16062. See the individual commands for more information."
  16063. (interactive)
  16064. (call-interactively
  16065. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16066. (defun org-cut-special ()
  16067. "Cut region in table or cut current subtree.
  16068. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16069. See the individual commands for more information."
  16070. (interactive)
  16071. (call-interactively
  16072. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16073. (defun org-paste-special (arg)
  16074. "Paste rectangular region into table, or past subtree relative to level.
  16075. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16076. See the individual commands for more information."
  16077. (interactive "P")
  16078. (if (org-at-table-p)
  16079. (org-table-paste-rectangle)
  16080. (org-paste-subtree arg)))
  16081. (defun org-edit-special (&optional arg)
  16082. "Call a special editor for the stuff at point.
  16083. When at a table, call the formula editor with `org-table-edit-formulas'.
  16084. When at the first line of an src example, call `org-edit-src-code'.
  16085. When in an #+include line, visit the include file. Otherwise call
  16086. `ffap' to visit the file at point."
  16087. (interactive)
  16088. ;; possibly prep session before editing source
  16089. (when arg
  16090. (let* ((info (org-babel-get-src-block-info))
  16091. (lang (nth 0 info))
  16092. (params (nth 2 info))
  16093. (session (cdr (assoc :session params))))
  16094. (when (and info session) ;; we are in a source-code block with a session
  16095. (funcall
  16096. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16097. (cond ;; proceed with `org-edit-special'
  16098. ((save-excursion
  16099. (beginning-of-line 1)
  16100. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16101. (find-file (org-trim (match-string 1))))
  16102. ((org-edit-src-code))
  16103. ((org-edit-fixed-width-region))
  16104. ((org-at-table.el-p)
  16105. (org-edit-src-code))
  16106. ((or (org-at-table-p)
  16107. (save-excursion
  16108. (beginning-of-line 1)
  16109. (looking-at "[ \t]*#\\+TBLFM:")))
  16110. (call-interactively 'org-table-edit-formulas))
  16111. (t (call-interactively 'ffap))))
  16112. (defun org-ctrl-c-ctrl-c (&optional arg)
  16113. "Set tags in headline, or update according to changed information at point.
  16114. This command does many different things, depending on context:
  16115. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16116. this is what we do.
  16117. - If the cursor is on a statistics cookie, update it.
  16118. - If the cursor is in a headline, prompt for tags and insert them
  16119. into the current line, aligned to `org-tags-column'. When called
  16120. with prefix arg, realign all tags in the current buffer.
  16121. - If the cursor is in one of the special #+KEYWORD lines, this
  16122. triggers scanning the buffer for these lines and updating the
  16123. information.
  16124. - If the cursor is inside a table, realign the table. This command
  16125. works even if the automatic table editor has been turned off.
  16126. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16127. the entire table.
  16128. - If the cursor is at a footnote reference or definition, jump to
  16129. the corresponding definition or references, respectively.
  16130. - If the cursor is a the beginning of a dynamic block, update it.
  16131. - If the current buffer is a capture buffer, close note and file it.
  16132. - If the cursor is on a <<<target>>>, update radio targets and
  16133. corresponding links in this buffer.
  16134. - If the cursor is on a numbered item in a plain list, renumber the
  16135. ordered list.
  16136. - If the cursor is on a checkbox, toggle it.
  16137. - If the cursor is on a code block, evaluate it. The variable
  16138. `org-confirm-babel-evaluate' can be used to control prompting
  16139. before code block evaluation, by default every code block
  16140. evaluation requires confirmation. Code block evaluation can be
  16141. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16142. (interactive "P")
  16143. (let ((org-enable-table-editor t))
  16144. (cond
  16145. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16146. org-occur-highlights
  16147. org-latex-fragment-image-overlays)
  16148. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16149. (org-remove-occur-highlights)
  16150. (org-remove-latex-fragment-image-overlays)
  16151. (message "Temporary highlights/overlays removed from current buffer"))
  16152. ((and (local-variable-p 'org-finish-function (current-buffer))
  16153. (fboundp org-finish-function))
  16154. (funcall org-finish-function))
  16155. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16156. ((or (looking-at org-property-start-re)
  16157. (org-at-property-p))
  16158. (call-interactively 'org-property-action))
  16159. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16160. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16161. (or (org-on-heading-p) (org-at-item-p)))
  16162. (call-interactively 'org-update-statistics-cookies))
  16163. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16164. ((org-at-table.el-p)
  16165. (message "Use C-c ' to edit table.el tables"))
  16166. ((org-at-table-p)
  16167. (org-table-maybe-eval-formula)
  16168. (if arg
  16169. (call-interactively 'org-table-recalculate)
  16170. (org-table-maybe-recalculate-line))
  16171. (call-interactively 'org-table-align)
  16172. (orgtbl-send-table 'maybe))
  16173. ((or (org-footnote-at-reference-p)
  16174. (org-footnote-at-definition-p))
  16175. (call-interactively 'org-footnote-action))
  16176. ((org-at-item-checkbox-p)
  16177. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16178. ;; list only if at top item.
  16179. (let* ((cbox (match-string 1))
  16180. (struct (org-list-struct))
  16181. (old-struct (copy-tree struct))
  16182. (parents (org-list-parents-alist struct))
  16183. (prevs (org-list-prevs-alist struct))
  16184. (orderedp (org-entry-get nil "ORDERED"))
  16185. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16186. block-item)
  16187. ;; Use a light version of `org-toggle-checkbox' to avoid
  16188. ;; computing list structure twice.
  16189. (org-list-set-checkbox (point-at-bol) struct
  16190. (cond
  16191. ((equal arg '(16)) "[-]")
  16192. ((equal arg '(4)) nil)
  16193. ((equal "[X]" cbox) "[ ]")
  16194. (t "[X]")))
  16195. (org-list-struct-fix-ind struct parents)
  16196. (org-list-struct-fix-bul struct prevs)
  16197. (setq block-item
  16198. (org-list-struct-fix-box struct parents prevs orderedp))
  16199. (when block-item
  16200. (message
  16201. "Checkboxes were removed due to unchecked box at line %d"
  16202. (org-current-line block-item)))
  16203. (org-list-struct-apply-struct struct old-struct)
  16204. (org-update-checkbox-count-maybe)
  16205. (when firstp (org-list-send-list 'maybe))))
  16206. ((org-at-item-p)
  16207. ;; Cursor at an item: repair list. Do checkbox related actions
  16208. ;; only if function was called with an argument. Send list only
  16209. ;; if at top item.
  16210. (let* ((struct (org-list-struct))
  16211. (old-struct (copy-tree struct))
  16212. (parents (org-list-parents-alist struct))
  16213. (prevs (org-list-prevs-alist struct))
  16214. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16215. (org-list-struct-fix-ind struct parents)
  16216. (org-list-struct-fix-bul struct prevs)
  16217. (when arg
  16218. (org-list-set-checkbox (point-at-bol) struct "[ ]")
  16219. (org-list-struct-fix-box struct parents prevs))
  16220. (org-list-struct-apply-struct struct old-struct)
  16221. (when arg (org-update-checkbox-count-maybe))
  16222. (when firstp (org-list-send-list 'maybe))))
  16223. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16224. ;; Dynamic block
  16225. (beginning-of-line 1)
  16226. (save-excursion (org-update-dblock)))
  16227. ((save-excursion
  16228. (beginning-of-line 1)
  16229. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16230. (cond
  16231. ((equal (match-string 1) "TBLFM")
  16232. ;; Recalculate the table before this line
  16233. (save-excursion
  16234. (beginning-of-line 1)
  16235. (skip-chars-backward " \r\n\t")
  16236. (if (org-at-table-p)
  16237. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16238. (t
  16239. (let ((org-inhibit-startup-visibility-stuff t)
  16240. (org-startup-align-all-tables nil))
  16241. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16242. (message "Local setup has been refreshed"))))
  16243. ((org-clock-update-time-maybe))
  16244. (t (error "C-c C-c can do nothing useful at this location")))))
  16245. (defun org-mode-restart ()
  16246. "Restart Org-mode, to scan again for special lines.
  16247. Also updates the keyword regular expressions."
  16248. (interactive)
  16249. (org-mode)
  16250. (message "Org-mode restarted"))
  16251. (defun org-kill-note-or-show-branches ()
  16252. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16253. (interactive)
  16254. (if (not org-finish-function)
  16255. (progn
  16256. (hide-subtree)
  16257. (call-interactively 'show-branches))
  16258. (let ((org-note-abort t))
  16259. (funcall org-finish-function))))
  16260. (defun org-return (&optional indent)
  16261. "Goto next table row or insert a newline.
  16262. Calls `org-table-next-row' or `newline', depending on context.
  16263. See the individual commands for more information."
  16264. (interactive)
  16265. (cond
  16266. ((bobp) (if indent (newline-and-indent) (newline)))
  16267. ((org-at-table-p)
  16268. (org-table-justify-field-maybe)
  16269. (call-interactively 'org-table-next-row))
  16270. ;; when `newline-and-indent' is called within a list, make sure
  16271. ;; text moved stays inside the item.
  16272. ((and (org-in-item-p) indent)
  16273. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16274. (progn
  16275. (newline)
  16276. (org-indent-line-to (length (match-string 0))))
  16277. (let ((ind (org-get-indentation)))
  16278. (newline)
  16279. (if (org-looking-back org-list-end-re)
  16280. (org-indent-line-function)
  16281. (org-indent-line-to ind)))))
  16282. ((and org-return-follows-link
  16283. (eq (get-text-property (point) 'face) 'org-link))
  16284. (call-interactively 'org-open-at-point))
  16285. ((and (org-at-heading-p)
  16286. (looking-at
  16287. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16288. (org-show-entry)
  16289. (end-of-line 1)
  16290. (newline))
  16291. (t (if indent (newline-and-indent) (newline)))))
  16292. (defun org-return-indent ()
  16293. "Goto next table row or insert a newline and indent.
  16294. Calls `org-table-next-row' or `newline-and-indent', depending on
  16295. context. See the individual commands for more information."
  16296. (interactive)
  16297. (org-return t))
  16298. (defun org-ctrl-c-star ()
  16299. "Compute table, or change heading status of lines.
  16300. Calls `org-table-recalculate' or `org-toggle-heading',
  16301. depending on context."
  16302. (interactive)
  16303. (cond
  16304. ((org-at-table-p)
  16305. (call-interactively 'org-table-recalculate))
  16306. (t
  16307. ;; Convert all lines in region to list items
  16308. (call-interactively 'org-toggle-heading))))
  16309. (defun org-ctrl-c-minus ()
  16310. "Insert separator line in table or modify bullet status of line.
  16311. Also turns a plain line or a region of lines into list items.
  16312. Calls `org-table-insert-hline', `org-toggle-item', or
  16313. `org-cycle-list-bullet', depending on context."
  16314. (interactive)
  16315. (cond
  16316. ((org-at-table-p)
  16317. (call-interactively 'org-table-insert-hline))
  16318. ((org-region-active-p)
  16319. (call-interactively 'org-toggle-item))
  16320. ((org-in-item-p)
  16321. (call-interactively 'org-cycle-list-bullet))
  16322. (t
  16323. (call-interactively 'org-toggle-item))))
  16324. (defun org-toggle-item (arg)
  16325. "Convert headings or normal lines to items, items to normal lines.
  16326. If there is no active region, only the current line is considered.
  16327. If the first non blank line in the region is an headline, convert
  16328. all headlines to items, shifting text accordingly.
  16329. If it is an item, convert all items to normal lines.
  16330. If it is normal text, change region into an item. With a prefix
  16331. argument ARG, change each line in region into an item."
  16332. (interactive "P")
  16333. (let ((shift-text
  16334. (function
  16335. ;; Shift text in current section to IND, from point to END.
  16336. ;; The function leaves point to END line.
  16337. (lambda (ind end)
  16338. (let ((min-i 1000) (end (copy-marker end)))
  16339. ;; First determine the minimum indentation (MIN-I) of
  16340. ;; the text.
  16341. (save-excursion
  16342. (catch 'exit
  16343. (while (< (point) end)
  16344. (let ((i (org-get-indentation)))
  16345. (cond
  16346. ;; Skip blank lines and inline tasks.
  16347. ((looking-at "^[ \t]*$"))
  16348. ((looking-at "^\\*+ "))
  16349. ;; We can't find less than 0 indentation.
  16350. ((zerop i) (throw 'exit (setq min-i 0)))
  16351. ((< i min-i) (setq min-i i))))
  16352. (forward-line))))
  16353. ;; Then indent each line so that a line indented to
  16354. ;; MIN-I becomes indented to IND. Ignore blank lines
  16355. ;; and inline tasks in the process.
  16356. (let ((delta (- ind min-i)))
  16357. (while (< (point) end)
  16358. (unless (or (looking-at "^[ \t]*$")
  16359. (looking-at "^\\*+ "))
  16360. (org-indent-line-to (+ (org-get-indentation) delta)))
  16361. (forward-line)))))))
  16362. (skip-blanks
  16363. (function
  16364. ;; Return beginning of first non-blank line, starting from
  16365. ;; line at POS.
  16366. (lambda (pos)
  16367. (save-excursion
  16368. (goto-char pos)
  16369. (skip-chars-forward " \r\t\n")
  16370. (point-at-bol)))))
  16371. beg end)
  16372. ;; Determine boundaries of changes.
  16373. (if (org-region-active-p)
  16374. (setq beg (funcall skip-blanks (region-beginning))
  16375. end (copy-marker (region-end)))
  16376. (setq beg (funcall skip-blanks (point-at-bol))
  16377. end (copy-marker (point-at-eol))))
  16378. ;; Depending on the starting line, choose an action on the text
  16379. ;; between BEG and END.
  16380. (org-with-limited-levels
  16381. (save-excursion
  16382. (goto-char beg)
  16383. (cond
  16384. ;; Case 1. Start at an item: de-itemize. Note that it only
  16385. ;; happens when a region is active: `org-ctrl-c-minus'
  16386. ;; would call `org-cycle-list-bullet' otherwise.
  16387. ((org-at-item-p)
  16388. (while (< (point) end)
  16389. (when (org-at-item-p)
  16390. (skip-chars-forward " \t")
  16391. (delete-region (point) (match-end 0)))
  16392. (forward-line)))
  16393. ;; Case 2. Start at an heading: convert to items.
  16394. ((org-on-heading-p)
  16395. (let* ((bul (org-list-bullet-string "-"))
  16396. (bul-len (length bul))
  16397. ;; Indentation of the first heading. It should be
  16398. ;; relative to the indentation of its parent, if any.
  16399. (start-ind (save-excursion
  16400. (cond
  16401. ((not org-adapt-indentation) 0)
  16402. ((not (outline-previous-heading)) 0)
  16403. (t (length (match-string 0))))))
  16404. ;; Level of first heading. Further headings will be
  16405. ;; compared to it to determine hierarchy in the list.
  16406. (ref-level (org-reduced-level (org-outline-level))))
  16407. (while (< (point) end)
  16408. (let* ((level (org-reduced-level (org-outline-level)))
  16409. (delta (max 0 (- level ref-level))))
  16410. ;; If current headline is less indented than the first
  16411. ;; one, set it as reference, in order to preserve
  16412. ;; subtrees.
  16413. (when (< level ref-level) (setq ref-level level))
  16414. (replace-match bul t t)
  16415. (org-indent-line-to (+ start-ind (* delta bul-len)))
  16416. ;; Ensure all text down to END (or SECTION-END) belongs
  16417. ;; to the newly created item.
  16418. (let ((section-end (save-excursion
  16419. (or (outline-next-heading) (point)))))
  16420. (forward-line)
  16421. (funcall shift-text
  16422. (+ start-ind (* (1+ delta) bul-len))
  16423. (min end section-end)))))))
  16424. ;; Case 3. Normal line with ARG: turn each non-item line into
  16425. ;; an item.
  16426. (arg
  16427. (while (< (point) end)
  16428. (unless (or (org-on-heading-p) (org-at-item-p))
  16429. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16430. (replace-match
  16431. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  16432. (forward-line)))
  16433. ;; Case 4. Normal line without ARG: make the first line of
  16434. ;; region an item, and shift indentation of others
  16435. ;; lines to set them as item's body.
  16436. (t (let* ((bul (org-list-bullet-string "-"))
  16437. (bul-len (length bul))
  16438. (ref-ind (org-get-indentation)))
  16439. (skip-chars-forward " \t")
  16440. (insert bul)
  16441. (forward-line)
  16442. (while (< (point) end)
  16443. ;; Ensure that lines less indented than first one
  16444. ;; still get included in item body.
  16445. (funcall shift-text
  16446. (+ ref-ind bul-len)
  16447. (min end (save-excursion (or (outline-next-heading)
  16448. (point)))))
  16449. (forward-line)))))))))
  16450. (defun org-toggle-heading (&optional nstars)
  16451. "Convert headings to normal text, or items or text to headings.
  16452. If there is no active region, only the current line is considered.
  16453. If the first non blank line is an headline, remove the stars from
  16454. all headlines in the region.
  16455. If it is a plain list item, turn all plain list items into headings.
  16456. If it is a normal line, turn each and every normal line (i.e. not
  16457. an heading or an item) in the region into a heading.
  16458. When converting a line into a heading, the number of stars is chosen
  16459. such that the lines become children of the current entry. However,
  16460. when a prefix argument is given, its value determines the number of
  16461. stars to add."
  16462. (interactive "P")
  16463. (let ((skip-blanks
  16464. (function
  16465. ;; Return beginning of first non-blank line, starting from
  16466. ;; line at POS.
  16467. (lambda (pos)
  16468. (save-excursion
  16469. (goto-char pos)
  16470. (skip-chars-forward " \r\t\n")
  16471. (point-at-bol)))))
  16472. beg end)
  16473. ;; Determine boundaries of changes. If region ends at a bol, do
  16474. ;; not consider the last line to be in the region.
  16475. (if (org-region-active-p)
  16476. (setq beg (funcall skip-blanks (region-beginning))
  16477. end (copy-marker (save-excursion
  16478. (goto-char (region-end))
  16479. (if (bolp) (point) (point-at-eol)))))
  16480. (setq beg (funcall skip-blanks (point-at-bol))
  16481. end (copy-marker (point-at-eol))))
  16482. ;; Ensure inline tasks don't count as headings.
  16483. (org-with-limited-levels
  16484. (save-excursion
  16485. (goto-char beg)
  16486. (cond
  16487. ;; Case 1. Started at an heading: de-star headings.
  16488. ((org-on-heading-p)
  16489. (while (< (point) end)
  16490. (when (org-on-heading-p t)
  16491. (looking-at outline-regexp) (replace-match ""))
  16492. (forward-line)))
  16493. ;; Case 2. Started at an item: change items into headlines.
  16494. ;; One star will be added by `org-list-to-subtree'.
  16495. ((org-at-item-p)
  16496. (let* ((stars (make-string
  16497. (if nstars
  16498. ;; subtract the star that will be added again by
  16499. ;; `org-list-to-subtree'
  16500. (1- (prefix-numeric-value current-prefix-arg))
  16501. (or (org-current-level) 0))
  16502. ?*))
  16503. (add-stars
  16504. (cond (nstars "") ; stars from prefix only
  16505. ((equal stars "") "") ; before first heading
  16506. (org-odd-levels-only "*") ; inside heading, odd
  16507. (t "")))) ; inside heading, oddeven
  16508. (while (< (point) end)
  16509. (when (org-at-item-p)
  16510. ;; Pay attention to cases when region ends before list.
  16511. (let* ((struct (org-list-struct))
  16512. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  16513. (save-restriction
  16514. (narrow-to-region (point) list-end)
  16515. (insert
  16516. (org-list-to-subtree
  16517. (org-list-parse-list t)
  16518. '(:istart (concat stars add-stars (funcall get-stars depth))
  16519. :icount (concat stars add-stars (funcall get-stars depth))))))))
  16520. (forward-line))))
  16521. ;; Case 3. Started at normal text: make every line an heading,
  16522. ;; skipping headlines and items.
  16523. (t (let* ((stars (make-string
  16524. (if nstars
  16525. (prefix-numeric-value current-prefix-arg)
  16526. (or (org-current-level) 0))
  16527. ?*))
  16528. (add-stars
  16529. (cond (nstars "") ; stars from prefix only
  16530. ((equal stars "") "*") ; before first heading
  16531. (org-odd-levels-only "**") ; inside heading, odd
  16532. (t "*"))) ; inside heading, oddeven
  16533. (rpl (concat stars add-stars " ")))
  16534. (while (< (point) end)
  16535. (when (and (not (org-on-heading-p)) (not (org-at-item-p))
  16536. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  16537. (replace-match (concat rpl (match-string 2))))
  16538. (forward-line)))))))))
  16539. (defun org-meta-return (&optional arg)
  16540. "Insert a new heading or wrap a region in a table.
  16541. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16542. See the individual commands for more information."
  16543. (interactive "P")
  16544. (cond
  16545. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16546. ((org-at-table-p)
  16547. (call-interactively 'org-table-wrap-region))
  16548. (t (call-interactively 'org-insert-heading))))
  16549. ;;; Menu entries
  16550. ;; Define the Org-mode menus
  16551. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16552. '("Tbl"
  16553. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16554. ["Next Field" org-cycle (org-at-table-p)]
  16555. ["Previous Field" org-shifttab (org-at-table-p)]
  16556. ["Next Row" org-return (org-at-table-p)]
  16557. "--"
  16558. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16559. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16560. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16561. "--"
  16562. ("Column"
  16563. ["Move Column Left" org-metaleft (org-at-table-p)]
  16564. ["Move Column Right" org-metaright (org-at-table-p)]
  16565. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16566. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16567. ("Row"
  16568. ["Move Row Up" org-metaup (org-at-table-p)]
  16569. ["Move Row Down" org-metadown (org-at-table-p)]
  16570. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16571. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16572. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16573. "--"
  16574. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16575. ("Rectangle"
  16576. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16577. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16578. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16579. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16580. "--"
  16581. ("Calculate"
  16582. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16583. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16584. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16585. "--"
  16586. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16587. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16588. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16589. "--"
  16590. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16591. "--"
  16592. ["Sum Column/Rectangle" org-table-sum
  16593. (or (org-at-table-p) (org-region-active-p))]
  16594. ["Which Column?" org-table-current-column (org-at-table-p)])
  16595. ["Debug Formulas"
  16596. org-table-toggle-formula-debugger
  16597. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16598. ["Show Col/Row Numbers"
  16599. org-table-toggle-coordinate-overlays
  16600. :style toggle
  16601. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16602. "--"
  16603. ["Create" org-table-create (and (not (org-at-table-p))
  16604. org-enable-table-editor)]
  16605. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16606. ["Import from File" org-table-import (not (org-at-table-p))]
  16607. ["Export to File" org-table-export (org-at-table-p)]
  16608. "--"
  16609. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16610. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16611. '("Org"
  16612. ("Show/Hide"
  16613. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16614. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16615. ["Sparse Tree..." org-sparse-tree t]
  16616. ["Reveal Context" org-reveal t]
  16617. ["Show All" show-all t]
  16618. "--"
  16619. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16620. "--"
  16621. ["New Heading" org-insert-heading t]
  16622. ("Navigate Headings"
  16623. ["Up" outline-up-heading t]
  16624. ["Next" outline-next-visible-heading t]
  16625. ["Previous" outline-previous-visible-heading t]
  16626. ["Next Same Level" outline-forward-same-level t]
  16627. ["Previous Same Level" outline-backward-same-level t]
  16628. "--"
  16629. ["Jump" org-goto t])
  16630. ("Edit Structure"
  16631. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16632. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16633. "--"
  16634. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16635. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16636. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16637. "--"
  16638. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16639. "--"
  16640. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16641. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16642. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16643. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16644. "--"
  16645. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16646. "--"
  16647. ["Convert to odd levels" org-convert-to-odd-levels t]
  16648. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16649. ("Editing"
  16650. ["Emphasis..." org-emphasize t]
  16651. ["Edit Source Example" org-edit-special t]
  16652. "--"
  16653. ["Footnote new/jump" org-footnote-action t]
  16654. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16655. ("Archive"
  16656. ["Archive (default method)" org-archive-subtree-default t]
  16657. "--"
  16658. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16659. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16660. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16661. )
  16662. "--"
  16663. ("Hyperlinks"
  16664. ["Store Link (Global)" org-store-link t]
  16665. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16666. ["Insert Link" org-insert-link t]
  16667. ["Follow Link" org-open-at-point t]
  16668. "--"
  16669. ["Next link" org-next-link t]
  16670. ["Previous link" org-previous-link t]
  16671. "--"
  16672. ["Descriptive Links"
  16673. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16674. :style radio
  16675. :selected (member '(org-link) buffer-invisibility-spec)]
  16676. ["Literal Links"
  16677. (progn
  16678. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16679. :style radio
  16680. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16681. "--"
  16682. ("TODO Lists"
  16683. ["TODO/DONE/-" org-todo t]
  16684. ("Select keyword"
  16685. ["Next keyword" org-shiftright (org-on-heading-p)]
  16686. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16687. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16688. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16689. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16690. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16691. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16692. "--"
  16693. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16694. :selected org-enforce-todo-dependencies :style toggle :active t]
  16695. "Settings for tree at point"
  16696. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16697. :selected (org-entry-get nil "ORDERED")
  16698. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16699. ["Do Children parallel" org-toggle-ordered-property :style radio
  16700. :selected (not (org-entry-get nil "ORDERED"))
  16701. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16702. "--"
  16703. ["Set Priority" org-priority t]
  16704. ["Priority Up" org-shiftup t]
  16705. ["Priority Down" org-shiftdown t]
  16706. "--"
  16707. ["Get news from all feeds" org-feed-update-all t]
  16708. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16709. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16710. ("TAGS and Properties"
  16711. ["Set Tags" org-set-tags-command t]
  16712. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16713. "--"
  16714. ["Set property" org-set-property t]
  16715. ["Column view of properties" org-columns t]
  16716. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16717. ("Dates and Scheduling"
  16718. ["Timestamp" org-time-stamp t]
  16719. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16720. ("Change Date"
  16721. ["1 Day Later" org-shiftright t]
  16722. ["1 Day Earlier" org-shiftleft t]
  16723. ["1 ... Later" org-shiftup t]
  16724. ["1 ... Earlier" org-shiftdown t])
  16725. ["Compute Time Range" org-evaluate-time-range t]
  16726. ["Schedule Item" org-schedule t]
  16727. ["Deadline" org-deadline t]
  16728. "--"
  16729. ["Custom time format" org-toggle-time-stamp-overlays
  16730. :style radio :selected org-display-custom-times]
  16731. "--"
  16732. ["Goto Calendar" org-goto-calendar t]
  16733. ["Date from Calendar" org-date-from-calendar t]
  16734. "--"
  16735. ["Start/Restart Timer" org-timer-start t]
  16736. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16737. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16738. ["Insert Timer String" org-timer t]
  16739. ["Insert Timer Item" org-timer-item t])
  16740. ("Logging work"
  16741. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16742. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16743. ["Clock out" org-clock-out t]
  16744. ["Clock cancel" org-clock-cancel t]
  16745. "--"
  16746. ["Mark as default task" org-clock-mark-default-task t]
  16747. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16748. ["Goto running clock" org-clock-goto t]
  16749. "--"
  16750. ["Display times" org-clock-display t]
  16751. ["Create clock table" org-clock-report t]
  16752. "--"
  16753. ["Record DONE time"
  16754. (progn (setq org-log-done (not org-log-done))
  16755. (message "Switching to %s will %s record a timestamp"
  16756. (car org-done-keywords)
  16757. (if org-log-done "automatically" "not")))
  16758. :style toggle :selected org-log-done])
  16759. "--"
  16760. ["Agenda Command..." org-agenda t]
  16761. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16762. ("File List for Agenda")
  16763. ("Special views current file"
  16764. ["TODO Tree" org-show-todo-tree t]
  16765. ["Check Deadlines" org-check-deadlines t]
  16766. ["Timeline" org-timeline t]
  16767. ["Tags/Property tree" org-match-sparse-tree t])
  16768. "--"
  16769. ["Export/Publish..." org-export t]
  16770. ("LaTeX"
  16771. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16772. :selected org-cdlatex-mode]
  16773. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16774. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16775. ["Modify math symbol" org-cdlatex-math-modify
  16776. (org-inside-LaTeX-fragment-p)]
  16777. ["Insert citation" org-reftex-citation t]
  16778. "--"
  16779. ["Template for BEAMER" org-insert-beamer-options-template t])
  16780. "--"
  16781. ("MobileOrg"
  16782. ["Push Files and Views" org-mobile-push t]
  16783. ["Get Captured and Flagged" org-mobile-pull t]
  16784. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16785. "--"
  16786. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16787. "--"
  16788. ("Documentation"
  16789. ["Show Version" org-version t]
  16790. ["Info Documentation" org-info t])
  16791. ("Customize"
  16792. ["Browse Org Group" org-customize t]
  16793. "--"
  16794. ["Expand This Menu" org-create-customize-menu
  16795. (fboundp 'customize-menu-create)])
  16796. ["Send bug report" org-submit-bug-report t]
  16797. "--"
  16798. ("Refresh/Reload"
  16799. ["Refresh setup current buffer" org-mode-restart t]
  16800. ["Reload Org (after update)" org-reload t]
  16801. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16802. ))
  16803. (defun org-info (&optional node)
  16804. "Read documentation for Org-mode in the info system.
  16805. With optional NODE, go directly to that node."
  16806. (interactive)
  16807. (info (format "(org)%s" (or node ""))))
  16808. ;;;###autoload
  16809. (defun org-submit-bug-report ()
  16810. "Submit a bug report on Org-mode via mail.
  16811. Don't hesitate to report any problems or inaccurate documentation.
  16812. If you don't have setup sending mail from (X)Emacs, please copy the
  16813. output buffer into your mail program, as it gives us important
  16814. information about your Org-mode version and configuration."
  16815. (interactive)
  16816. (require 'reporter)
  16817. (org-load-modules-maybe)
  16818. (org-require-autoloaded-modules)
  16819. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16820. (reporter-submit-bug-report
  16821. "emacs-orgmode@gnu.org"
  16822. (org-version)
  16823. (let (list)
  16824. (save-window-excursion
  16825. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16826. (delete-other-windows)
  16827. (erase-buffer)
  16828. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16829. We would like to add your full Org-mode and Outline configuration to the
  16830. bug report. This greatly simplifies the work of the maintainer and
  16831. other experts on the mailing list.
  16832. HOWEVER, some variables you have customized may contain private
  16833. information. The names of customers, colleagues, or friends, might
  16834. appear in the form of file names, tags, todo states, or search strings.
  16835. If you answer yes to the prompt, you might want to check and remove
  16836. such private information before sending the email.")
  16837. (add-text-properties (point-min) (point-max) '(face org-warning))
  16838. (when (yes-or-no-p "Include your Org-mode configuration ")
  16839. (mapatoms
  16840. (lambda (v)
  16841. (and (boundp v)
  16842. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16843. (or (and (symbol-value v)
  16844. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16845. (and
  16846. (get v 'custom-type) (get v 'standard-value)
  16847. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16848. (push v list)))))
  16849. (kill-buffer (get-buffer "*Warn about privacy*"))
  16850. list))
  16851. nil nil
  16852. "Remember to cover the basics, that is, what you expected to happen and
  16853. what in fact did happen. You don't know how to make a good report? See
  16854. http://orgmode.org/manual/Feedback.html#Feedback
  16855. Your bug report will be posted to the Org-mode mailing list.
  16856. ------------------------------------------------------------------------")
  16857. (save-excursion
  16858. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16859. (replace-match "\\1Bug: \\3 [\\2]")))))
  16860. (defun org-install-agenda-files-menu ()
  16861. (let ((bl (buffer-list)))
  16862. (save-excursion
  16863. (while bl
  16864. (set-buffer (pop bl))
  16865. (if (org-mode-p) (setq bl nil)))
  16866. (when (org-mode-p)
  16867. (easy-menu-change
  16868. '("Org") "File List for Agenda"
  16869. (append
  16870. (list
  16871. ["Edit File List" (org-edit-agenda-file-list) t]
  16872. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16873. ["Remove Current File from List" org-remove-file t]
  16874. ["Cycle through agenda files" org-cycle-agenda-files t]
  16875. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16876. "--")
  16877. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16878. ;;;; Documentation
  16879. ;;;###autoload
  16880. (defun org-require-autoloaded-modules ()
  16881. (interactive)
  16882. (mapc 'require
  16883. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16884. org-docbook org-exp org-html org-icalendar
  16885. org-id org-latex
  16886. org-publish org-remember org-table
  16887. org-timer org-xoxo)))
  16888. ;;;###autoload
  16889. (defun org-reload (&optional uncompiled)
  16890. "Reload all org lisp files.
  16891. With prefix arg UNCOMPILED, load the uncompiled versions."
  16892. (interactive "P")
  16893. (require 'find-func)
  16894. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16895. (dir-org (file-name-directory (org-find-library-name "org")))
  16896. (dir-org-contrib (ignore-errors
  16897. (file-name-directory
  16898. (org-find-library-name "org-contribdir"))))
  16899. (babel-files
  16900. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16901. (append (list nil "comint" "eval" "exp" "keys"
  16902. "lob" "ref" "table" "tangle")
  16903. (delq nil
  16904. (mapcar
  16905. (lambda (lang)
  16906. (when (cdr lang) (symbol-name (car lang))))
  16907. org-babel-load-languages)))))
  16908. (files
  16909. (append (directory-files dir-org t file-re)
  16910. babel-files
  16911. (and dir-org-contrib
  16912. (directory-files dir-org-contrib t file-re))))
  16913. (remove-re (concat (if (featurep 'xemacs)
  16914. "org-colview" "org-colview-xemacs")
  16915. "\\'")))
  16916. (setq files (mapcar 'file-name-sans-extension files))
  16917. (setq files (mapcar
  16918. (lambda (x) (if (string-match remove-re x) nil x))
  16919. files))
  16920. (setq files (delq nil files))
  16921. (mapc
  16922. (lambda (f)
  16923. (when (featurep (intern (file-name-nondirectory f)))
  16924. (if (and (not uncompiled)
  16925. (file-exists-p (concat f ".elc")))
  16926. (load (concat f ".elc") nil nil t)
  16927. (load (concat f ".el") nil nil t))))
  16928. files))
  16929. (org-version))
  16930. ;;;###autoload
  16931. (defun org-customize ()
  16932. "Call the customize function with org as argument."
  16933. (interactive)
  16934. (org-load-modules-maybe)
  16935. (org-require-autoloaded-modules)
  16936. (customize-browse 'org))
  16937. (defun org-create-customize-menu ()
  16938. "Create a full customization menu for Org-mode, insert it into the menu."
  16939. (interactive)
  16940. (org-load-modules-maybe)
  16941. (org-require-autoloaded-modules)
  16942. (if (fboundp 'customize-menu-create)
  16943. (progn
  16944. (easy-menu-change
  16945. '("Org") "Customize"
  16946. `(["Browse Org group" org-customize t]
  16947. "--"
  16948. ,(customize-menu-create 'org)
  16949. ["Set" Custom-set t]
  16950. ["Save" Custom-save t]
  16951. ["Reset to Current" Custom-reset-current t]
  16952. ["Reset to Saved" Custom-reset-saved t]
  16953. ["Reset to Standard Settings" Custom-reset-standard t]))
  16954. (message "\"Org\"-menu now contains full customization menu"))
  16955. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16956. ;;;; Miscellaneous stuff
  16957. ;;; Generally useful functions
  16958. (defun org-get-at-bol (property)
  16959. "Get text property PROPERTY at beginning of line."
  16960. (get-text-property (point-at-bol) property))
  16961. (defun org-find-text-property-in-string (prop s)
  16962. "Return the first non-nil value of property PROP in string S."
  16963. (or (get-text-property 0 prop s)
  16964. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16965. prop s)))
  16966. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16967. "Display the given MESSAGE as a warning."
  16968. (if (fboundp 'display-warning)
  16969. (display-warning 'org message
  16970. (if (featurep 'xemacs) 'warning :warning))
  16971. (let ((buf (get-buffer-create "*Org warnings*")))
  16972. (with-current-buffer buf
  16973. (goto-char (point-max))
  16974. (insert "Warning (Org): " message)
  16975. (unless (bolp)
  16976. (newline)))
  16977. (display-buffer buf)
  16978. (sit-for 0))))
  16979. (defun org-eval (form)
  16980. "Eval FORM and return result."
  16981. (condition-case error
  16982. (eval form)
  16983. (error (format "%%![Error: %s]" error))))
  16984. (defun org-in-commented-line ()
  16985. "Is point in a line starting with `#'?"
  16986. (equal (char-after (point-at-bol)) ?#))
  16987. (defun org-in-indented-comment-line ()
  16988. "Is point in a line starting with `#' after some white space?"
  16989. (save-excursion
  16990. (save-match-data
  16991. (goto-char (point-at-bol))
  16992. (looking-at "[ \t]*#"))))
  16993. (defun org-in-verbatim-emphasis ()
  16994. (save-match-data
  16995. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16996. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16997. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16998. (if (and marker (marker-buffer marker)
  16999. (buffer-live-p (marker-buffer marker)))
  17000. (progn
  17001. (switch-to-buffer (marker-buffer marker))
  17002. (if (or (> marker (point-max)) (< marker (point-min)))
  17003. (widen))
  17004. (goto-char marker)
  17005. (org-show-context 'org-goto))
  17006. (if bookmark
  17007. (bookmark-jump bookmark)
  17008. (error "Cannot find location"))))
  17009. (defun org-quote-csv-field (s)
  17010. "Quote field for inclusion in CSV material."
  17011. (if (string-match "[\",]" s)
  17012. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17013. s))
  17014. (defun org-force-self-insert (N)
  17015. "Needed to enforce self-insert under remapping."
  17016. (interactive "p")
  17017. (self-insert-command N))
  17018. (defun org-string-width (s)
  17019. "Compute width of string, ignoring invisible characters.
  17020. This ignores character with invisibility property `org-link', and also
  17021. characters with property `org-cwidth', because these will become invisible
  17022. upon the next fontification round."
  17023. (let (b l)
  17024. (when (or (eq t buffer-invisibility-spec)
  17025. (assq 'org-link buffer-invisibility-spec))
  17026. (while (setq b (text-property-any 0 (length s)
  17027. 'invisible 'org-link s))
  17028. (setq s (concat (substring s 0 b)
  17029. (substring s (or (next-single-property-change
  17030. b 'invisible s) (length s)))))))
  17031. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17032. (setq s (concat (substring s 0 b)
  17033. (substring s (or (next-single-property-change
  17034. b 'org-cwidth s) (length s))))))
  17035. (setq l (string-width s) b -1)
  17036. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17037. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17038. l))
  17039. (defun org-shorten-string (s maxlength)
  17040. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17041. If the string is shorter or has length MAXLENGTH, just return the
  17042. original string. If it is longer, the functions finds a space in the
  17043. string, breaks this string off at that locations and adds three dots
  17044. as ellipsis. Including the ellipsis, the string will not be longer
  17045. than MAXLENGTH. If finding a good breaking point in the string does
  17046. not work, the string is just chopped off in the middle of a word
  17047. if necessary."
  17048. (if (<= (length s) maxlength)
  17049. s
  17050. (let* ((n (max (- maxlength 4) 1))
  17051. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17052. (if (string-match re s)
  17053. (concat (match-string 1 s) "...")
  17054. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17055. (defun org-get-indentation (&optional line)
  17056. "Get the indentation of the current line, interpreting tabs.
  17057. When LINE is given, assume it represents a line and compute its indentation."
  17058. (if line
  17059. (if (string-match "^ *" (org-remove-tabs line))
  17060. (match-end 0))
  17061. (save-excursion
  17062. (beginning-of-line 1)
  17063. (skip-chars-forward " \t")
  17064. (current-column))))
  17065. (defun org-get-string-indentation (s)
  17066. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17067. (let ((n -1) (i 0) (w tab-width) c)
  17068. (catch 'exit
  17069. (while (< (setq n (1+ n)) (length s))
  17070. (setq c (aref s n))
  17071. (cond ((= c ?\ ) (setq i (1+ i)))
  17072. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17073. (t (throw 'exit t)))))
  17074. i))
  17075. (defun org-remove-tabs (s &optional width)
  17076. "Replace tabulators in S with spaces.
  17077. Assumes that s is a single line, starting in column 0."
  17078. (setq width (or width tab-width))
  17079. (while (string-match "\t" s)
  17080. (setq s (replace-match
  17081. (make-string
  17082. (- (* width (/ (+ (match-beginning 0) width) width))
  17083. (match-beginning 0)) ?\ )
  17084. t t s)))
  17085. s)
  17086. (defun org-fix-indentation (line ind)
  17087. "Fix indentation in LINE.
  17088. IND is a cons cell with target and minimum indentation.
  17089. If the current indentation in LINE is smaller than the minimum,
  17090. leave it alone. If it is larger than ind, set it to the target."
  17091. (let* ((l (org-remove-tabs line))
  17092. (i (org-get-indentation l))
  17093. (i1 (car ind)) (i2 (cdr ind)))
  17094. (if (>= i i2) (setq l (substring line i2)))
  17095. (if (> i1 0)
  17096. (concat (make-string i1 ?\ ) l)
  17097. l)))
  17098. (defun org-remove-indentation (code &optional n)
  17099. "Remove the maximum common indentation from the lines in CODE.
  17100. N may optionally be the number of spaces to remove."
  17101. (with-temp-buffer
  17102. (insert code)
  17103. (org-do-remove-indentation n)
  17104. (buffer-string)))
  17105. (defun org-do-remove-indentation (&optional n)
  17106. "Remove the maximum common indentation from the buffer."
  17107. (untabify (point-min) (point-max))
  17108. (let ((min 10000) re)
  17109. (if n
  17110. (setq min n)
  17111. (goto-char (point-min))
  17112. (while (re-search-forward "^ *[^ \n]" nil t)
  17113. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17114. (unless (or (= min 0) (= min 10000))
  17115. (setq re (format "^ \\{%d\\}" min))
  17116. (goto-char (point-min))
  17117. (while (re-search-forward re nil t)
  17118. (replace-match "")
  17119. (end-of-line 1))
  17120. min)))
  17121. (defun org-fill-template (template alist)
  17122. "Find each %key of ALIST in TEMPLATE and replace it."
  17123. (let ((case-fold-search nil)
  17124. entry key value)
  17125. (setq alist (sort (copy-sequence alist)
  17126. (lambda (a b) (< (length (car a)) (length (car b))))))
  17127. (while (setq entry (pop alist))
  17128. (setq template
  17129. (replace-regexp-in-string
  17130. (concat "%" (regexp-quote (car entry)))
  17131. (cdr entry) template t t)))
  17132. template))
  17133. (defun org-base-buffer (buffer)
  17134. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17135. (if (not buffer)
  17136. buffer
  17137. (or (buffer-base-buffer buffer)
  17138. buffer)))
  17139. (defun org-trim (s)
  17140. "Remove whitespace at beginning and end of string."
  17141. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17142. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17143. s)
  17144. (defun org-wrap (string &optional width lines)
  17145. "Wrap string to either a number of lines, or a width in characters.
  17146. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17147. that costs. If there is a word longer than WIDTH, the text is actually
  17148. wrapped to the length of that word.
  17149. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17150. many lines, whatever width that takes.
  17151. The return value is a list of lines, without newlines at the end."
  17152. (let* ((words (org-split-string string "[ \t\n]+"))
  17153. (maxword (apply 'max (mapcar 'org-string-width words)))
  17154. w ll)
  17155. (cond (width
  17156. (org-do-wrap words (max maxword width)))
  17157. (lines
  17158. (setq w maxword)
  17159. (setq ll (org-do-wrap words maxword))
  17160. (if (<= (length ll) lines)
  17161. ll
  17162. (setq ll words)
  17163. (while (> (length ll) lines)
  17164. (setq w (1+ w))
  17165. (setq ll (org-do-wrap words w)))
  17166. ll))
  17167. (t (error "Cannot wrap this")))))
  17168. (defun org-do-wrap (words width)
  17169. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17170. (let (lines line)
  17171. (while words
  17172. (setq line (pop words))
  17173. (while (and words (< (+ (length line) (length (car words))) width))
  17174. (setq line (concat line " " (pop words))))
  17175. (setq lines (push line lines)))
  17176. (nreverse lines)))
  17177. (defun org-split-string (string &optional separators)
  17178. "Splits STRING into substrings at SEPARATORS.
  17179. No empty strings are returned if there are matches at the beginning
  17180. and end of string."
  17181. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17182. (start 0)
  17183. notfirst
  17184. (list nil))
  17185. (while (and (string-match rexp string
  17186. (if (and notfirst
  17187. (= start (match-beginning 0))
  17188. (< start (length string)))
  17189. (1+ start) start))
  17190. (< (match-beginning 0) (length string)))
  17191. (setq notfirst t)
  17192. (or (eq (match-beginning 0) 0)
  17193. (and (eq (match-beginning 0) (match-end 0))
  17194. (eq (match-beginning 0) start))
  17195. (setq list
  17196. (cons (substring string start (match-beginning 0))
  17197. list)))
  17198. (setq start (match-end 0)))
  17199. (or (eq start (length string))
  17200. (setq list
  17201. (cons (substring string start)
  17202. list)))
  17203. (nreverse list)))
  17204. (defun org-quote-vert (s)
  17205. "Replace \"|\" with \"\\vert\"."
  17206. (while (string-match "|" s)
  17207. (setq s (replace-match "\\vert" t t s)))
  17208. s)
  17209. (defun org-uuidgen-p (s)
  17210. "Is S an ID created by UUIDGEN?"
  17211. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17212. (defun org-context ()
  17213. "Return a list of contexts of the current cursor position.
  17214. If several contexts apply, all are returned.
  17215. Each context entry is a list with a symbol naming the context, and
  17216. two positions indicating start and end of the context. Possible
  17217. contexts are:
  17218. :headline anywhere in a headline
  17219. :headline-stars on the leading stars in a headline
  17220. :todo-keyword on a TODO keyword (including DONE) in a headline
  17221. :tags on the TAGS in a headline
  17222. :priority on the priority cookie in a headline
  17223. :item on the first line of a plain list item
  17224. :item-bullet on the bullet/number of a plain list item
  17225. :checkbox on the checkbox in a plain list item
  17226. :table in an org-mode table
  17227. :table-special on a special filed in a table
  17228. :table-table in a table.el table
  17229. :link on a hyperlink
  17230. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17231. :target on a <<target>>
  17232. :radio-target on a <<<radio-target>>>
  17233. :latex-fragment on a LaTeX fragment
  17234. :latex-preview on a LaTeX fragment with overlayed preview image
  17235. This function expects the position to be visible because it uses font-lock
  17236. faces as a help to recognize the following contexts: :table-special, :link,
  17237. and :keyword."
  17238. (let* ((f (get-text-property (point) 'face))
  17239. (faces (if (listp f) f (list f)))
  17240. (p (point)) clist o)
  17241. ;; First the large context
  17242. (cond
  17243. ((org-on-heading-p t)
  17244. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17245. (when (progn
  17246. (beginning-of-line 1)
  17247. (looking-at org-todo-line-tags-regexp))
  17248. (push (org-point-in-group p 1 :headline-stars) clist)
  17249. (push (org-point-in-group p 2 :todo-keyword) clist)
  17250. (push (org-point-in-group p 4 :tags) clist))
  17251. (goto-char p)
  17252. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17253. (if (looking-at "\\[#[A-Z0-9]\\]")
  17254. (push (org-point-in-group p 0 :priority) clist)))
  17255. ((org-at-item-p)
  17256. (push (org-point-in-group p 2 :item-bullet) clist)
  17257. (push (list :item (point-at-bol)
  17258. (save-excursion (org-end-of-item) (point)))
  17259. clist)
  17260. (and (org-at-item-checkbox-p)
  17261. (push (org-point-in-group p 0 :checkbox) clist)))
  17262. ((org-at-table-p)
  17263. (push (list :table (org-table-begin) (org-table-end)) clist)
  17264. (if (memq 'org-formula faces)
  17265. (push (list :table-special
  17266. (previous-single-property-change p 'face)
  17267. (next-single-property-change p 'face)) clist)))
  17268. ((org-at-table-p 'any)
  17269. (push (list :table-table) clist)))
  17270. (goto-char p)
  17271. ;; Now the small context
  17272. (cond
  17273. ((org-at-timestamp-p)
  17274. (push (org-point-in-group p 0 :timestamp) clist))
  17275. ((memq 'org-link faces)
  17276. (push (list :link
  17277. (previous-single-property-change p 'face)
  17278. (next-single-property-change p 'face)) clist))
  17279. ((memq 'org-special-keyword faces)
  17280. (push (list :keyword
  17281. (previous-single-property-change p 'face)
  17282. (next-single-property-change p 'face)) clist))
  17283. ((org-on-target-p)
  17284. (push (org-point-in-group p 0 :target) clist)
  17285. (goto-char (1- (match-beginning 0)))
  17286. (if (looking-at org-radio-target-regexp)
  17287. (push (org-point-in-group p 0 :radio-target) clist))
  17288. (goto-char p))
  17289. ((setq o (car (delq nil
  17290. (mapcar
  17291. (lambda (x)
  17292. (if (memq x org-latex-fragment-image-overlays) x))
  17293. (overlays-at (point))))))
  17294. (push (list :latex-fragment
  17295. (overlay-start o) (overlay-end o)) clist)
  17296. (push (list :latex-preview
  17297. (overlay-start o) (overlay-end o)) clist))
  17298. ((org-inside-LaTeX-fragment-p)
  17299. ;; FIXME: positions wrong.
  17300. (push (list :latex-fragment (point) (point)) clist)))
  17301. (setq clist (nreverse (delq nil clist)))
  17302. clist))
  17303. ;; FIXME: Compare with at-regexp-p Do we need both?
  17304. (defun org-in-regexp (re &optional nlines visually)
  17305. "Check if point is inside a match of regexp.
  17306. Normally only the current line is checked, but you can include NLINES extra
  17307. lines both before and after point into the search.
  17308. If VISUALLY is set, require that the cursor is not after the match but
  17309. really on, so that the block visually is on the match."
  17310. (catch 'exit
  17311. (let ((pos (point))
  17312. (eol (point-at-eol (+ 1 (or nlines 0))))
  17313. (inc (if visually 1 0)))
  17314. (save-excursion
  17315. (beginning-of-line (- 1 (or nlines 0)))
  17316. (while (re-search-forward re eol t)
  17317. (if (and (<= (match-beginning 0) pos)
  17318. (>= (+ inc (match-end 0)) pos))
  17319. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17320. (defun org-at-regexp-p (regexp)
  17321. "Is point inside a match of REGEXP in the current line?"
  17322. (catch 'exit
  17323. (save-excursion
  17324. (let ((pos (point)) (end (point-at-eol)))
  17325. (beginning-of-line 1)
  17326. (while (re-search-forward regexp end t)
  17327. (if (and (<= (match-beginning 0) pos)
  17328. (>= (match-end 0) pos))
  17329. (throw 'exit t)))
  17330. nil))))
  17331. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  17332. "Return t if the current point is between matches of START-RE and END-RE.
  17333. This will also return t if point is on one of the two matches or
  17334. in an unfinished block. END-RE can be a string or a form
  17335. returning a string.
  17336. An optional third argument bounds the search for START-RE. It
  17337. defaults to previous heading or `point-min'."
  17338. (let ((pos (point))
  17339. (limit (or bound (save-excursion (outline-previous-heading)))))
  17340. (save-excursion
  17341. ;; we're on a block when point is on start-re...
  17342. (or (org-at-regexp-p start-re)
  17343. ;; ... or start-re can be found above...
  17344. (and (re-search-backward start-re limit t)
  17345. ;; ... but no end-re between start-re and point.
  17346. (not (re-search-forward (eval end-re) pos t)))))))
  17347. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17348. "Call `multi-occur' with buffers for all agenda files."
  17349. (interactive "sOrg-files matching: \np")
  17350. (let* ((files (org-agenda-files))
  17351. (tnames (mapcar 'file-truename files))
  17352. (extra org-agenda-text-search-extra-files)
  17353. f)
  17354. (when (eq (car extra) 'agenda-archives)
  17355. (setq extra (cdr extra))
  17356. (setq files (org-add-archive-files files)))
  17357. (while (setq f (pop extra))
  17358. (unless (member (file-truename f) tnames)
  17359. (add-to-list 'files f 'append)
  17360. (add-to-list 'tnames (file-truename f) 'append)))
  17361. (multi-occur
  17362. (mapcar (lambda (x)
  17363. (with-current-buffer
  17364. (or (get-file-buffer x) (find-file-noselect x))
  17365. (widen)
  17366. (current-buffer)))
  17367. files)
  17368. regexp)))
  17369. (if (boundp 'occur-mode-find-occurrence-hook)
  17370. ;; Emacs 23
  17371. (add-hook 'occur-mode-find-occurrence-hook
  17372. (lambda ()
  17373. (when (org-mode-p)
  17374. (org-reveal))))
  17375. ;; Emacs 22
  17376. (defadvice occur-mode-goto-occurrence
  17377. (after org-occur-reveal activate)
  17378. (and (org-mode-p) (org-reveal)))
  17379. (defadvice occur-mode-goto-occurrence-other-window
  17380. (after org-occur-reveal activate)
  17381. (and (org-mode-p) (org-reveal)))
  17382. (defadvice occur-mode-display-occurrence
  17383. (after org-occur-reveal activate)
  17384. (when (org-mode-p)
  17385. (let ((pos (occur-mode-find-occurrence)))
  17386. (with-current-buffer (marker-buffer pos)
  17387. (save-excursion
  17388. (goto-char pos)
  17389. (org-reveal)))))))
  17390. (defun org-occur-link-in-agenda-files ()
  17391. "Create a link and search for it in the agendas.
  17392. The link is not stored in `org-stored-links', it is just created
  17393. for the search purpose."
  17394. (interactive)
  17395. (let ((link (condition-case nil
  17396. (org-store-link nil)
  17397. (error "Unable to create a link to here"))))
  17398. (org-occur-in-agenda-files (regexp-quote link))))
  17399. (defun org-uniquify (list)
  17400. "Remove duplicate elements from LIST."
  17401. (let (res)
  17402. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  17403. res))
  17404. (defun org-delete-all (elts list)
  17405. "Remove all elements in ELTS from LIST."
  17406. (while elts
  17407. (setq list (delete (pop elts) list)))
  17408. list)
  17409. (defun org-count (cl-item cl-seq)
  17410. "Count the number of occurrences of ITEM in SEQ.
  17411. Taken from `count' in cl-seq.el with all keyword arguments removed."
  17412. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  17413. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  17414. (while (< cl-start cl-end)
  17415. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  17416. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  17417. (setq cl-start (1+ cl-start)))
  17418. cl-count))
  17419. (defun org-remove-if (predicate seq)
  17420. "Remove everything from SEQ that fulfills PREDICATE."
  17421. (let (res e)
  17422. (while seq
  17423. (setq e (pop seq))
  17424. (if (not (funcall predicate e)) (push e res)))
  17425. (nreverse res)))
  17426. (defun org-remove-if-not (predicate seq)
  17427. "Remove everything from SEQ that does not fulfill PREDICATE."
  17428. (let (res e)
  17429. (while seq
  17430. (setq e (pop seq))
  17431. (if (funcall predicate e) (push e res)))
  17432. (nreverse res)))
  17433. (defun org-back-over-empty-lines ()
  17434. "Move backwards over whitespace, to the beginning of the first empty line.
  17435. Returns the number of empty lines passed."
  17436. (let ((pos (point)))
  17437. (if (cdr (assoc 'heading org-blank-before-new-entry))
  17438. (skip-chars-backward " \t\n\r")
  17439. (forward-line -1))
  17440. (beginning-of-line 2)
  17441. (goto-char (min (point) pos))
  17442. (count-lines (point) pos)))
  17443. (defun org-skip-whitespace ()
  17444. (skip-chars-forward " \t\n\r"))
  17445. (defun org-point-in-group (point group &optional context)
  17446. "Check if POINT is in match-group GROUP.
  17447. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  17448. match. If the match group does not exist or point is not inside it,
  17449. return nil."
  17450. (and (match-beginning group)
  17451. (>= point (match-beginning group))
  17452. (<= point (match-end group))
  17453. (if context
  17454. (list context (match-beginning group) (match-end group))
  17455. t)))
  17456. (defun org-switch-to-buffer-other-window (&rest args)
  17457. "Switch to buffer in a second window on the current frame.
  17458. In particular, do not allow pop-up frames.
  17459. Returns the newly created buffer."
  17460. (let (pop-up-frames special-display-buffer-names special-display-regexps
  17461. special-display-function)
  17462. (apply 'switch-to-buffer-other-window args)))
  17463. (defun org-combine-plists (&rest plists)
  17464. "Create a single property list from all plists in PLISTS.
  17465. The process starts by copying the first list, and then setting properties
  17466. from the other lists. Settings in the last list are the most significant
  17467. ones and overrule settings in the other lists."
  17468. (let ((rtn (copy-sequence (pop plists)))
  17469. p v ls)
  17470. (while plists
  17471. (setq ls (pop plists))
  17472. (while ls
  17473. (setq p (pop ls) v (pop ls))
  17474. (setq rtn (plist-put rtn p v))))
  17475. rtn))
  17476. (defun org-move-line-down (arg)
  17477. "Move the current line down. With prefix argument, move it past ARG lines."
  17478. (interactive "p")
  17479. (let ((col (current-column))
  17480. beg end pos)
  17481. (beginning-of-line 1) (setq beg (point))
  17482. (beginning-of-line 2) (setq end (point))
  17483. (beginning-of-line (+ 1 arg))
  17484. (setq pos (move-marker (make-marker) (point)))
  17485. (insert (delete-and-extract-region beg end))
  17486. (goto-char pos)
  17487. (org-move-to-column col)))
  17488. (defun org-move-line-up (arg)
  17489. "Move the current line up. With prefix argument, move it past ARG lines."
  17490. (interactive "p")
  17491. (let ((col (current-column))
  17492. beg end pos)
  17493. (beginning-of-line 1) (setq beg (point))
  17494. (beginning-of-line 2) (setq end (point))
  17495. (beginning-of-line (- arg))
  17496. (setq pos (move-marker (make-marker) (point)))
  17497. (insert (delete-and-extract-region beg end))
  17498. (goto-char pos)
  17499. (org-move-to-column col)))
  17500. (defun org-replace-escapes (string table)
  17501. "Replace %-escapes in STRING with values in TABLE.
  17502. TABLE is an association list with keys like \"%a\" and string values.
  17503. The sequences in STRING may contain normal field width and padding information,
  17504. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  17505. so values can contain further %-escapes if they are define later in TABLE."
  17506. (let ((tbl (copy-alist table))
  17507. (case-fold-search nil)
  17508. (pchg 0)
  17509. e re rpl)
  17510. (while (setq e (pop tbl))
  17511. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  17512. (when (and (cdr e) (string-match re (cdr e)))
  17513. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  17514. (safe "SREF"))
  17515. (add-text-properties 0 3 (list 'sref sref) safe)
  17516. (setcdr e (replace-match safe t t (cdr e)))))
  17517. (while (string-match re string)
  17518. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  17519. (cdr e)))
  17520. (setq string (replace-match rpl t t string))))
  17521. (while (setq pchg (next-property-change pchg string))
  17522. (let ((sref (get-text-property pchg 'sref string)))
  17523. (when (and sref (string-match "SREF" string pchg))
  17524. (setq string (replace-match sref t t string)))))
  17525. string))
  17526. (defun org-sublist (list start end)
  17527. "Return a section of LIST, from START to END.
  17528. Counting starts at 1."
  17529. (let (rtn (c start))
  17530. (setq list (nthcdr (1- start) list))
  17531. (while (and list (<= c end))
  17532. (push (pop list) rtn)
  17533. (setq c (1+ c)))
  17534. (nreverse rtn)))
  17535. (defun org-find-base-buffer-visiting (file)
  17536. "Like `find-buffer-visiting' but always return the base buffer and
  17537. not an indirect buffer."
  17538. (let ((buf (or (get-file-buffer file)
  17539. (find-buffer-visiting file))))
  17540. (if buf
  17541. (or (buffer-base-buffer buf) buf)
  17542. nil)))
  17543. (defun org-image-file-name-regexp (&optional extensions)
  17544. "Return regexp matching the file names of images.
  17545. If EXTENSIONS is given, only match these."
  17546. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  17547. (image-file-name-regexp)
  17548. (let ((image-file-name-extensions
  17549. (or extensions
  17550. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17551. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  17552. (concat "\\."
  17553. (regexp-opt (nconc (mapcar 'upcase
  17554. image-file-name-extensions)
  17555. image-file-name-extensions)
  17556. t)
  17557. "\\'"))))
  17558. (defun org-file-image-p (file &optional extensions)
  17559. "Return non-nil if FILE is an image."
  17560. (save-match-data
  17561. (string-match (org-image-file-name-regexp extensions) file)))
  17562. (defun org-get-cursor-date ()
  17563. "Return the date at cursor in as a time.
  17564. This works in the calendar and in the agenda, anywhere else it just
  17565. returns the current time."
  17566. (let (date day defd)
  17567. (cond
  17568. ((eq major-mode 'calendar-mode)
  17569. (setq date (calendar-cursor-to-date)
  17570. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17571. ((eq major-mode 'org-agenda-mode)
  17572. (setq day (get-text-property (point) 'day))
  17573. (if day
  17574. (setq date (calendar-gregorian-from-absolute day)
  17575. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17576. (nth 2 date))))))
  17577. (or defd (current-time))))
  17578. (defvar org-agenda-action-marker (make-marker)
  17579. "Marker pointing to the entry for the next agenda action.")
  17580. (defun org-mark-entry-for-agenda-action ()
  17581. "Mark the current entry as target of an agenda action.
  17582. Agenda actions are actions executed from the agenda with the key `k',
  17583. which make use of the date at the cursor."
  17584. (interactive)
  17585. (move-marker org-agenda-action-marker
  17586. (save-excursion (org-back-to-heading t) (point))
  17587. (current-buffer))
  17588. (message
  17589. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17590. (defun org-mark-subtree ()
  17591. "Mark the current subtree.
  17592. This puts point at the start of the current subtree, and mark at the end.
  17593. If point is in an inline task, mark that task instead."
  17594. (interactive)
  17595. (let ((inline-task-p
  17596. (and (featurep 'org-inlinetask)
  17597. (org-inlinetask-in-task-p)))
  17598. (beg))
  17599. ;; Get beginning of subtree
  17600. (cond
  17601. (inline-task-p (org-inlinetask-goto-beginning))
  17602. ((org-at-heading-p) (beginning-of-line))
  17603. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  17604. (setq beg (point))
  17605. ;; Get end of it
  17606. (if inline-task-p
  17607. (org-inlinetask-goto-end)
  17608. (org-end-of-subtree))
  17609. ;; Mark zone
  17610. (push-mark (point) nil t)
  17611. (goto-char beg)))
  17612. ;;; Paragraph filling stuff.
  17613. ;; We want this to be just right, so use the full arsenal.
  17614. (defun org-indent-line-function ()
  17615. "Indent line depending on context."
  17616. (interactive)
  17617. (let* ((pos (point))
  17618. (itemp (org-at-item-p))
  17619. (case-fold-search t)
  17620. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17621. (inline-task-p (and (featurep 'org-inlinetask)
  17622. (org-inlinetask-in-task-p)))
  17623. (inline-re (and inline-task-p
  17624. (org-inlinetask-outline-regexp)))
  17625. column)
  17626. (beginning-of-line 1)
  17627. (cond
  17628. ;; Comments
  17629. ((looking-at "# ") (setq column 0))
  17630. ;; Headings
  17631. ((looking-at "\\*+ ") (setq column 0))
  17632. ;; Included files
  17633. ((looking-at "#\\+include:") (setq column 0))
  17634. ;; Footnote definition
  17635. ((looking-at org-footnote-definition-re) (setq column 0))
  17636. ;; Literal examples
  17637. ((looking-at "[ \t]*:[ \t]")
  17638. (setq column (org-get-indentation))) ; do nothing
  17639. ;; Lists
  17640. ((ignore-errors (goto-char (org-in-item-p)))
  17641. (setq column (if itemp
  17642. (org-get-indentation)
  17643. (org-list-item-body-column (point))))
  17644. (goto-char pos))
  17645. ;; Drawers
  17646. ((and (looking-at "[ \t]*:END:")
  17647. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17648. (save-excursion
  17649. (goto-char (1- (match-beginning 1)))
  17650. (setq column (current-column))))
  17651. ;; Special blocks
  17652. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17653. (save-excursion
  17654. (re-search-backward
  17655. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17656. (setq column (org-get-indentation (match-string 0))))
  17657. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17658. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17659. (save-excursion
  17660. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17661. (setq column
  17662. (if (equal (downcase (match-string 1)) "src")
  17663. ;; src blocks: let `org-edit-src-exit' handle them
  17664. (org-get-indentation)
  17665. (org-get-indentation (match-string 0)))))
  17666. ;; This line has nothing special, look at the previous relevant
  17667. ;; line to compute indentation
  17668. (t
  17669. (beginning-of-line 0)
  17670. (while (and (not (bobp))
  17671. (not (looking-at org-drawer-regexp))
  17672. ;; When point started in an inline task, do not move
  17673. ;; above task starting line.
  17674. (not (and inline-task-p (looking-at inline-re)))
  17675. ;; Skip drawers, blocks, empty lines, verbatim,
  17676. ;; comments, tables, footnotes definitions, lists,
  17677. ;; inline tasks.
  17678. (or (and (looking-at "[ \t]*:END:")
  17679. (re-search-backward org-drawer-regexp nil t))
  17680. (and (looking-at "[ \t]*#\\+end_")
  17681. (re-search-backward "[ \t]*#\\+begin_"nil t))
  17682. (looking-at "[ \t]*[\n:#|]")
  17683. (looking-at org-footnote-definition-re)
  17684. (and (ignore-errors (goto-char (org-in-item-p)))
  17685. (goto-char
  17686. (org-list-get-top-point (org-list-struct))))
  17687. (and (not inline-task-p)
  17688. (featurep 'org-inlinetask)
  17689. (org-inlinetask-in-task-p)
  17690. (or (org-inlinetask-goto-beginning) t))))
  17691. (beginning-of-line 0))
  17692. (cond
  17693. ;; There was an heading above.
  17694. ((looking-at "\\*+[ \t]+")
  17695. (if (not org-adapt-indentation)
  17696. (setq column 0)
  17697. (goto-char (match-end 0))
  17698. (setq column (current-column))))
  17699. ;; A drawer had started and is unfinished
  17700. ((looking-at org-drawer-regexp)
  17701. (goto-char (1- (match-beginning 1)))
  17702. (setq column (current-column)))
  17703. ;; Else, nothing noticeable found: get indentation and go on.
  17704. (t (setq column (org-get-indentation))))))
  17705. ;; Now apply indentation and move cursor accordingly
  17706. (goto-char pos)
  17707. (if (<= (current-column) (current-indentation))
  17708. (org-indent-line-to column)
  17709. (save-excursion (org-indent-line-to column)))
  17710. ;; Special polishing for properties, see `org-property-format'
  17711. (setq column (current-column))
  17712. (beginning-of-line 1)
  17713. (if (looking-at
  17714. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17715. (replace-match (concat (match-string 1)
  17716. (format org-property-format
  17717. (match-string 2) (match-string 3)))
  17718. t t))
  17719. (org-move-to-column column)))
  17720. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17721. "Variable to store copy of `adaptive-fill-regexp'.
  17722. Since `adaptive-fill-regexp' is set to never match, we need to
  17723. store a backup of its value before entering `org-mode' so that
  17724. the functionality can be provided as a fall-back.")
  17725. (defun org-set-autofill-regexps ()
  17726. (interactive)
  17727. ;; In the paragraph separator we include headlines, because filling
  17728. ;; text in a line directly attached to a headline would otherwise
  17729. ;; fill the headline as well.
  17730. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17731. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17732. ;; The paragraph starter includes hand-formatted lists.
  17733. (org-set-local
  17734. 'paragraph-start
  17735. (concat
  17736. "\f" "\\|"
  17737. "[ ]*$" "\\|"
  17738. "\\*+ " "\\|"
  17739. "[ \t]*#" "\\|"
  17740. (org-item-re) "\\|"
  17741. "[ \t]*[:|]" "\\|"
  17742. "\\$\\$" "\\|"
  17743. "\\\\\\(begin\\|end\\|[][]\\)"))
  17744. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17745. ;; But only if the user has not turned off tables or fixed-width regions
  17746. (org-set-local
  17747. 'auto-fill-inhibit-regexp
  17748. (concat "\\*+ \\|#\\+"
  17749. "\\|[ \t]*" org-keyword-time-regexp
  17750. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17751. (concat
  17752. "\\|[ \t]*["
  17753. (if org-enable-table-editor "|" "")
  17754. (if org-enable-fixed-width-editor ":" "")
  17755. "]"))))
  17756. ;; We use our own fill-paragraph function, to make sure that tables
  17757. ;; and fixed-width regions are not wrapped. That function will pass
  17758. ;; through to `fill-paragraph' when appropriate.
  17759. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17760. ;; Prevent auto-fill from inserting unwanted new items.
  17761. (org-set-local 'fill-nobreak-predicate
  17762. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  17763. fill-nobreak-predicate
  17764. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate)))
  17765. ;; Adaptive filling: To get full control, first make sure that
  17766. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17767. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17768. (org-set-local 'org-adaptive-fill-regexp-backup
  17769. adaptive-fill-regexp))
  17770. (org-set-local 'adaptive-fill-regexp "\000")
  17771. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  17772. (org-set-local 'adaptive-fill-function
  17773. 'org-adaptive-fill-function)
  17774. (org-set-local
  17775. 'align-mode-rules-list
  17776. '((org-in-buffer-settings
  17777. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17778. (modes . '(org-mode))))))
  17779. (defun org-fill-item-nobreak-p ()
  17780. "Non-nil when a line break at point would insert a new item."
  17781. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  17782. (defun org-fill-paragraph (&optional justify)
  17783. "Re-align a table, pass through to fill-paragraph if no table."
  17784. (let ((table-p (org-at-table-p))
  17785. (table.el-p (org-at-table.el-p))
  17786. (itemp (org-in-item-p)))
  17787. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17788. (save-excursion (goto-char (point-at-bol))
  17789. (looking-at outline-regexp)))
  17790. t) ; skip headlines
  17791. (table.el-p t) ; skip table.el tables
  17792. (table-p (org-table-align) t) ; align Org tables
  17793. (itemp ; align text in items
  17794. (let* ((struct (save-excursion (goto-char itemp)
  17795. (org-list-struct)))
  17796. (parents (org-list-parents-alist struct))
  17797. (children (org-list-get-children itemp struct parents))
  17798. beg end prev next prefix)
  17799. ;; Determine in which part of item point is: before
  17800. ;; first child, after last child, between two
  17801. ;; sub-lists, or simply in item if there's no child.
  17802. (cond
  17803. ((not children)
  17804. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17805. beg itemp
  17806. end (org-list-get-item-end itemp struct)))
  17807. ((< (point) (setq next (car children)))
  17808. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17809. beg itemp
  17810. end next))
  17811. ((> (point) (setq prev (car (last children))))
  17812. (setq beg (org-list-get-item-end prev struct)
  17813. end (org-list-get-item-end itemp struct)
  17814. prefix (save-excursion
  17815. (goto-char beg)
  17816. (skip-chars-forward " \t")
  17817. (make-string (current-column) ?\ ))))
  17818. (t (catch 'exit
  17819. (while (setq next (pop children))
  17820. (if (> (point) next)
  17821. (setq prev next)
  17822. (setq beg (org-list-get-item-end prev struct)
  17823. end next
  17824. prefix (save-excursion
  17825. (goto-char beg)
  17826. (skip-chars-forward " \t")
  17827. (make-string (current-column) ?\ )))
  17828. (throw 'exit nil))))))
  17829. ;; Use `fill-paragraph' with buffer narrowed to item
  17830. ;; without any child, and with our computed PREFIX.
  17831. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17832. (save-restriction
  17833. (narrow-to-region beg end)
  17834. (save-excursion (fill-paragraph justify)))) t))
  17835. ;; Special case where point is not in a list but is on
  17836. ;; a paragraph adjacent to a list: make sure this paragraph
  17837. ;; doesn't get merged with the end of the list by narrowing
  17838. ;; buffer first.
  17839. ((save-excursion (forward-paragraph -1)
  17840. (setq itemp (org-in-item-p)))
  17841. (let ((struct (save-excursion (goto-char itemp)
  17842. (org-list-struct))))
  17843. (save-restriction
  17844. (narrow-to-region (org-list-get-bottom-point struct)
  17845. (save-excursion (forward-paragraph 1)
  17846. (point)))
  17847. (fill-paragraph justify) t)))
  17848. ;; Else simply call `fill-paragraph'.
  17849. (t nil))))
  17850. ;; For reference, this is the default value of adaptive-fill-regexp
  17851. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17852. (defun org-adaptive-fill-function ()
  17853. "Return a fill prefix for org-mode files."
  17854. (let (itemp)
  17855. (save-excursion
  17856. (cond
  17857. ;; Comment line
  17858. ((looking-at "#[ \t]+")
  17859. (match-string-no-properties 0))
  17860. ;; Plain list item
  17861. ((org-at-item-p)
  17862. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  17863. ;; Point is in a list after `backward-paragraph': original
  17864. ;; point wasn't in the list, or filling would have been taken
  17865. ;; care of by `org-auto-fill-function', but the list and the
  17866. ;; real paragraph are not separated by a blank line. Thus, move
  17867. ;; point after the list to go back to real paragraph and
  17868. ;; determine fill-prefix.
  17869. ((setq itemp (org-in-item-p))
  17870. (goto-char itemp)
  17871. (let* ((struct (org-list-struct))
  17872. (bottom (org-list-get-bottom-point struct)))
  17873. (goto-char bottom)
  17874. (make-string (org-get-indentation) ?\ )))
  17875. ;; Other text
  17876. ((looking-at org-adaptive-fill-regexp-backup)
  17877. (match-string-no-properties 0))))))
  17878. (defun org-auto-fill-function ()
  17879. "Auto-fill function."
  17880. (let (itemp prefix)
  17881. ;; When in a list, compute an appropriate fill-prefix and make
  17882. ;; sure it will be used by `do-auto-fill'.
  17883. (if (setq itemp (org-in-item-p))
  17884. (progn
  17885. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  17886. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17887. (do-auto-fill)))
  17888. ;; Else just use `do-auto-fill'.
  17889. (do-auto-fill))))
  17890. ;;; Other stuff.
  17891. (defun org-toggle-fixed-width-section (arg)
  17892. "Toggle the fixed-width export.
  17893. If there is no active region, the QUOTE keyword at the current headline is
  17894. inserted or removed. When present, it causes the text between this headline
  17895. and the next to be exported as fixed-width text, and unmodified.
  17896. If there is an active region, this command adds or removes a colon as the
  17897. first character of this line. If the first character of a line is a colon,
  17898. this line is also exported in fixed-width font."
  17899. (interactive "P")
  17900. (let* ((cc 0)
  17901. (regionp (org-region-active-p))
  17902. (beg (if regionp (region-beginning) (point)))
  17903. (end (if regionp (region-end)))
  17904. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17905. (case-fold-search nil)
  17906. (re "[ \t]*\\(: \\)")
  17907. off)
  17908. (if regionp
  17909. (save-excursion
  17910. (goto-char beg)
  17911. (setq cc (current-column))
  17912. (beginning-of-line 1)
  17913. (setq off (looking-at re))
  17914. (while (> nlines 0)
  17915. (setq nlines (1- nlines))
  17916. (beginning-of-line 1)
  17917. (cond
  17918. (arg
  17919. (org-move-to-column cc t)
  17920. (insert ": \n")
  17921. (forward-line -1))
  17922. ((and off (looking-at re))
  17923. (replace-match "" t t nil 1))
  17924. ((not off) (org-move-to-column cc t) (insert ": ")))
  17925. (forward-line 1)))
  17926. (save-excursion
  17927. (org-back-to-heading)
  17928. (if (looking-at (concat outline-regexp
  17929. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17930. (replace-match "" t t nil 1)
  17931. (if (looking-at outline-regexp)
  17932. (progn
  17933. (goto-char (match-end 0))
  17934. (insert org-quote-string " "))))))))
  17935. (defun org-reftex-citation ()
  17936. "Use reftex-citation to insert a citation into the buffer.
  17937. This looks for a line like
  17938. #+BIBLIOGRAPHY: foo plain option:-d
  17939. and derives from it that foo.bib is the bibliography file relevant
  17940. for this document. It then installs the necessary environment for RefTeX
  17941. to work in this buffer and calls `reftex-citation' to insert a citation
  17942. into the buffer.
  17943. Export of such citations to both LaTeX and HTML is handled by the contributed
  17944. package org-exp-bibtex by Taru Karttunen."
  17945. (interactive)
  17946. (let ((reftex-docstruct-symbol 'rds)
  17947. (reftex-cite-format "\\cite{%l}")
  17948. rds bib)
  17949. (save-excursion
  17950. (save-restriction
  17951. (widen)
  17952. (let ((case-fold-search t)
  17953. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17954. (if (not (save-excursion
  17955. (or (re-search-forward re nil t)
  17956. (re-search-backward re nil t))))
  17957. (error "No bibliography defined in file")
  17958. (setq bib (concat (match-string 1) ".bib")
  17959. rds (list (list 'bib bib)))))))
  17960. (call-interactively 'reftex-citation)))
  17961. ;;;; Functions extending outline functionality
  17962. (defun org-beginning-of-line (&optional arg)
  17963. "Go to the beginning of the current line. If that is invisible, continue
  17964. to a visible line beginning. This makes the function of C-a more intuitive.
  17965. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17966. first attempt, and only move to after the tags when the cursor is already
  17967. beyond the end of the headline."
  17968. (interactive "P")
  17969. (let ((pos (point))
  17970. (special (if (consp org-special-ctrl-a/e)
  17971. (car org-special-ctrl-a/e)
  17972. org-special-ctrl-a/e))
  17973. refpos)
  17974. (if (org-bound-and-true-p line-move-visual)
  17975. (beginning-of-visual-line 1)
  17976. (beginning-of-line 1))
  17977. (if (and arg (fboundp 'move-beginning-of-line))
  17978. (call-interactively 'move-beginning-of-line)
  17979. (if (bobp)
  17980. nil
  17981. (backward-char 1)
  17982. (if (org-truely-invisible-p)
  17983. (while (and (not (bobp)) (org-truely-invisible-p))
  17984. (backward-char 1)
  17985. (beginning-of-line 1))
  17986. (forward-char 1))))
  17987. (when special
  17988. (cond
  17989. ((and (looking-at org-complex-heading-regexp)
  17990. (= (char-after (match-end 1)) ?\ ))
  17991. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17992. (point-at-eol)))
  17993. (goto-char
  17994. (if (eq special t)
  17995. (cond ((> pos refpos) refpos)
  17996. ((= pos (point)) refpos)
  17997. (t (point)))
  17998. (cond ((> pos (point)) (point))
  17999. ((not (eq last-command this-command)) (point))
  18000. (t refpos)))))
  18001. ((org-at-item-p)
  18002. (goto-char
  18003. (if (eq special t)
  18004. (cond ((> pos (match-end 0)) (match-end 0))
  18005. ((= pos (point)) (match-end 0))
  18006. (t (point)))
  18007. (cond ((> pos (point)) (point))
  18008. ((not (eq last-command this-command)) (point))
  18009. (t (match-end 0))))))))
  18010. (org-no-warnings
  18011. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18012. (defun org-end-of-line (&optional arg)
  18013. "Go to the end of the line.
  18014. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18015. first attempt, and only move to after the tags when the cursor is already
  18016. beyond the end of the headline."
  18017. (interactive "P")
  18018. (let ((special (if (consp org-special-ctrl-a/e)
  18019. (cdr org-special-ctrl-a/e)
  18020. org-special-ctrl-a/e)))
  18021. (if (or (not special)
  18022. (not (org-on-heading-p))
  18023. arg)
  18024. (call-interactively
  18025. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18026. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18027. (t 'end-of-line)))
  18028. (let ((pos (point)))
  18029. (beginning-of-line 1)
  18030. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18031. (if (eq special t)
  18032. (if (or (< pos (match-beginning 1))
  18033. (= pos (match-end 0)))
  18034. (goto-char (match-beginning 1))
  18035. (goto-char (match-end 0)))
  18036. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18037. (goto-char (match-end 0))
  18038. (goto-char (match-beginning 1))))
  18039. (call-interactively (if (fboundp 'move-end-of-line)
  18040. 'move-end-of-line
  18041. 'end-of-line)))))
  18042. (org-no-warnings
  18043. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18044. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18045. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18046. (defun org-backward-sentence (&optional arg)
  18047. "Go to beginning of sentence, or beginning of table field.
  18048. This will call `backward-sentence' or `org-table-beginning-of-field',
  18049. depending on context."
  18050. (interactive "P")
  18051. (cond
  18052. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18053. (t (call-interactively 'backward-sentence))))
  18054. (defun org-forward-sentence (&optional arg)
  18055. "Go to end of sentence, or end of table field.
  18056. This will call `forward-sentence' or `org-table-end-of-field',
  18057. depending on context."
  18058. (interactive "P")
  18059. (cond
  18060. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18061. (t (call-interactively 'forward-sentence))))
  18062. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18063. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18064. (defun org-kill-line (&optional arg)
  18065. "Kill line, to tags or end of line."
  18066. (interactive "P")
  18067. (cond
  18068. ((or (not org-special-ctrl-k)
  18069. (bolp)
  18070. (not (org-on-heading-p)))
  18071. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18072. org-ctrl-k-protect-subtree)
  18073. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18074. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18075. (error "C-k aborted - would kill hidden subtree")))
  18076. (call-interactively 'kill-line))
  18077. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18078. (kill-region (point) (match-beginning 1))
  18079. (org-set-tags nil t))
  18080. (t (kill-region (point) (point-at-eol)))))
  18081. (define-key org-mode-map "\C-k" 'org-kill-line)
  18082. (defun org-yank (&optional arg)
  18083. "Yank. If the kill is a subtree, treat it specially.
  18084. This command will look at the current kill and check if is a single
  18085. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18086. cursor is at the beginning of a line or after the stars of a currently
  18087. empty headline, then the yank is handled specially. How exactly depends
  18088. on the value of the following variables, both set by default.
  18089. org-yank-folded-subtrees
  18090. When set, the subtree(s) will be folded after insertion, but only
  18091. if doing so would now swallow text after the yanked text.
  18092. org-yank-adjusted-subtrees
  18093. When set, the subtree will be promoted or demoted in order to
  18094. fit into the local outline tree structure, which means that the level
  18095. will be adjusted so that it becomes the smaller one of the two
  18096. *visible* surrounding headings.
  18097. Any prefix to this command will cause `yank' to be called directly with
  18098. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18099. will just
  18100. plainly yank the text as it is.
  18101. \[1] The test checks if the first non-white line is a heading
  18102. and if there are no other headings with fewer stars."
  18103. (interactive "P")
  18104. (org-yank-generic 'yank arg))
  18105. (defun org-yank-generic (command arg)
  18106. "Perform some yank-like command.
  18107. This function implements the behavior described in the `org-yank'
  18108. documentation. However, it has been generalized to work for any
  18109. interactive command with similar behavior."
  18110. ;; pretend to be command COMMAND
  18111. (setq this-command command)
  18112. (if arg
  18113. (call-interactively command)
  18114. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18115. (and (org-kill-is-subtree-p)
  18116. (or (bolp)
  18117. (and (looking-at "[ \t]*$")
  18118. (string-match
  18119. "\\`\\*+\\'"
  18120. (buffer-substring (point-at-bol) (point)))))))
  18121. swallowp)
  18122. (cond
  18123. ((and subtreep org-yank-folded-subtrees)
  18124. (let ((beg (point))
  18125. end)
  18126. (if (and subtreep org-yank-adjusted-subtrees)
  18127. (org-paste-subtree nil nil 'for-yank)
  18128. (call-interactively command))
  18129. (setq end (point))
  18130. (goto-char beg)
  18131. (when (and (bolp) subtreep
  18132. (not (setq swallowp
  18133. (org-yank-folding-would-swallow-text beg end))))
  18134. (or (looking-at outline-regexp)
  18135. (re-search-forward (concat "^" outline-regexp) end t))
  18136. (while (and (< (point) end) (looking-at outline-regexp))
  18137. (hide-subtree)
  18138. (org-cycle-show-empty-lines 'folded)
  18139. (condition-case nil
  18140. (outline-forward-same-level 1)
  18141. (error (goto-char end)))))
  18142. (when swallowp
  18143. (message
  18144. "Inserted text not folded because that would swallow text"))
  18145. (goto-char end)
  18146. (skip-chars-forward " \t\n\r")
  18147. (beginning-of-line 1)
  18148. (push-mark beg 'nomsg)))
  18149. ((and subtreep org-yank-adjusted-subtrees)
  18150. (let ((beg (point-at-bol)))
  18151. (org-paste-subtree nil nil 'for-yank)
  18152. (push-mark beg 'nomsg)))
  18153. (t
  18154. (call-interactively command))))))
  18155. (defun org-yank-folding-would-swallow-text (beg end)
  18156. "Would hide-subtree at BEG swallow any text after END?"
  18157. (let (level)
  18158. (save-excursion
  18159. (goto-char beg)
  18160. (when (or (looking-at outline-regexp)
  18161. (re-search-forward (concat "^" outline-regexp) end t))
  18162. (setq level (org-outline-level)))
  18163. (goto-char end)
  18164. (skip-chars-forward " \t\r\n\v\f")
  18165. (if (or (eobp)
  18166. (and (bolp) (looking-at org-outline-regexp)
  18167. (<= (org-outline-level) level)))
  18168. nil ; Nothing would be swallowed
  18169. t)))) ; something would swallow
  18170. (define-key org-mode-map "\C-y" 'org-yank)
  18171. (defun org-truely-invisible-p ()
  18172. "Check if point is at a character currently not visible.
  18173. This version does not only check the character property, but also
  18174. `visible-mode'."
  18175. ;; Early versions of noutline don't have `outline-invisible-p'.
  18176. (if (org-bound-and-true-p visible-mode)
  18177. nil
  18178. (outline-invisible-p)))
  18179. (defun org-invisible-p2 ()
  18180. "Check if point is at a character currently not visible."
  18181. (save-excursion
  18182. (if (and (eolp) (not (bobp))) (backward-char 1))
  18183. ;; Early versions of noutline don't have `outline-invisible-p'.
  18184. (outline-invisible-p)))
  18185. (defun org-back-to-heading (&optional invisible-ok)
  18186. "Call `outline-back-to-heading', but provide a better error message."
  18187. (condition-case nil
  18188. (outline-back-to-heading invisible-ok)
  18189. (error (error "Before first headline at position %d in buffer %s"
  18190. (point) (current-buffer)))))
  18191. (defun org-beginning-of-defun ()
  18192. "Go to the beginning of the subtree, i.e. back to the heading."
  18193. (org-back-to-heading))
  18194. (defun org-end-of-defun ()
  18195. "Go to the end of the subtree."
  18196. (org-end-of-subtree nil t))
  18197. (defun org-before-first-heading-p ()
  18198. "Before first heading?"
  18199. (save-excursion
  18200. (end-of-line)
  18201. (null (re-search-backward "^\\*+ " nil t))))
  18202. (defun org-on-heading-p (&optional ignored)
  18203. (outline-on-heading-p t))
  18204. (defun org-at-heading-p (&optional ignored)
  18205. (outline-on-heading-p t))
  18206. (defun org-point-at-end-of-empty-headline ()
  18207. "If point is at the end of an empty headline, return t, else nil.
  18208. If the heading only contains a TODO keyword, it is still still considered
  18209. empty."
  18210. (and (looking-at "[ \t]*$")
  18211. (save-excursion
  18212. (beginning-of-line 1)
  18213. (let ((case-fold-search nil))
  18214. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  18215. "\\)?[ \t]*$"))))))
  18216. (defun org-at-heading-or-item-p ()
  18217. (or (org-on-heading-p) (org-at-item-p)))
  18218. (defun org-on-target-p ()
  18219. (or (org-in-regexp org-radio-target-regexp)
  18220. (org-in-regexp org-target-regexp)))
  18221. (defun org-up-heading-all (arg)
  18222. "Move to the heading line of which the present line is a subheading.
  18223. This function considers both visible and invisible heading lines.
  18224. With argument, move up ARG levels."
  18225. (if (fboundp 'outline-up-heading-all)
  18226. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18227. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18228. (defun org-up-heading-safe ()
  18229. "Move to the heading line of which the present line is a subheading.
  18230. This version will not throw an error. It will return the level of the
  18231. headline found, or nil if no higher level is found.
  18232. Also, this function will be a lot faster than `outline-up-heading',
  18233. because it relies on stars being the outline starters. This can really
  18234. make a significant difference in outlines with very many siblings."
  18235. (let (start-level re)
  18236. (org-back-to-heading t)
  18237. (setq start-level (funcall outline-level))
  18238. (if (equal start-level 1)
  18239. nil
  18240. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18241. (if (re-search-backward re nil t)
  18242. (funcall outline-level)))))
  18243. (defun org-first-sibling-p ()
  18244. "Is this heading the first child of its parents?"
  18245. (interactive)
  18246. (let ((re (concat "^" outline-regexp))
  18247. level l)
  18248. (unless (org-at-heading-p t)
  18249. (error "Not at a heading"))
  18250. (setq level (funcall outline-level))
  18251. (save-excursion
  18252. (if (not (re-search-backward re nil t))
  18253. t
  18254. (setq l (funcall outline-level))
  18255. (< l level)))))
  18256. (defun org-goto-sibling (&optional previous)
  18257. "Goto the next sibling, even if it is invisible.
  18258. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18259. when a sibling was found. When none is found, return nil and don't
  18260. move point."
  18261. (let ((fun (if previous 're-search-backward 're-search-forward))
  18262. (pos (point))
  18263. (re (concat "^" outline-regexp))
  18264. level l)
  18265. (when (condition-case nil (org-back-to-heading t) (error nil))
  18266. (setq level (funcall outline-level))
  18267. (catch 'exit
  18268. (or previous (forward-char 1))
  18269. (while (funcall fun re nil t)
  18270. (setq l (funcall outline-level))
  18271. (when (< l level) (goto-char pos) (throw 'exit nil))
  18272. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18273. (goto-char pos)
  18274. nil))))
  18275. (defun org-show-siblings ()
  18276. "Show all siblings of the current headline."
  18277. (save-excursion
  18278. (while (org-goto-sibling) (org-flag-heading nil)))
  18279. (save-excursion
  18280. (while (org-goto-sibling 'previous)
  18281. (org-flag-heading nil))))
  18282. (defun org-goto-first-child ()
  18283. "Goto the first child, even if it is invisible.
  18284. Return t when a child was found. Otherwise don't move point and
  18285. return nil."
  18286. (let (level (pos (point)) (re (concat "^" outline-regexp)))
  18287. (when (condition-case nil (org-back-to-heading t) (error nil))
  18288. (setq level (outline-level))
  18289. (forward-char 1)
  18290. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18291. (progn (goto-char (match-beginning 0)) t)
  18292. (goto-char pos) nil))))
  18293. (defun org-show-hidden-entry ()
  18294. "Show an entry where even the heading is hidden."
  18295. (save-excursion
  18296. (org-show-entry)))
  18297. (defun org-flag-heading (flag &optional entry)
  18298. "Flag the current heading. FLAG non-nil means make invisible.
  18299. When ENTRY is non-nil, show the entire entry."
  18300. (save-excursion
  18301. (org-back-to-heading t)
  18302. ;; Check if we should show the entire entry
  18303. (if entry
  18304. (progn
  18305. (org-show-entry)
  18306. (save-excursion
  18307. (and (outline-next-heading)
  18308. (org-flag-heading nil))))
  18309. (outline-flag-region (max (point-min) (1- (point)))
  18310. (save-excursion (outline-end-of-heading) (point))
  18311. flag))))
  18312. (defun org-get-next-sibling ()
  18313. "Move to next heading of the same level, and return point.
  18314. If there is no such heading, return nil.
  18315. This is like outline-next-sibling, but invisible headings are ok."
  18316. (let ((level (funcall outline-level)))
  18317. (outline-next-heading)
  18318. (while (and (not (eobp)) (> (funcall outline-level) level))
  18319. (outline-next-heading))
  18320. (if (or (eobp) (< (funcall outline-level) level))
  18321. nil
  18322. (point))))
  18323. (defun org-get-last-sibling ()
  18324. "Move to previous heading of the same level, and return point.
  18325. If there is no such heading, return nil."
  18326. (let ((opoint (point))
  18327. (level (funcall outline-level)))
  18328. (outline-previous-heading)
  18329. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18330. (while (and (> (funcall outline-level) level)
  18331. (not (bobp)))
  18332. (outline-previous-heading))
  18333. (if (< (funcall outline-level) level)
  18334. nil
  18335. (point)))))
  18336. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18337. ;; This contains an exact copy of the original function, but it uses
  18338. ;; `org-back-to-heading', to make it work also in invisible
  18339. ;; trees. And is uses an invisible-OK argument.
  18340. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  18341. ;; Furthermore, when used inside Org, finding the end of a large subtree
  18342. ;; with many children and grandchildren etc, this can be much faster
  18343. ;; than the outline version.
  18344. (org-back-to-heading invisible-OK)
  18345. (let ((first t)
  18346. (level (funcall outline-level)))
  18347. (if (and (org-mode-p) (< level 1000))
  18348. ;; A true heading (not a plain list item), in Org-mode
  18349. ;; This means we can easily find the end by looking
  18350. ;; only for the right number of stars. Using a regexp to do
  18351. ;; this is so much faster than using a Lisp loop.
  18352. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  18353. (forward-char 1)
  18354. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  18355. ;; something else, do it the slow way
  18356. (while (and (not (eobp))
  18357. (or first (> (funcall outline-level) level)))
  18358. (setq first nil)
  18359. (outline-next-heading)))
  18360. (unless to-heading
  18361. (if (memq (preceding-char) '(?\n ?\^M))
  18362. (progn
  18363. ;; Go to end of line before heading
  18364. (forward-char -1)
  18365. (if (memq (preceding-char) '(?\n ?\^M))
  18366. ;; leave blank line before heading
  18367. (forward-char -1))))))
  18368. (point))
  18369. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  18370. "Use Org version in org-mode, for dramatic speed-up."
  18371. (if (org-mode-p)
  18372. (progn
  18373. (org-end-of-subtree nil t)
  18374. (unless (eobp) (backward-char 1)))
  18375. ad-do-it))
  18376. (defun org-end-of-meta-data-and-drawers ()
  18377. "Jump to the first text after meta data and drawers in the current entry.
  18378. This will move over empty lines, lines with planning time stamps,
  18379. clocking lines, and drawers."
  18380. (org-back-to-heading t)
  18381. (let ((end (save-excursion (outline-next-heading) (point)))
  18382. (re (concat "[ \t]*$"
  18383. "\\|"
  18384. "\\(" org-drawer-regexp "\\)" ; group 1 are drawers
  18385. "\\|"
  18386. "\\([ \t]*\\(" org-keyword-time-regexp "\\)\\)")))
  18387. (forward-line 1)
  18388. (while (looking-at (concat "[ \t]*\\(" org-keyword-time-regexp "\\)"))
  18389. (if (not (match-end 1))
  18390. ;; empty or planning line
  18391. (forward-line 1)
  18392. ;; a drawer, find the end
  18393. (re-search-forward "^[ \t]*:END:" end 'move)
  18394. (forward-line 1)))
  18395. (point)))
  18396. (defun org-forward-same-level (arg &optional invisible-ok)
  18397. "Move forward to the arg'th subheading at same level as this one.
  18398. Stop at the first and last subheadings of a superior heading.
  18399. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  18400. it wil also look at invisible ones."
  18401. (interactive "p")
  18402. (org-back-to-heading invisible-ok)
  18403. (org-on-heading-p)
  18404. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18405. (re (format "^\\*\\{1,%d\\} " level))
  18406. l)
  18407. (forward-char 1)
  18408. (while (> arg 0)
  18409. (while (and (re-search-forward re nil 'move)
  18410. (setq l (- (match-end 0) (match-beginning 0) 1))
  18411. (= l level)
  18412. (not invisible-ok)
  18413. (progn (backward-char 1) (outline-invisible-p)))
  18414. (if (< l level) (setq arg 1)))
  18415. (setq arg (1- arg)))
  18416. (beginning-of-line 1)))
  18417. (defun org-backward-same-level (arg &optional invisible-ok)
  18418. "Move backward to the arg'th subheading at same level as this one.
  18419. Stop at the first and last subheadings of a superior heading."
  18420. (interactive "p")
  18421. (org-back-to-heading)
  18422. (org-on-heading-p)
  18423. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18424. (re (format "^\\*\\{1,%d\\} " level))
  18425. l)
  18426. (while (> arg 0)
  18427. (while (and (re-search-backward re nil 'move)
  18428. (setq l (- (match-end 0) (match-beginning 0) 1))
  18429. (= l level)
  18430. (not invisible-ok)
  18431. (outline-invisible-p))
  18432. (if (< l level) (setq arg 1)))
  18433. (setq arg (1- arg)))))
  18434. (defun org-show-subtree ()
  18435. "Show everything after this heading at deeper levels."
  18436. (outline-flag-region
  18437. (point)
  18438. (save-excursion
  18439. (org-end-of-subtree t t))
  18440. nil))
  18441. (defun org-show-entry ()
  18442. "Show the body directly following this heading.
  18443. Show the heading too, if it is currently invisible."
  18444. (interactive)
  18445. (save-excursion
  18446. (condition-case nil
  18447. (progn
  18448. (org-back-to-heading t)
  18449. (outline-flag-region
  18450. (max (point-min) (1- (point)))
  18451. (save-excursion
  18452. (if (re-search-forward
  18453. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  18454. (match-beginning 1)
  18455. (point-max)))
  18456. nil)
  18457. (org-cycle-hide-drawers 'children))
  18458. (error nil))))
  18459. (defun org-make-options-regexp (kwds &optional extra)
  18460. "Make a regular expression for keyword lines."
  18461. (concat
  18462. "^"
  18463. "#?[ \t]*\\+\\("
  18464. (mapconcat 'regexp-quote kwds "\\|")
  18465. (if extra (concat "\\|" extra))
  18466. "\\):[ \t]*"
  18467. "\\(.*\\)"))
  18468. ;; Make isearch reveal the necessary context
  18469. (defun org-isearch-end ()
  18470. "Reveal context after isearch exits."
  18471. (when isearch-success ; only if search was successful
  18472. (if (featurep 'xemacs)
  18473. ;; Under XEmacs, the hook is run in the correct place,
  18474. ;; we directly show the context.
  18475. (org-show-context 'isearch)
  18476. ;; In Emacs the hook runs *before* restoring the overlays.
  18477. ;; So we have to use a one-time post-command-hook to do this.
  18478. ;; (Emacs 22 has a special variable, see function `org-mode')
  18479. (unless (and (boundp 'isearch-mode-end-hook-quit)
  18480. isearch-mode-end-hook-quit)
  18481. ;; Only when the isearch was not quitted.
  18482. (org-add-hook 'post-command-hook 'org-isearch-post-command
  18483. 'append 'local)))))
  18484. (defun org-isearch-post-command ()
  18485. "Remove self from hook, and show context."
  18486. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  18487. (org-show-context 'isearch))
  18488. ;;;; Integration with and fixes for other packages
  18489. ;;; Imenu support
  18490. (defvar org-imenu-markers nil
  18491. "All markers currently used by Imenu.")
  18492. (make-variable-buffer-local 'org-imenu-markers)
  18493. (defun org-imenu-new-marker (&optional pos)
  18494. "Return a new marker for use by Imenu, and remember the marker."
  18495. (let ((m (make-marker)))
  18496. (move-marker m (or pos (point)))
  18497. (push m org-imenu-markers)
  18498. m))
  18499. (defun org-imenu-get-tree ()
  18500. "Produce the index for Imenu."
  18501. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  18502. (setq org-imenu-markers nil)
  18503. (let* ((n org-imenu-depth)
  18504. (re (concat "^" outline-regexp))
  18505. (subs (make-vector (1+ n) nil))
  18506. (last-level 0)
  18507. m level head)
  18508. (save-excursion
  18509. (save-restriction
  18510. (widen)
  18511. (goto-char (point-max))
  18512. (while (re-search-backward re nil t)
  18513. (setq level (org-reduced-level (funcall outline-level)))
  18514. (when (<= level n)
  18515. (looking-at org-complex-heading-regexp)
  18516. (setq head (org-link-display-format
  18517. (org-match-string-no-properties 4))
  18518. m (org-imenu-new-marker))
  18519. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  18520. (if (>= level last-level)
  18521. (push (cons head m) (aref subs level))
  18522. (push (cons head (aref subs (1+ level))) (aref subs level))
  18523. (loop for i from (1+ level) to n do (aset subs i nil)))
  18524. (setq last-level level)))))
  18525. (aref subs 1)))
  18526. (eval-after-load "imenu"
  18527. '(progn
  18528. (add-hook 'imenu-after-jump-hook
  18529. (lambda ()
  18530. (if (org-mode-p)
  18531. (org-show-context 'org-goto))))))
  18532. (defun org-link-display-format (link)
  18533. "Replace a link with either the description, or the link target
  18534. if no description is present"
  18535. (save-match-data
  18536. (if (string-match org-bracket-link-analytic-regexp link)
  18537. (replace-match (if (match-end 5)
  18538. (match-string 5 link)
  18539. (concat (match-string 1 link)
  18540. (match-string 3 link)))
  18541. nil t link)
  18542. link)))
  18543. ;; Speedbar support
  18544. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  18545. "Overlay marking the agenda restriction line in speedbar.")
  18546. (overlay-put org-speedbar-restriction-lock-overlay
  18547. 'face 'org-agenda-restriction-lock)
  18548. (overlay-put org-speedbar-restriction-lock-overlay
  18549. 'help-echo "Agendas are currently limited to this item.")
  18550. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  18551. (defun org-speedbar-set-agenda-restriction ()
  18552. "Restrict future agenda commands to the location at point in speedbar.
  18553. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  18554. (interactive)
  18555. (require 'org-agenda)
  18556. (let (p m tp np dir txt)
  18557. (cond
  18558. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18559. 'org-imenu t))
  18560. (setq m (get-text-property p 'org-imenu-marker))
  18561. (with-current-buffer (marker-buffer m)
  18562. (goto-char m)
  18563. (org-agenda-set-restriction-lock 'subtree)))
  18564. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18565. 'speedbar-function 'speedbar-find-file))
  18566. (setq tp (previous-single-property-change
  18567. (1+ p) 'speedbar-function)
  18568. np (next-single-property-change
  18569. tp 'speedbar-function)
  18570. dir (speedbar-line-directory)
  18571. txt (buffer-substring-no-properties (or tp (point-min))
  18572. (or np (point-max))))
  18573. (with-current-buffer (find-file-noselect
  18574. (let ((default-directory dir))
  18575. (expand-file-name txt)))
  18576. (unless (org-mode-p)
  18577. (error "Cannot restrict to non-Org-mode file"))
  18578. (org-agenda-set-restriction-lock 'file)))
  18579. (t (error "Don't know how to restrict Org-mode's agenda")))
  18580. (move-overlay org-speedbar-restriction-lock-overlay
  18581. (point-at-bol) (point-at-eol))
  18582. (setq current-prefix-arg nil)
  18583. (org-agenda-maybe-redo)))
  18584. (eval-after-load "speedbar"
  18585. '(progn
  18586. (speedbar-add-supported-extension ".org")
  18587. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  18588. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  18589. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  18590. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18591. (add-hook 'speedbar-visiting-tag-hook
  18592. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  18593. ;;; Fixes and Hacks for problems with other packages
  18594. ;; Make flyspell not check words in links, to not mess up our keymap
  18595. (defun org-mode-flyspell-verify ()
  18596. "Don't let flyspell put overlays at active buttons, or on
  18597. {todo,all-time,additional-option-like}-keywords."
  18598. (let ((pos (max (1- (point)) (point-min)))
  18599. (word (thing-at-point 'word)))
  18600. (and (not (get-text-property pos 'keymap))
  18601. (not (get-text-property pos 'org-no-flyspell))
  18602. (not (member word org-todo-keywords-1))
  18603. (not (member word org-all-time-keywords))
  18604. (not (member word org-additional-option-like-keywords)))))
  18605. (defun org-remove-flyspell-overlays-in (beg end)
  18606. "Remove flyspell overlays in region."
  18607. (and (org-bound-and-true-p flyspell-mode)
  18608. (fboundp 'flyspell-delete-region-overlays)
  18609. (flyspell-delete-region-overlays beg end))
  18610. (add-text-properties beg end '(org-no-flyspell t)))
  18611. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  18612. (eval-after-load "bookmark"
  18613. '(if (boundp 'bookmark-after-jump-hook)
  18614. ;; We can use the hook
  18615. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  18616. ;; Hook not available, use advice
  18617. (defadvice bookmark-jump (after org-make-visible activate)
  18618. "Make the position visible."
  18619. (org-bookmark-jump-unhide))))
  18620. ;; Make sure saveplace shows the location if it was hidden
  18621. (eval-after-load "saveplace"
  18622. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  18623. "Make the position visible."
  18624. (org-bookmark-jump-unhide)))
  18625. ;; Make sure ecb shows the location if it was hidden
  18626. (eval-after-load "ecb"
  18627. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  18628. "Make hierarchy visible when jumping into location from ECB tree buffer."
  18629. (if (org-mode-p)
  18630. (org-show-context))))
  18631. (defun org-bookmark-jump-unhide ()
  18632. "Unhide the current position, to show the bookmark location."
  18633. (and (org-mode-p)
  18634. (or (outline-invisible-p)
  18635. (save-excursion (goto-char (max (point-min) (1- (point))))
  18636. (outline-invisible-p)))
  18637. (org-show-context 'bookmark-jump)))
  18638. ;; Make session.el ignore our circular variable
  18639. (eval-after-load "session"
  18640. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  18641. ;;;; Experimental code
  18642. (defun org-closed-in-range ()
  18643. "Sparse tree of items closed in a certain time range.
  18644. Still experimental, may disappear in the future."
  18645. (interactive)
  18646. ;; Get the time interval from the user.
  18647. (let* ((time1 (org-float-time
  18648. (org-read-date nil 'to-time nil "Starting date: ")))
  18649. (time2 (org-float-time
  18650. (org-read-date nil 'to-time nil "End date:")))
  18651. ;; callback function
  18652. (callback (lambda ()
  18653. (let ((time
  18654. (org-float-time
  18655. (apply 'encode-time
  18656. (org-parse-time-string
  18657. (match-string 1))))))
  18658. ;; check if time in interval
  18659. (and (>= time time1) (<= time time2))))))
  18660. ;; make tree, check each match with the callback
  18661. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  18662. ;;;; Finish up
  18663. (provide 'org)
  18664. (run-hooks 'org-load-hook)
  18665. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  18666. ;;; org.el ends here