org.el 761 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315
  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 at 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 "#\\+BEGIN: 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. (called-interactively-p 'any)
  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. "Fontify #+ lines and blocks, in the correct ways."
  4568. (let ((case-fold-search t))
  4569. (if (re-search-forward
  4570. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4571. limit t)
  4572. (let ((beg (match-beginning 0))
  4573. (block-start (match-end 0))
  4574. (block-end nil)
  4575. (lang (match-string 7))
  4576. (beg1 (line-beginning-position 2))
  4577. (dc1 (downcase (match-string 2)))
  4578. (dc3 (downcase (match-string 3)))
  4579. end end1 quoting block-type ovl)
  4580. (cond
  4581. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4582. ;; a single line of backend-specific content
  4583. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4584. (remove-text-properties (match-beginning 0) (match-end 0)
  4585. '(display t invisible t intangible t))
  4586. (add-text-properties (match-beginning 1) (match-end 3)
  4587. '(font-lock-fontified t face org-meta-line))
  4588. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4589. '(font-lock-fontified t face org-block))
  4590. ; for backend-specific code
  4591. t)
  4592. ((and (match-end 4) (equal dc3 "begin"))
  4593. ;; Truly a block
  4594. (setq block-type (downcase (match-string 5))
  4595. quoting (member block-type org-protecting-blocks))
  4596. (when (re-search-forward
  4597. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4598. nil t) ;; on purpose, we look further than LIMIT
  4599. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4600. (setq block-end (match-beginning 0))
  4601. (when quoting
  4602. (remove-text-properties beg end
  4603. '(display t invisible t intangible t)))
  4604. (add-text-properties
  4605. beg end
  4606. '(font-lock-fontified t font-lock-multiline t))
  4607. (add-text-properties beg beg1 '(face org-meta-line))
  4608. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4609. ; for end_src
  4610. (cond
  4611. ((and lang org-src-fontify-natively)
  4612. (org-src-font-lock-fontify-block lang block-start block-end)
  4613. ;; remove old background overlays
  4614. (mapc (lambda (ov)
  4615. (if (eq (overlay-get ov 'face) 'org-block-background)
  4616. (delete-overlay ov)))
  4617. (overlays-at (/ (+ beg1 block-end) 2)))
  4618. ;; add a background overlay
  4619. (setq ovl (make-overlay beg1 block-end))
  4620. (overlay-put ovl 'face 'org-block-background)
  4621. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4622. (quoting
  4623. (add-text-properties beg1 (+ end1 1) '(face org-block)))
  4624. ; end of source block
  4625. ((not org-fontify-quote-and-verse-blocks))
  4626. ((string= block-type "quote")
  4627. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4628. ((string= block-type "verse")
  4629. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4630. (add-text-properties beg beg1 '(face org-block-begin-line))
  4631. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4632. t))
  4633. ((member dc1 '("title:" "author:" "email:" "date:"))
  4634. (add-text-properties
  4635. beg (match-end 3)
  4636. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4637. '(font-lock-fontified t invisible t)
  4638. '(font-lock-fontified t face org-document-info-keyword)))
  4639. (add-text-properties
  4640. (match-beginning 6) (match-end 6)
  4641. (if (string-equal dc1 "title:")
  4642. '(font-lock-fontified t face org-document-title)
  4643. '(font-lock-fontified t face org-document-info))))
  4644. ((not (member (char-after beg) '(?\ ?\t)))
  4645. ;; just any other in-buffer setting, but not indented
  4646. (add-text-properties
  4647. beg (1+ (match-end 0))
  4648. '(font-lock-fontified t face org-meta-line))
  4649. t)
  4650. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4651. "orgtbl:" "tblfm:" "tblname:" "result:"
  4652. "results:" "source:" "srcname:" "call:"))
  4653. (and (match-end 4) (equal dc3 "attr")))
  4654. (add-text-properties
  4655. beg (match-end 0)
  4656. '(font-lock-fontified t face org-meta-line))
  4657. t)
  4658. ((member dc3 '(" " ""))
  4659. (add-text-properties
  4660. beg (match-end 0)
  4661. '(font-lock-fontified t face font-lock-comment-face)))
  4662. (t nil))))))
  4663. (defun org-activate-angle-links (limit)
  4664. "Run through the buffer and add overlays to links."
  4665. (if (re-search-forward org-angle-link-re limit t)
  4666. (progn
  4667. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4668. (add-text-properties (match-beginning 0) (match-end 0)
  4669. (list 'mouse-face 'highlight
  4670. 'keymap org-mouse-map))
  4671. (org-rear-nonsticky-at (match-end 0))
  4672. t)))
  4673. (defun org-activate-footnote-links (limit)
  4674. "Run through the buffer and add overlays to links."
  4675. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4676. limit t)
  4677. (progn
  4678. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4679. (add-text-properties (match-beginning 2) (match-end 2)
  4680. (list 'mouse-face 'highlight
  4681. 'keymap org-mouse-map
  4682. 'help-echo
  4683. (if (= (point-at-bol) (match-beginning 2))
  4684. "Footnote definition"
  4685. "Footnote reference")
  4686. ))
  4687. (org-rear-nonsticky-at (match-end 2))
  4688. t)))
  4689. (defun org-activate-bracket-links (limit)
  4690. "Run through the buffer and add overlays to bracketed links."
  4691. (if (re-search-forward org-bracket-link-regexp limit t)
  4692. (let* ((help (concat "LINK: "
  4693. (org-match-string-no-properties 1)))
  4694. ;; FIXME: above we should remove the escapes.
  4695. ;; but that requires another match, protecting match data,
  4696. ;; a lot of overhead for font-lock.
  4697. (ip (org-maybe-intangible
  4698. (list 'invisible 'org-link
  4699. 'keymap org-mouse-map 'mouse-face 'highlight
  4700. 'font-lock-multiline t 'help-echo help)))
  4701. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4702. 'font-lock-multiline t 'help-echo help)))
  4703. ;; We need to remove the invisible property here. Table narrowing
  4704. ;; may have made some of this invisible.
  4705. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4706. (remove-text-properties (match-beginning 0) (match-end 0)
  4707. '(invisible nil))
  4708. (if (match-end 3)
  4709. (progn
  4710. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4711. (org-rear-nonsticky-at (match-beginning 3))
  4712. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4713. (org-rear-nonsticky-at (match-end 3))
  4714. (add-text-properties (match-end 3) (match-end 0) ip)
  4715. (org-rear-nonsticky-at (match-end 0)))
  4716. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4717. (org-rear-nonsticky-at (match-beginning 1))
  4718. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4719. (org-rear-nonsticky-at (match-end 1))
  4720. (add-text-properties (match-end 1) (match-end 0) ip)
  4721. (org-rear-nonsticky-at (match-end 0)))
  4722. t)))
  4723. (defun org-activate-dates (limit)
  4724. "Run through the buffer and add overlays to dates."
  4725. (if (re-search-forward org-tsr-regexp-both limit t)
  4726. (progn
  4727. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4728. (add-text-properties (match-beginning 0) (match-end 0)
  4729. (list 'mouse-face 'highlight
  4730. 'keymap org-mouse-map))
  4731. (org-rear-nonsticky-at (match-end 0))
  4732. (when org-display-custom-times
  4733. (if (match-end 3)
  4734. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4735. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4736. t)))
  4737. (defvar org-target-link-regexp nil
  4738. "Regular expression matching radio targets in plain text.")
  4739. (make-variable-buffer-local 'org-target-link-regexp)
  4740. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4741. "Regular expression matching a link target.")
  4742. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4743. "Regular expression matching a radio target.")
  4744. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4745. "Regular expression matching any target.")
  4746. (defun org-activate-target-links (limit)
  4747. "Run through the buffer and add overlays to target matches."
  4748. (when org-target-link-regexp
  4749. (let ((case-fold-search t))
  4750. (if (re-search-forward org-target-link-regexp limit t)
  4751. (progn
  4752. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4753. (add-text-properties (match-beginning 0) (match-end 0)
  4754. (list 'mouse-face 'highlight
  4755. 'keymap org-mouse-map
  4756. 'help-echo "Radio target link"
  4757. 'org-linked-text t))
  4758. (org-rear-nonsticky-at (match-end 0))
  4759. t)))))
  4760. (defun org-update-radio-target-regexp ()
  4761. "Find all radio targets in this file and update the regular expression."
  4762. (interactive)
  4763. (when (memq 'radio org-activate-links)
  4764. (setq org-target-link-regexp
  4765. (org-make-target-link-regexp (org-all-targets 'radio)))
  4766. (org-restart-font-lock)))
  4767. (defun org-hide-wide-columns (limit)
  4768. (let (s e)
  4769. (setq s (text-property-any (point) (or limit (point-max))
  4770. 'org-cwidth t))
  4771. (when s
  4772. (setq e (next-single-property-change s 'org-cwidth))
  4773. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4774. (goto-char e)
  4775. t)))
  4776. (defvar org-latex-and-specials-regexp nil
  4777. "Regular expression for highlighting export special stuff.")
  4778. (defvar org-match-substring-regexp)
  4779. (defvar org-match-substring-with-braces-regexp)
  4780. ;; This should be with the exporter code, but we also use if for font-locking
  4781. (defconst org-export-html-special-string-regexps
  4782. '(("\\\\-" . "&shy;")
  4783. ("---\\([^-]\\)" . "&mdash;\\1")
  4784. ("--\\([^-]\\)" . "&ndash;\\1")
  4785. ("\\.\\.\\." . "&hellip;"))
  4786. "Regular expressions for special string conversion.")
  4787. (defun org-compute-latex-and-specials-regexp ()
  4788. "Compute regular expression for stuff treated specially by exporters."
  4789. (if (not org-highlight-latex-fragments-and-specials)
  4790. (org-set-local 'org-latex-and-specials-regexp nil)
  4791. (require 'org-exp)
  4792. (let*
  4793. ((matchers (plist-get org-format-latex-options :matchers))
  4794. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4795. org-latex-regexps)))
  4796. (org-export-allow-BIND nil)
  4797. (options (org-combine-plists (org-default-export-plist)
  4798. (org-infile-export-plist)))
  4799. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4800. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4801. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4802. (org-export-html-expand (plist-get options :expand-quoted-html))
  4803. (org-export-with-special-strings (plist-get options :special-strings))
  4804. (re-sub
  4805. (cond
  4806. ((equal org-export-with-sub-superscripts '{})
  4807. (list org-match-substring-with-braces-regexp))
  4808. (org-export-with-sub-superscripts
  4809. (list org-match-substring-regexp))
  4810. (t nil)))
  4811. (re-latex
  4812. (if org-export-with-LaTeX-fragments
  4813. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4814. (re-macros
  4815. (if org-export-with-TeX-macros
  4816. (list (concat "\\\\"
  4817. (regexp-opt
  4818. (append
  4819. (delq nil
  4820. (mapcar 'car-safe
  4821. (append org-entities-user
  4822. org-entities)))
  4823. (if (boundp 'org-latex-entities)
  4824. (mapcar (lambda (x)
  4825. (or (car-safe x) x))
  4826. org-latex-entities)
  4827. nil))
  4828. 'words))) ; FIXME
  4829. ))
  4830. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4831. (re-special (if org-export-with-special-strings
  4832. (mapcar (lambda (x) (car x))
  4833. org-export-html-special-string-regexps)))
  4834. (re-rest
  4835. (delq nil
  4836. (list
  4837. (if org-export-html-expand "@<[^>\n]+>")
  4838. ))))
  4839. (org-set-local
  4840. 'org-latex-and-specials-regexp
  4841. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4842. re-rest) "\\|")))))
  4843. (defun org-do-latex-and-special-faces (limit)
  4844. "Run through the buffer and add overlays to links."
  4845. (when org-latex-and-specials-regexp
  4846. (let (rtn d)
  4847. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4848. limit t))
  4849. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4850. 'face))
  4851. '(org-code org-verbatim underline)))
  4852. (progn
  4853. (setq rtn t
  4854. d (cond ((member (char-after (1+ (match-beginning 0)))
  4855. '(?_ ?^)) 1)
  4856. (t 0)))
  4857. (font-lock-prepend-text-property
  4858. (+ d (match-beginning 0)) (match-end 0)
  4859. 'face 'org-latex-and-export-specials)
  4860. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4861. '(font-lock-multiline t)))))
  4862. rtn)))
  4863. (defun org-restart-font-lock ()
  4864. "Restart `font-lock-mode', to force refontification."
  4865. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4866. (font-lock-mode -1)
  4867. (font-lock-mode 1)))
  4868. (defun org-all-targets (&optional radio)
  4869. "Return a list of all targets in this file.
  4870. With optional argument RADIO, only find radio targets."
  4871. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4872. rtn)
  4873. (save-excursion
  4874. (goto-char (point-min))
  4875. (while (re-search-forward re nil t)
  4876. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4877. rtn)))
  4878. (defun org-make-target-link-regexp (targets)
  4879. "Make regular expression matching all strings in TARGETS.
  4880. The regular expression finds the targets also if there is a line break
  4881. between words."
  4882. (and targets
  4883. (concat
  4884. "\\<\\("
  4885. (mapconcat
  4886. (lambda (x)
  4887. (setq x (regexp-quote x))
  4888. (while (string-match " +" x)
  4889. (setq x (replace-match "\\s-+" t t x)))
  4890. x)
  4891. targets
  4892. "\\|")
  4893. "\\)\\>")))
  4894. (defun org-activate-tags (limit)
  4895. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4896. (progn
  4897. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4898. (add-text-properties (match-beginning 1) (match-end 1)
  4899. (list 'mouse-face 'highlight
  4900. 'keymap org-mouse-map))
  4901. (org-rear-nonsticky-at (match-end 1))
  4902. t)))
  4903. (defun org-outline-level ()
  4904. "Compute the outline level of the heading at point.
  4905. This function assumes that the cursor is at the beginning of a line matched
  4906. by `outline-regexp'. Otherwise it returns garbage.
  4907. If this is called at a normal headline, the level is the number of stars.
  4908. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  4909. (save-excursion
  4910. (looking-at outline-regexp)
  4911. (1- (- (match-end 0) (match-beginning 0)))))
  4912. (defvar org-font-lock-keywords nil)
  4913. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4914. "Regular expression matching a property line.")
  4915. (defvar org-font-lock-hook nil
  4916. "Functions to be called for special font lock stuff.")
  4917. (defvar org-font-lock-set-keywords-hook nil
  4918. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4919. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4920. it is installed to be used by font lock. This can be useful if something
  4921. needs to be inserted at a specific position in the font-lock sequence.")
  4922. (defun org-font-lock-hook (limit)
  4923. (run-hook-with-args 'org-font-lock-hook limit))
  4924. (defun org-set-font-lock-defaults ()
  4925. (let* ((em org-fontify-emphasized-text)
  4926. (lk org-activate-links)
  4927. (org-font-lock-extra-keywords
  4928. (list
  4929. ;; Call the hook
  4930. '(org-font-lock-hook)
  4931. ;; Headlines
  4932. `(,(if org-fontify-whole-heading-line
  4933. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4934. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4935. (1 (org-get-level-face 1))
  4936. (2 (org-get-level-face 2))
  4937. (3 (org-get-level-face 3)))
  4938. ;; Table lines
  4939. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4940. (1 'org-table t))
  4941. ;; Table internals
  4942. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4943. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4944. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4945. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  4946. ;; Drawers
  4947. (list org-drawer-regexp '(0 'org-special-keyword t))
  4948. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4949. ;; Properties
  4950. (list org-property-re
  4951. '(1 'org-special-keyword t)
  4952. '(3 'org-property-value t))
  4953. ;; Links
  4954. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4955. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4956. (if (memq 'plain lk) '(org-activate-plain-links))
  4957. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4958. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4959. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4960. (if (memq 'footnote lk) '(org-activate-footnote-links
  4961. (2 'org-footnote t)))
  4962. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4963. '(org-hide-wide-columns (0 nil append))
  4964. ;; TODO lines
  4965. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4966. '(1 (org-get-todo-face 1) t))
  4967. ;; DONE
  4968. (if org-fontify-done-headline
  4969. (list (concat "^[*]+ +\\<\\("
  4970. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4971. "\\)\\(.*\\)")
  4972. '(2 'org-headline-done t))
  4973. nil)
  4974. ;; Priorities
  4975. '(org-font-lock-add-priority-faces)
  4976. ;; Tags
  4977. '(org-font-lock-add-tag-faces)
  4978. ;; Special keywords
  4979. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4980. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4981. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4982. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4983. ;; Emphasis
  4984. (if em
  4985. (if (featurep 'xemacs)
  4986. '(org-do-emphasis-faces (0 nil append))
  4987. '(org-do-emphasis-faces)))
  4988. ;; Checkboxes
  4989. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4990. 1 'org-checkbox prepend)
  4991. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4992. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4993. (0 (org-get-checkbox-statistics-face) t)))
  4994. ;; Description list items
  4995. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  4996. 1 'bold prepend)
  4997. ;; ARCHIVEd headings
  4998. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4999. '(1 'org-archived prepend))
  5000. ;; Specials
  5001. '(org-do-latex-and-special-faces)
  5002. '(org-fontify-entities)
  5003. '(org-raise-scripts)
  5004. ;; Code
  5005. '(org-activate-code (1 'org-code t))
  5006. ;; COMMENT
  5007. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5008. "\\|" org-quote-string "\\)\\>")
  5009. '(1 'org-special-keyword t))
  5010. '("^#.*" (0 'font-lock-comment-face t))
  5011. ;; Blocks and meta lines
  5012. '(org-fontify-meta-lines-and-blocks)
  5013. )))
  5014. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5015. (run-hooks 'org-font-lock-set-keywords-hook)
  5016. ;; Now set the full font-lock-keywords
  5017. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5018. (org-set-local 'font-lock-defaults
  5019. '(org-font-lock-keywords t nil nil backward-paragraph))
  5020. (kill-local-variable 'font-lock-keywords) nil))
  5021. (defun org-toggle-pretty-entities ()
  5022. "Toggle the composition display of entities as UTF8 characters."
  5023. (interactive)
  5024. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5025. (org-restart-font-lock)
  5026. (if org-pretty-entities
  5027. (message "Entities are displayed as UTF8 characers")
  5028. (save-restriction
  5029. (widen)
  5030. (org-decompose-region (point-min) (point-max))
  5031. (message "Entities are displayed plain"))))
  5032. (defun org-fontify-entities (limit)
  5033. "Find an entity to fontify."
  5034. (let (ee)
  5035. (when org-pretty-entities
  5036. (catch 'match
  5037. (while (re-search-forward
  5038. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  5039. limit t)
  5040. (if (and (not (org-in-indented-comment-line))
  5041. (setq ee (org-entity-get (match-string 1)))
  5042. (= (length (nth 6 ee)) 1))
  5043. (progn
  5044. (add-text-properties
  5045. (match-beginning 0) (match-end 1)
  5046. (list 'font-lock-fontified t))
  5047. (compose-region (match-beginning 0) (match-end 1)
  5048. (nth 6 ee) nil)
  5049. (backward-char 1)
  5050. (throw 'match t))))
  5051. nil))))
  5052. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5053. "Fontify string S like in Org-mode."
  5054. (with-temp-buffer
  5055. (insert s)
  5056. (let ((org-odd-levels-only odd-levels))
  5057. (org-mode)
  5058. (font-lock-fontify-buffer)
  5059. (buffer-string))))
  5060. (defvar org-m nil)
  5061. (defvar org-l nil)
  5062. (defvar org-f nil)
  5063. (defun org-get-level-face (n)
  5064. "Get the right face for match N in font-lock matching of headlines."
  5065. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5066. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5067. (if org-cycle-level-faces
  5068. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5069. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5070. (cond
  5071. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5072. ((eq n 2) org-f)
  5073. (t (if org-level-color-stars-only nil org-f))))
  5074. (defun org-get-todo-face (kwd)
  5075. "Get the right face for a TODO keyword KWD.
  5076. If KWD is a number, get the corresponding match group."
  5077. (if (numberp kwd) (setq kwd (match-string kwd)))
  5078. (or (org-face-from-face-or-color
  5079. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5080. (and (member kwd org-done-keywords) 'org-done)
  5081. 'org-todo))
  5082. (defun org-face-from-face-or-color (context inherit face-or-color)
  5083. "Create a face list that inherits INHERIT, but sets the foreground color.
  5084. When FACE-OR-COLOR is not a string, just return it."
  5085. (if (stringp face-or-color)
  5086. (list :inherit inherit
  5087. (cdr (assoc context org-faces-easy-properties))
  5088. face-or-color)
  5089. face-or-color))
  5090. (defun org-font-lock-add-tag-faces (limit)
  5091. "Add the special tag faces."
  5092. (when (and org-tag-faces org-tags-special-faces-re)
  5093. (while (re-search-forward org-tags-special-faces-re limit t)
  5094. (add-text-properties (match-beginning 1) (match-end 1)
  5095. (list 'face (org-get-tag-face 1)
  5096. 'font-lock-fontified t))
  5097. (backward-char 1))))
  5098. (defun org-font-lock-add-priority-faces (limit)
  5099. "Add the special priority faces."
  5100. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5101. (add-text-properties
  5102. (match-beginning 0) (match-end 0)
  5103. (list 'face (or (org-face-from-face-or-color
  5104. 'priority 'org-special-keyword
  5105. (cdr (assoc (char-after (match-beginning 1))
  5106. org-priority-faces)))
  5107. 'org-special-keyword)
  5108. 'font-lock-fontified t))))
  5109. (defun org-get-tag-face (kwd)
  5110. "Get the right face for a TODO keyword KWD.
  5111. If KWD is a number, get the corresponding match group."
  5112. (if (numberp kwd) (setq kwd (match-string kwd)))
  5113. (or (org-face-from-face-or-color
  5114. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5115. 'org-tag))
  5116. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5117. "Remove fontification and activation overlays from links."
  5118. (font-lock-default-unfontify-region beg end)
  5119. (let* ((buffer-undo-list t)
  5120. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5121. (inhibit-modification-hooks t)
  5122. deactivate-mark buffer-file-name buffer-file-truename)
  5123. (org-decompose-region beg end)
  5124. (remove-text-properties
  5125. beg end
  5126. (if org-indent-mode
  5127. ;; also remove line-prefix and wrap-prefix properties
  5128. '(mouse-face t keymap t org-linked-text t
  5129. invisible t intangible t
  5130. line-prefix t wrap-prefix t
  5131. org-no-flyspell t org-emphasis t)
  5132. '(mouse-face t keymap t org-linked-text t
  5133. invisible t intangible t
  5134. org-no-flyspell t org-emphasis t)))
  5135. (org-remove-font-lock-display-properties beg end)))
  5136. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5137. ((raise 0.3) (height 0.7))
  5138. ((raise -0.5))
  5139. ((raise 0.5)))
  5140. "Display properties for showing superscripts and subscripts.")
  5141. (defun org-remove-font-lock-display-properties (beg end)
  5142. "Remove specific display properties that have been added by font lock.
  5143. The will remove the raise properties that are used to show superscripts
  5144. and subscripts."
  5145. (let (next prop)
  5146. (while (< beg end)
  5147. (setq next (next-single-property-change beg 'display nil end)
  5148. prop (get-text-property beg 'display))
  5149. (if (member prop org-script-display)
  5150. (put-text-property beg next 'display nil))
  5151. (setq beg next))))
  5152. (defun org-raise-scripts (limit)
  5153. "Add raise properties to sub/superscripts."
  5154. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5155. (if (re-search-forward
  5156. (if (eq org-use-sub-superscripts t)
  5157. org-match-substring-regexp
  5158. org-match-substring-with-braces-regexp)
  5159. limit t)
  5160. (let* ((pos (point)) table-p comment-p
  5161. (mpos (match-beginning 3))
  5162. (emph-p (get-text-property mpos 'org-emphasis))
  5163. (link-p (get-text-property mpos 'mouse-face))
  5164. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5165. (goto-char (point-at-bol))
  5166. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5167. comment-p (org-looking-at-p "[ \t]*#"))
  5168. (goto-char pos)
  5169. ;; FIXME: Should we go back one character here, for a_b^c
  5170. ;; (goto-char (1- pos)) ;????????????????????
  5171. (if (or comment-p emph-p link-p keyw-p)
  5172. t
  5173. (put-text-property (match-beginning 3) (match-end 0)
  5174. 'display
  5175. (if (equal (char-after (match-beginning 2)) ?^)
  5176. (nth (if table-p 3 1) org-script-display)
  5177. (nth (if table-p 2 0) org-script-display)))
  5178. (add-text-properties (match-beginning 2) (match-end 2)
  5179. (list 'invisible t
  5180. 'org-dwidth t 'org-dwidth-n 1))
  5181. (if (and (eq (char-after (match-beginning 3)) ?{)
  5182. (eq (char-before (match-end 3)) ?}))
  5183. (progn
  5184. (add-text-properties
  5185. (match-beginning 3) (1+ (match-beginning 3))
  5186. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5187. (add-text-properties
  5188. (1- (match-end 3)) (match-end 3)
  5189. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5190. t)))))
  5191. ;;;; Visibility cycling, including org-goto and indirect buffer
  5192. ;;; Cycling
  5193. (defvar org-cycle-global-status nil)
  5194. (make-variable-buffer-local 'org-cycle-global-status)
  5195. (defvar org-cycle-subtree-status nil)
  5196. (make-variable-buffer-local 'org-cycle-subtree-status)
  5197. ;;;###autoload
  5198. (defvar org-inlinetask-min-level)
  5199. (defun org-cycle (&optional arg)
  5200. "TAB-action and visibility cycling for Org-mode.
  5201. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5202. is outline visibility cycling, but it also invokes other actions
  5203. in special contexts.
  5204. - When this function is called with a prefix argument, rotate the entire
  5205. buffer through 3 states (global cycling)
  5206. 1. OVERVIEW: Show only top-level headlines.
  5207. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5208. 3. SHOW ALL: Show everything.
  5209. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5210. determined by the variable `org-startup-folded', and by any VISIBILITY
  5211. properties in the buffer.
  5212. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5213. including any drawers.
  5214. - When inside a table, re-align the table and move to the next field.
  5215. - When point is at the beginning of a headline, rotate the subtree started
  5216. by this line through 3 different states (local cycling)
  5217. 1. FOLDED: Only the main headline is shown.
  5218. 2. CHILDREN: The main headline and the direct children are shown.
  5219. From this state, you can move to one of the children
  5220. and zoom in further.
  5221. 3. SUBTREE: Show the entire subtree, including body text.
  5222. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5223. - When point is at the beginning of an empty headline and the variable
  5224. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5225. of the headline by demoting and promoting it to likely levels. This
  5226. speeds up creation document structure by pressing TAB once or several
  5227. times right after creating a new headline.
  5228. - When there is a numeric prefix, go up to a heading with level ARG, do
  5229. a `show-subtree' and return to the previous cursor position. If ARG
  5230. is negative, go up that many levels.
  5231. - When point is not at the beginning of a headline, execute the global
  5232. binding for TAB, which is re-indenting the line. See the option
  5233. `org-cycle-emulate-tab' for details.
  5234. - Special case: if point is at the beginning of the buffer and there is
  5235. no headline in line 1, this function will act as if called with prefix arg
  5236. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5237. But only if also the variable `org-cycle-global-at-bob' is t."
  5238. (interactive "P")
  5239. (org-load-modules-maybe)
  5240. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5241. (and org-cycle-level-after-item/entry-creation
  5242. (or (org-cycle-level)
  5243. (org-cycle-item-indentation))))
  5244. (let* ((limit-level
  5245. (or org-cycle-max-level
  5246. (and (boundp 'org-inlinetask-min-level)
  5247. org-inlinetask-min-level
  5248. (1- org-inlinetask-min-level))))
  5249. (nstars (and limit-level
  5250. (if org-odd-levels-only
  5251. (and limit-level (1- (* limit-level 2)))
  5252. limit-level)))
  5253. (outline-regexp
  5254. (if (not (org-mode-p))
  5255. outline-regexp
  5256. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5257. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5258. (not (looking-at outline-regexp))))
  5259. (org-cycle-hook
  5260. (if bob-special
  5261. (delq 'org-optimize-window-after-visibility-change
  5262. (copy-sequence org-cycle-hook))
  5263. org-cycle-hook))
  5264. (pos (point)))
  5265. (if (or bob-special (equal arg '(4)))
  5266. ;; special case: use global cycling
  5267. (setq arg t))
  5268. (cond
  5269. ((equal arg '(16))
  5270. (setq last-command 'dummy)
  5271. (org-set-startup-visibility)
  5272. (message "Startup visibility, plus VISIBILITY properties"))
  5273. ((equal arg '(64))
  5274. (show-all)
  5275. (message "Entire buffer visible, including drawers"))
  5276. ;; Table: enter it or move to the next field.
  5277. ((org-at-table-p 'any)
  5278. (if (org-at-table.el-p)
  5279. (message "Use C-c ' to edit table.el tables")
  5280. (if arg (org-table-edit-field t)
  5281. (org-table-justify-field-maybe)
  5282. (call-interactively 'org-table-next-field))))
  5283. ((run-hook-with-args-until-success
  5284. 'org-tab-after-check-for-table-hook))
  5285. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5286. ((eq arg t) (org-cycle-internal-global))
  5287. ;; Drawers: delegate to `org-flag-drawer'.
  5288. ((and org-drawers org-drawer-regexp
  5289. (save-excursion
  5290. (beginning-of-line 1)
  5291. (looking-at org-drawer-regexp)))
  5292. (org-flag-drawer ; toggle block visibility
  5293. (not (get-char-property (match-end 0) 'invisible))))
  5294. ;; Show-subtree, ARG levels up from here.
  5295. ((integerp arg)
  5296. (save-excursion
  5297. (org-back-to-heading)
  5298. (outline-up-heading (if (< arg 0) (- arg)
  5299. (- (funcall outline-level) arg)))
  5300. (org-show-subtree)))
  5301. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5302. ((and (featurep 'org-inlinetask)
  5303. (org-inlinetask-at-task-p)
  5304. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5305. (org-inlinetask-toggle-visibility))
  5306. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5307. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5308. (save-excursion (beginning-of-line 1)
  5309. (looking-at outline-regexp)))
  5310. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5311. (org-cycle-internal-local))
  5312. ;; From there: TAB emulation and template completion.
  5313. (buffer-read-only (org-back-to-heading))
  5314. ((run-hook-with-args-until-success
  5315. 'org-tab-after-check-for-cycling-hook))
  5316. ((org-try-structure-completion))
  5317. ((org-try-cdlatex-tab))
  5318. ((run-hook-with-args-until-success
  5319. 'org-tab-before-tab-emulation-hook))
  5320. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5321. (or (not (bolp))
  5322. (not (looking-at outline-regexp))))
  5323. (call-interactively (global-key-binding "\t")))
  5324. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5325. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5326. (or (and (eq org-cycle-emulate-tab 'white)
  5327. (= (match-end 0) (point-at-eol)))
  5328. (and (eq org-cycle-emulate-tab 'whitestart)
  5329. (>= (match-end 0) pos))))
  5330. t
  5331. (eq org-cycle-emulate-tab t))
  5332. (call-interactively (global-key-binding "\t")))
  5333. (t (save-excursion
  5334. (org-back-to-heading)
  5335. (org-cycle)))))))
  5336. (defun org-cycle-internal-global ()
  5337. "Do the global cycling action."
  5338. (cond
  5339. ((and (eq last-command this-command)
  5340. (eq org-cycle-global-status 'overview))
  5341. ;; We just created the overview - now do table of contents
  5342. ;; This can be slow in very large buffers, so indicate action
  5343. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5344. (message "CONTENTS...")
  5345. (org-content)
  5346. (message "CONTENTS...done")
  5347. (setq org-cycle-global-status 'contents)
  5348. (run-hook-with-args 'org-cycle-hook 'contents))
  5349. ((and (eq last-command this-command)
  5350. (eq org-cycle-global-status 'contents))
  5351. ;; We just showed the table of contents - now show everything
  5352. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5353. (show-all)
  5354. (message "SHOW ALL")
  5355. (setq org-cycle-global-status 'all)
  5356. (run-hook-with-args 'org-cycle-hook 'all))
  5357. (t
  5358. ;; Default action: go to overview
  5359. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5360. (org-overview)
  5361. (message "OVERVIEW")
  5362. (setq org-cycle-global-status 'overview)
  5363. (run-hook-with-args 'org-cycle-hook 'overview))))
  5364. (defun org-cycle-internal-local ()
  5365. "Do the local cycling action."
  5366. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5367. ;; First, determine end of headline (EOH), end of subtree or item
  5368. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5369. (save-excursion
  5370. (if (org-at-item-p)
  5371. (progn
  5372. (beginning-of-line)
  5373. (setq struct (org-list-struct))
  5374. (setq eoh (point-at-eol))
  5375. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5376. (setq has-children (org-list-has-child-p (point) struct)))
  5377. (org-back-to-heading)
  5378. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5379. (setq eos (save-excursion
  5380. (org-end-of-subtree t)
  5381. (unless (eobp)
  5382. (skip-chars-forward " \t\n"))
  5383. (if (eobp) (point) (1- (point)))))
  5384. (setq has-children
  5385. (or (save-excursion
  5386. (let ((level (funcall outline-level)))
  5387. (outline-next-heading)
  5388. (and (org-at-heading-p t)
  5389. (> (funcall outline-level) level))))
  5390. (save-excursion
  5391. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5392. ;; Determine end invisible part of buffer (EOL)
  5393. (beginning-of-line 2)
  5394. ;; XEmacs doesn't have `next-single-char-property-change'
  5395. (if (featurep 'xemacs)
  5396. (while (and (not (eobp)) ;; this is like `next-line'
  5397. (get-char-property (1- (point)) 'invisible))
  5398. (beginning-of-line 2))
  5399. (while (and (not (eobp)) ;; this is like `next-line'
  5400. (get-char-property (1- (point)) 'invisible))
  5401. (goto-char (next-single-char-property-change (point) 'invisible))
  5402. (and (eolp) (beginning-of-line 2))))
  5403. (setq eol (point)))
  5404. ;; Find out what to do next and set `this-command'
  5405. (cond
  5406. ((= eos eoh)
  5407. ;; Nothing is hidden behind this heading
  5408. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5409. (message "EMPTY ENTRY")
  5410. (setq org-cycle-subtree-status nil)
  5411. (save-excursion
  5412. (goto-char eos)
  5413. (outline-next-heading)
  5414. (if (outline-invisible-p) (org-flag-heading nil))))
  5415. ((and (or (>= eol eos)
  5416. (not (string-match "\\S-" (buffer-substring eol eos))))
  5417. (or has-children
  5418. (not (setq children-skipped
  5419. org-cycle-skip-children-state-if-no-children))))
  5420. ;; Entire subtree is hidden in one line: children view
  5421. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5422. (if (org-at-item-p)
  5423. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5424. (org-show-entry)
  5425. (show-children)
  5426. ;; Fold every list in subtree to top-level items.
  5427. (when (eq org-cycle-include-plain-lists 'integrate)
  5428. (save-excursion
  5429. (org-back-to-heading)
  5430. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5431. (beginning-of-line 1)
  5432. (let* ((struct (org-list-struct))
  5433. (prevs (org-list-prevs-alist struct))
  5434. (end (org-list-get-bottom-point struct)))
  5435. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5436. (org-list-get-all-items (point) struct prevs))
  5437. (goto-char end))))))
  5438. (message "CHILDREN")
  5439. (save-excursion
  5440. (goto-char eos)
  5441. (outline-next-heading)
  5442. (if (outline-invisible-p) (org-flag-heading nil)))
  5443. (setq org-cycle-subtree-status 'children)
  5444. (run-hook-with-args 'org-cycle-hook 'children))
  5445. ((or children-skipped
  5446. (and (eq last-command this-command)
  5447. (eq org-cycle-subtree-status 'children)))
  5448. ;; We just showed the children, or no children are there,
  5449. ;; now show everything.
  5450. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5451. (outline-flag-region eoh eos nil)
  5452. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5453. (setq org-cycle-subtree-status 'subtree)
  5454. (run-hook-with-args 'org-cycle-hook 'subtree))
  5455. (t
  5456. ;; Default action: hide the subtree.
  5457. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5458. (outline-flag-region eoh eos t)
  5459. (message "FOLDED")
  5460. (setq org-cycle-subtree-status 'folded)
  5461. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5462. ;;;###autoload
  5463. (defun org-global-cycle (&optional arg)
  5464. "Cycle the global visibility. For details see `org-cycle'.
  5465. With \\[universal-argument] prefix arg, switch to startup visibility.
  5466. With a numeric prefix, show all headlines up to that level."
  5467. (interactive "P")
  5468. (let ((org-cycle-include-plain-lists
  5469. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5470. (cond
  5471. ((integerp arg)
  5472. (show-all)
  5473. (hide-sublevels arg)
  5474. (setq org-cycle-global-status 'contents))
  5475. ((equal arg '(4))
  5476. (org-set-startup-visibility)
  5477. (message "Startup visibility, plus VISIBILITY properties."))
  5478. (t
  5479. (org-cycle '(4))))))
  5480. (defun org-set-startup-visibility ()
  5481. "Set the visibility required by startup options and properties."
  5482. (cond
  5483. ((eq org-startup-folded t)
  5484. (org-cycle '(4)))
  5485. ((eq org-startup-folded 'content)
  5486. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5487. (org-cycle '(4)) (org-cycle '(4)))))
  5488. (unless (eq org-startup-folded 'showeverything)
  5489. (if org-hide-block-startup (org-hide-block-all))
  5490. (org-set-visibility-according-to-property 'no-cleanup)
  5491. (org-cycle-hide-archived-subtrees 'all)
  5492. (org-cycle-hide-drawers 'all)
  5493. (org-cycle-show-empty-lines t)))
  5494. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5495. "Switch subtree visibilities according to :VISIBILITY: property."
  5496. (interactive)
  5497. (let (org-show-entry-below state)
  5498. (save-excursion
  5499. (goto-char (point-min))
  5500. (while (re-search-forward
  5501. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5502. nil t)
  5503. (setq state (match-string 1))
  5504. (save-excursion
  5505. (org-back-to-heading t)
  5506. (hide-subtree)
  5507. (org-reveal)
  5508. (cond
  5509. ((equal state '("fold" "folded"))
  5510. (hide-subtree))
  5511. ((equal state "children")
  5512. (org-show-hidden-entry)
  5513. (show-children))
  5514. ((equal state "content")
  5515. (save-excursion
  5516. (save-restriction
  5517. (org-narrow-to-subtree)
  5518. (org-content))))
  5519. ((member state '("all" "showall"))
  5520. (show-subtree)))))
  5521. (unless no-cleanup
  5522. (org-cycle-hide-archived-subtrees 'all)
  5523. (org-cycle-hide-drawers 'all)
  5524. (org-cycle-show-empty-lines 'all)))))
  5525. (defun org-overview ()
  5526. "Switch to overview mode, showing only top-level headlines.
  5527. Really, this shows all headlines with level equal or greater than the level
  5528. of the first headline in the buffer. This is important, because if the
  5529. first headline is not level one, then (hide-sublevels 1) gives confusing
  5530. results."
  5531. (interactive)
  5532. (let ((level (save-excursion
  5533. (goto-char (point-min))
  5534. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5535. (progn
  5536. (goto-char (match-beginning 0))
  5537. (funcall outline-level))))))
  5538. (and level (hide-sublevels level))))
  5539. (defun org-content (&optional arg)
  5540. "Show all headlines in the buffer, like a table of contents.
  5541. With numerical argument N, show content up to level N."
  5542. (interactive "P")
  5543. (save-excursion
  5544. ;; Visit all headings and show their offspring
  5545. (and (integerp arg) (org-overview))
  5546. (goto-char (point-max))
  5547. (catch 'exit
  5548. (while (and (progn (condition-case nil
  5549. (outline-previous-visible-heading 1)
  5550. (error (goto-char (point-min))))
  5551. t)
  5552. (looking-at outline-regexp))
  5553. (if (integerp arg)
  5554. (show-children (1- arg))
  5555. (show-branches))
  5556. (if (bobp) (throw 'exit nil))))))
  5557. (defun org-optimize-window-after-visibility-change (state)
  5558. "Adjust the window after a change in outline visibility.
  5559. This function is the default value of the hook `org-cycle-hook'."
  5560. (when (get-buffer-window (current-buffer))
  5561. (cond
  5562. ((eq state 'content) nil)
  5563. ((eq state 'all) nil)
  5564. ((eq state 'folded) nil)
  5565. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5566. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5567. (defun org-remove-empty-overlays-at (pos)
  5568. "Remove outline overlays that do not contain non-white stuff."
  5569. (mapc
  5570. (lambda (o)
  5571. (and (eq 'outline (overlay-get o 'invisible))
  5572. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5573. (overlay-end o))))
  5574. (delete-overlay o)))
  5575. (overlays-at pos)))
  5576. (defun org-clean-visibility-after-subtree-move ()
  5577. "Fix visibility issues after moving a subtree."
  5578. ;; First, find a reasonable region to look at:
  5579. ;; Start two siblings above, end three below
  5580. (let* ((beg (save-excursion
  5581. (and (org-get-last-sibling)
  5582. (org-get-last-sibling))
  5583. (point)))
  5584. (end (save-excursion
  5585. (and (org-get-next-sibling)
  5586. (org-get-next-sibling)
  5587. (org-get-next-sibling))
  5588. (if (org-at-heading-p)
  5589. (point-at-eol)
  5590. (point))))
  5591. (level (looking-at "\\*+"))
  5592. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5593. (save-excursion
  5594. (save-restriction
  5595. (narrow-to-region beg end)
  5596. (when re
  5597. ;; Properly fold already folded siblings
  5598. (goto-char (point-min))
  5599. (while (re-search-forward re nil t)
  5600. (if (and (not (outline-invisible-p))
  5601. (save-excursion
  5602. (goto-char (point-at-eol)) (outline-invisible-p)))
  5603. (hide-entry))))
  5604. (org-cycle-show-empty-lines 'overview)
  5605. (org-cycle-hide-drawers 'overview)))))
  5606. (defun org-cycle-show-empty-lines (state)
  5607. "Show empty lines above all visible headlines.
  5608. The region to be covered depends on STATE when called through
  5609. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5610. entire buffer covered. Note that an empty line is only shown if there
  5611. are at least `org-cycle-separator-lines' empty lines before the headline."
  5612. (when (not (= org-cycle-separator-lines 0))
  5613. (save-excursion
  5614. (let* ((n (abs org-cycle-separator-lines))
  5615. (re (cond
  5616. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5617. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5618. (t (let ((ns (number-to-string (- n 2))))
  5619. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5620. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5621. beg end b e)
  5622. (cond
  5623. ((memq state '(overview contents t))
  5624. (setq beg (point-min) end (point-max)))
  5625. ((memq state '(children folded))
  5626. (setq beg (point) end (progn (org-end-of-subtree t t)
  5627. (beginning-of-line 2)
  5628. (point)))))
  5629. (when beg
  5630. (goto-char beg)
  5631. (while (re-search-forward re end t)
  5632. (unless (get-char-property (match-end 1) 'invisible)
  5633. (setq e (match-end 1))
  5634. (if (< org-cycle-separator-lines 0)
  5635. (setq b (save-excursion
  5636. (goto-char (match-beginning 0))
  5637. (org-back-over-empty-lines)
  5638. (if (save-excursion
  5639. (goto-char (max (point-min) (1- (point))))
  5640. (org-on-heading-p))
  5641. (1- (point))
  5642. (point))))
  5643. (setq b (match-beginning 1)))
  5644. (outline-flag-region b e nil)))))))
  5645. ;; Never hide empty lines at the end of the file.
  5646. (save-excursion
  5647. (goto-char (point-max))
  5648. (outline-previous-heading)
  5649. (outline-end-of-heading)
  5650. (if (and (looking-at "[ \t\n]+")
  5651. (= (match-end 0) (point-max)))
  5652. (outline-flag-region (point) (match-end 0) nil))))
  5653. (defun org-show-empty-lines-in-parent ()
  5654. "Move to the parent and re-show empty lines before visible headlines."
  5655. (save-excursion
  5656. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5657. (org-cycle-show-empty-lines context))))
  5658. (defun org-files-list ()
  5659. "Return `org-agenda-files' list, plus all open org-mode files.
  5660. This is useful for operations that need to scan all of a user's
  5661. open and agenda-wise Org files."
  5662. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5663. (dolist (buf (buffer-list))
  5664. (with-current-buffer buf
  5665. (if (and (org-mode-p) (buffer-file-name))
  5666. (let ((file (expand-file-name (buffer-file-name))))
  5667. (unless (member file files)
  5668. (push file files))))))
  5669. files))
  5670. (defsubst org-entry-beginning-position ()
  5671. "Return the beginning position of the current entry."
  5672. (save-excursion (outline-back-to-heading t) (point)))
  5673. (defsubst org-entry-end-position ()
  5674. "Return the end position of the current entry."
  5675. (save-excursion (outline-next-heading) (point)))
  5676. (defun org-cycle-hide-drawers (state)
  5677. "Re-hide all drawers after a visibility state change."
  5678. (when (and (org-mode-p)
  5679. (not (memq state '(overview folded contents))))
  5680. (save-excursion
  5681. (let* ((globalp (memq state '(contents all)))
  5682. (beg (if globalp (point-min) (point)))
  5683. (end (if globalp (point-max)
  5684. (if (eq state 'children)
  5685. (save-excursion (outline-next-heading) (point))
  5686. (org-end-of-subtree t)))))
  5687. (goto-char beg)
  5688. (while (re-search-forward org-drawer-regexp end t)
  5689. (org-flag-drawer t))))))
  5690. (defun org-flag-drawer (flag)
  5691. (save-excursion
  5692. (beginning-of-line 1)
  5693. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5694. (let ((b (match-end 0))
  5695. (outline-regexp org-outline-regexp))
  5696. (if (re-search-forward
  5697. "^[ \t]*:END:"
  5698. (save-excursion (outline-next-heading) (point)) t)
  5699. (outline-flag-region b (point-at-eol) flag)
  5700. (error ":END: line missing at position %s" b))))))
  5701. (defun org-subtree-end-visible-p ()
  5702. "Is the end of the current subtree visible?"
  5703. (pos-visible-in-window-p
  5704. (save-excursion (org-end-of-subtree t) (point))))
  5705. (defun org-first-headline-recenter (&optional N)
  5706. "Move cursor to the first headline and recenter the headline.
  5707. Optional argument N means put the headline into the Nth line of the window."
  5708. (goto-char (point-min))
  5709. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5710. (beginning-of-line)
  5711. (recenter (prefix-numeric-value N))))
  5712. ;;; Saving and restoring visibility
  5713. (defun org-outline-overlay-data (&optional use-markers)
  5714. "Return a list of the locations of all outline overlays.
  5715. These are overlays with the `invisible' property value `outline'.
  5716. The return value is a list of cons cells, with start and stop
  5717. positions for each overlay.
  5718. If USE-MARKERS is set, return the positions as markers."
  5719. (let (beg end)
  5720. (save-excursion
  5721. (save-restriction
  5722. (widen)
  5723. (delq nil
  5724. (mapcar (lambda (o)
  5725. (when (eq (overlay-get o 'invisible) 'outline)
  5726. (setq beg (overlay-start o)
  5727. end (overlay-end o))
  5728. (and beg end (> end beg)
  5729. (if use-markers
  5730. (cons (move-marker (make-marker) beg)
  5731. (move-marker (make-marker) end))
  5732. (cons beg end)))))
  5733. (overlays-in (point-min) (point-max))))))))
  5734. (defun org-set-outline-overlay-data (data)
  5735. "Create visibility overlays for all positions in DATA.
  5736. DATA should have been made by `org-outline-overlay-data'."
  5737. (let (o)
  5738. (save-excursion
  5739. (save-restriction
  5740. (widen)
  5741. (show-all)
  5742. (mapc (lambda (c)
  5743. (setq o (make-overlay (car c) (cdr c)))
  5744. (overlay-put o 'invisible 'outline))
  5745. data)))))
  5746. ;;; Folding of blocks
  5747. (defconst org-block-regexp
  5748. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  5749. "Regular expression for hiding blocks.")
  5750. (defvar org-hide-block-overlays nil
  5751. "Overlays hiding blocks.")
  5752. (make-variable-buffer-local 'org-hide-block-overlays)
  5753. (defun org-block-map (function &optional start end)
  5754. "Call FUNCTION at the head of all source blocks in the current buffer.
  5755. Optional arguments START and END can be used to limit the range."
  5756. (let ((start (or start (point-min)))
  5757. (end (or end (point-max))))
  5758. (save-excursion
  5759. (goto-char start)
  5760. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5761. (save-excursion
  5762. (save-match-data
  5763. (goto-char (match-beginning 0))
  5764. (funcall function)))))))
  5765. (defun org-hide-block-toggle-all ()
  5766. "Toggle the visibility of all blocks in the current buffer."
  5767. (org-block-map #'org-hide-block-toggle))
  5768. (defun org-hide-block-all ()
  5769. "Fold all blocks in the current buffer."
  5770. (interactive)
  5771. (org-show-block-all)
  5772. (org-block-map #'org-hide-block-toggle-maybe))
  5773. (defun org-show-block-all ()
  5774. "Unfold all blocks in the current buffer."
  5775. (interactive)
  5776. (mapc 'delete-overlay org-hide-block-overlays)
  5777. (setq org-hide-block-overlays nil))
  5778. (defun org-hide-block-toggle-maybe ()
  5779. "Toggle visibility of block at point."
  5780. (interactive)
  5781. (let ((case-fold-search t))
  5782. (if (save-excursion
  5783. (beginning-of-line 1)
  5784. (looking-at org-block-regexp))
  5785. (progn (org-hide-block-toggle)
  5786. t) ;; to signal that we took action
  5787. nil))) ;; to signal that we did not
  5788. (defun org-hide-block-toggle (&optional force)
  5789. "Toggle the visibility of the current block."
  5790. (interactive)
  5791. (save-excursion
  5792. (beginning-of-line)
  5793. (if (re-search-forward org-block-regexp nil t)
  5794. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5795. (end (match-end 0)) ;; end of entire body
  5796. ov)
  5797. (if (memq t (mapcar (lambda (overlay)
  5798. (eq (overlay-get overlay 'invisible)
  5799. 'org-hide-block))
  5800. (overlays-at start)))
  5801. (if (or (not force) (eq force 'off))
  5802. (mapc (lambda (ov)
  5803. (when (member ov org-hide-block-overlays)
  5804. (setq org-hide-block-overlays
  5805. (delq ov org-hide-block-overlays)))
  5806. (when (eq (overlay-get ov 'invisible)
  5807. 'org-hide-block)
  5808. (delete-overlay ov)))
  5809. (overlays-at start)))
  5810. (setq ov (make-overlay start end))
  5811. (overlay-put ov 'invisible 'org-hide-block)
  5812. ;; make the block accessible to isearch
  5813. (overlay-put
  5814. ov 'isearch-open-invisible
  5815. (lambda (ov)
  5816. (when (member ov org-hide-block-overlays)
  5817. (setq org-hide-block-overlays
  5818. (delq ov org-hide-block-overlays)))
  5819. (when (eq (overlay-get ov 'invisible)
  5820. 'org-hide-block)
  5821. (delete-overlay ov))))
  5822. (push ov org-hide-block-overlays)))
  5823. (error "Not looking at a source block"))))
  5824. ;; org-tab-after-check-for-cycling-hook
  5825. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5826. ;; Remove overlays when changing major mode
  5827. (add-hook 'org-mode-hook
  5828. (lambda () (org-add-hook 'change-major-mode-hook
  5829. 'org-show-block-all 'append 'local)))
  5830. ;;; Org-goto
  5831. (defvar org-goto-window-configuration nil)
  5832. (defvar org-goto-marker nil)
  5833. (defvar org-goto-map
  5834. (let ((map (make-sparse-keymap)))
  5835. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5836. (while (setq cmd (pop cmds))
  5837. (substitute-key-definition cmd cmd map global-map)))
  5838. (suppress-keymap map)
  5839. (org-defkey map "\C-m" 'org-goto-ret)
  5840. (org-defkey map [(return)] 'org-goto-ret)
  5841. (org-defkey map [(left)] 'org-goto-left)
  5842. (org-defkey map [(right)] 'org-goto-right)
  5843. (org-defkey map [(control ?g)] 'org-goto-quit)
  5844. (org-defkey map "\C-i" 'org-cycle)
  5845. (org-defkey map [(tab)] 'org-cycle)
  5846. (org-defkey map [(down)] 'outline-next-visible-heading)
  5847. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5848. (if org-goto-auto-isearch
  5849. (if (fboundp 'define-key-after)
  5850. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5851. nil)
  5852. (org-defkey map "q" 'org-goto-quit)
  5853. (org-defkey map "n" 'outline-next-visible-heading)
  5854. (org-defkey map "p" 'outline-previous-visible-heading)
  5855. (org-defkey map "f" 'outline-forward-same-level)
  5856. (org-defkey map "b" 'outline-backward-same-level)
  5857. (org-defkey map "u" 'outline-up-heading))
  5858. (org-defkey map "/" 'org-occur)
  5859. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5860. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5861. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5862. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5863. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5864. map))
  5865. (defconst org-goto-help
  5866. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5867. RET=jump to location [Q]uit and return to previous location
  5868. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5869. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5870. ;; FIXME: Docstring does not mention both interfaces
  5871. (defun org-goto (&optional alternative-interface)
  5872. "Look up a different location in the current file, keeping current visibility.
  5873. When you want look-up or go to a different location in a document, the
  5874. fastest way is often to fold the entire buffer and then dive into the tree.
  5875. This method has the disadvantage, that the previous location will be folded,
  5876. which may not be what you want.
  5877. This command works around this by showing a copy of the current buffer
  5878. in an indirect buffer, in overview mode. You can dive into the tree in
  5879. that copy, use org-occur and incremental search to find a location.
  5880. When pressing RET or `Q', the command returns to the original buffer in
  5881. which the visibility is still unchanged. After RET is will also jump to
  5882. the location selected in the indirect buffer and expose the
  5883. the headline hierarchy above."
  5884. (interactive "P")
  5885. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5886. (org-refile-use-outline-path t)
  5887. (org-refile-target-verify-function nil)
  5888. (interface
  5889. (if (not alternative-interface)
  5890. org-goto-interface
  5891. (if (eq org-goto-interface 'outline)
  5892. 'outline-path-completion
  5893. 'outline)))
  5894. (org-goto-start-pos (point))
  5895. (selected-point
  5896. (if (eq interface 'outline)
  5897. (car (org-get-location (current-buffer) org-goto-help))
  5898. (let ((pa (org-refile-get-location "Goto")))
  5899. (org-refile-check-position pa)
  5900. (nth 3 pa)))))
  5901. (if selected-point
  5902. (progn
  5903. (org-mark-ring-push org-goto-start-pos)
  5904. (goto-char selected-point)
  5905. (if (or (outline-invisible-p) (org-invisible-p2))
  5906. (org-show-context 'org-goto)))
  5907. (message "Quit"))))
  5908. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5909. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5910. (defvar org-goto-local-auto-isearch-map) ; defined below
  5911. (defun org-get-location (buf help)
  5912. "Let the user select a location in the Org-mode buffer BUF.
  5913. This function uses a recursive edit. It returns the selected position
  5914. or nil."
  5915. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5916. (isearch-hide-immediately nil)
  5917. (isearch-search-fun-function
  5918. (lambda () 'org-goto-local-search-headings))
  5919. (org-goto-selected-point org-goto-exit-command)
  5920. (pop-up-frames nil)
  5921. (special-display-buffer-names nil)
  5922. (special-display-regexps nil)
  5923. (special-display-function nil))
  5924. (save-excursion
  5925. (save-window-excursion
  5926. (delete-other-windows)
  5927. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5928. (switch-to-buffer
  5929. (condition-case nil
  5930. (make-indirect-buffer (current-buffer) "*org-goto*")
  5931. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5932. (with-output-to-temp-buffer "*Help*"
  5933. (princ help))
  5934. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5935. (setq buffer-read-only nil)
  5936. (let ((org-startup-truncated t)
  5937. (org-startup-folded nil)
  5938. (org-startup-align-all-tables nil))
  5939. (org-mode)
  5940. (org-overview))
  5941. (setq buffer-read-only t)
  5942. (if (and (boundp 'org-goto-start-pos)
  5943. (integer-or-marker-p org-goto-start-pos))
  5944. (let ((org-show-hierarchy-above t)
  5945. (org-show-siblings t)
  5946. (org-show-following-heading t))
  5947. (goto-char org-goto-start-pos)
  5948. (and (outline-invisible-p) (org-show-context)))
  5949. (goto-char (point-min)))
  5950. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5951. (message "Select location and press RET")
  5952. (use-local-map org-goto-map)
  5953. (recursive-edit)
  5954. ))
  5955. (kill-buffer "*org-goto*")
  5956. (cons org-goto-selected-point org-goto-exit-command)))
  5957. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5958. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5959. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5960. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5961. (defun org-goto-local-search-headings (string bound noerror)
  5962. "Search and make sure that any matches are in headlines."
  5963. (catch 'return
  5964. (while (if isearch-forward
  5965. (search-forward string bound noerror)
  5966. (search-backward string bound noerror))
  5967. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5968. (and (member :headline context)
  5969. (not (member :tags context))))
  5970. (throw 'return (point))))))
  5971. (defun org-goto-local-auto-isearch ()
  5972. "Start isearch."
  5973. (interactive)
  5974. (goto-char (point-min))
  5975. (let ((keys (this-command-keys)))
  5976. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5977. (isearch-mode t)
  5978. (isearch-process-search-char (string-to-char keys)))))
  5979. (defun org-goto-ret (&optional arg)
  5980. "Finish `org-goto' by going to the new location."
  5981. (interactive "P")
  5982. (setq org-goto-selected-point (point)
  5983. org-goto-exit-command 'return)
  5984. (throw 'exit nil))
  5985. (defun org-goto-left ()
  5986. "Finish `org-goto' by going to the new location."
  5987. (interactive)
  5988. (if (org-on-heading-p)
  5989. (progn
  5990. (beginning-of-line 1)
  5991. (setq org-goto-selected-point (point)
  5992. org-goto-exit-command 'left)
  5993. (throw 'exit nil))
  5994. (error "Not on a heading")))
  5995. (defun org-goto-right ()
  5996. "Finish `org-goto' by going to the new location."
  5997. (interactive)
  5998. (if (org-on-heading-p)
  5999. (progn
  6000. (setq org-goto-selected-point (point)
  6001. org-goto-exit-command 'right)
  6002. (throw 'exit nil))
  6003. (error "Not on a heading")))
  6004. (defun org-goto-quit ()
  6005. "Finish `org-goto' without cursor motion."
  6006. (interactive)
  6007. (setq org-goto-selected-point nil)
  6008. (setq org-goto-exit-command 'quit)
  6009. (throw 'exit nil))
  6010. ;;; Indirect buffer display of subtrees
  6011. (defvar org-indirect-dedicated-frame nil
  6012. "This is the frame being used for indirect tree display.")
  6013. (defvar org-last-indirect-buffer nil)
  6014. (defun org-tree-to-indirect-buffer (&optional arg)
  6015. "Create indirect buffer and narrow it to current subtree.
  6016. With numerical prefix ARG, go up to this level and then take that tree.
  6017. If ARG is negative, go up that many levels.
  6018. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6019. indirect buffer previously made with this command, to avoid proliferation of
  6020. indirect buffers. However, when you call the command with a \
  6021. \\[universal-argument] prefix, or
  6022. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6023. is kept so that you can work with several indirect buffers at the same time.
  6024. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6025. \\[universal-argument] prefix also
  6026. requests that a new frame be made for the new buffer, so that the dedicated
  6027. frame is not changed."
  6028. (interactive "P")
  6029. (let ((cbuf (current-buffer))
  6030. (cwin (selected-window))
  6031. (pos (point))
  6032. beg end level heading ibuf)
  6033. (save-excursion
  6034. (org-back-to-heading t)
  6035. (when (numberp arg)
  6036. (setq level (org-outline-level))
  6037. (if (< arg 0) (setq arg (+ level arg)))
  6038. (while (> (setq level (org-outline-level)) arg)
  6039. (outline-up-heading 1 t)))
  6040. (setq beg (point)
  6041. heading (org-get-heading))
  6042. (org-end-of-subtree t t)
  6043. (if (org-on-heading-p) (backward-char 1))
  6044. (setq end (point)))
  6045. (if (and (buffer-live-p org-last-indirect-buffer)
  6046. (not (eq org-indirect-buffer-display 'new-frame))
  6047. (not arg))
  6048. (kill-buffer org-last-indirect-buffer))
  6049. (setq ibuf (org-get-indirect-buffer cbuf)
  6050. org-last-indirect-buffer ibuf)
  6051. (cond
  6052. ((or (eq org-indirect-buffer-display 'new-frame)
  6053. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6054. (select-frame (make-frame))
  6055. (delete-other-windows)
  6056. (switch-to-buffer ibuf)
  6057. (org-set-frame-title heading))
  6058. ((eq org-indirect-buffer-display 'dedicated-frame)
  6059. (raise-frame
  6060. (select-frame (or (and org-indirect-dedicated-frame
  6061. (frame-live-p org-indirect-dedicated-frame)
  6062. org-indirect-dedicated-frame)
  6063. (setq org-indirect-dedicated-frame (make-frame)))))
  6064. (delete-other-windows)
  6065. (switch-to-buffer ibuf)
  6066. (org-set-frame-title (concat "Indirect: " heading)))
  6067. ((eq org-indirect-buffer-display 'current-window)
  6068. (switch-to-buffer ibuf))
  6069. ((eq org-indirect-buffer-display 'other-window)
  6070. (pop-to-buffer ibuf))
  6071. (t (error "Invalid value")))
  6072. (if (featurep 'xemacs)
  6073. (save-excursion (org-mode) (turn-on-font-lock)))
  6074. (narrow-to-region beg end)
  6075. (show-all)
  6076. (goto-char pos)
  6077. (and (window-live-p cwin) (select-window cwin))))
  6078. (defun org-get-indirect-buffer (&optional buffer)
  6079. (setq buffer (or buffer (current-buffer)))
  6080. (let ((n 1) (base (buffer-name buffer)) bname)
  6081. (while (buffer-live-p
  6082. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6083. (setq n (1+ n)))
  6084. (condition-case nil
  6085. (make-indirect-buffer buffer bname 'clone)
  6086. (error (make-indirect-buffer buffer bname)))))
  6087. (defun org-set-frame-title (title)
  6088. "Set the title of the current frame to the string TITLE."
  6089. ;; FIXME: how to name a single frame in XEmacs???
  6090. (unless (featurep 'xemacs)
  6091. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6092. ;;;; Structure editing
  6093. ;;; Inserting headlines
  6094. (defun org-previous-line-empty-p ()
  6095. (save-excursion
  6096. (and (not (bobp))
  6097. (or (beginning-of-line 0) t)
  6098. (save-match-data
  6099. (looking-at "[ \t]*$")))))
  6100. (defun org-insert-heading (&optional force-heading invisible-ok)
  6101. "Insert a new heading or item with same depth at point.
  6102. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6103. If point is at the beginning of a headline, insert a sibling before the
  6104. current headline. If point is not at the beginning, split the line,
  6105. create the new headline with the text in the current line after point
  6106. \(but see also the variable `org-M-RET-may-split-line').
  6107. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6108. This is important for non-interactive uses of the command."
  6109. (interactive "P")
  6110. (if (or (= (buffer-size) 0)
  6111. (and (not (save-excursion
  6112. (and (ignore-errors (org-back-to-heading invisible-ok))
  6113. (org-on-heading-p))))
  6114. (not (org-in-item-p))))
  6115. (progn
  6116. (insert "\n* ")
  6117. (run-hooks 'org-insert-heading-hook))
  6118. (when (or force-heading (not (org-insert-item)))
  6119. (let* ((empty-line-p nil)
  6120. (level nil)
  6121. (on-heading (org-on-heading-p))
  6122. (head (save-excursion
  6123. (condition-case nil
  6124. (progn
  6125. (org-back-to-heading invisible-ok)
  6126. (when (and (not on-heading)
  6127. (featurep 'org-inlinetask)
  6128. (integerp org-inlinetask-min-level)
  6129. (>= (length (match-string 0))
  6130. org-inlinetask-min-level))
  6131. ;; Find a heading level before the inline task
  6132. (while (and (setq level (org-up-heading-safe))
  6133. (>= level org-inlinetask-min-level)))
  6134. (if (org-on-heading-p)
  6135. (org-back-to-heading invisible-ok)
  6136. (error "This should not happen")))
  6137. (setq empty-line-p (org-previous-line-empty-p))
  6138. (match-string 0))
  6139. (error "*"))))
  6140. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6141. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6142. pos hide-previous previous-pos)
  6143. (cond
  6144. ((and (org-on-heading-p) (bolp)
  6145. (or (bobp)
  6146. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6147. ;; insert before the current line
  6148. (open-line (if blank 2 1)))
  6149. ((and (bolp)
  6150. (not org-insert-heading-respect-content)
  6151. (or (bobp)
  6152. (save-excursion
  6153. (backward-char 1) (not (outline-invisible-p)))))
  6154. ;; insert right here
  6155. nil)
  6156. (t
  6157. ;; somewhere in the line
  6158. (save-excursion
  6159. (setq previous-pos (point-at-bol))
  6160. (end-of-line)
  6161. (setq hide-previous (outline-invisible-p)))
  6162. (and org-insert-heading-respect-content (org-show-subtree))
  6163. (let ((split
  6164. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6165. (save-excursion
  6166. (let ((p (point)))
  6167. (goto-char (point-at-bol))
  6168. (and (looking-at org-complex-heading-regexp)
  6169. (> p (match-beginning 4)))))))
  6170. tags pos)
  6171. (cond
  6172. (org-insert-heading-respect-content
  6173. (org-end-of-subtree nil t)
  6174. (when (featurep 'org-inlinetask)
  6175. (while (and (not (eobp))
  6176. (looking-at "\\(\\*+\\)[ \t]+")
  6177. (>= (length (match-string 1))
  6178. org-inlinetask-min-level))
  6179. (org-end-of-subtree nil t)))
  6180. (or (bolp) (newline))
  6181. (or (org-previous-line-empty-p)
  6182. (and blank (newline)))
  6183. (open-line 1))
  6184. ((org-on-heading-p)
  6185. (when hide-previous
  6186. (show-children)
  6187. (org-show-entry))
  6188. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6189. (setq tags (and (match-end 2) (match-string 2)))
  6190. (and (match-end 1)
  6191. (delete-region (match-beginning 1) (match-end 1)))
  6192. (setq pos (point-at-bol))
  6193. (or split (end-of-line 1))
  6194. (delete-horizontal-space)
  6195. (if (string-match "\\`\\*+\\'"
  6196. (buffer-substring (point-at-bol) (point)))
  6197. (insert " "))
  6198. (newline (if blank 2 1))
  6199. (when tags
  6200. (save-excursion
  6201. (goto-char pos)
  6202. (end-of-line 1)
  6203. (insert " " tags)
  6204. (org-set-tags nil 'align))))
  6205. (t
  6206. (or split (end-of-line 1))
  6207. (newline (if blank 2 1)))))))
  6208. (insert head) (just-one-space)
  6209. (setq pos (point))
  6210. (end-of-line 1)
  6211. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6212. (when (and org-insert-heading-respect-content hide-previous)
  6213. (save-excursion
  6214. (goto-char previous-pos)
  6215. (hide-subtree)))
  6216. (run-hooks 'org-insert-heading-hook)))))
  6217. (defun org-get-heading (&optional no-tags)
  6218. "Return the heading of the current entry, without the stars."
  6219. (save-excursion
  6220. (org-back-to-heading t)
  6221. (if (looking-at
  6222. (if no-tags
  6223. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6224. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6225. (match-string 1) "")))
  6226. (defun org-heading-components ()
  6227. "Return the components of the current heading.
  6228. This is a list with the following elements:
  6229. - the level as an integer
  6230. - the reduced level, different if `org-odd-levels-only' is set.
  6231. - the TODO keyword, or nil
  6232. - the priority character, like ?A, or nil if no priority is given
  6233. - the headline text itself, or the tags string if no headline text
  6234. - the tags string, or nil."
  6235. (save-excursion
  6236. (org-back-to-heading t)
  6237. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6238. (list (length (match-string 1))
  6239. (org-reduced-level (length (match-string 1)))
  6240. (org-match-string-no-properties 2)
  6241. (and (match-end 3) (aref (match-string 3) 2))
  6242. (org-match-string-no-properties 4)
  6243. (org-match-string-no-properties 5)))))
  6244. (defun org-get-entry ()
  6245. "Get the entry text, after heading, entire subtree."
  6246. (save-excursion
  6247. (org-back-to-heading t)
  6248. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6249. (defun org-insert-heading-after-current ()
  6250. "Insert a new heading with same level as current, after current subtree."
  6251. (interactive)
  6252. (org-back-to-heading)
  6253. (org-insert-heading)
  6254. (org-move-subtree-down)
  6255. (end-of-line 1))
  6256. (defun org-insert-heading-respect-content ()
  6257. (interactive)
  6258. (let ((org-insert-heading-respect-content t))
  6259. (org-insert-heading t)))
  6260. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6261. (interactive "P")
  6262. (let ((org-insert-heading-respect-content t))
  6263. (org-insert-todo-heading force-state t)))
  6264. (defun org-insert-todo-heading (arg &optional force-heading)
  6265. "Insert a new heading with the same level and TODO state as current heading.
  6266. If the heading has no TODO state, or if the state is DONE, use the first
  6267. state (TODO by default). Also with prefix arg, force first state."
  6268. (interactive "P")
  6269. (when (or force-heading (not (org-insert-item 'checkbox)))
  6270. (org-insert-heading force-heading)
  6271. (save-excursion
  6272. (org-back-to-heading)
  6273. (outline-previous-heading)
  6274. (looking-at org-todo-line-regexp))
  6275. (let*
  6276. ((new-mark-x
  6277. (if (or arg
  6278. (not (match-beginning 2))
  6279. (member (match-string 2) org-done-keywords))
  6280. (car org-todo-keywords-1)
  6281. (match-string 2)))
  6282. (new-mark
  6283. (or
  6284. (run-hook-with-args-until-success
  6285. 'org-todo-get-default-hook new-mark-x nil)
  6286. new-mark-x)))
  6287. (beginning-of-line 1)
  6288. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6289. (if org-treat-insert-todo-heading-as-state-change
  6290. (org-todo new-mark)
  6291. (insert new-mark " "))))
  6292. (when org-provide-todo-statistics
  6293. (org-update-parent-todo-statistics))))
  6294. (defun org-insert-subheading (arg)
  6295. "Insert a new subheading and demote it.
  6296. Works for outline headings and for plain lists alike."
  6297. (interactive "P")
  6298. (org-insert-heading arg)
  6299. (cond
  6300. ((org-on-heading-p) (org-do-demote))
  6301. ((org-at-item-p) (org-indent-item))))
  6302. (defun org-insert-todo-subheading (arg)
  6303. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6304. Works for outline headings and for plain lists alike."
  6305. (interactive "P")
  6306. (org-insert-todo-heading arg)
  6307. (cond
  6308. ((org-on-heading-p) (org-do-demote))
  6309. ((org-at-item-p) (org-indent-item))))
  6310. ;;; Promotion and Demotion
  6311. (defvar org-after-demote-entry-hook nil
  6312. "Hook run after an entry has been demoted.
  6313. The cursor will be at the beginning of the entry.
  6314. When a subtree is being demoted, the hook will be called for each node.")
  6315. (defvar org-after-promote-entry-hook nil
  6316. "Hook run after an entry has been promoted.
  6317. The cursor will be at the beginning of the entry.
  6318. When a subtree is being promoted, the hook will be called for each node.")
  6319. (defun org-promote-subtree ()
  6320. "Promote the entire subtree.
  6321. See also `org-promote'."
  6322. (interactive)
  6323. (save-excursion
  6324. (org-with-limited-levels (org-map-tree 'org-promote)))
  6325. (org-fix-position-after-promote))
  6326. (defun org-demote-subtree ()
  6327. "Demote the entire subtree. See `org-demote'.
  6328. See also `org-promote'."
  6329. (interactive)
  6330. (save-excursion
  6331. (org-with-limited-levels (org-map-tree 'org-demote)))
  6332. (org-fix-position-after-promote))
  6333. (defun org-do-promote ()
  6334. "Promote the current heading higher up the tree.
  6335. If the region is active in `transient-mark-mode', promote all headings
  6336. in the region."
  6337. (interactive)
  6338. (save-excursion
  6339. (if (org-region-active-p)
  6340. (org-map-region 'org-promote (region-beginning) (region-end))
  6341. (org-promote)))
  6342. (org-fix-position-after-promote))
  6343. (defun org-do-demote ()
  6344. "Demote the current heading lower down the tree.
  6345. If the region is active in `transient-mark-mode', demote all headings
  6346. in the region."
  6347. (interactive)
  6348. (save-excursion
  6349. (if (org-region-active-p)
  6350. (org-map-region 'org-demote (region-beginning) (region-end))
  6351. (org-demote)))
  6352. (org-fix-position-after-promote))
  6353. (defun org-fix-position-after-promote ()
  6354. "Make sure that after pro/demotion cursor position is right."
  6355. (let ((pos (point)))
  6356. (when (save-excursion
  6357. (beginning-of-line 1)
  6358. (looking-at org-todo-line-regexp)
  6359. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6360. (cond ((eobp) (insert " "))
  6361. ((eolp) (insert " "))
  6362. ((equal (char-after) ?\ ) (forward-char 1))))))
  6363. (defun org-current-level ()
  6364. "Return the level of the current entry, or nil if before the first headline.
  6365. The level is the number of stars at the beginning of the headline."
  6366. (save-excursion
  6367. (org-with-limited-levels
  6368. (ignore-errors
  6369. (org-back-to-heading t)
  6370. (funcall outline-level)))))
  6371. (defun org-get-previous-line-level ()
  6372. "Return the outline depth of the last headline before the current line.
  6373. Returns 0 for the first headline in the buffer, and nil if before the
  6374. first headline."
  6375. (let ((current-level (org-current-level))
  6376. (prev-level (when (> (line-number-at-pos) 1)
  6377. (save-excursion
  6378. (beginning-of-line 0)
  6379. (org-current-level)))))
  6380. (cond ((null current-level) nil) ; Before first headline
  6381. ((null prev-level) 0) ; At first headline
  6382. (prev-level))))
  6383. (defun org-reduced-level (l)
  6384. "Compute the effective level of a heading.
  6385. This takes into account the setting of `org-odd-levels-only'."
  6386. (cond
  6387. ((zerop l) 0)
  6388. (org-odd-levels-only (1+ (floor (/ l 2))))
  6389. (t l)))
  6390. (defun org-level-increment ()
  6391. "Return the number of stars that will be added or removed at a
  6392. time to headlines when structure editing, based on the value of
  6393. `org-odd-levels-only'."
  6394. (if org-odd-levels-only 2 1))
  6395. (defun org-get-valid-level (level &optional change)
  6396. "Rectify a level change under the influence of `org-odd-levels-only'
  6397. LEVEL is a current level, CHANGE is by how much the level should be
  6398. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6399. even level numbers will become the next higher odd number."
  6400. (if org-odd-levels-only
  6401. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6402. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6403. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6404. (max 1 (+ level (or change 0)))))
  6405. (if (boundp 'define-obsolete-function-alias)
  6406. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6407. (define-obsolete-function-alias 'org-get-legal-level
  6408. 'org-get-valid-level)
  6409. (define-obsolete-function-alias 'org-get-legal-level
  6410. 'org-get-valid-level "23.1")))
  6411. (defun org-promote ()
  6412. "Promote the current heading higher up the tree.
  6413. If the region is active in `transient-mark-mode', promote all headings
  6414. in the region."
  6415. (org-back-to-heading t)
  6416. (let* ((level (save-match-data (funcall outline-level)))
  6417. (after-change-functions (remove 'flyspell-after-change-function
  6418. after-change-functions))
  6419. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6420. (diff (abs (- level (length up-head) -1))))
  6421. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6422. (replace-match up-head nil t)
  6423. ;; Fixup tag positioning
  6424. (and org-auto-align-tags (org-set-tags nil t))
  6425. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6426. (run-hooks 'org-after-promote-entry-hook)))
  6427. (defun org-demote ()
  6428. "Demote the current heading lower down the tree.
  6429. If the region is active in `transient-mark-mode', demote all headings
  6430. in the region."
  6431. (org-back-to-heading t)
  6432. (let* ((level (save-match-data (funcall outline-level)))
  6433. (after-change-functions (remove 'flyspell-after-change-function
  6434. after-change-functions))
  6435. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6436. (diff (abs (- level (length down-head) -1))))
  6437. (replace-match down-head nil t)
  6438. ;; Fixup tag positioning
  6439. (and org-auto-align-tags (org-set-tags nil t))
  6440. (if org-adapt-indentation (org-fixup-indentation diff))
  6441. (run-hooks 'org-after-demote-entry-hook)))
  6442. (defun org-cycle-level ()
  6443. "Cycle the level of an empty headline through possible states.
  6444. This goes first to child, then to parent, level, then up the hierarchy.
  6445. After top level, it switches back to sibling level."
  6446. (interactive)
  6447. (let ((org-adapt-indentation nil))
  6448. (when (org-point-at-end-of-empty-headline)
  6449. (setq this-command 'org-cycle-level) ; Only needed for caching
  6450. (let ((cur-level (org-current-level))
  6451. (prev-level (org-get-previous-line-level)))
  6452. (cond
  6453. ;; If first headline in file, promote to top-level.
  6454. ((= prev-level 0)
  6455. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6456. do (org-do-promote)))
  6457. ;; If same level as prev, demote one.
  6458. ((= prev-level cur-level)
  6459. (org-do-demote))
  6460. ;; If parent is top-level, promote to top level if not already.
  6461. ((= prev-level 1)
  6462. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6463. do (org-do-promote)))
  6464. ;; If top-level, return to prev-level.
  6465. ((= cur-level 1)
  6466. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6467. do (org-do-demote)))
  6468. ;; If less than prev-level, promote one.
  6469. ((< cur-level prev-level)
  6470. (org-do-promote))
  6471. ;; If deeper than prev-level, promote until higher than
  6472. ;; prev-level.
  6473. ((> cur-level prev-level)
  6474. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6475. do (org-do-promote))))
  6476. t))))
  6477. (defun org-map-tree (fun)
  6478. "Call FUN for every heading underneath the current one."
  6479. (org-back-to-heading)
  6480. (let ((level (funcall outline-level)))
  6481. (save-excursion
  6482. (funcall fun)
  6483. (while (and (progn
  6484. (outline-next-heading)
  6485. (> (funcall outline-level) level))
  6486. (not (eobp)))
  6487. (funcall fun)))))
  6488. (defun org-map-region (fun beg end)
  6489. "Call FUN for every heading between BEG and END."
  6490. (let ((org-ignore-region t))
  6491. (save-excursion
  6492. (setq end (copy-marker end))
  6493. (goto-char beg)
  6494. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6495. (< (point) end))
  6496. (funcall fun))
  6497. (while (and (progn
  6498. (outline-next-heading)
  6499. (< (point) end))
  6500. (not (eobp)))
  6501. (funcall fun)))))
  6502. (defun org-fixup-indentation (diff)
  6503. "Change the indentation in the current entry by DIFF.
  6504. However, if any line in the current entry has no indentation, or if it
  6505. would end up with no indentation after the change, nothing at all is done."
  6506. (save-excursion
  6507. (let ((end (save-excursion (outline-next-heading)
  6508. (point-marker)))
  6509. (prohibit (if (> diff 0)
  6510. "^\\S-"
  6511. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6512. col)
  6513. (unless (save-excursion (end-of-line 1)
  6514. (re-search-forward prohibit end t))
  6515. (while (and (< (point) end)
  6516. (re-search-forward "^[ \t]+" end t))
  6517. (goto-char (match-end 0))
  6518. (setq col (current-column))
  6519. (if (< diff 0) (replace-match ""))
  6520. (org-indent-to-column (+ diff col))))
  6521. (move-marker end nil))))
  6522. (defun org-convert-to-odd-levels ()
  6523. "Convert an org-mode file with all levels allowed to one with odd levels.
  6524. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6525. level 5 etc."
  6526. (interactive)
  6527. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6528. (let ((outline-regexp org-outline-regexp)
  6529. (outline-level 'org-outline-level)
  6530. (org-odd-levels-only nil) n)
  6531. (save-excursion
  6532. (goto-char (point-min))
  6533. (while (re-search-forward "^\\*\\*+ " nil t)
  6534. (setq n (- (length (match-string 0)) 2))
  6535. (while (>= (setq n (1- n)) 0)
  6536. (org-demote))
  6537. (end-of-line 1))))))
  6538. (defun org-convert-to-oddeven-levels ()
  6539. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6540. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6541. file contains a section with an even level, conversion would
  6542. destroy the structure of the file. An error is signaled in this
  6543. case."
  6544. (interactive)
  6545. (goto-char (point-min))
  6546. ;; First check if there are no even levels
  6547. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6548. (org-show-context t)
  6549. (error "Not all levels are odd in this file. Conversion not possible"))
  6550. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6551. (let ((outline-regexp org-outline-regexp)
  6552. (outline-level 'org-outline-level)
  6553. (org-odd-levels-only nil) n)
  6554. (save-excursion
  6555. (goto-char (point-min))
  6556. (while (re-search-forward "^\\*\\*+ " nil t)
  6557. (setq n (/ (1- (length (match-string 0))) 2))
  6558. (while (>= (setq n (1- n)) 0)
  6559. (org-promote))
  6560. (end-of-line 1))))))
  6561. (defun org-tr-level (n)
  6562. "Make N odd if required."
  6563. (if org-odd-levels-only (1+ (/ n 2)) n))
  6564. ;;; Vertical tree motion, cutting and pasting of subtrees
  6565. (defun org-move-subtree-up (&optional arg)
  6566. "Move the current subtree up past ARG headlines of the same level."
  6567. (interactive "p")
  6568. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6569. (defun org-move-subtree-down (&optional arg)
  6570. "Move the current subtree down past ARG headlines of the same level."
  6571. (interactive "p")
  6572. (setq arg (prefix-numeric-value arg))
  6573. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6574. 'org-get-last-sibling))
  6575. (ins-point (make-marker))
  6576. (cnt (abs arg))
  6577. (col (current-column))
  6578. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6579. ;; Select the tree
  6580. (org-back-to-heading)
  6581. (setq beg0 (point))
  6582. (save-excursion
  6583. (setq ne-beg (org-back-over-empty-lines))
  6584. (setq beg (point)))
  6585. (save-match-data
  6586. (save-excursion (outline-end-of-heading)
  6587. (setq folded (outline-invisible-p)))
  6588. (outline-end-of-subtree))
  6589. (outline-next-heading)
  6590. (setq ne-end (org-back-over-empty-lines))
  6591. (setq end (point))
  6592. (goto-char beg0)
  6593. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6594. ;; include less whitespace
  6595. (save-excursion
  6596. (goto-char beg)
  6597. (forward-line (- ne-beg ne-end))
  6598. (setq beg (point))))
  6599. ;; Find insertion point, with error handling
  6600. (while (> cnt 0)
  6601. (or (and (funcall movfunc) (looking-at outline-regexp))
  6602. (progn (goto-char beg0)
  6603. (error "Cannot move past superior level or buffer limit")))
  6604. (setq cnt (1- cnt)))
  6605. (if (> arg 0)
  6606. ;; Moving forward - still need to move over subtree
  6607. (progn (org-end-of-subtree t t)
  6608. (save-excursion
  6609. (org-back-over-empty-lines)
  6610. (or (bolp) (newline)))))
  6611. (setq ne-ins (org-back-over-empty-lines))
  6612. (move-marker ins-point (point))
  6613. (setq txt (buffer-substring beg end))
  6614. (org-save-markers-in-region beg end)
  6615. (delete-region beg end)
  6616. (org-remove-empty-overlays-at beg)
  6617. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6618. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6619. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6620. (let ((bbb (point)))
  6621. (insert-before-markers txt)
  6622. (org-reinstall-markers-in-region bbb)
  6623. (move-marker ins-point bbb))
  6624. (or (bolp) (insert "\n"))
  6625. (setq ins-end (point))
  6626. (goto-char ins-point)
  6627. (org-skip-whitespace)
  6628. (when (and (< arg 0)
  6629. (org-first-sibling-p)
  6630. (> ne-ins ne-beg))
  6631. ;; Move whitespace back to beginning
  6632. (save-excursion
  6633. (goto-char ins-end)
  6634. (let ((kill-whole-line t))
  6635. (kill-line (- ne-ins ne-beg)) (point)))
  6636. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6637. (move-marker ins-point nil)
  6638. (if folded
  6639. (hide-subtree)
  6640. (org-show-entry)
  6641. (show-children)
  6642. (org-cycle-hide-drawers 'children))
  6643. (org-clean-visibility-after-subtree-move)
  6644. ;; move back to the initial column we were at
  6645. (move-to-column col)))
  6646. (defvar org-subtree-clip ""
  6647. "Clipboard for cut and paste of subtrees.
  6648. This is actually only a copy of the kill, because we use the normal kill
  6649. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6650. (defvar org-subtree-clip-folded nil
  6651. "Was the last copied subtree folded?
  6652. This is used to fold the tree back after pasting.")
  6653. (defun org-cut-subtree (&optional n)
  6654. "Cut the current subtree into the clipboard.
  6655. With prefix arg N, cut this many sequential subtrees.
  6656. This is a short-hand for marking the subtree and then cutting it."
  6657. (interactive "p")
  6658. (org-copy-subtree n 'cut))
  6659. (defun org-copy-subtree (&optional n cut force-store-markers)
  6660. "Cut the current subtree into the clipboard.
  6661. With prefix arg N, cut this many sequential subtrees.
  6662. This is a short-hand for marking the subtree and then copying it.
  6663. If CUT is non-nil, actually cut the subtree.
  6664. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6665. of some markers in the region, even if CUT is non-nil. This is
  6666. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6667. (interactive "p")
  6668. (let (beg end folded (beg0 (point)))
  6669. (if (called-interactively-p 'any)
  6670. (org-back-to-heading nil) ; take what looks like a subtree
  6671. (org-back-to-heading t)) ; take what is really there
  6672. (org-back-over-empty-lines)
  6673. (setq beg (point))
  6674. (skip-chars-forward " \t\r\n")
  6675. (save-match-data
  6676. (save-excursion (outline-end-of-heading)
  6677. (setq folded (outline-invisible-p)))
  6678. (condition-case nil
  6679. (org-forward-same-level (1- n) t)
  6680. (error nil))
  6681. (org-end-of-subtree t t))
  6682. (org-back-over-empty-lines)
  6683. (setq end (point))
  6684. (goto-char beg0)
  6685. (when (> end beg)
  6686. (setq org-subtree-clip-folded folded)
  6687. (when (or cut force-store-markers)
  6688. (org-save-markers-in-region beg end))
  6689. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6690. (setq org-subtree-clip (current-kill 0))
  6691. (message "%s: Subtree(s) with %d characters"
  6692. (if cut "Cut" "Copied")
  6693. (length org-subtree-clip)))))
  6694. (defun org-paste-subtree (&optional level tree for-yank)
  6695. "Paste the clipboard as a subtree, with modification of headline level.
  6696. The entire subtree is promoted or demoted in order to match a new headline
  6697. level.
  6698. If the cursor is at the beginning of a headline, the same level as
  6699. that headline is used to paste the tree
  6700. If not, the new level is derived from the *visible* headings
  6701. before and after the insertion point, and taken to be the inferior headline
  6702. level of the two. So if the previous visible heading is level 3 and the
  6703. next is level 4 (or vice versa), level 4 will be used for insertion.
  6704. This makes sure that the subtree remains an independent subtree and does
  6705. not swallow low level entries.
  6706. You can also force a different level, either by using a numeric prefix
  6707. argument, or by inserting the heading marker by hand. For example, if the
  6708. cursor is after \"*****\", then the tree will be shifted to level 5.
  6709. If optional TREE is given, use this text instead of the kill ring.
  6710. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6711. move back over whitespace before inserting, and move point to the end of
  6712. the inserted text when done."
  6713. (interactive "P")
  6714. (setq tree (or tree (and kill-ring (current-kill 0))))
  6715. (unless (org-kill-is-subtree-p tree)
  6716. (error "%s"
  6717. (substitute-command-keys
  6718. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6719. (let* ((visp (not (outline-invisible-p)))
  6720. (txt tree)
  6721. (^re (concat "^\\(" outline-regexp "\\)"))
  6722. (re (concat "\\(" outline-regexp "\\)"))
  6723. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6724. (old-level (if (string-match ^re txt)
  6725. (- (match-end 0) (match-beginning 0) 1)
  6726. -1))
  6727. (force-level (cond (level (prefix-numeric-value level))
  6728. ((and (looking-at "[ \t]*$")
  6729. (string-match
  6730. ^re_ (buffer-substring
  6731. (point-at-bol) (point))))
  6732. (- (match-end 1) (match-beginning 1)))
  6733. ((and (bolp)
  6734. (looking-at org-outline-regexp))
  6735. (- (match-end 0) (point) 1))
  6736. (t nil)))
  6737. (previous-level (save-excursion
  6738. (condition-case nil
  6739. (progn
  6740. (outline-previous-visible-heading 1)
  6741. (if (looking-at re)
  6742. (- (match-end 0) (match-beginning 0) 1)
  6743. 1))
  6744. (error 1))))
  6745. (next-level (save-excursion
  6746. (condition-case nil
  6747. (progn
  6748. (or (looking-at outline-regexp)
  6749. (outline-next-visible-heading 1))
  6750. (if (looking-at re)
  6751. (- (match-end 0) (match-beginning 0) 1)
  6752. 1))
  6753. (error 1))))
  6754. (new-level (or force-level (max previous-level next-level)))
  6755. (shift (if (or (= old-level -1)
  6756. (= new-level -1)
  6757. (= old-level new-level))
  6758. 0
  6759. (- new-level old-level)))
  6760. (delta (if (> shift 0) -1 1))
  6761. (func (if (> shift 0) 'org-demote 'org-promote))
  6762. (org-odd-levels-only nil)
  6763. beg end newend)
  6764. ;; Remove the forced level indicator
  6765. (if force-level
  6766. (delete-region (point-at-bol) (point)))
  6767. ;; Paste
  6768. (beginning-of-line 1)
  6769. (unless for-yank (org-back-over-empty-lines))
  6770. (setq beg (point))
  6771. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6772. (insert-before-markers txt)
  6773. (unless (string-match "\n\\'" txt) (insert "\n"))
  6774. (setq newend (point))
  6775. (org-reinstall-markers-in-region beg)
  6776. (setq end (point))
  6777. (goto-char beg)
  6778. (skip-chars-forward " \t\n\r")
  6779. (setq beg (point))
  6780. (if (and (outline-invisible-p) visp)
  6781. (save-excursion (outline-show-heading)))
  6782. ;; Shift if necessary
  6783. (unless (= shift 0)
  6784. (save-restriction
  6785. (narrow-to-region beg end)
  6786. (while (not (= shift 0))
  6787. (org-map-region func (point-min) (point-max))
  6788. (setq shift (+ delta shift)))
  6789. (goto-char (point-min))
  6790. (setq newend (point-max))))
  6791. (when (or (called-interactively-p 'interactive) for-yank)
  6792. (message "Clipboard pasted as level %d subtree" new-level))
  6793. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6794. kill-ring
  6795. (eq org-subtree-clip (current-kill 0))
  6796. org-subtree-clip-folded)
  6797. ;; The tree was folded before it was killed/copied
  6798. (hide-subtree))
  6799. (and for-yank (goto-char newend))))
  6800. (defun org-kill-is-subtree-p (&optional txt)
  6801. "Check if the current kill is an outline subtree, or a set of trees.
  6802. Returns nil if kill does not start with a headline, or if the first
  6803. headline level is not the largest headline level in the tree.
  6804. So this will actually accept several entries of equal levels as well,
  6805. which is OK for `org-paste-subtree'.
  6806. If optional TXT is given, check this string instead of the current kill."
  6807. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6808. (start-level (and kill
  6809. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6810. org-outline-regexp "\\)")
  6811. kill)
  6812. (- (match-end 2) (match-beginning 2) 1)))
  6813. (re (concat "^" org-outline-regexp))
  6814. (start (1+ (or (match-beginning 2) -1))))
  6815. (if (not start-level)
  6816. (progn
  6817. nil) ;; does not even start with a heading
  6818. (catch 'exit
  6819. (while (setq start (string-match re kill (1+ start)))
  6820. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6821. (throw 'exit nil)))
  6822. t))))
  6823. (defvar org-markers-to-move nil
  6824. "Markers that should be moved with a cut-and-paste operation.
  6825. Those markers are stored together with their positions relative to
  6826. the start of the region.")
  6827. (defun org-save-markers-in-region (beg end)
  6828. "Check markers in region.
  6829. If these markers are between BEG and END, record their position relative
  6830. to BEG, so that after moving the block of text, we can put the markers back
  6831. into place.
  6832. This function gets called just before an entry or tree gets cut from the
  6833. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6834. called immediately, to move the markers with the entries."
  6835. (setq org-markers-to-move nil)
  6836. (when (featurep 'org-clock)
  6837. (org-clock-save-markers-for-cut-and-paste beg end))
  6838. (when (featurep 'org-agenda)
  6839. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6840. (defun org-check-and-save-marker (marker beg end)
  6841. "Check if MARKER is between BEG and END.
  6842. If yes, remember the marker and the distance to BEG."
  6843. (when (and (marker-buffer marker)
  6844. (equal (marker-buffer marker) (current-buffer)))
  6845. (if (and (>= marker beg) (< marker end))
  6846. (push (cons marker (- marker beg)) org-markers-to-move))))
  6847. (defun org-reinstall-markers-in-region (beg)
  6848. "Move all remembered markers to their position relative to BEG."
  6849. (mapc (lambda (x)
  6850. (move-marker (car x) (+ beg (cdr x))))
  6851. org-markers-to-move)
  6852. (setq org-markers-to-move nil))
  6853. (defun org-narrow-to-subtree ()
  6854. "Narrow buffer to the current subtree."
  6855. (interactive)
  6856. (save-excursion
  6857. (save-match-data
  6858. (org-with-limited-levels
  6859. (narrow-to-region
  6860. (progn (org-back-to-heading t) (point))
  6861. (progn (org-end-of-subtree t t)
  6862. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6863. (point)))))))
  6864. (defun org-narrow-to-block ()
  6865. "Narrow buffer to the current block."
  6866. (interactive)
  6867. (let ((bstart "^[ \t]*#\\+begin")
  6868. (bend "[ \t]*#\\+end")
  6869. (case-fold-search t) ;; allow #+BEGIN
  6870. b_start b_end)
  6871. (if (org-in-regexps-block-p bstart bend)
  6872. (progn
  6873. (save-excursion (re-search-backward bstart nil t)
  6874. (setq b_start (match-beginning 0)))
  6875. (save-excursion (re-search-forward bend nil t)
  6876. (setq b_end (match-end 0)))
  6877. (narrow-to-region b_start b_end))
  6878. (error "Not in a block"))))
  6879. (eval-when-compile
  6880. (defvar org-property-drawer-re))
  6881. (defvar org-property-start-re) ;; defined below
  6882. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6883. "Clone the task (subtree) at point N times.
  6884. The clones will be inserted as siblings.
  6885. In interactive use, the user will be prompted for the number of
  6886. clones to be produced, and for a time SHIFT, which may be a
  6887. repeater as used in time stamps, for example `+3d'.
  6888. When a valid repeater is given and the entry contains any time
  6889. stamps, the clones will become a sequence in time, with time
  6890. stamps in the subtree shifted for each clone produced. If SHIFT
  6891. is nil or the empty string, time stamps will be left alone. The
  6892. ID property of the original subtree is removed.
  6893. If the original subtree did contain time stamps with a repeater,
  6894. the following will happen:
  6895. - the repeater will be removed in each clone
  6896. - an additional clone will be produced, with the current, unshifted
  6897. date(s) in the entry.
  6898. - the original entry will be placed *after* all the clones, with
  6899. repeater intact.
  6900. - the start days in the repeater in the original entry will be shifted
  6901. to past the last clone.
  6902. I this way you can spell out a number of instances of a repeating task,
  6903. and still retain the repeater to cover future instances of the task."
  6904. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6905. (let (beg end template task idprop
  6906. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6907. (if (not (and (integerp n) (> n 0)))
  6908. (error "Invalid number of replications %s" n))
  6909. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6910. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6911. shift)))
  6912. (error "Invalid shift specification %s" shift))
  6913. (when doshift
  6914. (setq shift-n (string-to-number (match-string 1 shift))
  6915. shift-what (cdr (assoc (match-string 2 shift)
  6916. '(("d" . day) ("w" . week)
  6917. ("m" . month) ("y" . year))))))
  6918. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6919. (setq nmin 1 nmax n)
  6920. (org-back-to-heading t)
  6921. (setq beg (point))
  6922. (setq idprop (org-entry-get nil "ID"))
  6923. (org-end-of-subtree t t)
  6924. (or (bolp) (insert "\n"))
  6925. (setq end (point))
  6926. (setq template (buffer-substring beg end))
  6927. (when (and doshift
  6928. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6929. (delete-region beg end)
  6930. (setq end beg)
  6931. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6932. (goto-char end)
  6933. (loop for n from nmin to nmax do
  6934. ;; prepare clone
  6935. (with-temp-buffer
  6936. (insert template)
  6937. (org-mode)
  6938. (goto-char (point-min))
  6939. (and idprop (if org-clone-delete-id
  6940. (org-entry-delete nil "ID")
  6941. (org-id-get-create t)))
  6942. (while (re-search-forward org-property-start-re nil t)
  6943. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6944. (goto-char (point-min))
  6945. (when doshift
  6946. (while (re-search-forward org-ts-regexp-both nil t)
  6947. (org-timestamp-change (* n shift-n) shift-what))
  6948. (unless (= n n-no-remove)
  6949. (goto-char (point-min))
  6950. (while (re-search-forward org-ts-regexp nil t)
  6951. (save-excursion
  6952. (goto-char (match-beginning 0))
  6953. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6954. (delete-region (match-beginning 1) (match-end 1)))))))
  6955. (setq task (buffer-string)))
  6956. (insert task))
  6957. (goto-char beg)))
  6958. ;;; Outline Sorting
  6959. (defun org-sort (with-case)
  6960. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6961. Optional argument WITH-CASE means sort case-sensitively.
  6962. With a double prefix argument, also remove duplicate entries."
  6963. (interactive "P")
  6964. (cond
  6965. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6966. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6967. (t
  6968. (org-call-with-arg 'org-sort-entries with-case))))
  6969. (defun org-sort-remove-invisible (s)
  6970. (remove-text-properties 0 (length s) org-rm-props s)
  6971. (while (string-match org-bracket-link-regexp s)
  6972. (setq s (replace-match (if (match-end 2)
  6973. (match-string 3 s)
  6974. (match-string 1 s)) t t s)))
  6975. s)
  6976. (defvar org-priority-regexp) ; defined later in the file
  6977. (defvar org-after-sorting-entries-or-items-hook nil
  6978. "Hook that is run after a bunch of entries or items have been sorted.
  6979. When children are sorted, the cursor is in the parent line when this
  6980. hook gets called. When a region or a plain list is sorted, the cursor
  6981. will be in the first entry of the sorted region/list.")
  6982. (defun org-sort-entries
  6983. (&optional with-case sorting-type getkey-func compare-func property)
  6984. "Sort entries on a certain level of an outline tree.
  6985. If there is an active region, the entries in the region are sorted.
  6986. Else, if the cursor is before the first entry, sort the top-level items.
  6987. Else, the children of the entry at point are sorted.
  6988. Sorting can be alphabetically, numerically, by date/time as given by
  6989. a time stamp, by a property or by priority.
  6990. The command prompts for the sorting type unless it has been given to the
  6991. function through the SORTING-TYPE argument, which needs to be a character,
  6992. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6993. precise meaning of each character:
  6994. n Numerically, by converting the beginning of the entry/item to a number.
  6995. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6996. t By date/time, either the first active time stamp in the entry, or, if
  6997. none exist, by the first inactive one.
  6998. s By the scheduled date/time.
  6999. d By deadline date/time.
  7000. c By creation time, which is assumed to be the first inactive time stamp
  7001. at the beginning of a line.
  7002. p By priority according to the cookie.
  7003. r By the value of a property.
  7004. Capital letters will reverse the sort order.
  7005. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7006. called with point at the beginning of the record. It must return either
  7007. a string or a number that should serve as the sorting key for that record.
  7008. Comparing entries ignores case by default. However, with an optional argument
  7009. WITH-CASE, the sorting considers case as well."
  7010. (interactive "P")
  7011. (let ((case-func (if with-case 'identity 'downcase))
  7012. start beg end stars re re2
  7013. txt what tmp)
  7014. ;; Find beginning and end of region to sort
  7015. (cond
  7016. ((org-region-active-p)
  7017. ;; we will sort the region
  7018. (setq end (region-end)
  7019. what "region")
  7020. (goto-char (region-beginning))
  7021. (if (not (org-on-heading-p)) (outline-next-heading))
  7022. (setq start (point)))
  7023. ((or (org-on-heading-p)
  7024. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7025. ;; we will sort the children of the current headline
  7026. (org-back-to-heading)
  7027. (setq start (point)
  7028. end (progn (org-end-of-subtree t t)
  7029. (or (bolp) (insert "\n"))
  7030. (org-back-over-empty-lines)
  7031. (point))
  7032. what "children")
  7033. (goto-char start)
  7034. (show-subtree)
  7035. (outline-next-heading))
  7036. (t
  7037. ;; we will sort the top-level entries in this file
  7038. (goto-char (point-min))
  7039. (or (org-on-heading-p) (outline-next-heading))
  7040. (setq start (point))
  7041. (goto-char (point-max))
  7042. (beginning-of-line 1)
  7043. (when (looking-at ".*?\\S-")
  7044. ;; File ends in a non-white line
  7045. (end-of-line 1)
  7046. (insert "\n"))
  7047. (setq end (point-max))
  7048. (setq what "top-level")
  7049. (goto-char start)
  7050. (show-all)))
  7051. (setq beg (point))
  7052. (if (>= beg end) (error "Nothing to sort"))
  7053. (looking-at "\\(\\*+\\)")
  7054. (setq stars (match-string 1)
  7055. re (concat "^" (regexp-quote stars) " +")
  7056. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7057. txt (buffer-substring beg end))
  7058. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7059. (if (and (not (equal stars "*")) (string-match re2 txt))
  7060. (error "Region to sort contains a level above the first entry"))
  7061. (unless sorting-type
  7062. (message
  7063. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7064. [t]ime [s]cheduled [d]eadline [c]reated
  7065. A/N/T/S/D/C/P/O/F means reversed:"
  7066. what)
  7067. (setq sorting-type (read-char-exclusive))
  7068. (and (= (downcase sorting-type) ?f)
  7069. (setq getkey-func
  7070. (org-icompleting-read "Sort using function: "
  7071. obarray 'fboundp t nil nil))
  7072. (setq getkey-func (intern getkey-func)))
  7073. (and (= (downcase sorting-type) ?r)
  7074. (setq property
  7075. (org-icompleting-read "Property: "
  7076. (mapcar 'list (org-buffer-property-keys t))
  7077. nil t))))
  7078. (message "Sorting entries...")
  7079. (save-restriction
  7080. (narrow-to-region start end)
  7081. (let ((dcst (downcase sorting-type))
  7082. (case-fold-search nil)
  7083. (now (current-time)))
  7084. (sort-subr
  7085. (/= dcst sorting-type)
  7086. ;; This function moves to the beginning character of the "record" to
  7087. ;; be sorted.
  7088. (lambda nil
  7089. (if (re-search-forward re nil t)
  7090. (goto-char (match-beginning 0))
  7091. (goto-char (point-max))))
  7092. ;; This function moves to the last character of the "record" being
  7093. ;; sorted.
  7094. (lambda nil
  7095. (save-match-data
  7096. (condition-case nil
  7097. (outline-forward-same-level 1)
  7098. (error
  7099. (goto-char (point-max))))))
  7100. ;; This function returns the value that gets sorted against.
  7101. (lambda nil
  7102. (cond
  7103. ((= dcst ?n)
  7104. (if (looking-at org-complex-heading-regexp)
  7105. (string-to-number (match-string 4))
  7106. nil))
  7107. ((= dcst ?a)
  7108. (if (looking-at org-complex-heading-regexp)
  7109. (funcall case-func (match-string 4))
  7110. nil))
  7111. ((= dcst ?t)
  7112. (let ((end (save-excursion (outline-next-heading) (point))))
  7113. (if (or (re-search-forward org-ts-regexp end t)
  7114. (re-search-forward org-ts-regexp-both end t))
  7115. (org-time-string-to-seconds (match-string 0))
  7116. (org-float-time now))))
  7117. ((= dcst ?c)
  7118. (let ((end (save-excursion (outline-next-heading) (point))))
  7119. (if (re-search-forward
  7120. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7121. end t)
  7122. (org-time-string-to-seconds (match-string 0))
  7123. (org-float-time now))))
  7124. ((= dcst ?s)
  7125. (let ((end (save-excursion (outline-next-heading) (point))))
  7126. (if (re-search-forward org-scheduled-time-regexp end t)
  7127. (org-time-string-to-seconds (match-string 1))
  7128. (org-float-time now))))
  7129. ((= dcst ?d)
  7130. (let ((end (save-excursion (outline-next-heading) (point))))
  7131. (if (re-search-forward org-deadline-time-regexp end t)
  7132. (org-time-string-to-seconds (match-string 1))
  7133. (org-float-time now))))
  7134. ((= dcst ?p)
  7135. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7136. (string-to-char (match-string 2))
  7137. org-default-priority))
  7138. ((= dcst ?r)
  7139. (or (org-entry-get nil property) ""))
  7140. ((= dcst ?o)
  7141. (if (looking-at org-complex-heading-regexp)
  7142. (- 9999 (length (member (match-string 2)
  7143. org-todo-keywords-1)))))
  7144. ((= dcst ?f)
  7145. (if getkey-func
  7146. (progn
  7147. (setq tmp (funcall getkey-func))
  7148. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7149. tmp)
  7150. (error "Invalid key function `%s'" getkey-func)))
  7151. (t (error "Invalid sorting type `%c'" sorting-type))))
  7152. nil
  7153. (cond
  7154. ((= dcst ?a) 'string<)
  7155. ((= dcst ?f) compare-func)
  7156. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7157. (t nil)))))
  7158. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7159. (message "Sorting entries...done")))
  7160. (defun org-do-sort (table what &optional with-case sorting-type)
  7161. "Sort TABLE of WHAT according to SORTING-TYPE.
  7162. The user will be prompted for the SORTING-TYPE if the call to this
  7163. function does not specify it. WHAT is only for the prompt, to indicate
  7164. what is being sorted. The sorting key will be extracted from
  7165. the car of the elements of the table.
  7166. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7167. (unless sorting-type
  7168. (message
  7169. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7170. what)
  7171. (setq sorting-type (read-char-exclusive)))
  7172. (let ((dcst (downcase sorting-type))
  7173. extractfun comparefun)
  7174. ;; Define the appropriate functions
  7175. (cond
  7176. ((= dcst ?n)
  7177. (setq extractfun 'string-to-number
  7178. comparefun (if (= dcst sorting-type) '< '>)))
  7179. ((= dcst ?a)
  7180. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7181. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7182. comparefun (if (= dcst sorting-type)
  7183. 'string<
  7184. (lambda (a b) (and (not (string< a b))
  7185. (not (string= a b)))))))
  7186. ((= dcst ?t)
  7187. (setq extractfun
  7188. (lambda (x)
  7189. (if (or (string-match org-ts-regexp x)
  7190. (string-match org-ts-regexp-both x))
  7191. (org-float-time
  7192. (org-time-string-to-time (match-string 0 x)))
  7193. 0))
  7194. comparefun (if (= dcst sorting-type) '< '>)))
  7195. (t (error "Invalid sorting type `%c'" sorting-type)))
  7196. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7197. table)
  7198. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7199. ;;; The orgstruct minor mode
  7200. ;; Define a minor mode which can be used in other modes in order to
  7201. ;; integrate the org-mode structure editing commands.
  7202. ;; This is really a hack, because the org-mode structure commands use
  7203. ;; keys which normally belong to the major mode. Here is how it
  7204. ;; works: The minor mode defines all the keys necessary to operate the
  7205. ;; structure commands, but wraps the commands into a function which
  7206. ;; tests if the cursor is currently at a headline or a plain list
  7207. ;; item. If that is the case, the structure command is used,
  7208. ;; temporarily setting many Org-mode variables like regular
  7209. ;; expressions for filling etc. However, when any of those keys is
  7210. ;; used at a different location, function uses `key-binding' to look
  7211. ;; up if the key has an associated command in another currently active
  7212. ;; keymap (minor modes, major mode, global), and executes that
  7213. ;; command. There might be problems if any of the keys is otherwise
  7214. ;; used as a prefix key.
  7215. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7216. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7217. ;; addresses this by checking explicitly for both bindings.
  7218. (defvar orgstruct-mode-map (make-sparse-keymap)
  7219. "Keymap for the minor `orgstruct-mode'.")
  7220. (defvar org-local-vars nil
  7221. "List of local variables, for use by `orgstruct-mode'.")
  7222. ;;;###autoload
  7223. (define-minor-mode orgstruct-mode
  7224. "Toggle the minor mode `orgstruct-mode'.
  7225. This mode is for using Org-mode structure commands in other
  7226. modes. The following keys behave as if Org-mode were active, if
  7227. the cursor is on a headline, or on a plain list item (both as
  7228. defined by Org-mode).
  7229. M-up Move entry/item up
  7230. M-down Move entry/item down
  7231. M-left Promote
  7232. M-right Demote
  7233. M-S-up Move entry/item up
  7234. M-S-down Move entry/item down
  7235. M-S-left Promote subtree
  7236. M-S-right Demote subtree
  7237. M-q Fill paragraph and items like in Org-mode
  7238. C-c ^ Sort entries
  7239. C-c - Cycle list bullet
  7240. TAB Cycle item visibility
  7241. M-RET Insert new heading/item
  7242. S-M-RET Insert new TODO heading / Checkbox item
  7243. C-c C-c Set tags / toggle checkbox"
  7244. nil " OrgStruct" nil
  7245. (org-load-modules-maybe)
  7246. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7247. ;;;###autoload
  7248. (defun turn-on-orgstruct ()
  7249. "Unconditionally turn on `orgstruct-mode'."
  7250. (orgstruct-mode 1))
  7251. (defun orgstruct++-mode (&optional arg)
  7252. "Toggle `orgstruct-mode', the enhanced version of it.
  7253. In addition to setting orgstruct-mode, this also exports all indentation
  7254. and autofilling variables from org-mode into the buffer. It will also
  7255. recognize item context in multiline items.
  7256. Note that turning off orgstruct-mode will *not* remove the
  7257. indentation/paragraph settings. This can only be done by refreshing the
  7258. major mode, for example with \\[normal-mode]."
  7259. (interactive "P")
  7260. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7261. (if (< arg 1)
  7262. (orgstruct-mode -1)
  7263. (orgstruct-mode 1)
  7264. (let (var val)
  7265. (mapc
  7266. (lambda (x)
  7267. (when (string-match
  7268. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7269. (symbol-name (car x)))
  7270. (setq var (car x) val (nth 1 x))
  7271. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7272. org-local-vars)
  7273. (org-set-local 'orgstruct-is-++ t))))
  7274. (defvar orgstruct-is-++ nil
  7275. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7276. (make-variable-buffer-local 'orgstruct-is-++)
  7277. ;;;###autoload
  7278. (defun turn-on-orgstruct++ ()
  7279. "Unconditionally turn on `orgstruct++-mode'."
  7280. (orgstruct++-mode 1))
  7281. (defun orgstruct-error ()
  7282. "Error when there is no default binding for a structure key."
  7283. (interactive)
  7284. (error "This key has no function outside structure elements"))
  7285. (defun orgstruct-setup ()
  7286. "Setup orgstruct keymaps."
  7287. (let ((nfunc 0)
  7288. (bindings
  7289. (list
  7290. '([(meta up)] org-metaup)
  7291. '([(meta down)] org-metadown)
  7292. '([(meta left)] org-metaleft)
  7293. '([(meta right)] org-metaright)
  7294. '([(meta shift up)] org-shiftmetaup)
  7295. '([(meta shift down)] org-shiftmetadown)
  7296. '([(meta shift left)] org-shiftmetaleft)
  7297. '([(meta shift right)] org-shiftmetaright)
  7298. '([?\e (up)] org-metaup)
  7299. '([?\e (down)] org-metadown)
  7300. '([?\e (left)] org-metaleft)
  7301. '([?\e (right)] org-metaright)
  7302. '([?\e (shift up)] org-shiftmetaup)
  7303. '([?\e (shift down)] org-shiftmetadown)
  7304. '([?\e (shift left)] org-shiftmetaleft)
  7305. '([?\e (shift right)] org-shiftmetaright)
  7306. '([(shift up)] org-shiftup)
  7307. '([(shift down)] org-shiftdown)
  7308. '([(shift left)] org-shiftleft)
  7309. '([(shift right)] org-shiftright)
  7310. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7311. '("\M-q" fill-paragraph)
  7312. '("\C-c^" org-sort)
  7313. '("\C-c-" org-cycle-list-bullet)))
  7314. elt key fun cmd)
  7315. (while (setq elt (pop bindings))
  7316. (setq nfunc (1+ nfunc))
  7317. (setq key (org-key (car elt))
  7318. fun (nth 1 elt)
  7319. cmd (orgstruct-make-binding fun nfunc key))
  7320. (org-defkey orgstruct-mode-map key cmd))
  7321. ;; Special treatment needed for TAB and RET
  7322. (org-defkey orgstruct-mode-map [(tab)]
  7323. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7324. (org-defkey orgstruct-mode-map "\C-i"
  7325. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7326. (org-defkey orgstruct-mode-map "\M-\C-m"
  7327. (orgstruct-make-binding 'org-insert-heading 105
  7328. "\M-\C-m" [(meta return)]))
  7329. (org-defkey orgstruct-mode-map [(meta return)]
  7330. (orgstruct-make-binding 'org-insert-heading 106
  7331. [(meta return)] "\M-\C-m"))
  7332. (org-defkey orgstruct-mode-map [(shift meta return)]
  7333. (orgstruct-make-binding 'org-insert-todo-heading 107
  7334. [(meta return)] "\M-\C-m"))
  7335. (org-defkey orgstruct-mode-map "\e\C-m"
  7336. (orgstruct-make-binding 'org-insert-heading 108
  7337. "\e\C-m" [?\e (return)]))
  7338. (org-defkey orgstruct-mode-map [?\e (return)]
  7339. (orgstruct-make-binding 'org-insert-heading 109
  7340. [?\e (return)] "\e\C-m"))
  7341. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7342. (orgstruct-make-binding 'org-insert-todo-heading 110
  7343. [?\e (return)] "\e\C-m"))
  7344. (unless org-local-vars
  7345. (setq org-local-vars (org-get-local-variables)))
  7346. t))
  7347. (defun orgstruct-make-binding (fun n &rest keys)
  7348. "Create a function for binding in the structure minor mode.
  7349. FUN is the command to call inside a table. N is used to create a unique
  7350. command name. KEYS are keys that should be checked in for a command
  7351. to execute outside of tables."
  7352. (eval
  7353. (list 'defun
  7354. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7355. '(arg)
  7356. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7357. "Outside of structure, run the binding of `"
  7358. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7359. "'.")
  7360. '(interactive "p")
  7361. (list 'if
  7362. `(org-context-p 'headline 'item
  7363. (and orgstruct-is-++
  7364. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7365. 'item-body))
  7366. (list 'org-run-like-in-org-mode (list 'quote fun))
  7367. (list 'let '(orgstruct-mode)
  7368. (list 'call-interactively
  7369. (append '(or)
  7370. (mapcar (lambda (k)
  7371. (list 'key-binding k))
  7372. keys)
  7373. '('orgstruct-error))))))))
  7374. (defun org-context-p (&rest contexts)
  7375. "Check if local context is any of CONTEXTS.
  7376. Possible values in the list of contexts are `table', `headline', and `item'."
  7377. (let ((pos (point)))
  7378. (goto-char (point-at-bol))
  7379. (prog1 (or (and (memq 'table contexts)
  7380. (looking-at "[ \t]*|"))
  7381. (and (memq 'headline contexts)
  7382. ;;????????? (looking-at "\\*+"))
  7383. (looking-at outline-regexp))
  7384. (and (memq 'item contexts)
  7385. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7386. (and (memq 'item-body contexts)
  7387. (org-in-item-p)))
  7388. (goto-char pos))))
  7389. (defun org-get-local-variables ()
  7390. "Return a list of all local variables in an org-mode buffer."
  7391. (let (varlist)
  7392. (with-current-buffer (get-buffer-create "*Org tmp*")
  7393. (erase-buffer)
  7394. (org-mode)
  7395. (setq varlist (buffer-local-variables)))
  7396. (kill-buffer "*Org tmp*")
  7397. (delq nil
  7398. (mapcar
  7399. (lambda (x)
  7400. (setq x
  7401. (if (symbolp x)
  7402. (list x)
  7403. (list (car x) (list 'quote (cdr x)))))
  7404. (if (string-match
  7405. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7406. (symbol-name (car x)))
  7407. x nil))
  7408. varlist))))
  7409. (defun org-clone-local-variables (from-buffer &optional regexp)
  7410. "Clone local variables from FROM-BUFFER.
  7411. Optional argument REGEXP selects variables to clone."
  7412. (mapc
  7413. (lambda (pair)
  7414. (and (symbolp (car pair))
  7415. (or (null regexp)
  7416. (string-match regexp (symbol-name (car pair))))
  7417. (set (make-local-variable (car pair))
  7418. (cdr pair))))
  7419. (buffer-local-variables from-buffer)))
  7420. ;;;###autoload
  7421. (defun org-run-like-in-org-mode (cmd)
  7422. "Run a command, pretending that the current buffer is in Org-mode.
  7423. This will temporarily bind local variables that are typically bound in
  7424. Org-mode to the values they have in Org-mode, and then interactively
  7425. call CMD."
  7426. (org-load-modules-maybe)
  7427. (unless org-local-vars
  7428. (setq org-local-vars (org-get-local-variables)))
  7429. (eval (list 'let org-local-vars
  7430. (list 'call-interactively (list 'quote cmd)))))
  7431. ;;;; Archiving
  7432. (defun org-get-category (&optional pos force-refresh)
  7433. "Get the category applying to position POS."
  7434. (if force-refresh (org-refresh-category-properties))
  7435. (let ((pos (or pos (point))))
  7436. (or (get-text-property pos 'org-category)
  7437. (progn (org-refresh-category-properties)
  7438. (get-text-property pos 'org-category)))))
  7439. (defun org-refresh-category-properties ()
  7440. "Refresh category text properties in the buffer."
  7441. (let ((def-cat (cond
  7442. ((null org-category)
  7443. (if buffer-file-name
  7444. (file-name-sans-extension
  7445. (file-name-nondirectory buffer-file-name))
  7446. "???"))
  7447. ((symbolp org-category) (symbol-name org-category))
  7448. (t org-category)))
  7449. beg end cat pos optionp)
  7450. (org-unmodified
  7451. (save-excursion
  7452. (save-restriction
  7453. (widen)
  7454. (goto-char (point-min))
  7455. (put-text-property (point) (point-max) 'org-category def-cat)
  7456. (while (re-search-forward
  7457. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7458. (setq pos (match-end 0)
  7459. optionp (equal (char-after (match-beginning 0)) ?#)
  7460. cat (org-trim (match-string 2)))
  7461. (if optionp
  7462. (setq beg (point-at-bol) end (point-max))
  7463. (org-back-to-heading t)
  7464. (setq beg (point) end (org-end-of-subtree t t)))
  7465. (put-text-property beg end 'org-category cat)
  7466. (goto-char pos)))))))
  7467. ;;;; Link Stuff
  7468. ;;; Link abbreviations
  7469. (defun org-link-expand-abbrev (link)
  7470. "Apply replacements as defined in `org-link-abbrev-alist."
  7471. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7472. (let* ((key (match-string 1 link))
  7473. (as (or (assoc key org-link-abbrev-alist-local)
  7474. (assoc key org-link-abbrev-alist)))
  7475. (tag (and (match-end 2) (match-string 3 link)))
  7476. rpl)
  7477. (if (not as)
  7478. link
  7479. (setq rpl (cdr as))
  7480. (cond
  7481. ((symbolp rpl) (funcall rpl tag))
  7482. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7483. ((string-match "%h" rpl)
  7484. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7485. (t (concat rpl tag)))))
  7486. link))
  7487. ;;; Storing and inserting links
  7488. (defvar org-insert-link-history nil
  7489. "Minibuffer history for links inserted with `org-insert-link'.")
  7490. (defvar org-stored-links nil
  7491. "Contains the links stored with `org-store-link'.")
  7492. (defvar org-store-link-plist nil
  7493. "Plist with info about the most recently link created with `org-store-link'.")
  7494. (defvar org-link-protocols nil
  7495. "Link protocols added to Org-mode using `org-add-link-type'.")
  7496. (defvar org-store-link-functions nil
  7497. "List of functions that are called to create and store a link.
  7498. Each function will be called in turn until one returns a non-nil
  7499. value. Each function should check if it is responsible for creating
  7500. this link (for example by looking at the major mode).
  7501. If not, it must exit and return nil.
  7502. If yes, it should return a non-nil value after a calling
  7503. `org-store-link-props' with a list of properties and values.
  7504. Special properties are:
  7505. :type The link prefix, like \"http\". This must be given.
  7506. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7507. This is obligatory as well.
  7508. :description Optional default description for the second pair
  7509. of brackets in an Org-mode link. The user can still change
  7510. this when inserting this link into an Org-mode buffer.
  7511. In addition to these, any additional properties can be specified
  7512. and then used in remember templates.")
  7513. (defun org-add-link-type (type &optional follow export)
  7514. "Add TYPE to the list of `org-link-types'.
  7515. Re-compute all regular expressions depending on `org-link-types'
  7516. FOLLOW and EXPORT are two functions.
  7517. FOLLOW should take the link path as the single argument and do whatever
  7518. is necessary to follow the link, for example find a file or display
  7519. a mail message.
  7520. EXPORT should format the link path for export to one of the export formats.
  7521. It should be a function accepting three arguments:
  7522. path the path of the link, the text after the prefix (like \"http:\")
  7523. desc the description of the link, if any, or a description added by
  7524. org-export-normalize-links if there is none
  7525. format the export format, a symbol like `html' or `latex' or `ascii'..
  7526. The function may use the FORMAT information to return different values
  7527. depending on the format. The return value will be put literally into
  7528. the exported file. If the return value is nil, this means Org should
  7529. do what it normally does with links which do not have EXPORT defined.
  7530. Org-mode has a built-in default for exporting links. If you are happy with
  7531. this default, there is no need to define an export function for the link
  7532. type. For a simple example of an export function, see `org-bbdb.el'."
  7533. (add-to-list 'org-link-types type t)
  7534. (org-make-link-regexps)
  7535. (if (assoc type org-link-protocols)
  7536. (setcdr (assoc type org-link-protocols) (list follow export))
  7537. (push (list type follow export) org-link-protocols)))
  7538. (defvar org-agenda-buffer-name)
  7539. ;;;###autoload
  7540. (defun org-store-link (arg)
  7541. "\\<org-mode-map>Store an org-link to the current location.
  7542. This link is added to `org-stored-links' and can later be inserted
  7543. into an org-buffer with \\[org-insert-link].
  7544. For some link types, a prefix arg is interpreted:
  7545. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7546. For file links, arg negates `org-context-in-file-links'."
  7547. (interactive "P")
  7548. (org-load-modules-maybe)
  7549. (setq org-store-link-plist nil) ; reset
  7550. (org-with-limited-levels
  7551. (let (link cpltxt desc description search txt custom-id agenda-link)
  7552. (cond
  7553. ((run-hook-with-args-until-success 'org-store-link-functions)
  7554. (setq link (plist-get org-store-link-plist :link)
  7555. desc (or (plist-get org-store-link-plist :description) link)))
  7556. ((equal (buffer-name) "*Org Edit Src Example*")
  7557. (let (label gc)
  7558. (while (or (not label)
  7559. (save-excursion
  7560. (save-restriction
  7561. (widen)
  7562. (goto-char (point-min))
  7563. (re-search-forward
  7564. (regexp-quote (format org-coderef-label-format label))
  7565. nil t))))
  7566. (when label (message "Label exists already") (sit-for 2))
  7567. (setq label (read-string "Code line label: " label)))
  7568. (end-of-line 1)
  7569. (setq link (format org-coderef-label-format label))
  7570. (setq gc (- 79 (length link)))
  7571. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7572. (insert link)
  7573. (setq link (concat "(" label ")") desc nil)))
  7574. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7575. ;; We are in the agenda, link to referenced location
  7576. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7577. (get-text-property (point) 'org-marker))))
  7578. (when m
  7579. (org-with-point-at m
  7580. (setq agenda-link
  7581. (if (called-interactively-p 'any)
  7582. (call-interactively 'org-store-link)
  7583. (org-store-link nil)))))))
  7584. ((eq major-mode 'calendar-mode)
  7585. (let ((cd (calendar-cursor-to-date)))
  7586. (setq link
  7587. (format-time-string
  7588. (car org-time-stamp-formats)
  7589. (apply 'encode-time
  7590. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7591. nil nil nil))))
  7592. (org-store-link-props :type "calendar" :date cd)))
  7593. ((eq major-mode 'w3-mode)
  7594. (setq cpltxt (if (and (buffer-name)
  7595. (not (string-match "Untitled" (buffer-name))))
  7596. (buffer-name)
  7597. (url-view-url t))
  7598. link (org-make-link (url-view-url t)))
  7599. (org-store-link-props :type "w3" :url (url-view-url t)))
  7600. ((eq major-mode 'w3m-mode)
  7601. (setq cpltxt (or w3m-current-title w3m-current-url)
  7602. link (org-make-link w3m-current-url))
  7603. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7604. ((setq search (run-hook-with-args-until-success
  7605. 'org-create-file-search-functions))
  7606. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7607. "::" search))
  7608. (setq cpltxt (or description link)))
  7609. ((eq major-mode 'image-mode)
  7610. (setq cpltxt (concat "file:"
  7611. (abbreviate-file-name buffer-file-name))
  7612. link (org-make-link cpltxt))
  7613. (org-store-link-props :type "image" :file buffer-file-name))
  7614. ((eq major-mode 'dired-mode)
  7615. ;; link to the file in the current line
  7616. (let ((file (dired-get-filename nil t)))
  7617. (setq file (if file
  7618. (abbreviate-file-name
  7619. (expand-file-name (dired-get-filename nil t)))
  7620. ;; otherwise, no file so use current directory.
  7621. default-directory))
  7622. (setq cpltxt (concat "file:" file)
  7623. link (org-make-link cpltxt))))
  7624. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7625. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7626. (cond
  7627. ((org-in-regexp "<<\\(.*?\\)>>")
  7628. (setq cpltxt
  7629. (concat "file:"
  7630. (abbreviate-file-name
  7631. (buffer-file-name (buffer-base-buffer)))
  7632. "::" (match-string 1))
  7633. link (org-make-link cpltxt)))
  7634. ((and (featurep 'org-id)
  7635. (or (eq org-link-to-org-use-id t)
  7636. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7637. (called-interactively-p 'any))
  7638. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7639. (called-interactively-p 'any)
  7640. (not custom-id))
  7641. (and org-link-to-org-use-id
  7642. (org-entry-get nil "ID"))))
  7643. ;; We can make a link using the ID.
  7644. (setq link (condition-case nil
  7645. (prog1 (org-id-store-link)
  7646. (setq desc (plist-get org-store-link-plist
  7647. :description)))
  7648. (error
  7649. ;; probably before first headline, link to file only
  7650. (concat "file:"
  7651. (abbreviate-file-name
  7652. (buffer-file-name (buffer-base-buffer))))))))
  7653. (t
  7654. ;; Just link to current headline
  7655. (setq cpltxt (concat "file:"
  7656. (abbreviate-file-name
  7657. (buffer-file-name (buffer-base-buffer)))))
  7658. ;; Add a context search string
  7659. (when (org-xor org-context-in-file-links arg)
  7660. (setq txt (cond
  7661. ((org-on-heading-p) nil)
  7662. ((org-region-active-p)
  7663. (buffer-substring (region-beginning) (region-end)))
  7664. (t nil)))
  7665. (when (or (null txt) (string-match "\\S-" txt))
  7666. (setq cpltxt
  7667. (concat cpltxt "::"
  7668. (condition-case nil
  7669. (org-make-org-heading-search-string txt)
  7670. (error "")))
  7671. desc (or (nth 4 (ignore-errors
  7672. (org-heading-components))) "NONE"))))
  7673. (if (string-match "::\\'" cpltxt)
  7674. (setq cpltxt (substring cpltxt 0 -2)))
  7675. (setq link (org-make-link cpltxt)))))
  7676. ((buffer-file-name (buffer-base-buffer))
  7677. ;; Just link to this file here.
  7678. (setq cpltxt (concat "file:"
  7679. (abbreviate-file-name
  7680. (buffer-file-name (buffer-base-buffer)))))
  7681. ;; Add a context string
  7682. (when (org-xor org-context-in-file-links arg)
  7683. (setq txt (if (org-region-active-p)
  7684. (buffer-substring (region-beginning) (region-end))
  7685. (buffer-substring (point-at-bol) (point-at-eol))))
  7686. ;; Only use search option if there is some text.
  7687. (when (string-match "\\S-" txt)
  7688. (setq cpltxt
  7689. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7690. desc "NONE")))
  7691. (setq link (org-make-link cpltxt)))
  7692. ((called-interactively-p 'interactive)
  7693. (error "Cannot link to a buffer which is not visiting a file"))
  7694. (t (setq link nil)))
  7695. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7696. (setq link (or link cpltxt)
  7697. desc (or desc cpltxt))
  7698. (if (equal desc "NONE") (setq desc nil))
  7699. (if (and (or (called-interactively-p 'any) executing-kbd-macro) link)
  7700. (progn
  7701. (setq org-stored-links
  7702. (cons (list link desc) org-stored-links))
  7703. (message "Stored: %s" (or desc link))
  7704. (when custom-id
  7705. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7706. "::#" custom-id))
  7707. (setq org-stored-links
  7708. (cons (list link desc) org-stored-links))))
  7709. (or agenda-link (and link (org-make-link-string link desc)))))))
  7710. (defun org-store-link-props (&rest plist)
  7711. "Store link properties, extract names and addresses."
  7712. (let (x adr)
  7713. (when (setq x (plist-get plist :from))
  7714. (setq adr (mail-extract-address-components x))
  7715. (setq plist (plist-put plist :fromname (car adr)))
  7716. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7717. (when (setq x (plist-get plist :to))
  7718. (setq adr (mail-extract-address-components x))
  7719. (setq plist (plist-put plist :toname (car adr)))
  7720. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7721. (let ((from (plist-get plist :from))
  7722. (to (plist-get plist :to)))
  7723. (when (and from to org-from-is-user-regexp)
  7724. (setq plist
  7725. (plist-put plist :fromto
  7726. (if (string-match org-from-is-user-regexp from)
  7727. (concat "to %t")
  7728. (concat "from %f"))))))
  7729. (setq org-store-link-plist plist))
  7730. (defun org-add-link-props (&rest plist)
  7731. "Add these properties to the link property list."
  7732. (let (key value)
  7733. (while plist
  7734. (setq key (pop plist) value (pop plist))
  7735. (setq org-store-link-plist
  7736. (plist-put org-store-link-plist key value)))))
  7737. (defun org-email-link-description (&optional fmt)
  7738. "Return the description part of an email link.
  7739. This takes information from `org-store-link-plist' and formats it
  7740. according to FMT (default from `org-email-link-description-format')."
  7741. (setq fmt (or fmt org-email-link-description-format))
  7742. (let* ((p org-store-link-plist)
  7743. (to (plist-get p :toaddress))
  7744. (from (plist-get p :fromaddress))
  7745. (table
  7746. (list
  7747. (cons "%c" (plist-get p :fromto))
  7748. (cons "%F" (plist-get p :from))
  7749. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7750. (cons "%T" (plist-get p :to))
  7751. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7752. (cons "%s" (plist-get p :subject))
  7753. (cons "%d" (plist-get p :date))
  7754. (cons "%m" (plist-get p :message-id)))))
  7755. (when (string-match "%c" fmt)
  7756. ;; Check if the user wrote this message
  7757. (if (and org-from-is-user-regexp from to
  7758. (save-match-data (string-match org-from-is-user-regexp from)))
  7759. (setq fmt (replace-match "to %t" t t fmt))
  7760. (setq fmt (replace-match "from %f" t t fmt))))
  7761. (org-replace-escapes fmt table)))
  7762. (defun org-make-org-heading-search-string (&optional string heading)
  7763. "Make search string for STRING or current headline."
  7764. (interactive)
  7765. (let ((s (or string (org-get-heading)))
  7766. (lines org-context-in-file-links))
  7767. (unless (and string (not heading))
  7768. ;; We are using a headline, clean up garbage in there.
  7769. (if (string-match org-todo-regexp s)
  7770. (setq s (replace-match "" t t s)))
  7771. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7772. (setq s (replace-match "" t t s)))
  7773. (setq s (org-trim s))
  7774. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7775. org-comment-string "\\)") s)
  7776. (setq s (replace-match "" t t s)))
  7777. (while (string-match org-ts-regexp s)
  7778. (setq s (replace-match "" t t s))))
  7779. (or string (setq s (concat "*" s))) ; Add * for headlines
  7780. (when (and string (integerp lines) (> lines 0))
  7781. (let ((slines (org-split-string s "\n")))
  7782. (when (< lines (length slines))
  7783. (setq s (mapconcat
  7784. 'identity
  7785. (reverse (nthcdr (- (length slines) lines)
  7786. (reverse slines))) "\n")))))
  7787. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7788. (defun org-make-link (&rest strings)
  7789. "Concatenate STRINGS."
  7790. (apply 'concat strings))
  7791. (defun org-make-link-string (link &optional description)
  7792. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7793. (unless (string-match "\\S-" link)
  7794. (error "Empty link"))
  7795. (when (and description
  7796. (stringp description)
  7797. (not (string-match "\\S-" description)))
  7798. (setq description nil))
  7799. (when (stringp description)
  7800. ;; Remove brackets from the description, they are fatal.
  7801. (while (string-match "\\[" description)
  7802. (setq description (replace-match "{" t t description)))
  7803. (while (string-match "\\]" description)
  7804. (setq description (replace-match "}" t t description))))
  7805. (when (equal (org-link-escape link) description)
  7806. ;; No description needed, it is identical
  7807. (setq description nil))
  7808. (when (and (not description)
  7809. (not (equal link (org-link-escape link))))
  7810. (setq description (org-extract-attributes link)))
  7811. (setq link (if (string-match org-link-types-re link)
  7812. (concat (match-string 1 link)
  7813. (org-link-escape (substring link (match-end 1))))
  7814. (org-link-escape link)))
  7815. (concat "[[" link "]"
  7816. (if description (concat "[" description "]") "")
  7817. "]"))
  7818. (defconst org-link-escape-chars
  7819. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  7820. "List of characters that should be escaped in link.
  7821. This is the list that is used for internal purposes.")
  7822. (defvar org-url-encoding-use-url-hexify nil)
  7823. (defconst org-link-escape-chars-browser
  7824. '(?\ )
  7825. "List of escapes for characters that are problematic in links.
  7826. This is the list that is used before handing over to the browser.")
  7827. (defun org-link-escape (text &optional table merge)
  7828. "Return percent escaped representation of TEXT.
  7829. TEXT is a string with the text to escape.
  7830. Optional argument TABLE is a list with characters that should be
  7831. escaped. When nil, `org-link-escape-chars' is used.
  7832. If optional argument MERGE is set, merge TABLE into
  7833. `org-link-escape-chars'."
  7834. (if (and org-url-encoding-use-url-hexify (not table))
  7835. (url-hexify-string text)
  7836. (cond
  7837. ((and table merge)
  7838. (mapc (lambda (defchr)
  7839. (unless (member defchr table)
  7840. (setq table (cons defchr table)))) org-link-escape-chars))
  7841. ((null table)
  7842. (setq table org-link-escape-chars)))
  7843. (mapconcat
  7844. (lambda (char)
  7845. (if (or (member char table)
  7846. (< char 32) (= char 37) (> char 126))
  7847. (mapconcat (lambda (sequence-element)
  7848. (format "%%%.2X" sequence-element))
  7849. (or (encode-coding-char char 'utf-8)
  7850. (error "Unable to percent escape character: %s"
  7851. (char-to-string char))) "")
  7852. (char-to-string char))) text "")))
  7853. (defun org-link-unescape (str)
  7854. "Unhex hexified unicode strings as returned from the JavaScript function
  7855. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  7856. (unless (and (null str) (string= "" str))
  7857. (let ((pos 0) (case-fold-search t) unhexed)
  7858. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  7859. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  7860. (setq str (replace-match unhexed t t str))
  7861. (setq pos (+ pos (length unhexed))))))
  7862. str)
  7863. (defun org-link-unescape-compound (hex)
  7864. "Unhexify unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  7865. Note: this function also decodes single byte encodings like
  7866. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  7867. (save-match-data
  7868. (let* ((bytes (cdr (split-string hex "%")))
  7869. (ret "")
  7870. (eat 0)
  7871. (sum 0))
  7872. (while bytes
  7873. (let* ((val (string-to-number (pop bytes) 16))
  7874. (shift-xor
  7875. (if (= 0 eat)
  7876. (cond
  7877. ((>= val 252) (cons 6 252))
  7878. ((>= val 248) (cons 5 248))
  7879. ((>= val 240) (cons 4 240))
  7880. ((>= val 224) (cons 3 224))
  7881. ((>= val 192) (cons 2 192))
  7882. (t (cons 0 0)))
  7883. (cons 6 128))))
  7884. (if (>= val 192) (setq eat (car shift-xor)))
  7885. (setq val (logxor val (cdr shift-xor)))
  7886. (setq sum (+ (lsh sum (car shift-xor)) val))
  7887. (if (> eat 0) (setq eat (- eat 1)))
  7888. (cond
  7889. ((= 0 eat) ;multi byte
  7890. (setq ret (concat ret (org-char-to-string sum)))
  7891. (setq sum 0))
  7892. ((not bytes) ; single byte(s)
  7893. (setq ret (org-link-unescape-single-byte-sequence hex))))
  7894. )) ;; end (while bytes
  7895. ret )))
  7896. (defun org-link-unescape-single-byte-sequence (hex)
  7897. "Unhexify hex-encoded single byte character sequences."
  7898. (mapconcat (lambda (byte)
  7899. (char-to-string (string-to-number byte 16)))
  7900. (cdr (split-string hex "%")) ""))
  7901. (defun org-xor (a b)
  7902. "Exclusive or."
  7903. (if a (not b) b))
  7904. (defun org-fixup-message-id-for-http (s)
  7905. "Replace special characters in a message id, so it can be used in an http query."
  7906. (when (string-match "%" s)
  7907. (setq s (mapconcat (lambda (c)
  7908. (if (eq c ?%)
  7909. "%25"
  7910. (char-to-string c)))
  7911. s "")))
  7912. (while (string-match "<" s)
  7913. (setq s (replace-match "%3C" t t s)))
  7914. (while (string-match ">" s)
  7915. (setq s (replace-match "%3E" t t s)))
  7916. (while (string-match "@" s)
  7917. (setq s (replace-match "%40" t t s)))
  7918. s)
  7919. ;;;###autoload
  7920. (defun org-insert-link-global ()
  7921. "Insert a link like Org-mode does.
  7922. This command can be called in any mode to insert a link in Org-mode syntax."
  7923. (interactive)
  7924. (org-load-modules-maybe)
  7925. (org-run-like-in-org-mode 'org-insert-link))
  7926. (defun org-insert-link (&optional complete-file link-location)
  7927. "Insert a link. At the prompt, enter the link.
  7928. Completion can be used to insert any of the link protocol prefixes like
  7929. http or ftp in use.
  7930. The history can be used to select a link previously stored with
  7931. `org-store-link'. When the empty string is entered (i.e. if you just
  7932. press RET at the prompt), the link defaults to the most recently
  7933. stored link. As SPC triggers completion in the minibuffer, you need to
  7934. use M-SPC or C-q SPC to force the insertion of a space character.
  7935. You will also be prompted for a description, and if one is given, it will
  7936. be displayed in the buffer instead of the link.
  7937. If there is already a link at point, this command will allow you to edit link
  7938. and description parts.
  7939. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7940. be selected using completion. The path to the file will be relative to the
  7941. current directory if the file is in the current directory or a subdirectory.
  7942. Otherwise, the link will be the absolute path as completed in the minibuffer
  7943. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7944. option `org-link-file-path-type'.
  7945. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7946. the current directory or below.
  7947. With three \\[universal-argument] prefixes, negate the meaning of
  7948. `org-keep-stored-link-after-insertion'.
  7949. If `org-make-link-description-function' is non-nil, this function will be
  7950. called with the link target, and the result will be the default
  7951. link description.
  7952. If the LINK-LOCATION parameter is non-nil, this value will be
  7953. used as the link location instead of reading one interactively."
  7954. (interactive "P")
  7955. (let* ((wcf (current-window-configuration))
  7956. (region (if (org-region-active-p)
  7957. (buffer-substring (region-beginning) (region-end))))
  7958. (remove (and region (list (region-beginning) (region-end))))
  7959. (desc region)
  7960. tmphist ; byte-compile incorrectly complains about this
  7961. (link link-location)
  7962. entry file all-prefixes)
  7963. (cond
  7964. (link-location) ; specified by arg, just use it.
  7965. ((org-in-regexp org-bracket-link-regexp 1)
  7966. ;; We do have a link at point, and we are going to edit it.
  7967. (setq remove (list (match-beginning 0) (match-end 0)))
  7968. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7969. (setq link (read-string "Link: "
  7970. (org-link-unescape
  7971. (org-match-string-no-properties 1)))))
  7972. ((or (org-in-regexp org-angle-link-re)
  7973. (org-in-regexp org-plain-link-re))
  7974. ;; Convert to bracket link
  7975. (setq remove (list (match-beginning 0) (match-end 0))
  7976. link (read-string "Link: "
  7977. (org-remove-angle-brackets (match-string 0)))))
  7978. ((member complete-file '((4) (16)))
  7979. ;; Completing read for file names.
  7980. (setq link (org-file-complete-link complete-file)))
  7981. (t
  7982. ;; Read link, with completion for stored links.
  7983. (with-output-to-temp-buffer "*Org Links*"
  7984. (princ "Insert a link.
  7985. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7986. (when org-stored-links
  7987. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7988. (princ (mapconcat
  7989. (lambda (x)
  7990. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7991. (reverse org-stored-links) "\n"))))
  7992. (let ((cw (selected-window)))
  7993. (select-window (get-buffer-window "*Org Links*" 'visible))
  7994. (with-current-buffer "*Org Links*" (setq truncate-lines) t)
  7995. (unless (pos-visible-in-window-p (point-max))
  7996. (org-fit-window-to-buffer))
  7997. (and (window-live-p cw) (select-window cw)))
  7998. ;; Fake a link history, containing the stored links.
  7999. (setq tmphist (append (mapcar 'car org-stored-links)
  8000. org-insert-link-history))
  8001. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8002. (mapcar 'car org-link-abbrev-alist)
  8003. org-link-types))
  8004. (unwind-protect
  8005. (progn
  8006. (setq link
  8007. (let ((org-completion-use-ido nil)
  8008. (org-completion-use-iswitchb nil))
  8009. (org-completing-read
  8010. "Link: "
  8011. (append
  8012. (mapcar (lambda (x) (list (concat x ":")))
  8013. all-prefixes)
  8014. (mapcar 'car org-stored-links))
  8015. nil nil nil
  8016. 'tmphist
  8017. (car (car org-stored-links)))))
  8018. (if (not (string-match "\\S-" link))
  8019. (error "No link selected"))
  8020. (if (or (member link all-prefixes)
  8021. (and (equal ":" (substring link -1))
  8022. (member (substring link 0 -1) all-prefixes)
  8023. (setq link (substring link 0 -1))))
  8024. (setq link (org-link-try-special-completion link))))
  8025. (set-window-configuration wcf)
  8026. (kill-buffer "*Org Links*"))
  8027. (setq entry (assoc link org-stored-links))
  8028. (or entry (push link org-insert-link-history))
  8029. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8030. (not org-keep-stored-link-after-insertion))
  8031. (setq org-stored-links (delq (assoc link org-stored-links)
  8032. org-stored-links)))
  8033. (setq desc (or desc (nth 1 entry)))))
  8034. (if (string-match org-plain-link-re link)
  8035. ;; URL-like link, normalize the use of angular brackets.
  8036. (setq link (org-make-link (org-remove-angle-brackets link))))
  8037. ;; Check if we are linking to the current file with a search option
  8038. ;; If yes, simplify the link by using only the search option.
  8039. (when (and buffer-file-name
  8040. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8041. (let* ((path (match-string 1 link))
  8042. (case-fold-search nil)
  8043. (search (match-string 2 link)))
  8044. (save-match-data
  8045. (if (equal (file-truename buffer-file-name) (file-truename path))
  8046. ;; We are linking to this same file, with a search option
  8047. (setq link search)))))
  8048. ;; Check if we can/should use a relative path. If yes, simplify the link
  8049. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8050. (let* ((type (match-string 1 link))
  8051. (path (match-string 2 link))
  8052. (origpath path)
  8053. (case-fold-search nil))
  8054. (cond
  8055. ((or (eq org-link-file-path-type 'absolute)
  8056. (equal complete-file '(16)))
  8057. (setq path (abbreviate-file-name (expand-file-name path))))
  8058. ((eq org-link-file-path-type 'noabbrev)
  8059. (setq path (expand-file-name path)))
  8060. ((eq org-link-file-path-type 'relative)
  8061. (setq path (file-relative-name path)))
  8062. (t
  8063. (save-match-data
  8064. (if (string-match (concat "^" (regexp-quote
  8065. (expand-file-name
  8066. (file-name-as-directory
  8067. default-directory))))
  8068. (expand-file-name path))
  8069. ;; We are linking a file with relative path name.
  8070. (setq path (substring (expand-file-name path)
  8071. (match-end 0)))
  8072. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8073. (setq link (concat type path))
  8074. (if (equal desc origpath)
  8075. (setq desc path))))
  8076. (if org-make-link-description-function
  8077. (setq desc (funcall org-make-link-description-function link desc)))
  8078. (setq desc (read-string "Description: " desc))
  8079. (unless (string-match "\\S-" desc) (setq desc nil))
  8080. (if remove (apply 'delete-region remove))
  8081. (insert (org-make-link-string link desc))))
  8082. (defun org-link-try-special-completion (type)
  8083. "If there is completion support for link type TYPE, offer it."
  8084. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8085. (if (functionp fun)
  8086. (funcall fun)
  8087. (read-string "Link (no completion support): " (concat type ":")))))
  8088. (defun org-file-complete-link (&optional arg)
  8089. "Create a file link using completion."
  8090. (let (file link)
  8091. (setq file (read-file-name "File: "))
  8092. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8093. (pwd1 (file-name-as-directory (abbreviate-file-name
  8094. (expand-file-name ".")))))
  8095. (cond
  8096. ((equal arg '(16))
  8097. (setq link (org-make-link
  8098. "file:"
  8099. (abbreviate-file-name (expand-file-name file)))))
  8100. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8101. (setq link (org-make-link "file:" (match-string 1 file))))
  8102. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8103. (expand-file-name file))
  8104. (setq link (org-make-link
  8105. "file:" (match-string 1 (expand-file-name file)))))
  8106. (t (setq link (org-make-link "file:" file)))))
  8107. link))
  8108. (defun org-completing-read (&rest args)
  8109. "Completing-read with SPACE being a normal character."
  8110. (let ((minibuffer-local-completion-map
  8111. (copy-keymap minibuffer-local-completion-map)))
  8112. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8113. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8114. (apply 'org-icompleting-read args)))
  8115. (defun org-completing-read-no-i (&rest args)
  8116. (let (org-completion-use-ido org-completion-use-iswitchb)
  8117. (apply 'org-completing-read args)))
  8118. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8119. "Use iswitch as a completing-read replacement to choose from choices.
  8120. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8121. from."
  8122. (let* ((iswitchb-use-virtual-buffers nil)
  8123. (iswitchb-make-buflist-hook
  8124. (lambda ()
  8125. (setq iswitchb-temp-buflist choices))))
  8126. (iswitchb-read-buffer prompt)))
  8127. (defun org-icompleting-read (&rest args)
  8128. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8129. (org-without-partial-completion
  8130. (if (and org-completion-use-ido
  8131. (fboundp 'ido-completing-read)
  8132. (boundp 'ido-mode) ido-mode
  8133. (listp (second args)))
  8134. (let ((ido-enter-matching-directory nil))
  8135. (apply 'ido-completing-read (concat (car args))
  8136. (if (consp (car (nth 1 args)))
  8137. (mapcar 'car (nth 1 args))
  8138. (nth 1 args))
  8139. (cddr args)))
  8140. (if (and org-completion-use-iswitchb
  8141. (boundp 'iswitchb-mode) iswitchb-mode
  8142. (listp (second args)))
  8143. (apply 'org-iswitchb-completing-read (concat (car args))
  8144. (if (consp (car (nth 1 args)))
  8145. (mapcar 'car (nth 1 args))
  8146. (nth 1 args))
  8147. (cddr args))
  8148. (apply 'completing-read args)))))
  8149. (defun org-extract-attributes (s)
  8150. "Extract the attributes cookie from a string and set as text property."
  8151. (let (a attr (start 0) key value)
  8152. (save-match-data
  8153. (when (string-match "{{\\([^}]+\\)}}$" s)
  8154. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8155. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8156. (setq key (match-string 1 a) value (match-string 2 a)
  8157. start (match-end 0)
  8158. attr (plist-put attr (intern key) value))))
  8159. (org-add-props s nil 'org-attr attr))
  8160. s))
  8161. (defun org-extract-attributes-from-string (tag)
  8162. (let (key value attr)
  8163. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8164. (setq key (match-string 1 tag) value (match-string 2 tag)
  8165. tag (replace-match "" t t tag)
  8166. attr (plist-put attr (intern key) value)))
  8167. (cons tag attr)))
  8168. (defun org-attributes-to-string (plist)
  8169. "Format a property list into an HTML attribute list."
  8170. (let ((s "") key value)
  8171. (while plist
  8172. (setq key (pop plist) value (pop plist))
  8173. (and value
  8174. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8175. s))
  8176. ;;; Opening/following a link
  8177. (defvar org-link-search-failed nil)
  8178. (defvar org-open-link-functions nil
  8179. "Hook for functions finding a plain text link.
  8180. These functions must take a single argument, the link content.
  8181. They will be called for links that look like [[link text][description]]
  8182. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8183. like a filename (e.g. \"./blue.png\").
  8184. These functions will be called *before* Org attempts to resolve the
  8185. link by doing text searches in the current buffer - so if you want a
  8186. link \"[[target]]\" to still find \"<<target>>\", your function should
  8187. handle this as a special case.
  8188. When the function does handle the link, it must return a non-nil value.
  8189. If it decides that it is not responsible for this link, it must return
  8190. nil to indicate that that Org-mode can continue with other options
  8191. like exact and fuzzy text search.")
  8192. (defun org-next-link ()
  8193. "Move forward to the next link.
  8194. If the link is in hidden text, expose it."
  8195. (interactive)
  8196. (when (and org-link-search-failed (eq this-command last-command))
  8197. (goto-char (point-min))
  8198. (message "Link search wrapped back to beginning of buffer"))
  8199. (setq org-link-search-failed nil)
  8200. (let* ((pos (point))
  8201. (ct (org-context))
  8202. (a (assoc :link ct)))
  8203. (if a (goto-char (nth 2 a)))
  8204. (if (re-search-forward org-any-link-re nil t)
  8205. (progn
  8206. (goto-char (match-beginning 0))
  8207. (if (outline-invisible-p) (org-show-context)))
  8208. (goto-char pos)
  8209. (setq org-link-search-failed t)
  8210. (error "No further link found"))))
  8211. (defun org-previous-link ()
  8212. "Move backward to the previous link.
  8213. If the link is in hidden text, expose it."
  8214. (interactive)
  8215. (when (and org-link-search-failed (eq this-command last-command))
  8216. (goto-char (point-max))
  8217. (message "Link search wrapped back to end of buffer"))
  8218. (setq org-link-search-failed nil)
  8219. (let* ((pos (point))
  8220. (ct (org-context))
  8221. (a (assoc :link ct)))
  8222. (if a (goto-char (nth 1 a)))
  8223. (if (re-search-backward org-any-link-re nil t)
  8224. (progn
  8225. (goto-char (match-beginning 0))
  8226. (if (outline-invisible-p) (org-show-context)))
  8227. (goto-char pos)
  8228. (setq org-link-search-failed t)
  8229. (error "No further link found"))))
  8230. (defun org-translate-link (s)
  8231. "Translate a link string if a translation function has been defined."
  8232. (if (and org-link-translation-function
  8233. (fboundp org-link-translation-function)
  8234. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8235. (progn
  8236. (setq s (funcall org-link-translation-function
  8237. (match-string 1) (match-string 2)))
  8238. (concat (car s) ":" (cdr s)))
  8239. s))
  8240. (defun org-translate-link-from-planner (type path)
  8241. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8242. This is still an experimental function, your mileage may vary."
  8243. (cond
  8244. ((member type '("http" "https" "news" "ftp"))
  8245. ;; standard Internet links are the same.
  8246. nil)
  8247. ((and (equal type "irc") (string-match "^//" path))
  8248. ;; Planner has two / at the beginning of an irc link, we have 1.
  8249. ;; We should have zero, actually....
  8250. (setq path (substring path 1)))
  8251. ((and (equal type "lisp") (string-match "^/" path))
  8252. ;; Planner has a slash, we do not.
  8253. (setq type "elisp" path (substring path 1)))
  8254. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8255. ;; A typical message link. Planner has the id after the final slash,
  8256. ;; we separate it with a hash mark
  8257. (setq path (concat (match-string 1 path) "#"
  8258. (org-remove-angle-brackets (match-string 2 path)))))
  8259. )
  8260. (cons type path))
  8261. (defun org-find-file-at-mouse (ev)
  8262. "Open file link or URL at mouse."
  8263. (interactive "e")
  8264. (mouse-set-point ev)
  8265. (org-open-at-point 'in-emacs))
  8266. (defun org-open-at-mouse (ev)
  8267. "Open file link or URL at mouse."
  8268. (interactive "e")
  8269. (mouse-set-point ev)
  8270. (if (eq major-mode 'org-agenda-mode)
  8271. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8272. (org-open-at-point))
  8273. (defvar org-window-config-before-follow-link nil
  8274. "The window configuration before following a link.
  8275. This is saved in case the need arises to restore it.")
  8276. (defvar org-open-link-marker (make-marker)
  8277. "Marker pointing to the location where `org-open-at-point; was called.")
  8278. ;;;###autoload
  8279. (defun org-open-at-point-global ()
  8280. "Follow a link like Org-mode does.
  8281. This command can be called in any mode to follow a link that has
  8282. Org-mode syntax."
  8283. (interactive)
  8284. (org-run-like-in-org-mode 'org-open-at-point))
  8285. ;;;###autoload
  8286. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8287. "Open a link in the string S, as if it was in Org-mode."
  8288. (interactive "sLink: \nP")
  8289. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8290. (with-temp-buffer
  8291. (let ((org-inhibit-startup t))
  8292. (org-mode)
  8293. (insert s)
  8294. (goto-char (point-min))
  8295. (when reference-buffer
  8296. (setq org-link-abbrev-alist-local
  8297. (with-current-buffer reference-buffer
  8298. org-link-abbrev-alist-local)))
  8299. (org-open-at-point arg reference-buffer)))))
  8300. (defvar org-open-at-point-functions nil
  8301. "Hook that is run when following a link at point.
  8302. Functions in this hook must return t if they identify and follow
  8303. a link at point. If they don't find anything interesting at point,
  8304. they must return nil.")
  8305. (defun org-open-at-point (&optional arg reference-buffer)
  8306. "Open link at or after point.
  8307. If there is no link at point, this function will search forward up to
  8308. the end of the current line.
  8309. Normally, files will be opened by an appropriate application. If the
  8310. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8311. With a double prefix argument, try to open outside of Emacs, in the
  8312. application the system uses for this file type."
  8313. (interactive "P")
  8314. ;; if in a code block, then open the block's results
  8315. (unless (call-interactively #'org-babel-open-src-block-result)
  8316. (org-load-modules-maybe)
  8317. (move-marker org-open-link-marker (point))
  8318. (setq org-window-config-before-follow-link (current-window-configuration))
  8319. (org-remove-occur-highlights nil nil t)
  8320. (cond
  8321. ((and (org-on-heading-p)
  8322. (not (org-in-regexp
  8323. (concat org-plain-link-re "\\|"
  8324. org-bracket-link-regexp "\\|"
  8325. org-angle-link-re "\\|"
  8326. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8327. (not (get-text-property (point) 'org-linked-text)))
  8328. (or (org-offer-links-in-entry arg)
  8329. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8330. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8331. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8332. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8333. (not (org-in-regexp org-bracket-link-regexp)))
  8334. (org-footnote-action))
  8335. (t
  8336. (let (type path link line search (pos (point)))
  8337. (catch 'match
  8338. (save-excursion
  8339. (skip-chars-forward "^]\n\r")
  8340. (when (org-in-regexp org-bracket-link-regexp 1)
  8341. (setq link (org-extract-attributes
  8342. (org-link-unescape (org-match-string-no-properties 1))))
  8343. (while (string-match " *\n *" link)
  8344. (setq link (replace-match " " t t link)))
  8345. (setq link (org-link-expand-abbrev link))
  8346. (cond
  8347. ((or (file-name-absolute-p link)
  8348. (string-match "^\\.\\.?/" link))
  8349. (setq type "file" path link))
  8350. ((string-match org-link-re-with-space3 link)
  8351. (setq type (match-string 1 link) path (match-string 2 link)))
  8352. (t (setq type "thisfile" path link)))
  8353. (throw 'match t)))
  8354. (when (get-text-property (point) 'org-linked-text)
  8355. (setq type "thisfile"
  8356. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8357. (1+ (point)) (point))
  8358. path (buffer-substring
  8359. (or (previous-single-property-change pos 'org-linked-text)
  8360. (point-min))
  8361. (or (next-single-property-change pos 'org-linked-text)
  8362. (point-max))))
  8363. (throw 'match t))
  8364. (save-excursion
  8365. (when (or (org-in-regexp org-angle-link-re)
  8366. (org-in-regexp org-plain-link-re))
  8367. (setq type (match-string 1) path (match-string 2))
  8368. (throw 'match t)))
  8369. (save-excursion
  8370. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8371. (setq type "tags"
  8372. path (match-string 1))
  8373. (while (string-match ":" path)
  8374. (setq path (replace-match "+" t t path)))
  8375. (throw 'match t)))
  8376. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8377. (setq type "tree-match"
  8378. path (match-string 1))
  8379. (throw 'match t)))
  8380. (unless path
  8381. (error "No link found"))
  8382. ;; switch back to reference buffer
  8383. ;; needed when if called in a temporary buffer through
  8384. ;; org-open-link-from-string
  8385. (with-current-buffer (or reference-buffer (current-buffer))
  8386. ;; Remove any trailing spaces in path
  8387. (if (string-match " +\\'" path)
  8388. (setq path (replace-match "" t t path)))
  8389. (if (and org-link-translation-function
  8390. (fboundp org-link-translation-function))
  8391. ;; Check if we need to translate the link
  8392. (let ((tmp (funcall org-link-translation-function type path)))
  8393. (setq type (car tmp) path (cdr tmp))))
  8394. (cond
  8395. ((assoc type org-link-protocols)
  8396. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8397. ((equal type "mailto")
  8398. (let ((cmd (car org-link-mailto-program))
  8399. (args (cdr org-link-mailto-program)) args1
  8400. (address path) (subject "") a)
  8401. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8402. (setq address (match-string 1 path)
  8403. subject (org-link-escape (match-string 2 path))))
  8404. (while args
  8405. (cond
  8406. ((not (stringp (car args))) (push (pop args) args1))
  8407. (t (setq a (pop args))
  8408. (if (string-match "%a" a)
  8409. (setq a (replace-match address t t a)))
  8410. (if (string-match "%s" a)
  8411. (setq a (replace-match subject t t a)))
  8412. (push a args1))))
  8413. (apply cmd (nreverse args1))))
  8414. ((member type '("http" "https" "ftp" "news"))
  8415. (browse-url (concat type ":" (org-link-escape
  8416. path org-link-escape-chars-browser))))
  8417. ((string= type "doi")
  8418. (browse-url (concat "http://dx.doi.org/"
  8419. (org-link-escape
  8420. path org-link-escape-chars-browser))))
  8421. ((member type '("message"))
  8422. (browse-url (concat type ":" path)))
  8423. ((string= type "tags")
  8424. (org-tags-view arg path))
  8425. ((string= type "tree-match")
  8426. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8427. ((string= type "file")
  8428. (if (string-match "::\\([0-9]+\\)\\'" path)
  8429. (setq line (string-to-number (match-string 1 path))
  8430. path (substring path 0 (match-beginning 0)))
  8431. (if (string-match "::\\(.+\\)\\'" path)
  8432. (setq search (match-string 1 path)
  8433. path (substring path 0 (match-beginning 0)))))
  8434. (if (string-match "[*?{]" (file-name-nondirectory path))
  8435. (dired path)
  8436. (org-open-file path arg line search)))
  8437. ((string= type "shell")
  8438. (let ((cmd path))
  8439. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8440. (string-match org-confirm-shell-link-not-regexp cmd))
  8441. (not org-confirm-shell-link-function)
  8442. (funcall org-confirm-shell-link-function
  8443. (format "Execute \"%s\" in shell? "
  8444. (org-add-props cmd nil
  8445. 'face 'org-warning))))
  8446. (progn
  8447. (message "Executing %s" cmd)
  8448. (shell-command cmd))
  8449. (error "Abort"))))
  8450. ((string= type "elisp")
  8451. (let ((cmd path))
  8452. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8453. (string-match org-confirm-elisp-link-not-regexp cmd))
  8454. (not org-confirm-elisp-link-function)
  8455. (funcall org-confirm-elisp-link-function
  8456. (format "Execute \"%s\" as elisp? "
  8457. (org-add-props cmd nil
  8458. 'face 'org-warning))))
  8459. (message "%s => %s" cmd
  8460. (if (equal (string-to-char cmd) ?\()
  8461. (eval (read cmd))
  8462. (call-interactively (read cmd))))
  8463. (error "Abort"))))
  8464. ((and (string= type "thisfile")
  8465. (run-hook-with-args-until-success
  8466. 'org-open-link-functions path)))
  8467. ((string= type "thisfile")
  8468. (if arg
  8469. (switch-to-buffer-other-window
  8470. (org-get-buffer-for-internal-link (current-buffer)))
  8471. (org-mark-ring-push))
  8472. (let ((cmd `(org-link-search
  8473. ,path
  8474. ,(cond ((equal arg '(4)) ''occur)
  8475. ((equal arg '(16)) ''org-occur)
  8476. (t nil))
  8477. ,pos)))
  8478. (condition-case nil (eval cmd)
  8479. (error (progn (widen) (eval cmd))))))
  8480. (t
  8481. (browse-url-at-point)))))))
  8482. (move-marker org-open-link-marker nil)
  8483. (run-hook-with-args 'org-follow-link-hook)))
  8484. (defun org-offer-links-in-entry (&optional nth zero)
  8485. "Offer links in the current entry and follow the selected link.
  8486. If there is only one link, follow it immediately as well.
  8487. If NTH is an integer, immediately pick the NTH link found.
  8488. If ZERO is a string, check also this string for a link, and if
  8489. there is one, offer it as link number zero."
  8490. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8491. "\\(" org-angle-link-re "\\)\\|"
  8492. "\\(" org-plain-link-re "\\)"))
  8493. (cnt ?0)
  8494. (in-emacs (if (integerp nth) nil nth))
  8495. have-zero end links link c)
  8496. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8497. (push (match-string 0 zero) links)
  8498. (setq cnt (1- cnt) have-zero t))
  8499. (save-excursion
  8500. (org-back-to-heading t)
  8501. (setq end (save-excursion (outline-next-heading) (point)))
  8502. (while (re-search-forward re end t)
  8503. (push (match-string 0) links))
  8504. (setq links (org-uniquify (reverse links))))
  8505. (cond
  8506. ((null links)
  8507. (message "No links"))
  8508. ((equal (length links) 1)
  8509. (setq link (list (car links))))
  8510. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8511. (setq link (nth (if have-zero nth (1- nth)) links)))
  8512. (t ; we have to select a link
  8513. (save-excursion
  8514. (save-window-excursion
  8515. (delete-other-windows)
  8516. (with-output-to-temp-buffer "*Select Link*"
  8517. (mapc (lambda (l)
  8518. (if (not (string-match org-bracket-link-regexp l))
  8519. (princ (format "[%c] %s\n" (incf cnt)
  8520. (org-remove-angle-brackets l)))
  8521. (if (match-end 3)
  8522. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8523. (match-string 3 l) (match-string 1 l)))
  8524. (princ (format "[%c] %s\n" (incf cnt)
  8525. (match-string 1 l))))))
  8526. links))
  8527. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8528. (message "Select link to open, RET to open all:")
  8529. (setq c (read-char-exclusive))
  8530. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8531. (when (equal c ?q) (error "Abort"))
  8532. (if (equal c ?\C-m)
  8533. (setq link links)
  8534. (setq nth (- c ?0))
  8535. (if have-zero (setq nth (1+ nth)))
  8536. (unless (and (integerp nth) (>= (length links) nth))
  8537. (error "Invalid link selection"))
  8538. (setq link (list (nth (1- nth) links))))))
  8539. (if link
  8540. (let ((buf (current-buffer)))
  8541. (dolist (l link)
  8542. (org-open-link-from-string l in-emacs buf))
  8543. t)
  8544. nil)))
  8545. ;; Add special file links that specify the way of opening
  8546. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8547. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8548. (defun org-open-file-with-system (path)
  8549. "Open file at PATH using the system way of opening it."
  8550. (org-open-file path 'system))
  8551. (defun org-open-file-with-emacs (path)
  8552. "Open file at PATH in Emacs."
  8553. (org-open-file path 'emacs))
  8554. (defun org-remove-file-link-modifiers ()
  8555. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8556. (goto-char (point-min))
  8557. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8558. (org-if-unprotected
  8559. (replace-match "file:" t t))))
  8560. (eval-after-load "org-exp"
  8561. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8562. 'org-remove-file-link-modifiers))
  8563. ;;;; Time estimates
  8564. (defun org-get-effort (&optional pom)
  8565. "Get the effort estimate for the current entry."
  8566. (org-entry-get pom org-effort-property))
  8567. ;;; File search
  8568. (defvar org-create-file-search-functions nil
  8569. "List of functions to construct the right search string for a file link.
  8570. These functions are called in turn with point at the location to
  8571. which the link should point.
  8572. A function in the hook should first test if it would like to
  8573. handle this file type, for example by checking the `major-mode'
  8574. or the file extension. If it decides not to handle this file, it
  8575. should just return nil to give other functions a chance. If it
  8576. does handle the file, it must return the search string to be used
  8577. when following the link. The search string will be part of the
  8578. file link, given after a double colon, and `org-open-at-point'
  8579. will automatically search for it. If special measures must be
  8580. taken to make the search successful, another function should be
  8581. added to the companion hook `org-execute-file-search-functions',
  8582. which see.
  8583. A function in this hook may also use `setq' to set the variable
  8584. `description' to provide a suggestion for the descriptive text to
  8585. be used for this link when it gets inserted into an Org-mode
  8586. buffer with \\[org-insert-link].")
  8587. (defvar org-execute-file-search-functions nil
  8588. "List of functions to execute a file search triggered by a link.
  8589. Functions added to this hook must accept a single argument, the
  8590. search string that was part of the file link, the part after the
  8591. double colon. The function must first check if it would like to
  8592. handle this search, for example by checking the `major-mode' or
  8593. the file extension. If it decides not to handle this search, it
  8594. should just return nil to give other functions a chance. If it
  8595. does handle the search, it must return a non-nil value to keep
  8596. other functions from trying.
  8597. Each function can access the current prefix argument through the
  8598. variable `current-prefix-argument'. Note that a single prefix is
  8599. used to force opening a link in Emacs, so it may be good to only
  8600. use a numeric or double prefix to guide the search function.
  8601. In case this is needed, a function in this hook can also restore
  8602. the window configuration before `org-open-at-point' was called using:
  8603. (set-window-configuration org-window-config-before-follow-link)")
  8604. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8605. (defun org-link-search (s &optional type avoid-pos)
  8606. "Search for a link search option.
  8607. If S is surrounded by forward slashes, it is interpreted as a
  8608. regular expression. In org-mode files, this will create an `org-occur'
  8609. sparse tree. In ordinary files, `occur' will be used to list matches.
  8610. If the current buffer is in `dired-mode', grep will be used to search
  8611. in all files. If AVOID-POS is given, ignore matches near that position."
  8612. (let ((case-fold-search t)
  8613. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8614. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8615. (append '(("") (" ") ("\t") ("\n"))
  8616. org-emphasis-alist)
  8617. "\\|") "\\)"))
  8618. (pos (point))
  8619. (pre nil) (post nil)
  8620. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8621. (cond
  8622. ;; First check if there are any special search functions
  8623. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8624. ;; Now try the builtin stuff
  8625. ((and (equal (string-to-char s0) ?#)
  8626. (> (length s0) 1)
  8627. (save-excursion
  8628. (goto-char (point-min))
  8629. (and
  8630. (re-search-forward
  8631. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8632. (setq type 'dedicated
  8633. pos (match-beginning 0))))
  8634. ;; There is an exact target for this
  8635. (goto-char pos)
  8636. (org-back-to-heading t)))
  8637. ((save-excursion
  8638. (goto-char (point-min))
  8639. (and
  8640. (re-search-forward
  8641. (concat "<<" (regexp-quote s0) ">>") nil t)
  8642. (setq type 'dedicated
  8643. pos (match-beginning 0))))
  8644. ;; There is an exact target for this
  8645. (goto-char pos))
  8646. ((and (string-match "^(\\(.*\\))$" s0)
  8647. (save-excursion
  8648. (goto-char (point-min))
  8649. (and
  8650. (re-search-forward
  8651. (concat "[^[]" (regexp-quote
  8652. (format org-coderef-label-format
  8653. (match-string 1 s0))))
  8654. nil t)
  8655. (setq type 'dedicated
  8656. pos (1+ (match-beginning 0))))))
  8657. ;; There is a coderef target for this
  8658. (goto-char pos))
  8659. ((string-match "^/\\(.*\\)/$" s)
  8660. ;; A regular expression
  8661. (cond
  8662. ((org-mode-p)
  8663. (org-occur (match-string 1 s)))
  8664. ;;((eq major-mode 'dired-mode)
  8665. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8666. (t (org-do-occur (match-string 1 s)))))
  8667. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8668. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8669. (goto-char (point-min))
  8670. (cond
  8671. ((let (case-fold-search)
  8672. (re-search-forward (format org-complex-heading-regexp-format
  8673. (regexp-quote s))
  8674. nil t))
  8675. ;; OK, found a match
  8676. (setq type 'dedicated)
  8677. (goto-char (match-beginning 0)))
  8678. ((and (not org-link-search-inhibit-query)
  8679. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8680. (y-or-n-p "No match - create this as a new heading? "))
  8681. (goto-char (point-max))
  8682. (or (bolp) (newline))
  8683. (insert "* " s "\n")
  8684. (beginning-of-line 0))
  8685. (t
  8686. (goto-char pos)
  8687. (error "No match"))))
  8688. (t
  8689. ;; A normal search string
  8690. (when (equal (string-to-char s) ?*)
  8691. ;; Anchor on headlines, post may include tags.
  8692. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8693. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8694. s (substring s 1)))
  8695. (remove-text-properties
  8696. 0 (length s)
  8697. '(face nil mouse-face nil keymap nil fontified nil) s)
  8698. ;; Make a series of regular expressions to find a match
  8699. (setq words (org-split-string s "[ \n\r\t]+")
  8700. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8701. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8702. "\\)" markers)
  8703. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8704. re2a (concat "[ \t\r\n]" re2a_)
  8705. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8706. re4 (concat "[^a-zA-Z_]" re4_)
  8707. re1 (concat pre re2 post)
  8708. re3 (concat pre (if pre re4_ re4) post)
  8709. re5 (concat pre ".*" re4)
  8710. re2 (concat pre re2)
  8711. re2a (concat pre (if pre re2a_ re2a))
  8712. re4 (concat pre (if pre re4_ re4))
  8713. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8714. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8715. re5 "\\)"
  8716. ))
  8717. (cond
  8718. ((eq type 'org-occur) (org-occur reall))
  8719. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8720. (t (goto-char (point-min))
  8721. (setq type 'fuzzy)
  8722. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8723. (org-search-not-self 1 re1 nil t)
  8724. (org-search-not-self 1 re2 nil t)
  8725. (org-search-not-self 1 re2a nil t)
  8726. (org-search-not-self 1 re3 nil t)
  8727. (org-search-not-self 1 re4 nil t)
  8728. (org-search-not-self 1 re5 nil t)
  8729. )
  8730. (goto-char (match-beginning 1))
  8731. (goto-char pos)
  8732. (error "No match"))))))
  8733. (and (org-mode-p) (org-show-context 'link-search))
  8734. type))
  8735. (defun org-search-not-self (group &rest args)
  8736. "Execute `re-search-forward', but only accept matches that do not
  8737. enclose the position of `org-open-link-marker'."
  8738. (let ((m org-open-link-marker))
  8739. (catch 'exit
  8740. (while (apply 're-search-forward args)
  8741. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8742. (goto-char (match-end group))
  8743. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8744. (> (match-beginning 0) (marker-position m))
  8745. (< (match-end 0) (marker-position m)))
  8746. (save-match-data
  8747. (or (not (org-in-regexp
  8748. org-bracket-link-analytic-regexp 1))
  8749. (not (match-end 4)) ; no description
  8750. (and (<= (match-beginning 4) (point))
  8751. (>= (match-end 4) (point))))))
  8752. (throw 'exit (point))))))))
  8753. (defun org-get-buffer-for-internal-link (buffer)
  8754. "Return a buffer to be used for displaying the link target of internal links."
  8755. (cond
  8756. ((not org-display-internal-link-with-indirect-buffer)
  8757. buffer)
  8758. ((string-match "(Clone)$" (buffer-name buffer))
  8759. (message "Buffer is already a clone, not making another one")
  8760. ;; we also do not modify visibility in this case
  8761. buffer)
  8762. (t ; make a new indirect buffer for displaying the link
  8763. (let* ((bn (buffer-name buffer))
  8764. (ibn (concat bn "(Clone)"))
  8765. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8766. (with-current-buffer ib (org-overview))
  8767. ib))))
  8768. (defun org-do-occur (regexp &optional cleanup)
  8769. "Call the Emacs command `occur'.
  8770. If CLEANUP is non-nil, remove the printout of the regular expression
  8771. in the *Occur* buffer. This is useful if the regex is long and not useful
  8772. to read."
  8773. (occur regexp)
  8774. (when cleanup
  8775. (let ((cwin (selected-window)) win beg end)
  8776. (when (setq win (get-buffer-window "*Occur*"))
  8777. (select-window win))
  8778. (goto-char (point-min))
  8779. (when (re-search-forward "match[a-z]+" nil t)
  8780. (setq beg (match-end 0))
  8781. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8782. (setq end (1- (match-beginning 0)))))
  8783. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8784. (goto-char (point-min))
  8785. (select-window cwin))))
  8786. ;;; The mark ring for links jumps
  8787. (defvar org-mark-ring nil
  8788. "Mark ring for positions before jumps in Org-mode.")
  8789. (defvar org-mark-ring-last-goto nil
  8790. "Last position in the mark ring used to go back.")
  8791. ;; Fill and close the ring
  8792. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8793. (loop for i from 1 to org-mark-ring-length do
  8794. (push (make-marker) org-mark-ring))
  8795. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8796. org-mark-ring)
  8797. (defun org-mark-ring-push (&optional pos buffer)
  8798. "Put the current position or POS into the mark ring and rotate it."
  8799. (interactive)
  8800. (setq pos (or pos (point)))
  8801. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8802. (move-marker (car org-mark-ring)
  8803. (or pos (point))
  8804. (or buffer (current-buffer)))
  8805. (message "%s"
  8806. (substitute-command-keys
  8807. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8808. (defun org-mark-ring-goto (&optional n)
  8809. "Jump to the previous position in the mark ring.
  8810. With prefix arg N, jump back that many stored positions. When
  8811. called several times in succession, walk through the entire ring.
  8812. Org-mode commands jumping to a different position in the current file,
  8813. or to another Org-mode file, automatically push the old position
  8814. onto the ring."
  8815. (interactive "p")
  8816. (let (p m)
  8817. (if (eq last-command this-command)
  8818. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8819. (setq p org-mark-ring))
  8820. (setq org-mark-ring-last-goto p)
  8821. (setq m (car p))
  8822. (switch-to-buffer (marker-buffer m))
  8823. (goto-char m)
  8824. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8825. (defun org-remove-angle-brackets (s)
  8826. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8827. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8828. s)
  8829. (defun org-add-angle-brackets (s)
  8830. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8831. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8832. s)
  8833. (defun org-remove-double-quotes (s)
  8834. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8835. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8836. s)
  8837. ;;; Following specific links
  8838. (defun org-follow-timestamp-link ()
  8839. (cond
  8840. ((org-at-date-range-p t)
  8841. (let ((org-agenda-start-on-weekday)
  8842. (t1 (match-string 1))
  8843. (t2 (match-string 2)))
  8844. (setq t1 (time-to-days (org-time-string-to-time t1))
  8845. t2 (time-to-days (org-time-string-to-time t2)))
  8846. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8847. ((org-at-timestamp-p t)
  8848. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8849. (substring (match-string 1) 0 10)))
  8850. 1))
  8851. (t (error "This should not happen"))))
  8852. ;;; Following file links
  8853. (defvar org-wait nil)
  8854. (defun org-open-file (path &optional in-emacs line search)
  8855. "Open the file at PATH.
  8856. First, this expands any special file name abbreviations. Then the
  8857. configuration variable `org-file-apps' is checked if it contains an
  8858. entry for this file type, and if yes, the corresponding command is launched.
  8859. If no application is found, Emacs simply visits the file.
  8860. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8861. With a double \\[universal-argument] \\[universal-argument] \
  8862. prefix arg, Org tries to avoid opening in Emacs
  8863. and to use an external application to visit the file.
  8864. Optional LINE specifies a line to go to, optional SEARCH a string
  8865. to search for. If LINE or SEARCH is given, the file will be
  8866. opened in Emacs, unless an entry from org-file-apps that makes
  8867. use of groups in a regexp matches.
  8868. If the file does not exist, an error is thrown."
  8869. (let* ((file (if (equal path "")
  8870. buffer-file-name
  8871. (substitute-in-file-name (expand-file-name path))))
  8872. (file-apps (append org-file-apps (org-default-apps)))
  8873. (apps (org-remove-if
  8874. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8875. (apps-dlink (org-remove-if-not
  8876. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8877. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8878. (dirp (if remp nil (file-directory-p file)))
  8879. (file (if (and dirp org-open-directory-means-index-dot-org)
  8880. (concat (file-name-as-directory file) "index.org")
  8881. file))
  8882. (a-m-a-p (assq 'auto-mode apps))
  8883. (dfile (downcase file))
  8884. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8885. (link (cond ((and (eq line nil)
  8886. (eq search nil))
  8887. file)
  8888. (line
  8889. (concat file "::" (number-to-string line)))
  8890. (search
  8891. (concat file "::" search))))
  8892. (dlink (downcase link))
  8893. (old-buffer (current-buffer))
  8894. (old-pos (point))
  8895. (old-mode major-mode)
  8896. ext cmd link-match-data)
  8897. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8898. (setq ext (match-string 1 dfile))
  8899. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8900. (setq ext (match-string 1 dfile))))
  8901. (cond
  8902. ((member in-emacs '((16) system))
  8903. (setq cmd (cdr (assoc 'system apps))))
  8904. (in-emacs (setq cmd 'emacs))
  8905. (t
  8906. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8907. (and dirp (cdr (assoc 'directory apps)))
  8908. ; first, try matching against apps-dlink
  8909. ; if we get a match here, store the match data for later
  8910. (let ((match (assoc-default dlink apps-dlink
  8911. 'string-match)))
  8912. (if match
  8913. (progn (setq link-match-data (match-data))
  8914. match)
  8915. (progn (setq in-emacs (or in-emacs line search))
  8916. nil))) ; if we have no match in apps-dlink,
  8917. ; always open the file in emacs if line or search
  8918. ; is given (for backwards compatibility)
  8919. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8920. 'string-match)
  8921. (cdr (assoc ext apps))
  8922. (cdr (assoc t apps))))))
  8923. (when (eq cmd 'system)
  8924. (setq cmd (cdr (assoc 'system apps))))
  8925. (when (eq cmd 'default)
  8926. (setq cmd (cdr (assoc t apps))))
  8927. (when (eq cmd 'mailcap)
  8928. (require 'mailcap)
  8929. (mailcap-parse-mailcaps)
  8930. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8931. (command (mailcap-mime-info mime-type)))
  8932. (if (stringp command)
  8933. (setq cmd command)
  8934. (setq cmd 'emacs))))
  8935. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8936. (not (file-exists-p file))
  8937. (not org-open-non-existing-files))
  8938. (error "No such file: %s" file))
  8939. (cond
  8940. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8941. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8942. (while (string-match "['\"]%s['\"]" cmd)
  8943. (setq cmd (replace-match "%s" t t cmd)))
  8944. (while (string-match "%s" cmd)
  8945. (setq cmd (replace-match
  8946. (save-match-data
  8947. (shell-quote-argument
  8948. (convert-standard-filename file)))
  8949. t t cmd)))
  8950. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8951. (save-match-data
  8952. (let ((match-index 1)
  8953. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8954. (set-match-data link-match-data)
  8955. (while (<= match-index number-of-groups)
  8956. (let ((regex (concat "%" (number-to-string match-index)))
  8957. (replace-with (match-string match-index dlink)))
  8958. (while (string-match regex cmd)
  8959. (setq cmd (replace-match replace-with t t cmd))))
  8960. (setq match-index (+ match-index 1)))))
  8961. (save-window-excursion
  8962. (start-process-shell-command cmd nil cmd)
  8963. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8964. ))
  8965. ((or (stringp cmd)
  8966. (eq cmd 'emacs))
  8967. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8968. (widen)
  8969. (if line (org-goto-line line)
  8970. (if search (org-link-search search))))
  8971. ((consp cmd)
  8972. (let ((file (convert-standard-filename file)))
  8973. (save-match-data
  8974. (set-match-data link-match-data)
  8975. (eval cmd))))
  8976. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8977. (and (org-mode-p) (eq old-mode 'org-mode)
  8978. (or (not (equal old-buffer (current-buffer)))
  8979. (not (equal old-pos (point))))
  8980. (org-mark-ring-push old-pos old-buffer))))
  8981. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8982. "This function returns non-nil if `entry' uses a regular
  8983. expression which should be matched against the whole link by
  8984. org-open-file.
  8985. It assumes that is the case when the entry uses a regular
  8986. expression which has at least one grouping construct and the
  8987. action is either a lisp form or a command string containing
  8988. '%1', i.e. using at least one subexpression match as a
  8989. parameter."
  8990. (let ((selector (car entry))
  8991. (action (cdr entry)))
  8992. (if (stringp selector)
  8993. (and (> (regexp-opt-depth selector) 0)
  8994. (or (and (stringp action)
  8995. (string-match "%[0-9]" action))
  8996. (consp action)))
  8997. nil)))
  8998. (defun org-default-apps ()
  8999. "Return the default applications for this operating system."
  9000. (cond
  9001. ((eq system-type 'darwin)
  9002. org-file-apps-defaults-macosx)
  9003. ((eq system-type 'windows-nt)
  9004. org-file-apps-defaults-windowsnt)
  9005. (t org-file-apps-defaults-gnu)))
  9006. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9007. "Convert extensions to regular expressions in the cars of LIST.
  9008. Also, weed out any non-string entries, because the return value is used
  9009. only for regexp matching.
  9010. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9011. point to the symbol `emacs', indicating that the file should
  9012. be opened in Emacs."
  9013. (append
  9014. (delq nil
  9015. (mapcar (lambda (x)
  9016. (if (not (stringp (car x)))
  9017. nil
  9018. (if (string-match "\\W" (car x))
  9019. x
  9020. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9021. list))
  9022. (if add-auto-mode
  9023. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9024. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9025. (defun org-file-remote-p (file)
  9026. "Test whether FILE specifies a location on a remote system.
  9027. Return non-nil if the location is indeed remote.
  9028. For example, the filename \"/user@host:/foo\" specifies a location
  9029. on the system \"/user@host:\"."
  9030. (cond ((fboundp 'file-remote-p)
  9031. (file-remote-p file))
  9032. ((fboundp 'tramp-handle-file-remote-p)
  9033. (tramp-handle-file-remote-p file))
  9034. ((and (boundp 'ange-ftp-name-format)
  9035. (string-match (car ange-ftp-name-format) file))
  9036. t)
  9037. (t nil)))
  9038. ;;;; Refiling
  9039. (defun org-get-org-file ()
  9040. "Read a filename, with default directory `org-directory'."
  9041. (let ((default (or org-default-notes-file remember-data-file)))
  9042. (read-file-name (format "File name [%s]: " default)
  9043. (file-name-as-directory org-directory)
  9044. default)))
  9045. (defun org-notes-order-reversed-p ()
  9046. "Check if the current file should receive notes in reversed order."
  9047. (cond
  9048. ((not org-reverse-note-order) nil)
  9049. ((eq t org-reverse-note-order) t)
  9050. ((not (listp org-reverse-note-order)) nil)
  9051. (t (catch 'exit
  9052. (let ((all org-reverse-note-order)
  9053. entry)
  9054. (while (setq entry (pop all))
  9055. (if (string-match (car entry) buffer-file-name)
  9056. (throw 'exit (cdr entry))))
  9057. nil)))))
  9058. (defvar org-refile-target-table nil
  9059. "The list of refile targets, created by `org-refile'.")
  9060. (defvar org-agenda-new-buffers nil
  9061. "Buffers created to visit agenda files.")
  9062. (defvar org-refile-cache nil
  9063. "Cache for refile targets.")
  9064. (defvar org-refile-markers nil
  9065. "All the markers used for caching refile locations.")
  9066. (defun org-refile-marker (pos)
  9067. "Get a new refile marker, but only if caching is in use."
  9068. (if (not org-refile-use-cache)
  9069. pos
  9070. (let ((m (make-marker)))
  9071. (move-marker m pos)
  9072. (push m org-refile-markers)
  9073. m)))
  9074. (defun org-refile-cache-clear ()
  9075. "Clear the refile cache and disable all the markers."
  9076. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9077. (setq org-refile-markers nil)
  9078. (setq org-refile-cache nil)
  9079. (message "Refile cache has been cleared"))
  9080. (defun org-refile-cache-check-set (set)
  9081. "Check if all the markers in the cache still have live buffers."
  9082. (let (marker)
  9083. (catch 'exit
  9084. (while (and set (setq marker (nth 3 (pop set))))
  9085. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9086. (when (and marker (null (marker-buffer marker)))
  9087. (message "not found") (sit-for 3)
  9088. (throw 'exit nil)))
  9089. t)))
  9090. (defun org-refile-cache-put (set &rest identifiers)
  9091. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9092. (let* ((key (sha1 (prin1-to-string identifiers)))
  9093. (entry (assoc key org-refile-cache)))
  9094. (if entry
  9095. (setcdr entry set)
  9096. (push (cons key set) org-refile-cache))))
  9097. (defun org-refile-cache-get (&rest identifiers)
  9098. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9099. (cond
  9100. ((not org-refile-cache) nil)
  9101. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9102. (t
  9103. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9104. org-refile-cache))))
  9105. (and set (org-refile-cache-check-set set) set)))))
  9106. (defun org-refile-get-targets (&optional default-buffer)
  9107. "Produce a table with refile targets."
  9108. (let ((case-fold-search nil)
  9109. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9110. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9111. targets tgs txt re files f desc descre fast-path-p level pos0)
  9112. (message "Getting targets...")
  9113. (with-current-buffer (or default-buffer (current-buffer))
  9114. (while (setq entry (pop entries))
  9115. (setq files (car entry) desc (cdr entry))
  9116. (setq fast-path-p nil)
  9117. (cond
  9118. ((null files) (setq files (list (current-buffer))))
  9119. ((eq files 'org-agenda-files)
  9120. (setq files (org-agenda-files 'unrestricted)))
  9121. ((and (symbolp files) (fboundp files))
  9122. (setq files (funcall files)))
  9123. ((and (symbolp files) (boundp files))
  9124. (setq files (symbol-value files))))
  9125. (if (stringp files) (setq files (list files)))
  9126. (cond
  9127. ((eq (car desc) :tag)
  9128. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9129. ((eq (car desc) :todo)
  9130. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9131. ((eq (car desc) :regexp)
  9132. (setq descre (cdr desc)))
  9133. ((eq (car desc) :level)
  9134. (setq descre (concat "^\\*\\{" (number-to-string
  9135. (if org-odd-levels-only
  9136. (1- (* 2 (cdr desc)))
  9137. (cdr desc)))
  9138. "\\}[ \t]")))
  9139. ((eq (car desc) :maxlevel)
  9140. (setq fast-path-p t)
  9141. (setq descre (concat "^\\*\\{1," (number-to-string
  9142. (if org-odd-levels-only
  9143. (1- (* 2 (cdr desc)))
  9144. (cdr desc)))
  9145. "\\}[ \t]")))
  9146. (t (error "Bad refiling target description %s" desc)))
  9147. (while (setq f (pop files))
  9148. (with-current-buffer
  9149. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9150. (or
  9151. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9152. (progn
  9153. (if (bufferp f) (setq f (buffer-file-name
  9154. (buffer-base-buffer f))))
  9155. (setq f (and f (expand-file-name f)))
  9156. (if (eq org-refile-use-outline-path 'file)
  9157. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9158. (save-excursion
  9159. (save-restriction
  9160. (widen)
  9161. (goto-char (point-min))
  9162. (while (re-search-forward descre nil t)
  9163. (goto-char (setq pos0 (point-at-bol)))
  9164. (catch 'next
  9165. (when org-refile-target-verify-function
  9166. (save-match-data
  9167. (or (funcall org-refile-target-verify-function)
  9168. (throw 'next t))))
  9169. (when (looking-at org-complex-heading-regexp)
  9170. (setq level (org-reduced-level
  9171. (- (match-end 1) (match-beginning 1)))
  9172. txt (org-link-display-format (match-string 4))
  9173. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9174. re (format org-complex-heading-regexp-format
  9175. (regexp-quote (match-string 4))))
  9176. (when org-refile-use-outline-path
  9177. (setq txt (mapconcat
  9178. 'org-protect-slash
  9179. (append
  9180. (if (eq org-refile-use-outline-path
  9181. 'file)
  9182. (list (file-name-nondirectory
  9183. (buffer-file-name
  9184. (buffer-base-buffer))))
  9185. (if (eq org-refile-use-outline-path
  9186. 'full-file-path)
  9187. (list (buffer-file-name
  9188. (buffer-base-buffer)))))
  9189. (org-get-outline-path fast-path-p
  9190. level txt)
  9191. (list txt))
  9192. "/")))
  9193. (push (list txt f re (org-refile-marker (point)))
  9194. tgs)))
  9195. (when (= (point) pos0)
  9196. ;; verification function has not moved point
  9197. (goto-char (point-at-eol))))))))
  9198. (when org-refile-use-cache
  9199. (org-refile-cache-put tgs (buffer-file-name) descre))
  9200. (setq targets (append tgs targets))
  9201. ))))
  9202. (message "Getting targets...done")
  9203. (nreverse targets)))
  9204. (defun org-protect-slash (s)
  9205. (while (string-match "/" s)
  9206. (setq s (replace-match "\\" t t s)))
  9207. s)
  9208. (defvar org-olpa (make-vector 20 nil))
  9209. (defun org-get-outline-path (&optional fastp level heading)
  9210. "Return the outline path to the current entry, as a list.
  9211. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9212. routine which makes outline path derivations for an entire file,
  9213. avoiding backtracing. Refile target collection makes use of that."
  9214. (if fastp
  9215. (progn
  9216. (if (> level 19)
  9217. (error "Outline path failure, more than 19 levels"))
  9218. (loop for i from level upto 19 do
  9219. (aset org-olpa i nil))
  9220. (prog1
  9221. (delq nil (append org-olpa nil))
  9222. (aset org-olpa level heading)))
  9223. (let (rtn case-fold-search)
  9224. (save-excursion
  9225. (save-restriction
  9226. (widen)
  9227. (while (org-up-heading-safe)
  9228. (when (looking-at org-complex-heading-regexp)
  9229. (push (org-match-string-no-properties 4) rtn)))
  9230. rtn)))))
  9231. (defun org-format-outline-path (path &optional width prefix)
  9232. "Format the outline path PATH for display.
  9233. Width is the maximum number of characters that is available.
  9234. Prefix is a prefix to be included in the returned string,
  9235. such as the file name."
  9236. (setq width (or width 79))
  9237. (if prefix (setq width (- width (length prefix))))
  9238. (if (not path)
  9239. (or prefix "")
  9240. (let* ((nsteps (length path))
  9241. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9242. (maxwidth (if (<= total-width width)
  9243. 10000 ;; everything fits
  9244. ;; we need to shorten the level headings
  9245. (/ (- width nsteps) nsteps)))
  9246. (org-odd-levels-only nil)
  9247. (n 0)
  9248. (total (1+ (length prefix))))
  9249. (setq maxwidth (max maxwidth 10))
  9250. (concat prefix
  9251. (mapconcat
  9252. (lambda (h)
  9253. (setq n (1+ n))
  9254. (if (and (= n nsteps) (< maxwidth 10000))
  9255. (setq maxwidth (- total-width total)))
  9256. (if (< (length h) maxwidth)
  9257. (progn (setq total (+ total (length h) 1)) h)
  9258. (setq h (substring h 0 (- maxwidth 2))
  9259. total (+ total maxwidth 1))
  9260. (if (string-match "[ \t]+\\'" h)
  9261. (setq h (substring h 0 (match-beginning 0))))
  9262. (setq h (concat h "..")))
  9263. (org-add-props h nil 'face
  9264. (nth (% (1- n) org-n-level-faces)
  9265. org-level-faces))
  9266. h)
  9267. path "/")))))
  9268. (defun org-display-outline-path (&optional file current)
  9269. "Display the current outline path in the echo area."
  9270. (interactive "P")
  9271. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9272. (case-fold-search nil)
  9273. (path (and (org-mode-p) (org-get-outline-path))))
  9274. (if current (setq path (append path
  9275. (save-excursion
  9276. (org-back-to-heading t)
  9277. (if (looking-at org-complex-heading-regexp)
  9278. (list (match-string 4)))))))
  9279. (message "%s"
  9280. (org-format-outline-path
  9281. path
  9282. (1- (frame-width))
  9283. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9284. (defvar org-refile-history nil
  9285. "History for refiling operations.")
  9286. (defvar org-after-refile-insert-hook nil
  9287. "Hook run after `org-refile' has inserted its stuff at the new location.
  9288. Note that this is still *before* the stuff will be removed from
  9289. the *old* location.")
  9290. (defvar org-capture-last-stored-marker)
  9291. (defun org-refile (&optional goto default-buffer rfloc)
  9292. "Move the entry at point to another heading.
  9293. The list of target headings is compiled using the information in
  9294. `org-refile-targets', which see. This list is created before each use
  9295. and will therefore always be up-to-date.
  9296. At the target location, the entry is filed as a subitem of the target heading.
  9297. Depending on `org-reverse-note-order', the new subitem will either be the
  9298. first or the last subitem.
  9299. If there is an active region, all entries in that region will be moved.
  9300. However, the region must fulfill the requirement that the first heading
  9301. is the first one sets the top-level of the moved text - at most siblings
  9302. below it are allowed.
  9303. With prefix arg GOTO, the command will only visit the target location,
  9304. not actually move anything.
  9305. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9306. go to the location where the last refiling
  9307. operation has put the subtree.
  9308. With a prefix argument of `2', refile to the running clock.
  9309. RFLOC can be a refile location obtained in a different way.
  9310. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9311. If you are using target caching (see `org-refile-use-cache'),
  9312. You have to clear the target cache in order to find new targets.
  9313. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9314. (interactive "P")
  9315. (if (member goto '(0 (64)))
  9316. (org-refile-cache-clear)
  9317. (let* ((cbuf (current-buffer))
  9318. (regionp (org-region-active-p))
  9319. (region-start (and regionp (region-beginning)))
  9320. (region-end (and regionp (region-end)))
  9321. (region-length (and regionp (- region-end region-start)))
  9322. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9323. pos it nbuf file re level reversed)
  9324. (setq last-command nil)
  9325. (when regionp
  9326. (goto-char region-start)
  9327. (or (bolp) (goto-char (point-at-bol)))
  9328. (setq region-start (point))
  9329. (unless (org-kill-is-subtree-p
  9330. (buffer-substring region-start region-end))
  9331. (error "The region is not a (sequence of) subtree(s)")))
  9332. (if (equal goto '(16))
  9333. (org-refile-goto-last-stored)
  9334. (when (or
  9335. (and (equal goto 2)
  9336. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9337. (prog1
  9338. (setq it (list (or org-clock-heading "running clock")
  9339. (buffer-file-name
  9340. (marker-buffer org-clock-hd-marker))
  9341. ""
  9342. (marker-position org-clock-hd-marker)))
  9343. (setq goto nil)))
  9344. (setq it (or rfloc
  9345. (save-excursion
  9346. (org-refile-get-location
  9347. (if goto "Goto" "Refile to") default-buffer
  9348. org-refile-allow-creating-parent-nodes)))))
  9349. (setq file (nth 1 it)
  9350. re (nth 2 it)
  9351. pos (nth 3 it))
  9352. (if (and (not goto)
  9353. pos
  9354. (equal (buffer-file-name) file)
  9355. (if regionp
  9356. (and (>= pos region-start)
  9357. (<= pos region-end))
  9358. (and (>= pos (point))
  9359. (< pos (save-excursion
  9360. (org-end-of-subtree t t))))))
  9361. (error "Cannot refile to position inside the tree or region"))
  9362. (setq nbuf (or (find-buffer-visiting file)
  9363. (find-file-noselect file)))
  9364. (if goto
  9365. (progn
  9366. (switch-to-buffer nbuf)
  9367. (goto-char pos)
  9368. (org-show-context 'org-goto))
  9369. (if regionp
  9370. (progn
  9371. (org-kill-new (buffer-substring region-start region-end))
  9372. (org-save-markers-in-region region-start region-end))
  9373. (org-copy-subtree 1 nil t))
  9374. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9375. (find-file-noselect file)))
  9376. (setq reversed (org-notes-order-reversed-p))
  9377. (save-excursion
  9378. (save-restriction
  9379. (widen)
  9380. (if pos
  9381. (progn
  9382. (goto-char pos)
  9383. (looking-at outline-regexp)
  9384. (setq level (org-get-valid-level (funcall outline-level) 1))
  9385. (goto-char
  9386. (if reversed
  9387. (or (outline-next-heading) (point-max))
  9388. (or (save-excursion (org-get-next-sibling))
  9389. (org-end-of-subtree t t)
  9390. (point-max)))))
  9391. (setq level 1)
  9392. (if (not reversed)
  9393. (goto-char (point-max))
  9394. (goto-char (point-min))
  9395. (or (outline-next-heading) (goto-char (point-max)))))
  9396. (if (not (bolp)) (newline))
  9397. (org-paste-subtree level)
  9398. (when org-log-refile
  9399. (org-add-log-setup 'refile nil nil 'findpos
  9400. org-log-refile)
  9401. (unless (eq org-log-refile 'note)
  9402. (save-excursion (org-add-log-note))))
  9403. (and org-auto-align-tags (org-set-tags nil t))
  9404. (bookmark-set "org-refile-last-stored")
  9405. ;; If we are refiling for capture, make sure that the
  9406. ;; last-capture pointers point here
  9407. (when (org-bound-and-true-p org-refile-for-capture)
  9408. (bookmark-set "org-capture-last-stored-marker")
  9409. (move-marker org-capture-last-stored-marker (point)))
  9410. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9411. (run-hooks 'org-after-refile-insert-hook))))
  9412. (if regionp
  9413. (delete-region (point) (+ (point) region-length))
  9414. (org-cut-subtree))
  9415. (when (featurep 'org-inlinetask)
  9416. (org-inlinetask-remove-END-maybe))
  9417. (setq org-markers-to-move nil)
  9418. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9419. (defun org-refile-goto-last-stored ()
  9420. "Go to the location where the last refile was stored."
  9421. (interactive)
  9422. (bookmark-jump "org-refile-last-stored")
  9423. (message "This is the location of the last refile"))
  9424. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9425. "Prompt the user for a refile location, using PROMPT.
  9426. PROMPT should not be suffixed with a colon and a space, because
  9427. this function appends the default value from
  9428. `org-refile-history' automatically, if that is not empty."
  9429. (let ((org-refile-targets org-refile-targets)
  9430. (org-refile-use-outline-path org-refile-use-outline-path))
  9431. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  9432. (unless org-refile-target-table
  9433. (error "No refile targets"))
  9434. (let* ((prompt (concat prompt
  9435. (and (car org-refile-history)
  9436. (concat " (default " (car org-refile-history) ")"))
  9437. ": "))
  9438. (cbuf (current-buffer))
  9439. (partial-completion-mode nil)
  9440. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9441. (cfunc (if (and org-refile-use-outline-path
  9442. org-outline-path-complete-in-steps)
  9443. 'org-olpath-completing-read
  9444. 'org-icompleting-read))
  9445. (extra (if org-refile-use-outline-path "/" ""))
  9446. (filename (and cfn (expand-file-name cfn)))
  9447. (tbl (mapcar
  9448. (lambda (x)
  9449. (if (and (not (member org-refile-use-outline-path
  9450. '(file full-file-path)))
  9451. (not (equal filename (nth 1 x))))
  9452. (cons (concat (car x) extra " ("
  9453. (file-name-nondirectory (nth 1 x)) ")")
  9454. (cdr x))
  9455. (cons (concat (car x) extra) (cdr x))))
  9456. org-refile-target-table))
  9457. (completion-ignore-case t)
  9458. pa answ parent-target child parent old-hist)
  9459. (setq old-hist org-refile-history)
  9460. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9461. nil 'org-refile-history (car org-refile-history)))
  9462. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9463. (org-refile-check-position pa)
  9464. (if pa
  9465. (progn
  9466. (when (or (not org-refile-history)
  9467. (not (eq old-hist org-refile-history))
  9468. (not (equal (car pa) (car org-refile-history))))
  9469. (setq org-refile-history
  9470. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9471. org-refile-history
  9472. (cdr org-refile-history))))
  9473. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9474. (pop org-refile-history)))
  9475. pa)
  9476. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9477. (progn
  9478. (setq parent (match-string 1 answ)
  9479. child (match-string 2 answ))
  9480. (setq parent-target (or (assoc parent tbl)
  9481. (assoc (concat parent "/") tbl)))
  9482. (when (and parent-target
  9483. (or (eq new-nodes t)
  9484. (and (eq new-nodes 'confirm)
  9485. (y-or-n-p (format "Create new node \"%s\"? "
  9486. child)))))
  9487. (org-refile-new-child parent-target child)))
  9488. (error "Invalid target location")))))
  9489. (defun org-refile-check-position (refile-pointer)
  9490. "Check if the refile pointer matches the readline to which it points."
  9491. (let* ((file (nth 1 refile-pointer))
  9492. (re (nth 2 refile-pointer))
  9493. (pos (nth 3 refile-pointer))
  9494. buffer)
  9495. (when (org-string-nw-p re)
  9496. (setq buffer (if (markerp pos)
  9497. (marker-buffer pos)
  9498. (or (find-buffer-visiting file)
  9499. (find-file-noselect file))))
  9500. (with-current-buffer buffer
  9501. (save-excursion
  9502. (save-restriction
  9503. (widen)
  9504. (goto-char pos)
  9505. (beginning-of-line 1)
  9506. (unless (org-looking-at-p re)
  9507. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9508. (defun org-refile-new-child (parent-target child)
  9509. "Use refile target PARENT-TARGET to add new CHILD below it."
  9510. (unless parent-target
  9511. (error "Cannot find parent for new node"))
  9512. (let ((file (nth 1 parent-target))
  9513. (pos (nth 3 parent-target))
  9514. level)
  9515. (with-current-buffer (or (find-buffer-visiting file)
  9516. (find-file-noselect file))
  9517. (save-excursion
  9518. (save-restriction
  9519. (widen)
  9520. (if pos
  9521. (goto-char pos)
  9522. (goto-char (point-max))
  9523. (if (not (bolp)) (newline)))
  9524. (when (looking-at outline-regexp)
  9525. (setq level (funcall outline-level))
  9526. (org-end-of-subtree t t))
  9527. (org-back-over-empty-lines)
  9528. (insert "\n" (make-string
  9529. (if pos (org-get-valid-level level 1) 1) ?*)
  9530. " " child "\n")
  9531. (beginning-of-line 0)
  9532. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9533. (defun org-olpath-completing-read (prompt collection &rest args)
  9534. "Read an outline path like a file name."
  9535. (let ((thetable collection)
  9536. (org-completion-use-ido nil) ; does not work with ido.
  9537. (org-completion-use-iswitchb nil)) ; or iswitchb
  9538. (apply
  9539. 'org-icompleting-read prompt
  9540. (lambda (string predicate &optional flag)
  9541. (let (rtn r f (l (length string)))
  9542. (cond
  9543. ((eq flag nil)
  9544. ;; try completion
  9545. (try-completion string thetable))
  9546. ((eq flag t)
  9547. ;; all-completions
  9548. (setq rtn (all-completions string thetable predicate))
  9549. (mapcar
  9550. (lambda (x)
  9551. (setq r (substring x l))
  9552. (if (string-match " ([^)]*)$" x)
  9553. (setq f (match-string 0 x))
  9554. (setq f ""))
  9555. (if (string-match "/" r)
  9556. (concat string (substring r 0 (match-end 0)) f)
  9557. x))
  9558. rtn))
  9559. ((eq flag 'lambda)
  9560. ;; exact match?
  9561. (assoc string thetable)))
  9562. ))
  9563. args)))
  9564. ;;;; Dynamic blocks
  9565. (defun org-find-dblock (name)
  9566. "Find the first dynamic block with name NAME in the buffer.
  9567. If not found, stay at current position and return nil."
  9568. (let (pos)
  9569. (save-excursion
  9570. (goto-char (point-min))
  9571. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9572. nil t)
  9573. (match-beginning 0))))
  9574. (if pos (goto-char pos))
  9575. pos))
  9576. (defconst org-dblock-start-re
  9577. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9578. "Matches the start line of a dynamic block, with parameters.")
  9579. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9580. "Matches the end of a dynamic block.")
  9581. (defun org-create-dblock (plist)
  9582. "Create a dynamic block section, with parameters taken from PLIST.
  9583. PLIST must contain a :name entry which is used as name of the block."
  9584. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9585. (end-of-line 1)
  9586. (newline))
  9587. (let ((col (current-column))
  9588. (name (plist-get plist :name)))
  9589. (insert "#+BEGIN: " name)
  9590. (while plist
  9591. (if (eq (car plist) :name)
  9592. (setq plist (cddr plist))
  9593. (insert " " (prin1-to-string (pop plist)))))
  9594. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9595. (beginning-of-line -2)))
  9596. (defun org-prepare-dblock ()
  9597. "Prepare dynamic block for refresh.
  9598. This empties the block, puts the cursor at the insert position and returns
  9599. the property list including an extra property :name with the block name."
  9600. (unless (looking-at org-dblock-start-re)
  9601. (error "Not at a dynamic block"))
  9602. (let* ((begdel (1+ (match-end 0)))
  9603. (name (org-no-properties (match-string 1)))
  9604. (params (append (list :name name)
  9605. (read (concat "(" (match-string 3) ")")))))
  9606. (save-excursion
  9607. (beginning-of-line 1)
  9608. (skip-chars-forward " \t")
  9609. (setq params (plist-put params :indentation-column (current-column))))
  9610. (unless (re-search-forward org-dblock-end-re nil t)
  9611. (error "Dynamic block not terminated"))
  9612. (setq params
  9613. (append params
  9614. (list :content (buffer-substring
  9615. begdel (match-beginning 0)))))
  9616. (delete-region begdel (match-beginning 0))
  9617. (goto-char begdel)
  9618. (open-line 1)
  9619. params))
  9620. (defun org-map-dblocks (&optional command)
  9621. "Apply COMMAND to all dynamic blocks in the current buffer.
  9622. If COMMAND is not given, use `org-update-dblock'."
  9623. (let ((cmd (or command 'org-update-dblock)))
  9624. (save-excursion
  9625. (goto-char (point-min))
  9626. (while (re-search-forward org-dblock-start-re nil t)
  9627. (goto-char (match-beginning 0))
  9628. (save-excursion
  9629. (condition-case nil
  9630. (funcall cmd)
  9631. (error (message "Error during update of dynamic block"))))
  9632. (unless (re-search-forward org-dblock-end-re nil t)
  9633. (error "Dynamic block not terminated"))))))
  9634. (defun org-dblock-update (&optional arg)
  9635. "User command for updating dynamic blocks.
  9636. Update the dynamic block at point. With prefix ARG, update all dynamic
  9637. blocks in the buffer."
  9638. (interactive "P")
  9639. (if arg
  9640. (org-update-all-dblocks)
  9641. (or (looking-at org-dblock-start-re)
  9642. (org-beginning-of-dblock))
  9643. (org-update-dblock)))
  9644. (defun org-update-dblock ()
  9645. "Update the dynamic block at point.
  9646. This means to empty the block, parse for parameters and then call
  9647. the correct writing function."
  9648. (interactive)
  9649. (save-window-excursion
  9650. (let* ((pos (point))
  9651. (line (org-current-line))
  9652. (params (org-prepare-dblock))
  9653. (name (plist-get params :name))
  9654. (indent (plist-get params :indentation-column))
  9655. (cmd (intern (concat "org-dblock-write:" name))))
  9656. (message "Updating dynamic block `%s' at line %d..." name line)
  9657. (funcall cmd params)
  9658. (message "Updating dynamic block `%s' at line %d...done" name line)
  9659. (goto-char pos)
  9660. (when (and indent (> indent 0))
  9661. (setq indent (make-string indent ?\ ))
  9662. (save-excursion
  9663. (org-beginning-of-dblock)
  9664. (forward-line 1)
  9665. (while (not (looking-at org-dblock-end-re))
  9666. (insert indent)
  9667. (beginning-of-line 2))
  9668. (when (looking-at org-dblock-end-re)
  9669. (and (looking-at "[ \t]+")
  9670. (replace-match ""))
  9671. (insert indent)))))))
  9672. (defun org-beginning-of-dblock ()
  9673. "Find the beginning of the dynamic block at point.
  9674. Error if there is no such block at point."
  9675. (let ((pos (point))
  9676. beg)
  9677. (end-of-line 1)
  9678. (if (and (re-search-backward org-dblock-start-re nil t)
  9679. (setq beg (match-beginning 0))
  9680. (re-search-forward org-dblock-end-re nil t)
  9681. (> (match-end 0) pos))
  9682. (goto-char beg)
  9683. (goto-char pos)
  9684. (error "Not in a dynamic block"))))
  9685. (defun org-update-all-dblocks ()
  9686. "Update all dynamic blocks in the buffer.
  9687. This function can be used in a hook."
  9688. (interactive)
  9689. (when (org-mode-p)
  9690. (org-map-dblocks 'org-update-dblock)))
  9691. ;;;; Completion
  9692. (defconst org-additional-option-like-keywords
  9693. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9694. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9695. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9696. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9697. "BEGIN:" "END:"
  9698. "ORGTBL" "TBLFM:" "TBLNAME:"
  9699. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9700. "BEGIN_QUOTE" "END_QUOTE"
  9701. "BEGIN_VERSE" "END_VERSE"
  9702. "BEGIN_CENTER" "END_CENTER"
  9703. "BEGIN_SRC" "END_SRC"
  9704. "BEGIN_RESULT" "END_RESULT"
  9705. "SOURCE:" "SRCNAME:" "FUNCTION:"
  9706. "RESULTS:"
  9707. "HEADER:" "HEADERS:"
  9708. "BABEL:"
  9709. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9710. "CAPTION:" "LABEL:"
  9711. "SETUPFILE:"
  9712. "INCLUDE:"
  9713. "BIND:"
  9714. "MACRO:"))
  9715. (defcustom org-structure-template-alist
  9716. '(
  9717. ("s" "#+begin_src ?\n\n#+end_src"
  9718. "<src lang=\"?\">\n\n</src>")
  9719. ("e" "#+begin_example\n?\n#+end_example"
  9720. "<example>\n?\n</example>")
  9721. ("q" "#+begin_quote\n?\n#+end_quote"
  9722. "<quote>\n?\n</quote>")
  9723. ("v" "#+begin_verse\n?\n#+end_verse"
  9724. "<verse>\n?\n/verse>")
  9725. ("c" "#+begin_center\n?\n#+end_center"
  9726. "<center>\n?\n/center>")
  9727. ("l" "#+begin_latex\n?\n#+end_latex"
  9728. "<literal style=\"latex\">\n?\n</literal>")
  9729. ("L" "#+latex: "
  9730. "<literal style=\"latex\">?</literal>")
  9731. ("h" "#+begin_html\n?\n#+end_html"
  9732. "<literal style=\"html\">\n?\n</literal>")
  9733. ("H" "#+html: "
  9734. "<literal style=\"html\">?</literal>")
  9735. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9736. ("A" "#+ascii: ")
  9737. ("i" "#+index: ?"
  9738. "#+index: ?")
  9739. ("I" "#+include %file ?"
  9740. "<include file=%file markup=\"?\">")
  9741. )
  9742. "Structure completion elements.
  9743. This is a list of abbreviation keys and values. The value gets inserted
  9744. if you type `<' followed by the key and then press the completion key,
  9745. usually `M-TAB'. %file will be replaced by a file name after prompting
  9746. for the file using completion. The cursor will be placed at the position
  9747. of the `?` in the template.
  9748. There are two templates for each key, the first uses the original Org syntax,
  9749. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9750. the default when the /org-mtags.el/ module has been loaded. See also the
  9751. variable `org-mtags-prefer-muse-templates'.
  9752. This is an experimental feature, it is undecided if it is going to stay in."
  9753. :group 'org-completion
  9754. :type '(repeat
  9755. (string :tag "Key")
  9756. (string :tag "Template")
  9757. (string :tag "Muse Template")))
  9758. (defun org-try-structure-completion ()
  9759. "Try to complete a structure template before point.
  9760. This looks for strings like \"<e\" on an otherwise empty line and
  9761. expands them."
  9762. (let ((l (buffer-substring (point-at-bol) (point)))
  9763. a)
  9764. (when (and (looking-at "[ \t]*$")
  9765. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  9766. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9767. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9768. (match-beginning 1)) a)
  9769. t)))
  9770. (defun org-complete-expand-structure-template (start cell)
  9771. "Expand a structure template."
  9772. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9773. (rpl (nth (if musep 2 1) cell))
  9774. (ind ""))
  9775. (delete-region start (point))
  9776. (when (string-match "\\`#\\+" rpl)
  9777. (cond
  9778. ((bolp))
  9779. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9780. (setq ind (buffer-substring (point-at-bol) (point))))
  9781. (t (newline))))
  9782. (setq start (point))
  9783. (if (string-match "%file" rpl)
  9784. (setq rpl (replace-match
  9785. (concat
  9786. "\""
  9787. (save-match-data
  9788. (abbreviate-file-name (read-file-name "Include file: ")))
  9789. "\"")
  9790. t t rpl)))
  9791. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9792. (concat "\n" ind)))
  9793. (insert rpl)
  9794. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9795. ;;;; TODO, DEADLINE, Comments
  9796. (defun org-toggle-comment ()
  9797. "Change the COMMENT state of an entry."
  9798. (interactive)
  9799. (save-excursion
  9800. (org-back-to-heading)
  9801. (let (case-fold-search)
  9802. (if (looking-at (concat outline-regexp
  9803. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9804. (replace-match "" t t nil 1)
  9805. (if (looking-at outline-regexp)
  9806. (progn
  9807. (goto-char (match-end 0))
  9808. (insert org-comment-string " ")))))))
  9809. (defvar org-last-todo-state-is-todo nil
  9810. "This is non-nil when the last TODO state change led to a TODO state.
  9811. If the last change removed the TODO tag or switched to DONE, then
  9812. this is nil.")
  9813. (defvar org-setting-tags nil) ; dynamically skipped
  9814. (defvar org-todo-setup-filter-hook nil
  9815. "Hook for functions that pre-filter todo specs.
  9816. Each function takes a todo spec and returns either nil or the spec
  9817. transformed into canonical form." )
  9818. (defvar org-todo-get-default-hook nil
  9819. "Hook for functions that get a default item for todo.
  9820. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9821. nil or a string to be used for the todo mark." )
  9822. (defvar org-agenda-headline-snapshot-before-repeat)
  9823. (defun org-todo (&optional arg)
  9824. "Change the TODO state of an item.
  9825. The state of an item is given by a keyword at the start of the heading,
  9826. like
  9827. *** TODO Write paper
  9828. *** DONE Call mom
  9829. The different keywords are specified in the variable `org-todo-keywords'.
  9830. By default the available states are \"TODO\" and \"DONE\".
  9831. So for this example: when the item starts with TODO, it is changed to DONE.
  9832. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9833. DONE are present, add TODO at the beginning of the heading.
  9834. With \\[universal-argument] prefix arg, use completion to determine the new \
  9835. state.
  9836. With numeric prefix arg, switch to that state.
  9837. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9838. keywords (nextset).
  9839. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9840. For calling through lisp, arg is also interpreted in the following way:
  9841. 'none -> empty state
  9842. \"\"(empty string) -> switch to empty state
  9843. 'done -> switch to DONE
  9844. 'nextset -> switch to the next set of keywords
  9845. 'previousset -> switch to the previous set of keywords
  9846. \"WAITING\" -> switch to the specified keyword, but only if it
  9847. really is a member of `org-todo-keywords'."
  9848. (interactive "P")
  9849. (if (equal arg '(16)) (setq arg 'nextset))
  9850. (let ((org-blocker-hook org-blocker-hook)
  9851. (case-fold-search nil))
  9852. (when (equal arg '(64))
  9853. (setq arg nil org-blocker-hook nil))
  9854. (when (and org-blocker-hook
  9855. (or org-inhibit-blocking
  9856. (org-entry-get nil "NOBLOCKING")))
  9857. (setq org-blocker-hook nil))
  9858. (save-excursion
  9859. (catch 'exit
  9860. (org-back-to-heading t)
  9861. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9862. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9863. (looking-at " *"))
  9864. (let* ((match-data (match-data))
  9865. (startpos (point-at-bol))
  9866. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9867. (org-log-done org-log-done)
  9868. (org-log-repeat org-log-repeat)
  9869. (org-todo-log-states org-todo-log-states)
  9870. (this (match-string 1))
  9871. (hl-pos (match-beginning 0))
  9872. (head (org-get-todo-sequence-head this))
  9873. (ass (assoc head org-todo-kwd-alist))
  9874. (interpret (nth 1 ass))
  9875. (done-word (nth 3 ass))
  9876. (final-done-word (nth 4 ass))
  9877. (last-state (or this ""))
  9878. (completion-ignore-case t)
  9879. (member (member this org-todo-keywords-1))
  9880. (tail (cdr member))
  9881. (state (cond
  9882. ((and org-todo-key-trigger
  9883. (or (and (equal arg '(4))
  9884. (eq org-use-fast-todo-selection 'prefix))
  9885. (and (not arg) org-use-fast-todo-selection
  9886. (not (eq org-use-fast-todo-selection
  9887. 'prefix)))))
  9888. ;; Use fast selection
  9889. (org-fast-todo-selection))
  9890. ((and (equal arg '(4))
  9891. (or (not org-use-fast-todo-selection)
  9892. (not org-todo-key-trigger)))
  9893. ;; Read a state with completion
  9894. (org-icompleting-read
  9895. "State: " (mapcar (lambda(x) (list x))
  9896. org-todo-keywords-1)
  9897. nil t))
  9898. ((eq arg 'right)
  9899. (if this
  9900. (if tail (car tail) nil)
  9901. (car org-todo-keywords-1)))
  9902. ((eq arg 'left)
  9903. (if (equal member org-todo-keywords-1)
  9904. nil
  9905. (if this
  9906. (nth (- (length org-todo-keywords-1)
  9907. (length tail) 2)
  9908. org-todo-keywords-1)
  9909. (org-last org-todo-keywords-1))))
  9910. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9911. (setq arg nil))) ; hack to fall back to cycling
  9912. (arg
  9913. ;; user or caller requests a specific state
  9914. (cond
  9915. ((equal arg "") nil)
  9916. ((eq arg 'none) nil)
  9917. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9918. ((eq arg 'nextset)
  9919. (or (car (cdr (member head org-todo-heads)))
  9920. (car org-todo-heads)))
  9921. ((eq arg 'previousset)
  9922. (let ((org-todo-heads (reverse org-todo-heads)))
  9923. (or (car (cdr (member head org-todo-heads)))
  9924. (car org-todo-heads))))
  9925. ((car (member arg org-todo-keywords-1)))
  9926. ((stringp arg)
  9927. (error "State `%s' not valid in this file" arg))
  9928. ((nth (1- (prefix-numeric-value arg))
  9929. org-todo-keywords-1))))
  9930. ((null member) (or head (car org-todo-keywords-1)))
  9931. ((equal this final-done-word) nil) ;; -> make empty
  9932. ((null tail) nil) ;; -> first entry
  9933. ((memq interpret '(type priority))
  9934. (if (eq this-command last-command)
  9935. (car tail)
  9936. (if (> (length tail) 0)
  9937. (or done-word (car org-done-keywords))
  9938. nil)))
  9939. (t
  9940. (car tail))))
  9941. (state (or
  9942. (run-hook-with-args-until-success
  9943. 'org-todo-get-default-hook state last-state)
  9944. state))
  9945. (next (if state (concat " " state " ") " "))
  9946. (change-plist (list :type 'todo-state-change :from this :to state
  9947. :position startpos))
  9948. dolog now-done-p)
  9949. (when org-blocker-hook
  9950. (setq org-last-todo-state-is-todo
  9951. (not (member this org-done-keywords)))
  9952. (unless (save-excursion
  9953. (save-match-data
  9954. (org-with-wide-buffer
  9955. (run-hook-with-args-until-failure
  9956. 'org-blocker-hook change-plist))))
  9957. (if (called-interactively-p 'interactive)
  9958. (error "TODO state change from %s to %s blocked" this state)
  9959. ;; fail silently
  9960. (message "TODO state change from %s to %s blocked" this state)
  9961. (throw 'exit nil))))
  9962. (store-match-data match-data)
  9963. (replace-match next t t)
  9964. (unless (pos-visible-in-window-p hl-pos)
  9965. (message "TODO state changed to %s" (org-trim next)))
  9966. (unless head
  9967. (setq head (org-get-todo-sequence-head state)
  9968. ass (assoc head org-todo-kwd-alist)
  9969. interpret (nth 1 ass)
  9970. done-word (nth 3 ass)
  9971. final-done-word (nth 4 ass)))
  9972. (when (memq arg '(nextset previousset))
  9973. (message "Keyword-Set %d/%d: %s"
  9974. (- (length org-todo-sets) -1
  9975. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9976. (length org-todo-sets)
  9977. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9978. (setq org-last-todo-state-is-todo
  9979. (not (member state org-done-keywords)))
  9980. (setq now-done-p (and (member state org-done-keywords)
  9981. (not (member this org-done-keywords))))
  9982. (and logging (org-local-logging logging))
  9983. (when (and (or org-todo-log-states org-log-done)
  9984. (not (eq org-inhibit-logging t))
  9985. (not (memq arg '(nextset previousset))))
  9986. ;; we need to look at recording a time and note
  9987. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9988. (nth 2 (assoc this org-todo-log-states))))
  9989. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9990. (setq dolog 'time))
  9991. (when (and state
  9992. (member state org-not-done-keywords)
  9993. (not (member this org-not-done-keywords)))
  9994. ;; This is now a todo state and was not one before
  9995. ;; If there was a CLOSED time stamp, get rid of it.
  9996. (org-add-planning-info nil nil 'closed))
  9997. (when (and now-done-p org-log-done)
  9998. ;; It is now done, and it was not done before
  9999. (org-add-planning-info 'closed (org-current-time))
  10000. (if (and (not dolog) (eq 'note org-log-done))
  10001. (org-add-log-setup 'done state this 'findpos 'note)))
  10002. (when (and state dolog)
  10003. ;; This is a non-nil state, and we need to log it
  10004. (org-add-log-setup 'state state this 'findpos dolog)))
  10005. ;; Fixup tag positioning
  10006. (org-todo-trigger-tag-changes state)
  10007. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10008. (when org-provide-todo-statistics
  10009. (org-update-parent-todo-statistics))
  10010. (run-hooks 'org-after-todo-state-change-hook)
  10011. (if (and arg (not (member state org-done-keywords)))
  10012. (setq head (org-get-todo-sequence-head state)))
  10013. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10014. ;; Do we need to trigger a repeat?
  10015. (when now-done-p
  10016. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10017. ;; This is for the agenda, take a snapshot of the headline.
  10018. (save-match-data
  10019. (setq org-agenda-headline-snapshot-before-repeat
  10020. (org-get-heading))))
  10021. (org-auto-repeat-maybe state))
  10022. ;; Fixup cursor location if close to the keyword
  10023. (if (and (outline-on-heading-p)
  10024. (not (bolp))
  10025. (save-excursion (beginning-of-line 1)
  10026. (looking-at org-todo-line-regexp))
  10027. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10028. (progn
  10029. (goto-char (or (match-end 2) (match-end 1)))
  10030. (and (looking-at " ") (just-one-space))))
  10031. (when org-trigger-hook
  10032. (save-excursion
  10033. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10034. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10035. "Block turning an entry into a TODO, using the hierarchy.
  10036. This checks whether the current task should be blocked from state
  10037. changes. Such blocking occurs when:
  10038. 1. The task has children which are not all in a completed state.
  10039. 2. A task has a parent with the property :ORDERED:, and there
  10040. are siblings prior to the current task with incomplete
  10041. status.
  10042. 3. The parent of the task is blocked because it has siblings that should
  10043. be done first, or is child of a block grandparent TODO entry."
  10044. (if (not org-enforce-todo-dependencies)
  10045. t ; if locally turned off don't block
  10046. (catch 'dont-block
  10047. ;; If this is not a todo state change, or if this entry is already DONE,
  10048. ;; do not block
  10049. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10050. (member (plist-get change-plist :from)
  10051. (cons 'done org-done-keywords))
  10052. (member (plist-get change-plist :to)
  10053. (cons 'todo org-not-done-keywords))
  10054. (not (plist-get change-plist :to)))
  10055. (throw 'dont-block t))
  10056. ;; If this task has children, and any are undone, it's blocked
  10057. (save-excursion
  10058. (org-back-to-heading t)
  10059. (let ((this-level (funcall outline-level)))
  10060. (outline-next-heading)
  10061. (let ((child-level (funcall outline-level)))
  10062. (while (and (not (eobp))
  10063. (> child-level this-level))
  10064. ;; this todo has children, check whether they are all
  10065. ;; completed
  10066. (if (and (not (org-entry-is-done-p))
  10067. (org-entry-is-todo-p))
  10068. (throw 'dont-block nil))
  10069. (outline-next-heading)
  10070. (setq child-level (funcall outline-level))))))
  10071. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10072. ;; any previous siblings are undone, it's blocked
  10073. (save-excursion
  10074. (org-back-to-heading t)
  10075. (let* ((pos (point))
  10076. (parent-pos (and (org-up-heading-safe) (point))))
  10077. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10078. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10079. (forward-line 1)
  10080. (re-search-forward org-not-done-heading-regexp pos t))
  10081. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10082. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10083. (while t
  10084. (goto-char parent-pos)
  10085. (if (not (looking-at org-not-done-heading-regexp))
  10086. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10087. (setq pos (point))
  10088. (setq parent-pos (and (org-up-heading-safe) (point)))
  10089. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10090. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10091. (forward-line 1)
  10092. (re-search-forward org-not-done-heading-regexp pos t))
  10093. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10094. (defcustom org-track-ordered-property-with-tag nil
  10095. "Should the ORDERED property also be shown as a tag?
  10096. The ORDERED property decides if an entry should require subtasks to be
  10097. completed in sequence. Since a property is not very visible, setting
  10098. this option means that toggling the ORDERED property with the command
  10099. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10100. not relevant for the behavior, but it makes things more visible.
  10101. Note that toggling the tag with tags commands will not change the property
  10102. and therefore not influence behavior!
  10103. This can be t, meaning the tag ORDERED should be used, It can also be a
  10104. string to select a different tag for this task."
  10105. :group 'org-todo
  10106. :type '(choice
  10107. (const :tag "No tracking" nil)
  10108. (const :tag "Track with ORDERED tag" t)
  10109. (string :tag "Use other tag")))
  10110. (defun org-toggle-ordered-property ()
  10111. "Toggle the ORDERED property of the current entry.
  10112. For better visibility, you can track the value of this property with a tag.
  10113. See variable `org-track-ordered-property-with-tag'."
  10114. (interactive)
  10115. (let* ((t1 org-track-ordered-property-with-tag)
  10116. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10117. (save-excursion
  10118. (org-back-to-heading)
  10119. (if (org-entry-get nil "ORDERED")
  10120. (progn
  10121. (org-delete-property "ORDERED")
  10122. (and tag (org-toggle-tag tag 'off))
  10123. (message "Subtasks can be completed in arbitrary order"))
  10124. (org-entry-put nil "ORDERED" "t")
  10125. (and tag (org-toggle-tag tag 'on))
  10126. (message "Subtasks must be completed in sequence")))))
  10127. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10128. (defun org-block-todo-from-checkboxes (change-plist)
  10129. "Block turning an entry into a TODO, using checkboxes.
  10130. This checks whether the current task should be blocked from state
  10131. changes because there are unchecked boxes in this entry."
  10132. (if (not org-enforce-todo-checkbox-dependencies)
  10133. t ; if locally turned off don't block
  10134. (catch 'dont-block
  10135. ;; If this is not a todo state change, or if this entry is already DONE,
  10136. ;; do not block
  10137. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10138. (member (plist-get change-plist :from)
  10139. (cons 'done org-done-keywords))
  10140. (member (plist-get change-plist :to)
  10141. (cons 'todo org-not-done-keywords))
  10142. (not (plist-get change-plist :to)))
  10143. (throw 'dont-block t))
  10144. ;; If this task has checkboxes that are not checked, it's blocked
  10145. (save-excursion
  10146. (org-back-to-heading t)
  10147. (let ((beg (point)) end)
  10148. (outline-next-heading)
  10149. (setq end (point))
  10150. (goto-char beg)
  10151. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10152. end t)
  10153. (progn
  10154. (if (boundp 'org-blocked-by-checkboxes)
  10155. (setq org-blocked-by-checkboxes t))
  10156. (throw 'dont-block nil)))))
  10157. t))) ; do not block
  10158. (defun org-entry-blocked-p ()
  10159. "Is the current entry blocked?"
  10160. (if (org-entry-get nil "NOBLOCKING")
  10161. nil ;; Never block this entry
  10162. (not
  10163. (run-hook-with-args-until-failure
  10164. 'org-blocker-hook
  10165. (list :type 'todo-state-change
  10166. :position (point)
  10167. :from 'todo
  10168. :to 'done)))))
  10169. (defun org-update-statistics-cookies (all)
  10170. "Update the statistics cookie, either from TODO or from checkboxes.
  10171. This should be called with the cursor in a line with a statistics cookie."
  10172. (interactive "P")
  10173. (if all
  10174. (progn
  10175. (org-update-checkbox-count 'all)
  10176. (org-map-entries 'org-update-parent-todo-statistics))
  10177. (if (not (org-on-heading-p))
  10178. (org-update-checkbox-count)
  10179. (let ((pos (move-marker (make-marker) (point)))
  10180. end l1 l2)
  10181. (ignore-errors (org-back-to-heading t))
  10182. (if (not (org-on-heading-p))
  10183. (org-update-checkbox-count)
  10184. (setq l1 (org-outline-level))
  10185. (setq end (save-excursion
  10186. (outline-next-heading)
  10187. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10188. (point)))
  10189. (if (and (save-excursion
  10190. (re-search-forward
  10191. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10192. (not (save-excursion (re-search-forward
  10193. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10194. (org-update-checkbox-count)
  10195. (if (and l2 (> l2 l1))
  10196. (progn
  10197. (goto-char end)
  10198. (org-update-parent-todo-statistics))
  10199. (goto-char pos)
  10200. (beginning-of-line 1)
  10201. (while (re-search-forward
  10202. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10203. (point-at-eol) t)
  10204. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10205. (goto-char pos)
  10206. (move-marker pos nil)))))
  10207. (defvar org-entry-property-inherited-from) ;; defined below
  10208. (defun org-update-parent-todo-statistics ()
  10209. "Update any statistics cookie in the parent of the current headline.
  10210. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10211. the entire subtree and this will travel up the hierarchy and update
  10212. statistics everywhere."
  10213. (interactive)
  10214. (let* ((lim 0) prop
  10215. (recursive (or (not org-hierarchical-todo-statistics)
  10216. (string-match
  10217. "\\<recursive\\>"
  10218. (or (setq prop (org-entry-get
  10219. nil "COOKIE_DATA" 'inherit)) ""))))
  10220. (lim (or (and prop (marker-position
  10221. org-entry-property-inherited-from))
  10222. lim))
  10223. (first t)
  10224. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10225. level ltoggle l1 new ndel
  10226. (cnt-all 0) (cnt-done 0) is-percent kwd
  10227. checkbox-beg ov ovs ove cookie-present)
  10228. (catch 'exit
  10229. (save-excursion
  10230. (beginning-of-line 1)
  10231. (if (org-at-heading-p)
  10232. (setq ltoggle (funcall outline-level))
  10233. (error "This should not happen"))
  10234. (while (and (setq level (org-up-heading-safe))
  10235. (or recursive first)
  10236. (>= (point) lim))
  10237. (setq first nil cookie-present nil)
  10238. (unless (and level
  10239. (not (string-match
  10240. "\\<checkbox\\>"
  10241. (downcase
  10242. (or (org-entry-get
  10243. nil "COOKIE_DATA")
  10244. "")))))
  10245. (throw 'exit nil))
  10246. (while (re-search-forward box-re (point-at-eol) t)
  10247. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10248. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10249. (save-match-data
  10250. (unless (outline-next-heading) (throw 'exit nil))
  10251. (while (and (looking-at org-complex-heading-regexp)
  10252. (> (setq l1 (length (match-string 1))) level))
  10253. (setq kwd (and (or recursive (= l1 ltoggle))
  10254. (match-string 2)))
  10255. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10256. (and (listp org-provide-todo-statistics)
  10257. (or (member kwd org-provide-todo-statistics)
  10258. (member kwd org-done-keywords))))
  10259. (setq cnt-all (1+ cnt-all))
  10260. (if (eq org-provide-todo-statistics t)
  10261. (and kwd (setq cnt-all (1+ cnt-all)))))
  10262. (and (member kwd org-done-keywords)
  10263. (setq cnt-done (1+ cnt-done)))
  10264. (outline-next-heading)))
  10265. (setq new
  10266. (if is-percent
  10267. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10268. (format "[%d/%d]" cnt-done cnt-all))
  10269. ndel (- (match-end 0) checkbox-beg))
  10270. ;; handle overlays when updating cookie from column view
  10271. (when (setq ov (car (overlays-at checkbox-beg)))
  10272. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10273. (delete-overlay ov))
  10274. (goto-char checkbox-beg)
  10275. (insert new)
  10276. (delete-region (point) (+ (point) ndel))
  10277. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10278. (when ov (move-overlay ov ovs ove)))
  10279. (when cookie-present
  10280. (run-hook-with-args 'org-after-todo-statistics-hook
  10281. cnt-done (- cnt-all cnt-done))))))
  10282. (run-hooks 'org-todo-statistics-hook)))
  10283. (defvar org-after-todo-statistics-hook nil
  10284. "Hook that is called after a TODO statistics cookie has been updated.
  10285. Each function is called with two arguments: the number of not-done entries
  10286. and the number of done entries.
  10287. For example, the following function, when added to this hook, will switch
  10288. an entry to DONE when all children are done, and back to TODO when new
  10289. entries are set to a TODO status. Note that this hook is only called
  10290. when there is a statistics cookie in the headline!
  10291. (defun org-summary-todo (n-done n-not-done)
  10292. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10293. (let (org-log-done org-log-states) ; turn off logging
  10294. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10295. ")
  10296. (defvar org-todo-statistics-hook nil
  10297. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10298. This hook runs even if there is no statistics cookie present, in which case
  10299. `org-after-todo-statistics-hook' would not run.")
  10300. (defun org-todo-trigger-tag-changes (state)
  10301. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10302. (let ((l org-todo-state-tags-triggers)
  10303. changes)
  10304. (when (or (not state) (equal state ""))
  10305. (setq changes (append changes (cdr (assoc "" l)))))
  10306. (when (and (stringp state) (> (length state) 0))
  10307. (setq changes (append changes (cdr (assoc state l)))))
  10308. (when (member state org-not-done-keywords)
  10309. (setq changes (append changes (cdr (assoc 'todo l)))))
  10310. (when (member state org-done-keywords)
  10311. (setq changes (append changes (cdr (assoc 'done l)))))
  10312. (dolist (c changes)
  10313. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10314. (defun org-local-logging (value)
  10315. "Get logging settings from a property VALUE."
  10316. (let* (words w a)
  10317. ;; directly set the variables, they are already local.
  10318. (setq org-log-done nil
  10319. org-log-repeat nil
  10320. org-todo-log-states nil)
  10321. (setq words (org-split-string value))
  10322. (while (setq w (pop words))
  10323. (cond
  10324. ((setq a (assoc w org-startup-options))
  10325. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10326. (set (nth 1 a) (nth 2 a))))
  10327. ((setq a (org-extract-log-state-settings w))
  10328. (and (member (car a) org-todo-keywords-1)
  10329. (push a org-todo-log-states)))))))
  10330. (defun org-get-todo-sequence-head (kwd)
  10331. "Return the head of the TODO sequence to which KWD belongs.
  10332. If KWD is not set, check if there is a text property remembering the
  10333. right sequence."
  10334. (let (p)
  10335. (cond
  10336. ((not kwd)
  10337. (or (get-text-property (point-at-bol) 'org-todo-head)
  10338. (progn
  10339. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10340. nil (point-at-eol)))
  10341. (get-text-property p 'org-todo-head))))
  10342. ((not (member kwd org-todo-keywords-1))
  10343. (car org-todo-keywords-1))
  10344. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10345. (defun org-fast-todo-selection ()
  10346. "Fast TODO keyword selection with single keys.
  10347. Returns the new TODO keyword, or nil if no state change should occur."
  10348. (let* ((fulltable org-todo-key-alist)
  10349. (done-keywords org-done-keywords) ;; needed for the faces.
  10350. (maxlen (apply 'max (mapcar
  10351. (lambda (x)
  10352. (if (stringp (car x)) (string-width (car x)) 0))
  10353. fulltable)))
  10354. (expert nil)
  10355. (fwidth (+ maxlen 3 1 3))
  10356. (ncol (/ (- (window-width) 4) fwidth))
  10357. tg cnt e c tbl
  10358. groups ingroup)
  10359. (save-excursion
  10360. (save-window-excursion
  10361. (if expert
  10362. (set-buffer (get-buffer-create " *Org todo*"))
  10363. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10364. (erase-buffer)
  10365. (org-set-local 'org-done-keywords done-keywords)
  10366. (setq tbl fulltable cnt 0)
  10367. (while (setq e (pop tbl))
  10368. (cond
  10369. ((equal e '(:startgroup))
  10370. (push '() groups) (setq ingroup t)
  10371. (when (not (= cnt 0))
  10372. (setq cnt 0)
  10373. (insert "\n"))
  10374. (insert "{ "))
  10375. ((equal e '(:endgroup))
  10376. (setq ingroup nil cnt 0)
  10377. (insert "}\n"))
  10378. ((equal e '(:newline))
  10379. (when (not (= cnt 0))
  10380. (setq cnt 0)
  10381. (insert "\n")
  10382. (setq e (car tbl))
  10383. (while (equal (car tbl) '(:newline))
  10384. (insert "\n")
  10385. (setq tbl (cdr tbl)))))
  10386. (t
  10387. (setq tg (car e) c (cdr e))
  10388. (if ingroup (push tg (car groups)))
  10389. (setq tg (org-add-props tg nil 'face
  10390. (org-get-todo-face tg)))
  10391. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10392. (insert "[" c "] " tg (make-string
  10393. (- fwidth 4 (length tg)) ?\ ))
  10394. (when (= (setq cnt (1+ cnt)) ncol)
  10395. (insert "\n")
  10396. (if ingroup (insert " "))
  10397. (setq cnt 0)))))
  10398. (insert "\n")
  10399. (goto-char (point-min))
  10400. (if (not expert) (org-fit-window-to-buffer))
  10401. (message "[a-z..]:Set [SPC]:clear")
  10402. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10403. (cond
  10404. ((or (= c ?\C-g)
  10405. (and (= c ?q) (not (rassoc c fulltable))))
  10406. (setq quit-flag t))
  10407. ((= c ?\ ) nil)
  10408. ((setq e (rassoc c fulltable) tg (car e))
  10409. tg)
  10410. (t (setq quit-flag t)))))))
  10411. (defun org-entry-is-todo-p ()
  10412. (member (org-get-todo-state) org-not-done-keywords))
  10413. (defun org-entry-is-done-p ()
  10414. (member (org-get-todo-state) org-done-keywords))
  10415. (defun org-get-todo-state ()
  10416. (save-excursion
  10417. (org-back-to-heading t)
  10418. (and (looking-at org-todo-line-regexp)
  10419. (match-end 2)
  10420. (match-string 2))))
  10421. (defun org-at-date-range-p (&optional inactive-ok)
  10422. "Is the cursor inside a date range?"
  10423. (interactive)
  10424. (save-excursion
  10425. (catch 'exit
  10426. (let ((pos (point)))
  10427. (skip-chars-backward "^[<\r\n")
  10428. (skip-chars-backward "<[")
  10429. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10430. (>= (match-end 0) pos)
  10431. (throw 'exit t))
  10432. (skip-chars-backward "^<[\r\n")
  10433. (skip-chars-backward "<[")
  10434. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10435. (>= (match-end 0) pos)
  10436. (throw 'exit t)))
  10437. nil)))
  10438. (defun org-get-repeat (&optional tagline)
  10439. "Check if there is a deadline/schedule with repeater in this entry."
  10440. (save-match-data
  10441. (save-excursion
  10442. (org-back-to-heading t)
  10443. (and (re-search-forward (if tagline
  10444. (concat tagline "\\s-*" org-repeat-re)
  10445. org-repeat-re)
  10446. (org-entry-end-position) t)
  10447. (match-string-no-properties 1)))))
  10448. (defvar org-last-changed-timestamp)
  10449. (defvar org-last-inserted-timestamp)
  10450. (defvar org-log-post-message)
  10451. (defvar org-log-note-purpose)
  10452. (defvar org-log-note-how)
  10453. (defvar org-log-note-extra)
  10454. (defun org-auto-repeat-maybe (done-word)
  10455. "Check if the current headline contains a repeated deadline/schedule.
  10456. If yes, set TODO state back to what it was and change the base date
  10457. of repeating deadline/scheduled time stamps to new date.
  10458. This function is run automatically after each state change to a DONE state."
  10459. ;; last-state is dynamically scoped into this function
  10460. (let* ((repeat (org-get-repeat))
  10461. (aa (assoc last-state org-todo-kwd-alist))
  10462. (interpret (nth 1 aa))
  10463. (head (nth 2 aa))
  10464. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10465. (msg "Entry repeats: ")
  10466. (org-log-done nil)
  10467. (org-todo-log-states nil)
  10468. re type n what ts time to-state)
  10469. (when repeat
  10470. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10471. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10472. org-todo-repeat-to-state))
  10473. (unless (and to-state (member to-state org-todo-keywords-1))
  10474. (setq to-state (if (eq interpret 'type) last-state head)))
  10475. (org-todo to-state)
  10476. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10477. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10478. (org-time-stamp-format t t))))
  10479. (when org-log-repeat
  10480. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10481. (memq 'org-add-log-note post-command-hook))
  10482. ;; OK, we are already setup for some record
  10483. (if (eq org-log-repeat 'note)
  10484. ;; make sure we take a note, not only a time stamp
  10485. (setq org-log-note-how 'note))
  10486. ;; Set up for taking a record
  10487. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10488. last-state
  10489. 'findpos org-log-repeat)))
  10490. (org-back-to-heading t)
  10491. (org-add-planning-info nil nil 'closed)
  10492. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10493. org-deadline-time-regexp "\\)\\|\\("
  10494. org-ts-regexp "\\)"))
  10495. (while (re-search-forward
  10496. re (save-excursion (outline-next-heading) (point)) t)
  10497. (setq type (if (match-end 1) org-scheduled-string
  10498. (if (match-end 3) org-deadline-string "Plain:"))
  10499. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10500. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10501. (setq n (string-to-number (match-string 2 ts))
  10502. what (match-string 3 ts))
  10503. (if (equal what "w") (setq n (* n 7) what "d"))
  10504. ;; Preparation, see if we need to modify the start date for the change
  10505. (when (match-end 1)
  10506. (setq time (save-match-data (org-time-string-to-time ts)))
  10507. (cond
  10508. ((equal (match-string 1 ts) ".")
  10509. ;; Shift starting date to today
  10510. (org-timestamp-change
  10511. (- (time-to-days (current-time)) (time-to-days time))
  10512. 'day))
  10513. ((equal (match-string 1 ts) "+")
  10514. (let ((nshiftmax 10) (nshift 0))
  10515. (while (or (= nshift 0)
  10516. (<= (time-to-days time)
  10517. (time-to-days (current-time))))
  10518. (when (= (incf nshift) nshiftmax)
  10519. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10520. (error "Abort")))
  10521. (org-timestamp-change n (cdr (assoc what whata)))
  10522. (org-at-timestamp-p t)
  10523. (setq ts (match-string 1))
  10524. (setq time (save-match-data (org-time-string-to-time ts)))))
  10525. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10526. ;; rematch, so that we have everything in place for the real shift
  10527. (org-at-timestamp-p t)
  10528. (setq ts (match-string 1))
  10529. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10530. (org-timestamp-change n (cdr (assoc what whata)))
  10531. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10532. (setq org-log-post-message msg)
  10533. (message "%s" msg))))
  10534. (defun org-show-todo-tree (arg)
  10535. "Make a compact tree which shows all headlines marked with TODO.
  10536. The tree will show the lines where the regexp matches, and all higher
  10537. headlines above the match.
  10538. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10539. With a numeric prefix N, construct a sparse tree for the Nth element
  10540. of `org-todo-keywords-1'."
  10541. (interactive "P")
  10542. (let ((case-fold-search nil)
  10543. (kwd-re
  10544. (cond ((null arg) org-not-done-regexp)
  10545. ((equal arg '(4))
  10546. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10547. (mapcar 'list org-todo-keywords-1))))
  10548. (concat "\\("
  10549. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10550. "\\)\\>")))
  10551. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10552. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10553. org-todo-keywords-1)))
  10554. (t (error "Invalid prefix argument: %s" arg)))))
  10555. (message "%d TODO entries found"
  10556. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10557. (defun org-deadline (&optional remove time)
  10558. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10559. With argument REMOVE, remove any deadline from the item.
  10560. When TIME is set, it should be an internal time specification, and the
  10561. scheduling will use the corresponding date."
  10562. (interactive "P")
  10563. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10564. (repeater (and old-date
  10565. (string-match
  10566. "\\([.+]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10567. old-date)
  10568. (match-string 1 old-date))))
  10569. (if remove
  10570. (progn
  10571. (when (and old-date org-log-redeadline)
  10572. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10573. org-log-redeadline))
  10574. (org-remove-timestamp-with-keyword org-deadline-string)
  10575. (message "Item no longer has a deadline."))
  10576. (org-add-planning-info 'deadline time 'closed)
  10577. (when (and old-date org-log-redeadline
  10578. (not (equal old-date
  10579. (substring org-last-inserted-timestamp 1 -1))))
  10580. (org-add-log-setup 'redeadline nil old-date 'findpos
  10581. org-log-redeadline))
  10582. (when repeater
  10583. (save-excursion
  10584. (org-back-to-heading t)
  10585. (when (re-search-forward (concat org-deadline-string " "
  10586. org-last-inserted-timestamp)
  10587. (save-excursion
  10588. (outline-next-heading) (point)) t)
  10589. (goto-char (1- (match-end 0)))
  10590. (insert " " repeater)
  10591. (setq org-last-inserted-timestamp
  10592. (concat (substring org-last-inserted-timestamp 0 -1)
  10593. " " repeater
  10594. (substring org-last-inserted-timestamp -1))))))
  10595. (message "Deadline on %s" org-last-inserted-timestamp))))
  10596. (defun org-schedule (&optional remove time)
  10597. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10598. With argument REMOVE, remove any scheduling date from the item.
  10599. When TIME is set, it should be an internal time specification, and the
  10600. scheduling will use the corresponding date."
  10601. (interactive "P")
  10602. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10603. (repeater (and old-date
  10604. (string-match
  10605. "\\([.+]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10606. old-date)
  10607. (match-string 1 old-date))))
  10608. (if remove
  10609. (progn
  10610. (when (and old-date org-log-reschedule)
  10611. (org-add-log-setup 'delschedule nil old-date 'findpos
  10612. org-log-reschedule))
  10613. (org-remove-timestamp-with-keyword org-scheduled-string)
  10614. (message "Item is no longer scheduled."))
  10615. (org-add-planning-info 'scheduled time 'closed)
  10616. (when (and old-date org-log-reschedule
  10617. (not (equal old-date
  10618. (substring org-last-inserted-timestamp 1 -1))))
  10619. (org-add-log-setup 'reschedule nil old-date 'findpos
  10620. org-log-reschedule))
  10621. (when repeater
  10622. (save-excursion
  10623. (org-back-to-heading t)
  10624. (when (re-search-forward (concat org-scheduled-string " "
  10625. org-last-inserted-timestamp)
  10626. (save-excursion
  10627. (outline-next-heading) (point)) t)
  10628. (goto-char (1- (match-end 0)))
  10629. (insert " " repeater)
  10630. (setq org-last-inserted-timestamp
  10631. (concat (substring org-last-inserted-timestamp 0 -1)
  10632. " " repeater
  10633. (substring org-last-inserted-timestamp -1))))))
  10634. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10635. (defun org-get-scheduled-time (pom &optional inherit)
  10636. "Get the scheduled time as a time tuple, of a format suitable
  10637. for calling org-schedule with, or if there is no scheduling,
  10638. returns nil."
  10639. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10640. (when time
  10641. (apply 'encode-time (org-parse-time-string time)))))
  10642. (defun org-get-deadline-time (pom &optional inherit)
  10643. "Get the deadline as a time tuple, of a format suitable for
  10644. calling org-deadline with, or if there is no scheduling, returns
  10645. nil."
  10646. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10647. (when time
  10648. (apply 'encode-time (org-parse-time-string time)))))
  10649. (defun org-remove-timestamp-with-keyword (keyword)
  10650. "Remove all time stamps with KEYWORD in the current entry."
  10651. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10652. beg)
  10653. (save-excursion
  10654. (org-back-to-heading t)
  10655. (setq beg (point))
  10656. (outline-next-heading)
  10657. (while (re-search-backward re beg t)
  10658. (replace-match "")
  10659. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10660. (equal (char-before) ?\ ))
  10661. (backward-delete-char 1)
  10662. (if (string-match "^[ \t]*$" (buffer-substring
  10663. (point-at-bol) (point-at-eol)))
  10664. (delete-region (point-at-bol)
  10665. (min (point-max) (1+ (point-at-eol))))))))))
  10666. (defun org-add-planning-info (what &optional time &rest remove)
  10667. "Insert new timestamp with keyword in the line directly after the headline.
  10668. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10669. If non is given, the user is prompted for a date.
  10670. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10671. be removed."
  10672. (interactive)
  10673. (let (org-time-was-given org-end-time-was-given ts
  10674. end default-time default-input)
  10675. (catch 'exit
  10676. (when (and (not time) (memq what '(scheduled deadline)))
  10677. ;; Try to get a default date/time from existing timestamp
  10678. (save-excursion
  10679. (org-back-to-heading t)
  10680. (setq end (save-excursion (outline-next-heading) (point)))
  10681. (when (re-search-forward (if (eq what 'scheduled)
  10682. org-scheduled-time-regexp
  10683. org-deadline-time-regexp)
  10684. end t)
  10685. (setq ts (match-string 1)
  10686. default-time
  10687. (apply 'encode-time (org-parse-time-string ts))
  10688. default-input (and ts (org-get-compact-tod ts))))))
  10689. (when what
  10690. ;; If necessary, get the time from the user
  10691. (setq time (or time (org-read-date nil 'to-time nil nil
  10692. default-time default-input))))
  10693. (when (and org-insert-labeled-timestamps-at-point
  10694. (member what '(scheduled deadline)))
  10695. (insert
  10696. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10697. (org-insert-time-stamp time org-time-was-given
  10698. nil nil nil (list org-end-time-was-given))
  10699. (setq what nil))
  10700. (save-excursion
  10701. (save-restriction
  10702. (let (col list elt ts buffer-invisibility-spec)
  10703. (org-back-to-heading t)
  10704. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10705. (goto-char (match-end 1))
  10706. (setq col (current-column))
  10707. (goto-char (match-end 0))
  10708. (if (eobp) (insert "\n") (forward-char 1))
  10709. (when (and (not what)
  10710. (not (looking-at
  10711. (concat "[ \t]*"
  10712. org-keyword-time-not-clock-regexp))))
  10713. ;; Nothing to add, nothing to remove...... :-)
  10714. (throw 'exit nil))
  10715. (if (and (not (looking-at outline-regexp))
  10716. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10717. "[^\r\n]*"))
  10718. (not (equal (match-string 1) org-clock-string)))
  10719. (narrow-to-region (match-beginning 0) (match-end 0))
  10720. (insert-before-markers "\n")
  10721. (backward-char 1)
  10722. (narrow-to-region (point) (point))
  10723. (and org-adapt-indentation (org-indent-to-column col)))
  10724. ;; Check if we have to remove something.
  10725. (setq list (cons what remove))
  10726. (while list
  10727. (setq elt (pop list))
  10728. (goto-char (point-min))
  10729. (when (or (and (eq elt 'scheduled)
  10730. (re-search-forward org-scheduled-time-regexp nil t))
  10731. (and (eq elt 'deadline)
  10732. (re-search-forward org-deadline-time-regexp nil t))
  10733. (and (eq elt 'closed)
  10734. (re-search-forward org-closed-time-regexp nil t)))
  10735. (replace-match "")
  10736. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10737. (skip-chars-backward " ")
  10738. (if (looking-at " +") (replace-match ""))))
  10739. (goto-char (point-max))
  10740. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10741. (when what
  10742. (insert
  10743. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10744. (cond ((eq what 'scheduled) org-scheduled-string)
  10745. ((eq what 'deadline) org-deadline-string)
  10746. ((eq what 'closed) org-closed-string))
  10747. " ")
  10748. (setq ts (org-insert-time-stamp
  10749. time
  10750. (or org-time-was-given
  10751. (and (eq what 'closed) org-log-done-with-time))
  10752. (eq what 'closed)
  10753. nil nil (list org-end-time-was-given)))
  10754. (end-of-line 1))
  10755. (goto-char (point-min))
  10756. (widen)
  10757. (if (and (looking-at "[ \t]*\n")
  10758. (equal (char-before) ?\n))
  10759. (delete-region (1- (point)) (point-at-eol)))
  10760. ts))))))
  10761. (defvar org-log-note-marker (make-marker))
  10762. (defvar org-log-note-purpose nil)
  10763. (defvar org-log-note-state nil)
  10764. (defvar org-log-note-previous-state nil)
  10765. (defvar org-log-note-how nil)
  10766. (defvar org-log-note-extra nil)
  10767. (defvar org-log-note-window-configuration nil)
  10768. (defvar org-log-note-return-to (make-marker))
  10769. (defvar org-log-post-message nil
  10770. "Message to be displayed after a log note has been stored.
  10771. The auto-repeater uses this.")
  10772. (defun org-add-note ()
  10773. "Add a note to the current entry.
  10774. This is done in the same way as adding a state change note."
  10775. (interactive)
  10776. (org-add-log-setup 'note nil nil 'findpos nil))
  10777. (defvar org-property-end-re)
  10778. (defun org-add-log-setup (&optional purpose state prev-state
  10779. findpos how extra)
  10780. "Set up the post command hook to take a note.
  10781. If this is about to TODO state change, the new state is expected in STATE.
  10782. When FINDPOS is non-nil, find the correct position for the note in
  10783. the current entry. If not, assume that it can be inserted at point.
  10784. HOW is an indicator what kind of note should be created.
  10785. EXTRA is additional text that will be inserted into the notes buffer."
  10786. (let* ((org-log-into-drawer (org-log-into-drawer))
  10787. (drawer (cond ((stringp org-log-into-drawer)
  10788. org-log-into-drawer)
  10789. (org-log-into-drawer "LOGBOOK")
  10790. (t nil))))
  10791. (save-restriction
  10792. (save-excursion
  10793. (when findpos
  10794. (org-back-to-heading t)
  10795. (narrow-to-region (point) (save-excursion
  10796. (outline-next-heading) (point)))
  10797. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10798. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10799. "[^\r\n]*\\)?"))
  10800. (goto-char (match-end 0))
  10801. (cond
  10802. (drawer
  10803. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10804. nil t)
  10805. (progn
  10806. (goto-char (match-end 0))
  10807. (or org-log-states-order-reversed
  10808. (and (re-search-forward org-property-end-re nil t)
  10809. (goto-char (1- (match-beginning 0))))))
  10810. (insert "\n:" drawer ":\n:END:")
  10811. (beginning-of-line 0)
  10812. (org-indent-line-function)
  10813. (beginning-of-line 2)
  10814. (org-indent-line-function)
  10815. (end-of-line 0)))
  10816. ((and org-log-state-notes-insert-after-drawers
  10817. (save-excursion
  10818. (forward-line) (looking-at org-drawer-regexp)))
  10819. (forward-line)
  10820. (while (looking-at org-drawer-regexp)
  10821. (goto-char (match-end 0))
  10822. (re-search-forward org-property-end-re (point-max) t)
  10823. (forward-line))
  10824. (forward-line -1)))
  10825. (unless org-log-states-order-reversed
  10826. (and (= (char-after) ?\n) (forward-char 1))
  10827. (org-skip-over-state-notes)
  10828. (skip-chars-backward " \t\n\r")))
  10829. (move-marker org-log-note-marker (point))
  10830. (setq org-log-note-purpose purpose
  10831. org-log-note-state state
  10832. org-log-note-previous-state prev-state
  10833. org-log-note-how how
  10834. org-log-note-extra extra)
  10835. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10836. (defun org-skip-over-state-notes ()
  10837. "Skip past the list of State notes in an entry."
  10838. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10839. (when (ignore-errors (goto-char (org-in-item-p)))
  10840. (let* ((struct (org-list-struct))
  10841. (prevs (org-list-prevs-alist struct)))
  10842. (while (looking-at "[ \t]*- State")
  10843. (goto-char (or (org-list-get-next-item (point) struct prevs)
  10844. (org-list-get-item-end (point) struct)))))))
  10845. (defun org-add-log-note (&optional purpose)
  10846. "Pop up a window for taking a note, and add this note later at point."
  10847. (remove-hook 'post-command-hook 'org-add-log-note)
  10848. (setq org-log-note-window-configuration (current-window-configuration))
  10849. (delete-other-windows)
  10850. (move-marker org-log-note-return-to (point))
  10851. (switch-to-buffer (marker-buffer org-log-note-marker))
  10852. (goto-char org-log-note-marker)
  10853. (org-switch-to-buffer-other-window "*Org Note*")
  10854. (erase-buffer)
  10855. (if (memq org-log-note-how '(time state))
  10856. (let (current-prefix-arg) (org-store-log-note))
  10857. (let ((org-inhibit-startup t)) (org-mode))
  10858. (insert (format "# Insert note for %s.
  10859. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10860. (cond
  10861. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10862. ((eq org-log-note-purpose 'done) "closed todo item")
  10863. ((eq org-log-note-purpose 'state)
  10864. (format "state change from \"%s\" to \"%s\""
  10865. (or org-log-note-previous-state "")
  10866. (or org-log-note-state "")))
  10867. ((eq org-log-note-purpose 'reschedule)
  10868. "rescheduling")
  10869. ((eq org-log-note-purpose 'delschedule)
  10870. "no longer scheduled")
  10871. ((eq org-log-note-purpose 'redeadline)
  10872. "changing deadline")
  10873. ((eq org-log-note-purpose 'deldeadline)
  10874. "removing deadline")
  10875. ((eq org-log-note-purpose 'refile)
  10876. "refiling")
  10877. ((eq org-log-note-purpose 'note)
  10878. "this entry")
  10879. (t (error "This should not happen")))))
  10880. (if org-log-note-extra (insert org-log-note-extra))
  10881. (org-set-local 'org-finish-function 'org-store-log-note)))
  10882. (defvar org-note-abort nil) ; dynamically scoped
  10883. (defun org-store-log-note ()
  10884. "Finish taking a log note, and insert it to where it belongs."
  10885. (let ((txt (buffer-string))
  10886. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10887. lines ind bul)
  10888. (kill-buffer (current-buffer))
  10889. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10890. (setq txt (replace-match "" t t txt)))
  10891. (if (string-match "\\s-+\\'" txt)
  10892. (setq txt (replace-match "" t t txt)))
  10893. (setq lines (org-split-string txt "\n"))
  10894. (when (and note (string-match "\\S-" note))
  10895. (setq note
  10896. (org-replace-escapes
  10897. note
  10898. (list (cons "%u" (user-login-name))
  10899. (cons "%U" user-full-name)
  10900. (cons "%t" (format-time-string
  10901. (org-time-stamp-format 'long 'inactive)
  10902. (current-time)))
  10903. (cons "%T" (format-time-string
  10904. (org-time-stamp-format 'long nil)
  10905. (current-time)))
  10906. (cons "%s" (if org-log-note-state
  10907. (concat "\"" org-log-note-state "\"")
  10908. ""))
  10909. (cons "%S" (if org-log-note-previous-state
  10910. (concat "\"" org-log-note-previous-state "\"")
  10911. "\"\"")))))
  10912. (if lines (setq note (concat note " \\\\")))
  10913. (push note lines))
  10914. (when (or current-prefix-arg org-note-abort)
  10915. (when org-log-into-drawer
  10916. (org-remove-empty-drawer-at
  10917. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10918. org-log-note-marker))
  10919. (setq lines nil))
  10920. (when lines
  10921. (with-current-buffer (marker-buffer org-log-note-marker)
  10922. (save-excursion
  10923. (goto-char org-log-note-marker)
  10924. (move-marker org-log-note-marker nil)
  10925. (end-of-line 1)
  10926. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10927. (setq ind (save-excursion
  10928. (if (ignore-errors (goto-char (org-in-item-p)))
  10929. (let ((struct (org-list-struct)))
  10930. (org-list-get-ind
  10931. (org-list-get-top-point struct) struct))
  10932. (skip-chars-backward " \r\t\n")
  10933. (cond
  10934. ((and (org-at-heading-p)
  10935. org-adapt-indentation)
  10936. (1+ (org-current-level)))
  10937. ((org-at-heading-p) 0)
  10938. (t (org-get-indentation))))))
  10939. (setq bul (org-list-bullet-string "-"))
  10940. (org-indent-line-to ind)
  10941. (insert bul (pop lines))
  10942. (let ((ind-body (+ (length bul) ind)))
  10943. (while lines
  10944. (insert "\n")
  10945. (org-indent-line-to ind-body)
  10946. (insert (pop lines))))
  10947. (message "Note stored")
  10948. (org-back-to-heading t)
  10949. (org-cycle-hide-drawers 'children)))))
  10950. (set-window-configuration org-log-note-window-configuration)
  10951. (with-current-buffer (marker-buffer org-log-note-return-to)
  10952. (goto-char org-log-note-return-to))
  10953. (move-marker org-log-note-return-to nil)
  10954. (and org-log-post-message (message "%s" org-log-post-message)))
  10955. (defun org-remove-empty-drawer-at (drawer pos)
  10956. "Remove an empty drawer DRAWER at position POS.
  10957. POS may also be a marker."
  10958. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10959. (save-excursion
  10960. (save-restriction
  10961. (widen)
  10962. (goto-char pos)
  10963. (if (org-in-regexp
  10964. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10965. (replace-match ""))))))
  10966. (defun org-sparse-tree (&optional arg)
  10967. "Create a sparse tree, prompt for the details.
  10968. This command can create sparse trees. You first need to select the type
  10969. of match used to create the tree:
  10970. t Show all TODO entries.
  10971. T Show entries with a specific TODO keyword.
  10972. m Show entries selected by a tags/property match.
  10973. p Enter a property name and its value (both with completion on existing
  10974. names/values) and show entries with that property.
  10975. r Show entries matching a regular expression (`/' can be used as well)
  10976. d Show deadlines due within `org-deadline-warning-days'.
  10977. b Show deadlines and scheduled items before a date.
  10978. a Show deadlines and scheduled items after a date."
  10979. (interactive "P")
  10980. (let (ans kwd value)
  10981. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  10982. (setq ans (read-char-exclusive))
  10983. (cond
  10984. ((equal ans ?d)
  10985. (call-interactively 'org-check-deadlines))
  10986. ((equal ans ?b)
  10987. (call-interactively 'org-check-before-date))
  10988. ((equal ans ?a)
  10989. (call-interactively 'org-check-after-date))
  10990. ((equal ans ?t)
  10991. (org-show-todo-tree nil))
  10992. ((equal ans ?T)
  10993. (org-show-todo-tree '(4)))
  10994. ((member ans '(?T ?m))
  10995. (call-interactively 'org-match-sparse-tree))
  10996. ((member ans '(?p ?P))
  10997. (setq kwd (org-icompleting-read "Property: "
  10998. (mapcar 'list (org-buffer-property-keys))))
  10999. (setq value (org-icompleting-read "Value: "
  11000. (mapcar 'list (org-property-values kwd))))
  11001. (unless (string-match "\\`{.*}\\'" value)
  11002. (setq value (concat "\"" value "\"")))
  11003. (org-match-sparse-tree arg (concat kwd "=" value)))
  11004. ((member ans '(?r ?R ?/))
  11005. (call-interactively 'org-occur))
  11006. (t (error "No such sparse tree command \"%c\"" ans)))))
  11007. (defvar org-occur-highlights nil
  11008. "List of overlays used for occur matches.")
  11009. (make-variable-buffer-local 'org-occur-highlights)
  11010. (defvar org-occur-parameters nil
  11011. "Parameters of the active org-occur calls.
  11012. This is a list, each call to org-occur pushes as cons cell,
  11013. containing the regular expression and the callback, onto the list.
  11014. The list can contain several entries if `org-occur' has been called
  11015. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11016. will only contain one set of parameters. When the highlights are
  11017. removed (for example with `C-c C-c', or with the next edit (depending
  11018. on `org-remove-highlights-with-change'), this variable is emptied
  11019. as well.")
  11020. (make-variable-buffer-local 'org-occur-parameters)
  11021. (defun org-occur (regexp &optional keep-previous callback)
  11022. "Make a compact tree which shows all matches of REGEXP.
  11023. The tree will show the lines where the regexp matches, and all higher
  11024. headlines above the match. It will also show the heading after the match,
  11025. to make sure editing the matching entry is easy.
  11026. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11027. call to `org-occur' will be kept, to allow stacking of calls to this
  11028. command.
  11029. If CALLBACK is non-nil, it is a function which is called to confirm
  11030. that the match should indeed be shown."
  11031. (interactive "sRegexp: \nP")
  11032. (when (equal regexp "")
  11033. (error "Regexp cannot be empty"))
  11034. (unless keep-previous
  11035. (org-remove-occur-highlights nil nil t))
  11036. (push (cons regexp callback) org-occur-parameters)
  11037. (let ((cnt 0))
  11038. (save-excursion
  11039. (goto-char (point-min))
  11040. (if (or (not keep-previous) ; do not want to keep
  11041. (not org-occur-highlights)) ; no previous matches
  11042. ;; hide everything
  11043. (org-overview))
  11044. (while (re-search-forward regexp nil t)
  11045. (when (or (not callback)
  11046. (save-match-data (funcall callback)))
  11047. (setq cnt (1+ cnt))
  11048. (when org-highlight-sparse-tree-matches
  11049. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11050. (org-show-context 'occur-tree))))
  11051. (when org-remove-highlights-with-change
  11052. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11053. nil 'local))
  11054. (unless org-sparse-tree-open-archived-trees
  11055. (org-hide-archived-subtrees (point-min) (point-max)))
  11056. (run-hooks 'org-occur-hook)
  11057. (if (called-interactively-p 'interactive)
  11058. (message "%d match(es) for regexp %s" cnt regexp))
  11059. cnt))
  11060. (defun org-occur-next-match (&optional n reset)
  11061. "Function for `next-error-function' to find sparse tree matches.
  11062. N is the number of matches to move, when negative move backwards.
  11063. RESET is entirely ignored - this function always goes back to the
  11064. starting point when no match is found."
  11065. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11066. (search-func (if (< n 0)
  11067. 'previous-single-char-property-change
  11068. 'next-single-char-property-change))
  11069. (n (abs n))
  11070. (pos (point))
  11071. p1)
  11072. (catch 'exit
  11073. (while (setq p1 (funcall search-func (point) 'org-type))
  11074. (when (equal p1 limit)
  11075. (goto-char pos)
  11076. (error "No more matches"))
  11077. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11078. (setq n (1- n))
  11079. (when (= n 0)
  11080. (goto-char p1)
  11081. (throw 'exit (point))))
  11082. (goto-char p1))
  11083. (goto-char p1)
  11084. (error "No more matches"))))
  11085. (defun org-show-context (&optional key)
  11086. "Make sure point and context are visible.
  11087. How much context is shown depends upon the variables
  11088. `org-show-hierarchy-above', `org-show-following-heading'. and
  11089. `org-show-siblings'."
  11090. (let ((heading-p (org-on-heading-p t))
  11091. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11092. (following-p (org-get-alist-option org-show-following-heading key))
  11093. (entry-p (org-get-alist-option org-show-entry-below key))
  11094. (siblings-p (org-get-alist-option org-show-siblings key)))
  11095. (catch 'exit
  11096. ;; Show heading or entry text
  11097. (if (and heading-p (not entry-p))
  11098. (org-flag-heading nil) ; only show the heading
  11099. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11100. (org-show-hidden-entry))) ; show entire entry
  11101. (when following-p
  11102. ;; Show next sibling, or heading below text
  11103. (save-excursion
  11104. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11105. (org-flag-heading nil))))
  11106. (when siblings-p (org-show-siblings))
  11107. (when hierarchy-p
  11108. ;; show all higher headings, possibly with siblings
  11109. (save-excursion
  11110. (while (and (condition-case nil
  11111. (progn (org-up-heading-all 1) t)
  11112. (error nil))
  11113. (not (bobp)))
  11114. (org-flag-heading nil)
  11115. (when siblings-p (org-show-siblings))))))))
  11116. (defvar org-reveal-start-hook nil
  11117. "Hook run before revealing a location.")
  11118. (defun org-reveal (&optional siblings)
  11119. "Show current entry, hierarchy above it, and the following headline.
  11120. This can be used to show a consistent set of context around locations
  11121. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11122. not t for the search context.
  11123. With optional argument SIBLINGS, on each level of the hierarchy all
  11124. siblings are shown. This repairs the tree structure to what it would
  11125. look like when opened with hierarchical calls to `org-cycle'.
  11126. With double optional argument \\[universal-argument] \\[universal-argument], \
  11127. go to the parent and show the
  11128. entire tree."
  11129. (interactive "P")
  11130. (run-hooks 'org-reveal-start-hook)
  11131. (let ((org-show-hierarchy-above t)
  11132. (org-show-following-heading t)
  11133. (org-show-siblings (if siblings t org-show-siblings)))
  11134. (org-show-context nil))
  11135. (when (equal siblings '(16))
  11136. (save-excursion
  11137. (when (org-up-heading-safe)
  11138. (org-show-subtree)
  11139. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11140. (defun org-highlight-new-match (beg end)
  11141. "Highlight from BEG to END and mark the highlight is an occur headline."
  11142. (let ((ov (make-overlay beg end)))
  11143. (overlay-put ov 'face 'secondary-selection)
  11144. (overlay-put ov 'org-type 'org-occur)
  11145. (push ov org-occur-highlights)))
  11146. (defun org-remove-occur-highlights (&optional beg end noremove)
  11147. "Remove the occur highlights from the buffer.
  11148. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11149. from the `before-change-functions' in the current buffer."
  11150. (interactive)
  11151. (unless org-inhibit-highlight-removal
  11152. (mapc 'delete-overlay org-occur-highlights)
  11153. (setq org-occur-highlights nil)
  11154. (setq org-occur-parameters nil)
  11155. (unless noremove
  11156. (remove-hook 'before-change-functions
  11157. 'org-remove-occur-highlights 'local))))
  11158. ;;;; Priorities
  11159. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11160. "Regular expression matching the priority indicator.")
  11161. (defvar org-remove-priority-next-time nil)
  11162. (defun org-priority-up ()
  11163. "Increase the priority of the current item."
  11164. (interactive)
  11165. (org-priority 'up))
  11166. (defun org-priority-down ()
  11167. "Decrease the priority of the current item."
  11168. (interactive)
  11169. (org-priority 'down))
  11170. (defun org-priority (&optional action)
  11171. "Change the priority of an item by ARG.
  11172. ACTION can be `set', `up', `down', or a character."
  11173. (interactive)
  11174. (unless org-enable-priority-commands
  11175. (error "Priority commands are disabled"))
  11176. (setq action (or action 'set))
  11177. (let (current new news have remove)
  11178. (save-excursion
  11179. (org-back-to-heading t)
  11180. (if (looking-at org-priority-regexp)
  11181. (setq current (string-to-char (match-string 2))
  11182. have t))
  11183. (cond
  11184. ((eq action 'remove)
  11185. (setq remove t new ?\ ))
  11186. ((or (eq action 'set)
  11187. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11188. (if (not (eq action 'set))
  11189. (setq new action)
  11190. (message "Priority %c-%c, SPC to remove: "
  11191. org-highest-priority org-lowest-priority)
  11192. (save-match-data
  11193. (setq new (read-char-exclusive))))
  11194. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11195. (= (upcase org-lowest-priority) org-lowest-priority))
  11196. (setq new (upcase new)))
  11197. (cond ((equal new ?\ ) (setq remove t))
  11198. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11199. (error "Priority must be between `%c' and `%c'"
  11200. org-highest-priority org-lowest-priority))))
  11201. ((eq action 'up)
  11202. (setq new (if have
  11203. (1- current) ; normal cycling
  11204. ;; last priority was empty
  11205. (if (eq last-command this-command)
  11206. org-lowest-priority ; wrap around empty to lowest
  11207. ;; default
  11208. (if org-priority-start-cycle-with-default
  11209. org-default-priority
  11210. (1- org-default-priority))))))
  11211. ((eq action 'down)
  11212. (setq new (if have
  11213. (1+ current) ; normal cycling
  11214. ;; last priority was empty
  11215. (if (eq last-command this-command)
  11216. org-highest-priority ; wrap around empty to highest
  11217. ;; default
  11218. (if org-priority-start-cycle-with-default
  11219. org-default-priority
  11220. (1+ org-default-priority))))))
  11221. (t (error "Invalid action")))
  11222. (if (or (< (upcase new) org-highest-priority)
  11223. (> (upcase new) org-lowest-priority))
  11224. (if (and (memq action '(up down))
  11225. (not have) (not (eq last-command this-command)))
  11226. ;; `new' is from default priority
  11227. (error
  11228. "The default can not be set, see `org-default-priority' why")
  11229. ;; normal cycling: `new' is beyond highest/lowest priority
  11230. ;; and is wrapped around to the empty priority
  11231. (setq remove t)))
  11232. (setq news (format "%c" new))
  11233. (if have
  11234. (if remove
  11235. (replace-match "" t t nil 1)
  11236. (replace-match news t t nil 2))
  11237. (if remove
  11238. (error "No priority cookie found in line")
  11239. (let ((case-fold-search nil))
  11240. (looking-at org-todo-line-regexp))
  11241. (if (match-end 2)
  11242. (progn
  11243. (goto-char (match-end 2))
  11244. (insert " [#" news "]"))
  11245. (goto-char (match-beginning 3))
  11246. (insert "[#" news "] "))))
  11247. (org-preserve-lc (org-set-tags nil 'align)))
  11248. (if remove
  11249. (message "Priority removed")
  11250. (message "Priority of current item set to %s" news))))
  11251. (defun org-get-priority (s)
  11252. "Find priority cookie and return priority."
  11253. (if (functionp org-get-priority-function)
  11254. (funcall org-get-priority-function)
  11255. (save-match-data
  11256. (if (not (string-match org-priority-regexp s))
  11257. (* 1000 (- org-lowest-priority org-default-priority))
  11258. (* 1000 (- org-lowest-priority
  11259. (string-to-char (match-string 2 s))))))))
  11260. ;;;; Tags
  11261. (defvar org-agenda-archives-mode)
  11262. (defvar org-map-continue-from nil
  11263. "Position from where mapping should continue.
  11264. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11265. (defvar org-scanner-tags nil
  11266. "The current tag list while the tags scanner is running.")
  11267. (defvar org-trust-scanner-tags nil
  11268. "Should `org-get-tags-at' use the tags for the scanner.
  11269. This is for internal dynamical scoping only.
  11270. When this is non-nil, the function `org-get-tags-at' will return the value
  11271. of `org-scanner-tags' instead of building the list by itself. This
  11272. can lead to large speed-ups when the tags scanner is used in a file with
  11273. many entries, and when the list of tags is retrieved, for example to
  11274. obtain a list of properties. Building the tags list for each entry in such
  11275. a file becomes an N^2 operation - but with this variable set, it scales
  11276. as N.")
  11277. (defun org-scan-tags (action matcher &optional todo-only)
  11278. "Scan headline tags with inheritance and produce output ACTION.
  11279. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11280. or `agenda' to produce an entry list for an agenda view. It can also be
  11281. a Lisp form or a function that should be called at each matched headline, in
  11282. this case the return value is a list of all return values from these calls.
  11283. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11284. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11285. only lines with a TODO keyword are included in the output."
  11286. (require 'org-agenda)
  11287. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11288. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11289. (org-re
  11290. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11291. (props (list 'face 'default
  11292. 'done-face 'org-agenda-done
  11293. 'undone-face 'default
  11294. 'mouse-face 'highlight
  11295. 'org-not-done-regexp org-not-done-regexp
  11296. 'org-todo-regexp org-todo-regexp
  11297. 'help-echo
  11298. (format "mouse-2 or RET jump to org file %s"
  11299. (abbreviate-file-name
  11300. (or (buffer-file-name (buffer-base-buffer))
  11301. (buffer-name (buffer-base-buffer)))))))
  11302. (case-fold-search nil)
  11303. (org-map-continue-from nil)
  11304. lspos tags tags-list
  11305. (tags-alist (list (cons 0 org-file-tags)))
  11306. (llast 0) rtn rtn1 level category i txt
  11307. todo marker entry priority)
  11308. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11309. (setq action (list 'lambda nil action)))
  11310. (save-excursion
  11311. (goto-char (point-min))
  11312. (when (eq action 'sparse-tree)
  11313. (org-overview)
  11314. (org-remove-occur-highlights))
  11315. (while (re-search-forward re nil t)
  11316. (catch :skip
  11317. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11318. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11319. (goto-char (setq lspos (match-beginning 0)))
  11320. (setq level (org-reduced-level (funcall outline-level))
  11321. category (org-get-category))
  11322. (setq i llast llast level)
  11323. ;; remove tag lists from same and sublevels
  11324. (while (>= i level)
  11325. (when (setq entry (assoc i tags-alist))
  11326. (setq tags-alist (delete entry tags-alist)))
  11327. (setq i (1- i)))
  11328. ;; add the next tags
  11329. (when tags
  11330. (setq tags (org-split-string tags ":")
  11331. tags-alist
  11332. (cons (cons level tags) tags-alist)))
  11333. ;; compile tags for current headline
  11334. (setq tags-list
  11335. (if org-use-tag-inheritance
  11336. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11337. tags)
  11338. org-scanner-tags tags-list)
  11339. (when org-use-tag-inheritance
  11340. (setcdr (car tags-alist)
  11341. (mapcar (lambda (x)
  11342. (setq x (copy-sequence x))
  11343. (org-add-prop-inherited x))
  11344. (cdar tags-alist))))
  11345. (when (and tags org-use-tag-inheritance
  11346. (or (not (eq t org-use-tag-inheritance))
  11347. org-tags-exclude-from-inheritance))
  11348. ;; selective inheritance, remove uninherited ones
  11349. (setcdr (car tags-alist)
  11350. (org-remove-uninherited-tags (cdar tags-alist))))
  11351. (when (and
  11352. ;; eval matcher only when the todo condition is OK
  11353. (and (or (not todo-only) (member todo org-not-done-keywords))
  11354. (let ((case-fold-search t)) (eval matcher)))
  11355. ;; Call the skipper, but return t if it does not skip,
  11356. ;; so that the `and' form continues evaluating
  11357. (progn
  11358. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11359. t)
  11360. ;; Check if timestamps are deselecting this entry
  11361. (or (not todo-only)
  11362. (and (member todo org-not-done-keywords)
  11363. (or (not org-agenda-tags-todo-honor-ignore-options)
  11364. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11365. ;; Extra check for the archive tag
  11366. ;; FIXME: Does the skipper already do this????
  11367. (or
  11368. (not (member org-archive-tag tags-list))
  11369. ;; we have an archive tag, should we use this anyway?
  11370. (or (not org-agenda-skip-archived-trees)
  11371. (and (eq action 'agenda) org-agenda-archives-mode))))
  11372. ;; select this headline
  11373. (cond
  11374. ((eq action 'sparse-tree)
  11375. (and org-highlight-sparse-tree-matches
  11376. (org-get-heading) (match-end 0)
  11377. (org-highlight-new-match
  11378. (match-beginning 0) (match-beginning 1)))
  11379. (org-show-context 'tags-tree))
  11380. ((eq action 'agenda)
  11381. (setq txt (org-format-agenda-item
  11382. ""
  11383. (concat
  11384. (if (eq org-tags-match-list-sublevels 'indented)
  11385. (make-string (1- level) ?.) "")
  11386. (org-get-heading))
  11387. category
  11388. tags-list
  11389. )
  11390. priority (org-get-priority txt))
  11391. (goto-char lspos)
  11392. (setq marker (org-agenda-new-marker))
  11393. (org-add-props txt props
  11394. 'org-marker marker 'org-hd-marker marker 'org-category category
  11395. 'todo-state todo
  11396. 'priority priority 'type "tagsmatch")
  11397. (push txt rtn))
  11398. ((functionp action)
  11399. (setq org-map-continue-from nil)
  11400. (save-excursion
  11401. (setq rtn1 (funcall action))
  11402. (push rtn1 rtn)))
  11403. (t (error "Invalid action")))
  11404. ;; if we are to skip sublevels, jump to end of subtree
  11405. (unless org-tags-match-list-sublevels
  11406. (org-end-of-subtree t)
  11407. (backward-char 1))))
  11408. ;; Get the correct position from where to continue
  11409. (if org-map-continue-from
  11410. (goto-char org-map-continue-from)
  11411. (and (= (point) lspos) (end-of-line 1)))))
  11412. (when (and (eq action 'sparse-tree)
  11413. (not org-sparse-tree-open-archived-trees))
  11414. (org-hide-archived-subtrees (point-min) (point-max)))
  11415. (nreverse rtn)))
  11416. (defun org-remove-uninherited-tags (tags)
  11417. "Remove all tags that are not inherited from the list TAGS."
  11418. (cond
  11419. ((eq org-use-tag-inheritance t)
  11420. (if org-tags-exclude-from-inheritance
  11421. (org-delete-all org-tags-exclude-from-inheritance tags)
  11422. tags))
  11423. ((not org-use-tag-inheritance) nil)
  11424. ((stringp org-use-tag-inheritance)
  11425. (delq nil (mapcar
  11426. (lambda (x)
  11427. (if (and (string-match org-use-tag-inheritance x)
  11428. (not (member x org-tags-exclude-from-inheritance)))
  11429. x nil))
  11430. tags)))
  11431. ((listp org-use-tag-inheritance)
  11432. (delq nil (mapcar
  11433. (lambda (x)
  11434. (if (member x org-use-tag-inheritance) x nil))
  11435. tags)))))
  11436. (defvar todo-only) ;; dynamically scoped
  11437. (defun org-match-sparse-tree (&optional todo-only match)
  11438. "Create a sparse tree according to tags string MATCH.
  11439. MATCH can contain positive and negative selection of tags, like
  11440. \"+WORK+URGENT-WITHBOSS\".
  11441. If optional argument TODO-ONLY is non-nil, only select lines that are
  11442. also TODO lines."
  11443. (interactive "P")
  11444. (org-prepare-agenda-buffers (list (current-buffer)))
  11445. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11446. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11447. (defvar org-cached-props nil)
  11448. (defun org-cached-entry-get (pom property)
  11449. (if (or (eq t org-use-property-inheritance)
  11450. (and (stringp org-use-property-inheritance)
  11451. (string-match org-use-property-inheritance property))
  11452. (and (listp org-use-property-inheritance)
  11453. (member property org-use-property-inheritance)))
  11454. ;; Caching is not possible, check it directly
  11455. (org-entry-get pom property 'inherit)
  11456. ;; Get all properties, so that we can do complicated checks easily
  11457. (cdr (assoc property (or org-cached-props
  11458. (setq org-cached-props
  11459. (org-entry-properties pom)))))))
  11460. (defun org-global-tags-completion-table (&optional files)
  11461. "Return the list of all tags in all agenda buffer/files.
  11462. Optional FILES argument is a list of files to which can be used
  11463. instead of the agenda files."
  11464. (save-excursion
  11465. (org-uniquify
  11466. (delq nil
  11467. (apply 'append
  11468. (mapcar
  11469. (lambda (file)
  11470. (set-buffer (find-file-noselect file))
  11471. (append (org-get-buffer-tags)
  11472. (mapcar (lambda (x) (if (stringp (car-safe x))
  11473. (list (car-safe x)) nil))
  11474. org-tag-alist)))
  11475. (if (and files (car files))
  11476. files
  11477. (org-agenda-files))))))))
  11478. (defun org-make-tags-matcher (match)
  11479. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11480. ;; todo-only is scoped dynamically into this function, and the function
  11481. ;; may change it if the matcher asks for it.
  11482. (unless match
  11483. ;; Get a new match request, with completion
  11484. (let ((org-last-tags-completion-table
  11485. (org-global-tags-completion-table)))
  11486. (setq match (org-completing-read-no-i
  11487. "Match: " 'org-tags-completion-function nil nil nil
  11488. 'org-tags-history))))
  11489. ;; Parse the string and create a lisp form
  11490. (let ((match0 match)
  11491. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11492. minus tag mm
  11493. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11494. orterms term orlist re-p str-p level-p level-op time-p
  11495. prop-p pn pv po gv rest)
  11496. (if (string-match "/+" match)
  11497. ;; match contains also a todo-matching request
  11498. (progn
  11499. (setq tagsmatch (substring match 0 (match-beginning 0))
  11500. todomatch (substring match (match-end 0)))
  11501. (if (string-match "^!" todomatch)
  11502. (setq todo-only t todomatch (substring todomatch 1)))
  11503. (if (string-match "^\\s-*$" todomatch)
  11504. (setq todomatch nil)))
  11505. ;; only matching tags
  11506. (setq tagsmatch match todomatch nil))
  11507. ;; Make the tags matcher
  11508. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11509. (setq tagsmatcher t)
  11510. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11511. (while (setq term (pop orterms))
  11512. (while (and (equal (substring term -1) "\\") orterms)
  11513. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11514. (while (string-match re term)
  11515. (setq rest (substring term (match-end 0))
  11516. minus (and (match-end 1)
  11517. (equal (match-string 1 term) "-"))
  11518. tag (save-match-data (replace-regexp-in-string
  11519. "\\\\-" "-"
  11520. (match-string 2 term)))
  11521. re-p (equal (string-to-char tag) ?{)
  11522. level-p (match-end 4)
  11523. prop-p (match-end 5)
  11524. mm (cond
  11525. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11526. (level-p
  11527. (setq level-op (org-op-to-function (match-string 3 term)))
  11528. `(,level-op level ,(string-to-number
  11529. (match-string 4 term))))
  11530. (prop-p
  11531. (setq pn (match-string 5 term)
  11532. po (match-string 6 term)
  11533. pv (match-string 7 term)
  11534. re-p (equal (string-to-char pv) ?{)
  11535. str-p (equal (string-to-char pv) ?\")
  11536. time-p (save-match-data
  11537. (string-match "^\"[[<].*[]>]\"$" pv))
  11538. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11539. (if time-p (setq pv (org-matcher-time pv)))
  11540. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11541. (cond
  11542. ((equal pn "CATEGORY")
  11543. (setq gv '(get-text-property (point) 'org-category)))
  11544. ((equal pn "TODO")
  11545. (setq gv 'todo))
  11546. (t
  11547. (setq gv `(org-cached-entry-get nil ,pn))))
  11548. (if re-p
  11549. (if (eq po 'org<>)
  11550. `(not (string-match ,pv (or ,gv "")))
  11551. `(string-match ,pv (or ,gv "")))
  11552. (if str-p
  11553. `(,po (or ,gv "") ,pv)
  11554. `(,po (string-to-number (or ,gv ""))
  11555. ,(string-to-number pv) ))))
  11556. (t `(member ,tag tags-list)))
  11557. mm (if minus (list 'not mm) mm)
  11558. term rest)
  11559. (push mm tagsmatcher))
  11560. (push (if (> (length tagsmatcher) 1)
  11561. (cons 'and tagsmatcher)
  11562. (car tagsmatcher))
  11563. orlist)
  11564. (setq tagsmatcher nil))
  11565. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11566. (setq tagsmatcher
  11567. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11568. ;; Make the todo matcher
  11569. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11570. (setq todomatcher t)
  11571. (setq orterms (org-split-string todomatch "|") orlist nil)
  11572. (while (setq term (pop orterms))
  11573. (while (string-match re term)
  11574. (setq minus (and (match-end 1)
  11575. (equal (match-string 1 term) "-"))
  11576. kwd (match-string 2 term)
  11577. re-p (equal (string-to-char kwd) ?{)
  11578. term (substring term (match-end 0))
  11579. mm (if re-p
  11580. `(string-match ,(substring kwd 1 -1) todo)
  11581. (list 'equal 'todo kwd))
  11582. mm (if minus (list 'not mm) mm))
  11583. (push mm todomatcher))
  11584. (push (if (> (length todomatcher) 1)
  11585. (cons 'and todomatcher)
  11586. (car todomatcher))
  11587. orlist)
  11588. (setq todomatcher nil))
  11589. (setq todomatcher (if (> (length orlist) 1)
  11590. (cons 'or orlist) (car orlist))))
  11591. ;; Return the string and lisp forms of the matcher
  11592. (setq matcher (if todomatcher
  11593. (list 'and tagsmatcher todomatcher)
  11594. tagsmatcher))
  11595. (cons match0 matcher)))
  11596. (defun org-op-to-function (op &optional stringp)
  11597. "Turn an operator into the appropriate function."
  11598. (setq op
  11599. (cond
  11600. ((equal op "<" ) '(< string< org-time<))
  11601. ((equal op ">" ) '(> org-string> org-time>))
  11602. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11603. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11604. ((member op '("=" "==")) '(= string= org-time=))
  11605. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11606. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11607. (defun org<> (a b) (not (= a b)))
  11608. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11609. (defun org-string>= (a b) (not (string< a b)))
  11610. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11611. (defun org-string<> (a b) (not (string= a b)))
  11612. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11613. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11614. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11615. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11616. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11617. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11618. (defun org-2ft (s)
  11619. "Convert S to a floating point time.
  11620. If S is already a number, just return it. If it is a string, parse
  11621. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11622. (cond
  11623. ((numberp s) s)
  11624. ((stringp s)
  11625. (condition-case nil
  11626. (float-time (apply 'encode-time (org-parse-time-string s)))
  11627. (error 0.)))
  11628. (t 0.)))
  11629. (defun org-time-today ()
  11630. "Time in seconds today at 0:00.
  11631. Returns the float number of seconds since the beginning of the
  11632. epoch to the beginning of today (00:00)."
  11633. (float-time (apply 'encode-time
  11634. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11635. (defun org-matcher-time (s)
  11636. "Interpret a time comparison value."
  11637. (save-match-data
  11638. (cond
  11639. ((string= s "<now>") (float-time))
  11640. ((string= s "<today>") (org-time-today))
  11641. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11642. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11643. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11644. (+ (org-time-today)
  11645. (* (string-to-number (match-string 1 s))
  11646. (cdr (assoc (match-string 2 s)
  11647. '(("d" . 86400.0) ("w" . 604800.0)
  11648. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11649. (t (org-2ft s)))))
  11650. (defun org-match-any-p (re list)
  11651. "Does re match any element of list?"
  11652. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11653. (delq nil list))
  11654. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11655. (defvar org-tags-overlay (make-overlay 1 1))
  11656. (org-detach-overlay org-tags-overlay)
  11657. (defun org-get-local-tags-at (&optional pos)
  11658. "Get a list of tags defined in the current headline."
  11659. (org-get-tags-at pos 'local))
  11660. (defun org-get-local-tags ()
  11661. "Get a list of tags defined in the current headline."
  11662. (org-get-tags-at nil 'local))
  11663. (defun org-get-tags-at (&optional pos local)
  11664. "Get a list of all headline tags applicable at POS.
  11665. POS defaults to point. If tags are inherited, the list contains
  11666. the targets in the same sequence as the headlines appear, i.e.
  11667. the tags of the current headline come last.
  11668. When LOCAL is non-nil, only return tags from the current headline,
  11669. ignore inherited ones."
  11670. (interactive)
  11671. (if (and org-trust-scanner-tags
  11672. (or (not pos) (equal pos (point)))
  11673. (not local))
  11674. org-scanner-tags
  11675. (let (tags ltags lastpos parent)
  11676. (save-excursion
  11677. (save-restriction
  11678. (widen)
  11679. (goto-char (or pos (point)))
  11680. (save-match-data
  11681. (catch 'done
  11682. (condition-case nil
  11683. (progn
  11684. (org-back-to-heading t)
  11685. (while (not (equal lastpos (point)))
  11686. (setq lastpos (point))
  11687. (when (looking-at
  11688. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11689. (setq ltags (org-split-string
  11690. (org-match-string-no-properties 1) ":"))
  11691. (when parent
  11692. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11693. (setq tags (append
  11694. (if parent
  11695. (org-remove-uninherited-tags ltags)
  11696. ltags)
  11697. tags)))
  11698. (or org-use-tag-inheritance (throw 'done t))
  11699. (if local (throw 'done t))
  11700. (or (org-up-heading-safe) (error nil))
  11701. (setq parent t)))
  11702. (error nil)))))
  11703. (if local
  11704. tags
  11705. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  11706. (defun org-add-prop-inherited (s)
  11707. (add-text-properties 0 (length s) '(inherited t) s)
  11708. s)
  11709. (defun org-toggle-tag (tag &optional onoff)
  11710. "Toggle the tag TAG for the current line.
  11711. If ONOFF is `on' or `off', don't toggle but set to this state."
  11712. (let (res current)
  11713. (save-excursion
  11714. (org-back-to-heading t)
  11715. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11716. (point-at-eol) t)
  11717. (progn
  11718. (setq current (match-string 1))
  11719. (replace-match ""))
  11720. (setq current ""))
  11721. (setq current (nreverse (org-split-string current ":")))
  11722. (cond
  11723. ((eq onoff 'on)
  11724. (setq res t)
  11725. (or (member tag current) (push tag current)))
  11726. ((eq onoff 'off)
  11727. (or (not (member tag current)) (setq current (delete tag current))))
  11728. (t (if (member tag current)
  11729. (setq current (delete tag current))
  11730. (setq res t)
  11731. (push tag current))))
  11732. (end-of-line 1)
  11733. (if current
  11734. (progn
  11735. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11736. (org-set-tags nil t))
  11737. (delete-horizontal-space))
  11738. (run-hooks 'org-after-tags-change-hook))
  11739. res))
  11740. (defun org-align-tags-here (to-col)
  11741. ;; Assumes that this is a headline
  11742. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11743. (beginning-of-line 1)
  11744. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11745. (< pos (match-beginning 2)))
  11746. (progn
  11747. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11748. (goto-char (match-beginning 1))
  11749. (insert " ")
  11750. (delete-region (point) (1+ (match-beginning 2)))
  11751. (setq ncol (max (1+ (current-column))
  11752. (1+ col)
  11753. (if (> to-col 0)
  11754. to-col
  11755. (- (abs to-col) tags-l))))
  11756. (setq p (point))
  11757. (insert (make-string (- ncol (current-column)) ?\ ))
  11758. (setq ncol (current-column))
  11759. (when indent-tabs-mode (tabify p (point-at-eol)))
  11760. (org-move-to-column (min ncol col) t))
  11761. (goto-char pos))))
  11762. (defun org-set-tags-command (&optional arg just-align)
  11763. "Call the set-tags command for the current entry."
  11764. (interactive "P")
  11765. (if (org-on-heading-p)
  11766. (org-set-tags arg just-align)
  11767. (save-excursion
  11768. (org-back-to-heading t)
  11769. (org-set-tags arg just-align))))
  11770. (defun org-set-tags-to (data)
  11771. "Set the tags of the current entry to DATA, replacing the current tags.
  11772. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11773. If DATA is nil or the empty string, any tags will be removed."
  11774. (interactive "sTags: ")
  11775. (setq data
  11776. (cond
  11777. ((eq data nil) "")
  11778. ((equal data "") "")
  11779. ((stringp data)
  11780. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11781. ":"))
  11782. ((listp data)
  11783. (concat ":" (mapconcat 'identity data ":") ":"))
  11784. (t nil)))
  11785. (when data
  11786. (save-excursion
  11787. (org-back-to-heading t)
  11788. (when (looking-at org-complex-heading-regexp)
  11789. (if (match-end 5)
  11790. (progn
  11791. (goto-char (match-beginning 5))
  11792. (insert data)
  11793. (delete-region (point) (point-at-eol))
  11794. (org-set-tags nil 'align))
  11795. (goto-char (point-at-eol))
  11796. (insert " " data)
  11797. (org-set-tags nil 'align)))
  11798. (beginning-of-line 1)
  11799. (if (looking-at ".*?\\([ \t]+\\)$")
  11800. (delete-region (match-beginning 1) (match-end 1))))))
  11801. (defun org-align-all-tags ()
  11802. "Align the tags i all headings."
  11803. (interactive)
  11804. (save-excursion
  11805. (or (ignore-errors (org-back-to-heading t))
  11806. (outline-next-heading))
  11807. (if (org-on-heading-p)
  11808. (org-set-tags t)
  11809. (message "No headings"))))
  11810. (defvar org-indent-indentation-per-level)
  11811. (defun org-set-tags (&optional arg just-align)
  11812. "Set the tags for the current headline.
  11813. With prefix ARG, realign all tags in headings in the current buffer."
  11814. (interactive "P")
  11815. (let* ((re (concat "^" outline-regexp))
  11816. (current (org-get-tags-string))
  11817. (col (current-column))
  11818. (org-setting-tags t)
  11819. table current-tags inherited-tags ; computed below when needed
  11820. tags p0 c0 c1 rpl di tc level)
  11821. (if arg
  11822. (save-excursion
  11823. (goto-char (point-min))
  11824. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11825. (while (re-search-forward re nil t)
  11826. (org-set-tags nil t)
  11827. (end-of-line 1)))
  11828. (message "All tags realigned to column %d" org-tags-column))
  11829. (if just-align
  11830. (setq tags current)
  11831. ;; Get a new set of tags from the user
  11832. (save-excursion
  11833. (setq table (append org-tag-persistent-alist
  11834. (or org-tag-alist (org-get-buffer-tags))
  11835. (and
  11836. org-complete-tags-always-offer-all-agenda-tags
  11837. (org-global-tags-completion-table
  11838. (org-agenda-files))))
  11839. org-last-tags-completion-table table
  11840. current-tags (org-split-string current ":")
  11841. inherited-tags (nreverse
  11842. (nthcdr (length current-tags)
  11843. (nreverse (org-get-tags-at))))
  11844. tags
  11845. (if (or (eq t org-use-fast-tag-selection)
  11846. (and org-use-fast-tag-selection
  11847. (delq nil (mapcar 'cdr table))))
  11848. (org-fast-tag-selection
  11849. current-tags inherited-tags table
  11850. (if org-fast-tag-selection-include-todo
  11851. org-todo-key-alist))
  11852. (let ((org-add-colon-after-tag-completion t))
  11853. (org-trim
  11854. (org-without-partial-completion
  11855. (org-icompleting-read "Tags: "
  11856. 'org-tags-completion-function
  11857. nil nil current 'org-tags-history)))))))
  11858. (while (string-match "[-+&]+" tags)
  11859. ;; No boolean logic, just a list
  11860. (setq tags (replace-match ":" t t tags))))
  11861. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  11862. (if org-tags-sort-function
  11863. (setq tags (mapconcat 'identity
  11864. (sort (org-split-string
  11865. tags (org-re "[^[:alnum:]_@#%]+"))
  11866. org-tags-sort-function) ":")))
  11867. (if (string-match "\\`[\t ]*\\'" tags)
  11868. (setq tags "")
  11869. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11870. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11871. ;; Insert new tags at the correct column
  11872. (beginning-of-line 1)
  11873. (setq level (or (and (looking-at org-outline-regexp)
  11874. (- (match-end 0) (point) 1))
  11875. 1))
  11876. (cond
  11877. ((and (equal current "") (equal tags "")))
  11878. ((re-search-forward
  11879. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11880. (point-at-eol) t)
  11881. (if (equal tags "")
  11882. (setq rpl "")
  11883. (goto-char (match-beginning 0))
  11884. (setq c0 (current-column)
  11885. ;; compute offset for the case of org-indent-mode active
  11886. di (if org-indent-mode
  11887. (* (1- org-indent-indentation-per-level) (1- level))
  11888. 0)
  11889. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11890. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11891. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11892. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11893. (replace-match rpl t t)
  11894. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11895. tags)
  11896. (t (error "Tags alignment failed")))
  11897. (org-move-to-column col)
  11898. (unless just-align
  11899. (run-hooks 'org-after-tags-change-hook)))))
  11900. (defun org-change-tag-in-region (beg end tag off)
  11901. "Add or remove TAG for each entry in the region.
  11902. This works in the agenda, and also in an org-mode buffer."
  11903. (interactive
  11904. (list (region-beginning) (region-end)
  11905. (let ((org-last-tags-completion-table
  11906. (if (org-mode-p)
  11907. (org-get-buffer-tags)
  11908. (org-global-tags-completion-table))))
  11909. (org-icompleting-read
  11910. "Tag: " 'org-tags-completion-function nil nil nil
  11911. 'org-tags-history))
  11912. (progn
  11913. (message "[s]et or [r]emove? ")
  11914. (equal (read-char-exclusive) ?r))))
  11915. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11916. (let ((agendap (equal major-mode 'org-agenda-mode))
  11917. l1 l2 m buf pos newhead (cnt 0))
  11918. (goto-char end)
  11919. (setq l2 (1- (org-current-line)))
  11920. (goto-char beg)
  11921. (setq l1 (org-current-line))
  11922. (loop for l from l1 to l2 do
  11923. (org-goto-line l)
  11924. (setq m (get-text-property (point) 'org-hd-marker))
  11925. (when (or (and (org-mode-p) (org-on-heading-p))
  11926. (and agendap m))
  11927. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11928. pos (if agendap m (point)))
  11929. (with-current-buffer buf
  11930. (save-excursion
  11931. (save-restriction
  11932. (goto-char pos)
  11933. (setq cnt (1+ cnt))
  11934. (org-toggle-tag tag (if off 'off 'on))
  11935. (setq newhead (org-get-heading)))))
  11936. (and agendap (org-agenda-change-all-lines newhead m))))
  11937. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11938. (defun org-tags-completion-function (string predicate &optional flag)
  11939. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11940. (confirm (lambda (x) (stringp (car x)))))
  11941. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11942. (setq s1 (match-string 1 string)
  11943. s2 (match-string 2 string))
  11944. (setq s1 "" s2 string))
  11945. (cond
  11946. ((eq flag nil)
  11947. ;; try completion
  11948. (setq rtn (try-completion s2 ctable confirm))
  11949. (if (stringp rtn)
  11950. (setq rtn
  11951. (concat s1 s2 (substring rtn (length s2))
  11952. (if (and org-add-colon-after-tag-completion
  11953. (assoc rtn ctable))
  11954. ":" ""))))
  11955. rtn)
  11956. ((eq flag t)
  11957. ;; all-completions
  11958. (all-completions s2 ctable confirm)
  11959. )
  11960. ((eq flag 'lambda)
  11961. ;; exact match?
  11962. (assoc s2 ctable)))
  11963. ))
  11964. (defun org-fast-tag-insert (kwd tags face &optional end)
  11965. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11966. (insert (format "%-12s" (concat kwd ":"))
  11967. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11968. (or end "")))
  11969. (defun org-fast-tag-show-exit (flag)
  11970. (save-excursion
  11971. (org-goto-line 3)
  11972. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11973. (replace-match ""))
  11974. (when flag
  11975. (end-of-line 1)
  11976. (org-move-to-column (- (window-width) 19) t)
  11977. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11978. (defun org-set-current-tags-overlay (current prefix)
  11979. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11980. (if (featurep 'xemacs)
  11981. (org-overlay-display org-tags-overlay (concat prefix s)
  11982. 'secondary-selection)
  11983. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11984. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11985. (defvar org-last-tag-selection-key nil)
  11986. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11987. "Fast tag selection with single keys.
  11988. CURRENT is the current list of tags in the headline, INHERITED is the
  11989. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11990. possibly with grouping information. TODO-TABLE is a similar table with
  11991. TODO keywords, should these have keys assigned to them.
  11992. If the keys are nil, a-z are automatically assigned.
  11993. Returns the new tags string, or nil to not change the current settings."
  11994. (let* ((fulltable (append table todo-table))
  11995. (maxlen (apply 'max (mapcar
  11996. (lambda (x)
  11997. (if (stringp (car x)) (string-width (car x)) 0))
  11998. fulltable)))
  11999. (buf (current-buffer))
  12000. (expert (eq org-fast-tag-selection-single-key 'expert))
  12001. (buffer-tags nil)
  12002. (fwidth (+ maxlen 3 1 3))
  12003. (ncol (/ (- (window-width) 4) fwidth))
  12004. (i-face 'org-done)
  12005. (c-face 'org-todo)
  12006. tg cnt e c char c1 c2 ntable tbl rtn
  12007. ov-start ov-end ov-prefix
  12008. (exit-after-next org-fast-tag-selection-single-key)
  12009. (done-keywords org-done-keywords)
  12010. groups ingroup)
  12011. (save-excursion
  12012. (beginning-of-line 1)
  12013. (if (looking-at
  12014. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12015. (setq ov-start (match-beginning 1)
  12016. ov-end (match-end 1)
  12017. ov-prefix "")
  12018. (setq ov-start (1- (point-at-eol))
  12019. ov-end (1+ ov-start))
  12020. (skip-chars-forward "^\n\r")
  12021. (setq ov-prefix
  12022. (concat
  12023. (buffer-substring (1- (point)) (point))
  12024. (if (> (current-column) org-tags-column)
  12025. " "
  12026. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12027. (move-overlay org-tags-overlay ov-start ov-end)
  12028. (save-window-excursion
  12029. (if expert
  12030. (set-buffer (get-buffer-create " *Org tags*"))
  12031. (delete-other-windows)
  12032. (split-window-vertically)
  12033. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12034. (erase-buffer)
  12035. (org-set-local 'org-done-keywords done-keywords)
  12036. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12037. (org-fast-tag-insert "Current" current c-face "\n\n")
  12038. (org-fast-tag-show-exit exit-after-next)
  12039. (org-set-current-tags-overlay current ov-prefix)
  12040. (setq tbl fulltable char ?a cnt 0)
  12041. (while (setq e (pop tbl))
  12042. (cond
  12043. ((equal (car e) :startgroup)
  12044. (push '() groups) (setq ingroup t)
  12045. (when (not (= cnt 0))
  12046. (setq cnt 0)
  12047. (insert "\n"))
  12048. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12049. ((equal (car e) :endgroup)
  12050. (setq ingroup nil cnt 0)
  12051. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12052. ((equal e '(:newline))
  12053. (when (not (= cnt 0))
  12054. (setq cnt 0)
  12055. (insert "\n")
  12056. (setq e (car tbl))
  12057. (while (equal (car tbl) '(:newline))
  12058. (insert "\n")
  12059. (setq tbl (cdr tbl)))))
  12060. (t
  12061. (setq tg (copy-sequence (car e)) c2 nil)
  12062. (if (cdr e)
  12063. (setq c (cdr e))
  12064. ;; automatically assign a character.
  12065. (setq c1 (string-to-char
  12066. (downcase (substring
  12067. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12068. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12069. (while (or (rassoc char ntable) (rassoc char table))
  12070. (setq char (1+ char)))
  12071. (setq c2 c1))
  12072. (setq c (or c2 char)))
  12073. (if ingroup (push tg (car groups)))
  12074. (setq tg (org-add-props tg nil 'face
  12075. (cond
  12076. ((not (assoc tg table))
  12077. (org-get-todo-face tg))
  12078. ((member tg current) c-face)
  12079. ((member tg inherited) i-face)
  12080. (t nil))))
  12081. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12082. (insert "[" c "] " tg (make-string
  12083. (- fwidth 4 (length tg)) ?\ ))
  12084. (push (cons tg c) ntable)
  12085. (when (= (setq cnt (1+ cnt)) ncol)
  12086. (insert "\n")
  12087. (if ingroup (insert " "))
  12088. (setq cnt 0)))))
  12089. (setq ntable (nreverse ntable))
  12090. (insert "\n")
  12091. (goto-char (point-min))
  12092. (if (not expert) (org-fit-window-to-buffer))
  12093. (setq rtn
  12094. (catch 'exit
  12095. (while t
  12096. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12097. (if (not groups) "no " "")
  12098. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12099. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12100. (setq org-last-tag-selection-key c)
  12101. (cond
  12102. ((= c ?\r) (throw 'exit t))
  12103. ((= c ?!)
  12104. (setq groups (not groups))
  12105. (goto-char (point-min))
  12106. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12107. ((= c ?\C-c)
  12108. (if (not expert)
  12109. (org-fast-tag-show-exit
  12110. (setq exit-after-next (not exit-after-next)))
  12111. (setq expert nil)
  12112. (delete-other-windows)
  12113. (set-window-buffer (split-window-vertically) " *Org tags*")
  12114. (org-switch-to-buffer-other-window " *Org tags*")
  12115. (org-fit-window-to-buffer)))
  12116. ((or (= c ?\C-g)
  12117. (and (= c ?q) (not (rassoc c ntable))))
  12118. (org-detach-overlay org-tags-overlay)
  12119. (setq quit-flag t))
  12120. ((= c ?\ )
  12121. (setq current nil)
  12122. (if exit-after-next (setq exit-after-next 'now)))
  12123. ((= c ?\t)
  12124. (condition-case nil
  12125. (setq tg (org-icompleting-read
  12126. "Tag: "
  12127. (or buffer-tags
  12128. (with-current-buffer buf
  12129. (org-get-buffer-tags)))))
  12130. (quit (setq tg "")))
  12131. (when (string-match "\\S-" tg)
  12132. (add-to-list 'buffer-tags (list tg))
  12133. (if (member tg current)
  12134. (setq current (delete tg current))
  12135. (push tg current)))
  12136. (if exit-after-next (setq exit-after-next 'now)))
  12137. ((setq e (rassoc c todo-table) tg (car e))
  12138. (with-current-buffer buf
  12139. (save-excursion (org-todo tg)))
  12140. (if exit-after-next (setq exit-after-next 'now)))
  12141. ((setq e (rassoc c ntable) tg (car e))
  12142. (if (member tg current)
  12143. (setq current (delete tg current))
  12144. (loop for g in groups do
  12145. (if (member tg g)
  12146. (mapc (lambda (x)
  12147. (setq current (delete x current)))
  12148. g)))
  12149. (push tg current))
  12150. (if exit-after-next (setq exit-after-next 'now))))
  12151. ;; Create a sorted list
  12152. (setq current
  12153. (sort current
  12154. (lambda (a b)
  12155. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12156. (if (eq exit-after-next 'now) (throw 'exit t))
  12157. (goto-char (point-min))
  12158. (beginning-of-line 2)
  12159. (delete-region (point) (point-at-eol))
  12160. (org-fast-tag-insert "Current" current c-face)
  12161. (org-set-current-tags-overlay current ov-prefix)
  12162. (while (re-search-forward
  12163. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12164. (setq tg (match-string 1))
  12165. (add-text-properties
  12166. (match-beginning 1) (match-end 1)
  12167. (list 'face
  12168. (cond
  12169. ((member tg current) c-face)
  12170. ((member tg inherited) i-face)
  12171. (t (get-text-property (match-beginning 1) 'face))))))
  12172. (goto-char (point-min)))))
  12173. (org-detach-overlay org-tags-overlay)
  12174. (if rtn
  12175. (mapconcat 'identity current ":")
  12176. nil))))
  12177. (defun org-get-tags-string ()
  12178. "Get the TAGS string in the current headline."
  12179. (unless (org-on-heading-p t)
  12180. (error "Not on a heading"))
  12181. (save-excursion
  12182. (beginning-of-line 1)
  12183. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12184. (org-match-string-no-properties 1)
  12185. "")))
  12186. (defun org-get-tags ()
  12187. "Get the list of tags specified in the current headline."
  12188. (org-split-string (org-get-tags-string) ":"))
  12189. (defun org-get-buffer-tags ()
  12190. "Get a table of all tags used in the buffer, for completion."
  12191. (let (tags)
  12192. (save-excursion
  12193. (goto-char (point-min))
  12194. (while (re-search-forward
  12195. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12196. (when (equal (char-after (point-at-bol 0)) ?*)
  12197. (mapc (lambda (x) (add-to-list 'tags x))
  12198. (org-split-string (org-match-string-no-properties 1) ":")))))
  12199. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12200. (mapcar 'list tags)))
  12201. ;;;; The mapping API
  12202. ;;;###autoload
  12203. (defun org-map-entries (func &optional match scope &rest skip)
  12204. "Call FUNC at each headline selected by MATCH in SCOPE.
  12205. FUNC is a function or a lisp form. The function will be called without
  12206. arguments, with the cursor positioned at the beginning of the headline.
  12207. The return values of all calls to the function will be collected and
  12208. returned as a list.
  12209. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12210. does not need to preserve point. After evaluation, the cursor will be
  12211. moved to the end of the line (presumably of the headline of the
  12212. processed entry) and search continues from there. Under some
  12213. circumstances, this may not produce the wanted results. For example,
  12214. if you have removed (e.g. archived) the current (sub)tree it could
  12215. mean that the next entry will be skipped entirely. In such cases, you
  12216. can specify the position from where search should continue by making
  12217. FUNC set the variable `org-map-continue-from' to the desired buffer
  12218. position.
  12219. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12220. Only headlines that are matched by this query will be considered during
  12221. the iteration. When MATCH is nil or t, all headlines will be
  12222. visited by the iteration.
  12223. SCOPE determines the scope of this command. It can be any of:
  12224. nil The current buffer, respecting the restriction if any
  12225. tree The subtree started with the entry at point
  12226. file The current buffer, without restriction
  12227. file-with-archives
  12228. The current buffer, and any archives associated with it
  12229. agenda All agenda files
  12230. agenda-with-archives
  12231. All agenda files with any archive files associated with them
  12232. \(file1 file2 ...)
  12233. If this is a list, all files in the list will be scanned
  12234. The remaining args are treated as settings for the skipping facilities of
  12235. the scanner. The following items can be given here:
  12236. archive skip trees with the archive tag.
  12237. comment skip trees with the COMMENT keyword
  12238. function or Emacs Lisp form:
  12239. will be used as value for `org-agenda-skip-function', so whenever
  12240. the function returns t, FUNC will not be called for that
  12241. entry and search will continue from the point where the
  12242. function leaves it.
  12243. If your function needs to retrieve the tags including inherited tags
  12244. at the *current* entry, you can use the value of the variable
  12245. `org-scanner-tags' which will be much faster than getting the value
  12246. with `org-get-tags-at'. If your function gets properties with
  12247. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12248. to t around the call to `org-entry-properties' to get the same speedup.
  12249. Note that if your function moves around to retrieve tags and properties at
  12250. a *different* entry, you cannot use these techniques."
  12251. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12252. (org-agenda-skip-archived-trees (memq 'archive skip))
  12253. (org-agenda-skip-comment-trees (memq 'comment skip))
  12254. (org-agenda-skip-function
  12255. (car (org-delete-all '(comment archive) skip)))
  12256. (org-tags-match-list-sublevels t)
  12257. matcher file res
  12258. org-todo-keywords-for-agenda
  12259. org-done-keywords-for-agenda
  12260. org-todo-keyword-alist-for-agenda
  12261. org-drawers-for-agenda
  12262. org-tag-alist-for-agenda)
  12263. (cond
  12264. ((eq match t) (setq matcher t))
  12265. ((eq match nil) (setq matcher t))
  12266. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12267. (save-excursion
  12268. (save-restriction
  12269. (when (eq scope 'tree)
  12270. (org-back-to-heading t)
  12271. (org-narrow-to-subtree)
  12272. (setq scope nil))
  12273. (if (not scope)
  12274. (progn
  12275. (org-prepare-agenda-buffers
  12276. (list (buffer-file-name (current-buffer))))
  12277. (setq res (org-scan-tags func matcher)))
  12278. ;; Get the right scope
  12279. (cond
  12280. ((and scope (listp scope) (symbolp (car scope)))
  12281. (setq scope (eval scope)))
  12282. ((eq scope 'agenda)
  12283. (setq scope (org-agenda-files t)))
  12284. ((eq scope 'agenda-with-archives)
  12285. (setq scope (org-agenda-files t))
  12286. (setq scope (org-add-archive-files scope)))
  12287. ((eq scope 'file)
  12288. (setq scope (list (buffer-file-name))))
  12289. ((eq scope 'file-with-archives)
  12290. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12291. (org-prepare-agenda-buffers scope)
  12292. (while (setq file (pop scope))
  12293. (with-current-buffer (org-find-base-buffer-visiting file)
  12294. (save-excursion
  12295. (save-restriction
  12296. (widen)
  12297. (goto-char (point-min))
  12298. (setq res (append res (org-scan-tags func matcher))))))))))
  12299. res))
  12300. ;;;; Properties
  12301. ;;; Setting and retrieving properties
  12302. (defconst org-special-properties
  12303. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12304. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12305. "The special properties valid in Org-mode.
  12306. These are properties that are not defined in the property drawer,
  12307. but in some other way.")
  12308. (defconst org-default-properties
  12309. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12310. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12311. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12312. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12313. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12314. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12315. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12316. "Some properties that are used by Org-mode for various purposes.
  12317. Being in this list makes sure that they are offered for completion.")
  12318. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12319. "Regular expression matching the first line of a property drawer.")
  12320. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12321. "Regular expression matching the last line of a property drawer.")
  12322. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12323. "Regular expression matching the first line of a property drawer.")
  12324. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12325. "Regular expression matching the first line of a property drawer.")
  12326. (defconst org-property-drawer-re
  12327. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12328. org-property-end-re "\\)\n?")
  12329. "Matches an entire property drawer.")
  12330. (defconst org-clock-drawer-re
  12331. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12332. org-property-end-re "\\)\n?")
  12333. "Matches an entire clock drawer.")
  12334. (defsubst org-re-property (property)
  12335. "Return a regexp matching PROPERTY.
  12336. Match group 1 will be set to the value "
  12337. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12338. (defun org-property-action ()
  12339. "Do an action on properties."
  12340. (interactive)
  12341. (let (c)
  12342. (org-at-property-p)
  12343. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12344. (setq c (read-char-exclusive))
  12345. (cond
  12346. ((equal c ?s)
  12347. (call-interactively 'org-set-property))
  12348. ((equal c ?d)
  12349. (call-interactively 'org-delete-property))
  12350. ((equal c ?D)
  12351. (call-interactively 'org-delete-property-globally))
  12352. ((equal c ?c)
  12353. (call-interactively 'org-compute-property-at-point))
  12354. (t (error "No such property action %c" c)))))
  12355. (defun org-set-effort (&optional value)
  12356. "Set the effort property of the current entry.
  12357. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12358. allowed value."
  12359. (interactive "P")
  12360. (if (equal value 0) (setq value 10))
  12361. (let* ((completion-ignore-case t)
  12362. (prop org-effort-property)
  12363. (cur (org-entry-get nil prop))
  12364. (allowed (org-property-get-allowed-values nil prop 'table))
  12365. (existing (mapcar 'list (org-property-values prop)))
  12366. rpl
  12367. (val (cond
  12368. ((stringp value) value)
  12369. ((and allowed (integerp value))
  12370. (or (car (nth (1- value) allowed))
  12371. (car (org-last allowed))))
  12372. (allowed
  12373. (message "Select 1-9,0, [RET%s]: %s"
  12374. (if cur (concat "=" cur) "")
  12375. (mapconcat 'car allowed " "))
  12376. (setq rpl (read-char-exclusive))
  12377. (if (equal rpl ?\r)
  12378. cur
  12379. (setq rpl (- rpl ?0))
  12380. (if (equal rpl 0) (setq rpl 10))
  12381. (if (and (> rpl 0) (<= rpl (length allowed)))
  12382. (car (nth (1- rpl) allowed))
  12383. (org-completing-read "Effort: " allowed nil))))
  12384. (t
  12385. (let (org-completion-use-ido org-completion-use-iswitchb)
  12386. (org-completing-read
  12387. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12388. (concat "[" cur "]") "")
  12389. ": ")
  12390. existing nil nil "" nil cur))))))
  12391. (unless (equal (org-entry-get nil prop) val)
  12392. (org-entry-put nil prop val))
  12393. (message "%s is now %s" prop val)))
  12394. (defun org-at-property-p ()
  12395. "Is cursor inside a property drawer?"
  12396. (save-excursion
  12397. (beginning-of-line 1)
  12398. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12399. (save-match-data ;; Used by calling procedures
  12400. (let ((p (point))
  12401. (range (unless (org-before-first-heading-p)
  12402. (org-get-property-block))))
  12403. (and range (<= (car range) p) (< p (cdr range))))))))
  12404. (defun org-get-property-block (&optional beg end force)
  12405. "Return the (beg . end) range of the body of the property drawer.
  12406. BEG and END can be beginning and end of subtree, if not given
  12407. they will be found.
  12408. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12409. (catch 'exit
  12410. (save-excursion
  12411. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12412. (end (or end (progn (outline-next-heading) (point)))))
  12413. (goto-char beg)
  12414. (if (re-search-forward org-property-start-re end t)
  12415. (setq beg (1+ (match-end 0)))
  12416. (if force
  12417. (save-excursion
  12418. (org-insert-property-drawer)
  12419. (setq end (progn (outline-next-heading) (point))))
  12420. (throw 'exit nil))
  12421. (goto-char beg)
  12422. (if (re-search-forward org-property-start-re end t)
  12423. (setq beg (1+ (match-end 0)))))
  12424. (if (re-search-forward org-property-end-re end t)
  12425. (setq end (match-beginning 0))
  12426. (or force (throw 'exit nil))
  12427. (goto-char beg)
  12428. (setq end beg)
  12429. (org-indent-line-function)
  12430. (insert ":END:\n"))
  12431. (cons beg end)))))
  12432. (defun org-entry-properties (&optional pom which specific)
  12433. "Get all properties of the entry at point-or-marker POM.
  12434. This includes the TODO keyword, the tags, time strings for deadline,
  12435. scheduled, and clocking, and any additional properties defined in the
  12436. entry. The return value is an alist, keys may occur multiple times
  12437. if the property key was used several times.
  12438. POM may also be nil, in which case the current entry is used.
  12439. If WHICH is nil or `all', get all properties. If WHICH is
  12440. `special' or `standard', only get that subclass. If WHICH
  12441. is a string only get exactly this property. SPECIFIC can be a string, the
  12442. specific property we are interested in. Specifying it can speed
  12443. things up because then unnecessary parsing is avoided."
  12444. (setq which (or which 'all))
  12445. (org-with-point-at pom
  12446. (let ((clockstr (substring org-clock-string 0 -1))
  12447. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12448. (case-fold-search nil)
  12449. beg end range props sum-props key key1 value string clocksum)
  12450. (save-excursion
  12451. (when (condition-case nil
  12452. (and (org-mode-p) (org-back-to-heading t))
  12453. (error nil))
  12454. (setq beg (point))
  12455. (setq sum-props (get-text-property (point) 'org-summaries))
  12456. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12457. (outline-next-heading)
  12458. (setq end (point))
  12459. (when (memq which '(all special))
  12460. ;; Get the special properties, like TODO and tags
  12461. (goto-char beg)
  12462. (when (and (or (not specific) (string= specific "TODO"))
  12463. (looking-at org-todo-line-regexp) (match-end 2))
  12464. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12465. (when (and (or (not specific) (string= specific "PRIORITY"))
  12466. (looking-at org-priority-regexp))
  12467. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12468. (when (or (not specific) (string= specific "FILE"))
  12469. (push (cons "FILE" buffer-file-name) props))
  12470. (when (and (or (not specific) (string= specific "TAGS"))
  12471. (setq value (org-get-tags-string))
  12472. (string-match "\\S-" value))
  12473. (push (cons "TAGS" value) props))
  12474. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12475. (setq value (org-get-tags-at)))
  12476. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12477. ":"))
  12478. props))
  12479. (when (or (not specific) (string= specific "BLOCKED"))
  12480. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12481. (when (or (not specific)
  12482. (member specific
  12483. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12484. "TIMESTAMP" "TIMESTAMP_IA")))
  12485. (catch 'match
  12486. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12487. (setq key (if (match-end 1)
  12488. (substring (org-match-string-no-properties 1)
  12489. 0 -1))
  12490. string (if (equal key clockstr)
  12491. (org-no-properties
  12492. (org-trim
  12493. (buffer-substring
  12494. (match-beginning 3) (goto-char
  12495. (point-at-eol)))))
  12496. (substring (org-match-string-no-properties 3)
  12497. 1 -1)))
  12498. ;; Get the correct property name from the key. This is
  12499. ;; necessary if the user has configured time keywords.
  12500. (setq key1 (concat key ":"))
  12501. (cond
  12502. ((not key)
  12503. (setq key
  12504. (if (= (char-after (match-beginning 3)) ?\[)
  12505. "TIMESTAMP_IA" "TIMESTAMP")))
  12506. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12507. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12508. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12509. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12510. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12511. (progn
  12512. (push (cons key string) props)
  12513. ;; no need to search further if match is found
  12514. (throw 'match t))
  12515. (when (or (equal key "CLOCK") (not (assoc key props)))
  12516. (push (cons key string) props))))))
  12517. )
  12518. (when (memq which '(all standard))
  12519. ;; Get the standard properties, like :PROP: ...
  12520. (setq range (org-get-property-block beg end))
  12521. (when range
  12522. (goto-char (car range))
  12523. (while (re-search-forward
  12524. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12525. (cdr range) t)
  12526. (setq key (org-match-string-no-properties 1)
  12527. value (org-trim (or (org-match-string-no-properties 2) "")))
  12528. (unless (member key excluded)
  12529. (push (cons key (or value "")) props)))))
  12530. (if clocksum
  12531. (push (cons "CLOCKSUM"
  12532. (org-columns-number-to-string (/ (float clocksum) 60.)
  12533. 'add_times))
  12534. props))
  12535. (unless (assoc "CATEGORY" props)
  12536. (push (cons "CATEGORY" (org-get-category)) props))
  12537. (append sum-props (nreverse props)))))))
  12538. (defun org-entry-get (pom property &optional inherit literal-nil)
  12539. "Get value of PROPERTY for entry at point-or-marker POM.
  12540. If INHERIT is non-nil and the entry does not have the property,
  12541. then also check higher levels of the hierarchy.
  12542. If INHERIT is the symbol `selective', use inheritance only if the setting
  12543. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12544. If the property is present but empty, the return value is the empty string.
  12545. If the property is not present at all, nil is returned.
  12546. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12547. do not interpret it as the list atom nil. This is used for inheritance
  12548. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12549. (org-with-point-at pom
  12550. (if (and inherit (if (eq inherit 'selective)
  12551. (org-property-inherit-p property)
  12552. t))
  12553. (org-entry-get-with-inheritance property literal-nil)
  12554. (if (member property org-special-properties)
  12555. ;; We need a special property. Use `org-entry-properties' to
  12556. ;; retrieve it, but specify the wanted property
  12557. (cdr (assoc property (org-entry-properties nil 'special property)))
  12558. (let ((range (unless (org-before-first-heading-p)
  12559. (org-get-property-block))))
  12560. (if (and range
  12561. (goto-char (car range))
  12562. (re-search-forward
  12563. (org-re-property property)
  12564. (cdr range) t))
  12565. ;; Found the property, return it.
  12566. (if (match-end 1)
  12567. (if literal-nil
  12568. (org-match-string-no-properties 1)
  12569. (org-not-nil (org-match-string-no-properties 1)))
  12570. "")))))))
  12571. (defun org-property-or-variable-value (var &optional inherit)
  12572. "Check if there is a property fixing the value of VAR.
  12573. If yes, return this value. If not, return the current value of the variable."
  12574. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12575. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12576. (read prop)
  12577. (symbol-value var))))
  12578. (defun org-entry-delete (pom property)
  12579. "Delete the property PROPERTY from entry at point-or-marker POM."
  12580. (org-with-point-at pom
  12581. (if (member property org-special-properties)
  12582. nil ; cannot delete these properties.
  12583. (let ((range (org-get-property-block)))
  12584. (if (and range
  12585. (goto-char (car range))
  12586. (re-search-forward
  12587. (org-re-property property)
  12588. (cdr range) t))
  12589. (progn
  12590. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12591. t)
  12592. nil)))))
  12593. ;; Multi-values properties are properties that contain multiple values
  12594. ;; These values are assumed to be single words, separated by whitespace.
  12595. (defun org-entry-add-to-multivalued-property (pom property value)
  12596. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12597. (let* ((old (org-entry-get pom property))
  12598. (values (and old (org-split-string old "[ \t]"))))
  12599. (setq value (org-entry-protect-space value))
  12600. (unless (member value values)
  12601. (setq values (cons value values))
  12602. (org-entry-put pom property
  12603. (mapconcat 'identity values " ")))))
  12604. (defun org-entry-remove-from-multivalued-property (pom property value)
  12605. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12606. (let* ((old (org-entry-get pom property))
  12607. (values (and old (org-split-string old "[ \t]"))))
  12608. (setq value (org-entry-protect-space value))
  12609. (when (member value values)
  12610. (setq values (delete value values))
  12611. (org-entry-put pom property
  12612. (mapconcat 'identity values " ")))))
  12613. (defun org-entry-member-in-multivalued-property (pom property value)
  12614. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12615. (let* ((old (org-entry-get pom property))
  12616. (values (and old (org-split-string old "[ \t]"))))
  12617. (setq value (org-entry-protect-space value))
  12618. (member value values)))
  12619. (defun org-entry-get-multivalued-property (pom property)
  12620. "Return a list of values in a multivalued property."
  12621. (let* ((value (org-entry-get pom property))
  12622. (values (and value (org-split-string value "[ \t]"))))
  12623. (mapcar 'org-entry-restore-space values)))
  12624. (defun org-entry-put-multivalued-property (pom property &rest values)
  12625. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12626. VALUES should be a list of strings. Spaces will be protected."
  12627. (org-entry-put pom property
  12628. (mapconcat 'org-entry-protect-space values " "))
  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-protect-space (s)
  12633. "Protect spaces and newline in string S."
  12634. (while (string-match " " s)
  12635. (setq s (replace-match "%20" t t s)))
  12636. (while (string-match "\n" s)
  12637. (setq s (replace-match "%0A" t t s)))
  12638. s)
  12639. (defun org-entry-restore-space (s)
  12640. "Restore spaces and newline in string S."
  12641. (while (string-match "%20" s)
  12642. (setq s (replace-match " " t t s)))
  12643. (while (string-match "%0A" s)
  12644. (setq s (replace-match "\n" t t s)))
  12645. s)
  12646. (defvar org-entry-property-inherited-from (make-marker)
  12647. "Marker pointing to the entry from where a property was inherited.
  12648. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12649. location of the entry where the inheritance search matched. If there was
  12650. no match, the marker will point nowhere.
  12651. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12652. is set.")
  12653. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12654. "Get entry property, and search higher levels if not present.
  12655. The search will stop at the first ancestor which has the property defined.
  12656. If the value found is \"nil\", return nil to show that the property
  12657. should be considered as undefined (this is the meaning of nil here).
  12658. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12659. (move-marker org-entry-property-inherited-from nil)
  12660. (let (tmp)
  12661. (unless (org-before-first-heading-p)
  12662. (save-excursion
  12663. (save-restriction
  12664. (widen)
  12665. (catch 'ex
  12666. (while t
  12667. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12668. (org-back-to-heading t)
  12669. (move-marker org-entry-property-inherited-from (point))
  12670. (throw 'ex tmp))
  12671. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12672. (setq tmp (or tmp
  12673. (cdr (assoc property org-file-properties))
  12674. (cdr (assoc property org-global-properties))
  12675. (cdr (assoc property org-global-properties-fixed))))
  12676. (if literal-nil tmp (org-not-nil tmp))))
  12677. (defvar org-property-changed-functions nil
  12678. "Hook called when the value of a property has changed.
  12679. Each hook function should accept two arguments, the name of the property
  12680. and the new value.")
  12681. (defun org-entry-put (pom property value)
  12682. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12683. (org-with-point-at pom
  12684. (org-back-to-heading t)
  12685. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12686. range)
  12687. (cond
  12688. ((equal property "TODO")
  12689. (when (and (stringp value) (string-match "\\S-" value)
  12690. (not (member value org-todo-keywords-1)))
  12691. (error "\"%s\" is not a valid TODO state" value))
  12692. (if (or (not value)
  12693. (not (string-match "\\S-" value)))
  12694. (setq value 'none))
  12695. (org-todo value)
  12696. (org-set-tags nil 'align))
  12697. ((equal property "PRIORITY")
  12698. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12699. (string-to-char value) ?\ ))
  12700. (org-set-tags nil 'align))
  12701. ((equal property "SCHEDULED")
  12702. (if (re-search-forward org-scheduled-time-regexp end t)
  12703. (cond
  12704. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12705. ((eq value 'later) (org-timestamp-change 1 'day))
  12706. (t (call-interactively 'org-schedule)))
  12707. (call-interactively 'org-schedule)))
  12708. ((equal property "DEADLINE")
  12709. (if (re-search-forward org-deadline-time-regexp end t)
  12710. (cond
  12711. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12712. ((eq value 'later) (org-timestamp-change 1 'day))
  12713. (t (call-interactively 'org-deadline)))
  12714. (call-interactively 'org-deadline)))
  12715. ((member property org-special-properties)
  12716. (error "The %s property can not yet be set with `org-entry-put'"
  12717. property))
  12718. (t ; a non-special property
  12719. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12720. (setq range (org-get-property-block beg end 'force))
  12721. (goto-char (car range))
  12722. (if (re-search-forward
  12723. (org-re-property property) (cdr range) t)
  12724. (progn
  12725. (delete-region (match-beginning 0) (match-end 0))
  12726. (goto-char (match-beginning 0)))
  12727. (goto-char (cdr range))
  12728. (insert "\n")
  12729. (backward-char 1)
  12730. (org-indent-line-function))
  12731. (insert ":" property ":")
  12732. (and value (insert " " value))
  12733. (org-indent-line-function)))))
  12734. (run-hook-with-args 'org-property-changed-functions property value)))
  12735. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12736. "Get all property keys in the current buffer.
  12737. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12738. like tags and TODO state.
  12739. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12740. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  12741. and others.
  12742. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12743. formats in the current buffer."
  12744. (let (rtn range cfmt s p)
  12745. (save-excursion
  12746. (save-restriction
  12747. (widen)
  12748. (goto-char (point-min))
  12749. (while (re-search-forward org-property-start-re nil t)
  12750. (setq range (org-get-property-block))
  12751. (goto-char (car range))
  12752. (while (re-search-forward
  12753. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12754. (cdr range) t)
  12755. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12756. (outline-next-heading))))
  12757. (when include-specials
  12758. (setq rtn (append org-special-properties rtn)))
  12759. (when include-defaults
  12760. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12761. (add-to-list 'rtn org-effort-property))
  12762. (when include-columns
  12763. (save-excursion
  12764. (save-restriction
  12765. (widen)
  12766. (goto-char (point-min))
  12767. (while (re-search-forward
  12768. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12769. nil t)
  12770. (setq cfmt (match-string 2) s 0)
  12771. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12772. cfmt s)
  12773. (setq s (match-end 0)
  12774. p (match-string 1 cfmt))
  12775. (unless (or (equal p "ITEM")
  12776. (member p org-special-properties))
  12777. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12778. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12779. (defun org-property-values (key)
  12780. "Return a list of all values of property KEY in the current buffer."
  12781. (save-excursion
  12782. (save-restriction
  12783. (widen)
  12784. (goto-char (point-min))
  12785. (let ((re (org-re-property key))
  12786. values)
  12787. (while (re-search-forward re nil t)
  12788. (add-to-list 'values (org-trim (match-string 1))))
  12789. (delete "" values)))))
  12790. (defun org-insert-property-drawer ()
  12791. "Insert a property drawer into the current entry."
  12792. (interactive)
  12793. (org-back-to-heading t)
  12794. (looking-at outline-regexp)
  12795. (let ((indent (if org-adapt-indentation
  12796. (- (match-end 0)(match-beginning 0))
  12797. 0))
  12798. (beg (point))
  12799. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12800. end hiddenp)
  12801. (outline-next-heading)
  12802. (setq end (point))
  12803. (goto-char beg)
  12804. (while (re-search-forward re end t))
  12805. (setq hiddenp (outline-invisible-p))
  12806. (end-of-line 1)
  12807. (and (equal (char-after) ?\n) (forward-char 1))
  12808. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12809. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12810. ;; just skip this line
  12811. (beginning-of-line 2)
  12812. ;; Drawer start, find the end
  12813. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12814. (beginning-of-line 1)))
  12815. (org-skip-over-state-notes)
  12816. (skip-chars-backward " \t\n\r")
  12817. (if (eq (char-before) ?*) (forward-char 1))
  12818. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12819. (beginning-of-line 0)
  12820. (org-indent-to-column indent)
  12821. (beginning-of-line 2)
  12822. (org-indent-to-column indent)
  12823. (beginning-of-line 0)
  12824. (if hiddenp
  12825. (save-excursion
  12826. (org-back-to-heading t)
  12827. (hide-entry))
  12828. (org-flag-drawer t))))
  12829. (defvar org-property-set-functions-alist nil
  12830. "Property set function alist.
  12831. Each entry should have the following format:
  12832. (PROPERTY . READ-FUNCTION)
  12833. The read function will be called with the same argument as
  12834. `org-completing-read'.")
  12835. (defun org-set-property-function (property)
  12836. "Get the function that should be used to set PROPERTY.
  12837. This is computed according to `org-property-set-functions-alist'."
  12838. (or (cdr (assoc property org-property-set-functions-alist))
  12839. 'org-completing-read))
  12840. (defun org-read-property-value (property)
  12841. "Read PROPERTY value from user."
  12842. (let* ((completion-ignore-case t)
  12843. (allowed (org-property-get-allowed-values nil property 'table))
  12844. (cur (org-entry-get nil property))
  12845. (prompt (concat property " value"
  12846. (if (and cur (string-match "\\S-" cur))
  12847. (concat " [" cur "]") "") ": "))
  12848. (set-function (org-set-property-function property))
  12849. (val (if allowed
  12850. (funcall set-function prompt allowed nil
  12851. (not (get-text-property 0 'org-unrestricted
  12852. (caar allowed))))
  12853. (let (org-completion-use-ido org-completion-use-iswitchb)
  12854. (funcall set-function prompt
  12855. (mapcar 'list (org-property-values property))
  12856. nil nil "" nil cur)))))
  12857. (if (equal val "")
  12858. cur
  12859. val)))
  12860. (defun org-read-property-name ()
  12861. "Read a property name."
  12862. (let* ((completion-ignore-case t)
  12863. (keys (org-buffer-property-keys nil t t))
  12864. (property (org-icompleting-read "Property: " (mapcar 'list keys))))
  12865. (if (member property keys)
  12866. property
  12867. (or (cdr (assoc (downcase property)
  12868. (mapcar (lambda (x) (cons (downcase x) x))
  12869. keys)))
  12870. property))))
  12871. (defun org-set-property (property value)
  12872. "In the current entry, set PROPERTY to VALUE.
  12873. When called interactively, this will prompt for a property name, offering
  12874. completion on existing and default properties. And then it will prompt
  12875. for a value, offering completion either on allowed values (via an inherited
  12876. xxx_ALL property) or on existing values in other instances of this property
  12877. in the current file."
  12878. (interactive (list nil nil))
  12879. (let* ((property (or property (org-read-property-name)))
  12880. (value (or value (org-read-property-value property))))
  12881. (unless (equal (org-entry-get nil property) value)
  12882. (org-entry-put nil property value))))
  12883. (defun org-delete-property (property)
  12884. "In the current entry, delete PROPERTY."
  12885. (interactive
  12886. (let* ((completion-ignore-case t)
  12887. (prop (org-icompleting-read "Property: "
  12888. (org-entry-properties nil 'standard))))
  12889. (list prop)))
  12890. (message "Property %s %s" property
  12891. (if (org-entry-delete nil property)
  12892. "deleted"
  12893. "was not present in the entry")))
  12894. (defun org-delete-property-globally (property)
  12895. "Remove PROPERTY globally, from all entries."
  12896. (interactive
  12897. (let* ((completion-ignore-case t)
  12898. (prop (org-icompleting-read
  12899. "Globally remove property: "
  12900. (mapcar 'list (org-buffer-property-keys)))))
  12901. (list prop)))
  12902. (save-excursion
  12903. (save-restriction
  12904. (widen)
  12905. (goto-char (point-min))
  12906. (let ((cnt 0))
  12907. (while (re-search-forward
  12908. (org-re-property property)
  12909. nil t)
  12910. (setq cnt (1+ cnt))
  12911. (replace-match ""))
  12912. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12913. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12914. (defun org-compute-property-at-point ()
  12915. "Compute the property at point.
  12916. This looks for an enclosing column format, extracts the operator and
  12917. then applies it to the property in the column format's scope."
  12918. (interactive)
  12919. (unless (org-at-property-p)
  12920. (error "Not at a property"))
  12921. (let ((prop (org-match-string-no-properties 2)))
  12922. (org-columns-get-format-and-top-level)
  12923. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12924. (error "No operator defined for property %s" prop))
  12925. (org-columns-compute prop)))
  12926. (defvar org-property-allowed-value-functions nil
  12927. "Hook for functions supplying allowed values for a specific property.
  12928. The functions must take a single argument, the name of the property, and
  12929. return a flat list of allowed values. If \":ETC\" is one of
  12930. the values, this means that these values are intended as defaults for
  12931. completion, but that other values should be allowed too.
  12932. The functions must return nil if they are not responsible for this
  12933. property.")
  12934. (defun org-property-get-allowed-values (pom property &optional table)
  12935. "Get allowed values for the property PROPERTY.
  12936. When TABLE is non-nil, return an alist that can directly be used for
  12937. completion."
  12938. (let (vals)
  12939. (cond
  12940. ((equal property "TODO")
  12941. (setq vals (org-with-point-at pom
  12942. (append org-todo-keywords-1 '("")))))
  12943. ((equal property "PRIORITY")
  12944. (let ((n org-lowest-priority))
  12945. (while (>= n org-highest-priority)
  12946. (push (char-to-string n) vals)
  12947. (setq n (1- n)))))
  12948. ((member property org-special-properties))
  12949. ((setq vals (run-hook-with-args-until-success
  12950. 'org-property-allowed-value-functions property)))
  12951. (t
  12952. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12953. (when (and vals (string-match "\\S-" vals))
  12954. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12955. (setq vals (mapcar (lambda (x)
  12956. (cond ((stringp x) x)
  12957. ((numberp x) (number-to-string x))
  12958. ((symbolp x) (symbol-name x))
  12959. (t "???")))
  12960. vals)))))
  12961. (when (member ":ETC" vals)
  12962. (setq vals (remove ":ETC" vals))
  12963. (org-add-props (car vals) '(org-unrestricted t)))
  12964. (if table (mapcar 'list vals) vals)))
  12965. (defun org-property-previous-allowed-value (&optional previous)
  12966. "Switch to the next allowed value for this property."
  12967. (interactive)
  12968. (org-property-next-allowed-value t))
  12969. (defun org-property-next-allowed-value (&optional previous)
  12970. "Switch to the next allowed value for this property."
  12971. (interactive)
  12972. (unless (org-at-property-p)
  12973. (error "Not at a property"))
  12974. (let* ((key (match-string 2))
  12975. (value (match-string 3))
  12976. (allowed (or (org-property-get-allowed-values (point) key)
  12977. (and (member value '("[ ]" "[-]" "[X]"))
  12978. '("[ ]" "[X]"))))
  12979. nval)
  12980. (unless allowed
  12981. (error "Allowed values for this property have not been defined"))
  12982. (if previous (setq allowed (reverse allowed)))
  12983. (if (member value allowed)
  12984. (setq nval (car (cdr (member value allowed)))))
  12985. (setq nval (or nval (car allowed)))
  12986. (if (equal nval value)
  12987. (error "Only one allowed value for this property"))
  12988. (org-at-property-p)
  12989. (replace-match (concat " :" key ": " nval) t t)
  12990. (org-indent-line-function)
  12991. (beginning-of-line 1)
  12992. (skip-chars-forward " \t")
  12993. (run-hook-with-args 'org-property-changed-functions key nval)))
  12994. (defun org-find-olp (path &optional this-buffer)
  12995. "Return a marker pointing to the entry at outline path OLP.
  12996. If anything goes wrong, throw an error.
  12997. You can wrap this call to catch the error like this:
  12998. (condition-case msg
  12999. (org-mobile-locate-entry (match-string 4))
  13000. (error (nth 1 msg)))
  13001. The return value will then be either a string with the error message,
  13002. or a marker if everything is OK.
  13003. If THIS-BUFFER is set, the outline path does not contain a file,
  13004. only headings."
  13005. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13006. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13007. (level 1)
  13008. (lmin 1)
  13009. (lmax 1)
  13010. limit re end found pos heading cnt flevel)
  13011. (unless buffer (error "File not found :%s" file))
  13012. (with-current-buffer buffer
  13013. (save-excursion
  13014. (save-restriction
  13015. (widen)
  13016. (setq limit (point-max))
  13017. (goto-char (point-min))
  13018. (while (setq heading (pop path))
  13019. (setq re (format org-complex-heading-regexp-format
  13020. (regexp-quote heading)))
  13021. (setq cnt 0 pos (point))
  13022. (while (re-search-forward re end t)
  13023. (setq level (- (match-end 1) (match-beginning 1)))
  13024. (if (and (>= level lmin) (<= level lmax))
  13025. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13026. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13027. lmax heading))
  13028. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13029. lmax heading))
  13030. (goto-char found)
  13031. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13032. (setq end (save-excursion (org-end-of-subtree t t))))
  13033. (when (org-on-heading-p)
  13034. (move-marker (make-marker) (point))))))))
  13035. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13036. "Find node HEADING in BUFFER.
  13037. Return a marker to the heading if it was found, or nil if not.
  13038. If POS-ONLY is set, return just the position instead of a marker.
  13039. The heading text must match exact, but it may have a TODO keyword,
  13040. a priority cookie and tags in the standard locations."
  13041. (with-current-buffer (or buffer (current-buffer))
  13042. (save-excursion
  13043. (save-restriction
  13044. (widen)
  13045. (goto-char (point-min))
  13046. (let (case-fold-search)
  13047. (if (re-search-forward
  13048. (format org-complex-heading-regexp-format
  13049. (regexp-quote heading)) nil t)
  13050. (if pos-only
  13051. (match-beginning 0)
  13052. (move-marker (make-marker) (match-beginning 0)))))))))
  13053. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13054. "Find Org node headline HEADING in all .org files in directory DIR.
  13055. When the target headline is found, return a marker to this location."
  13056. (let ((files (directory-files (or dir default-directory)
  13057. nil "\\`[^.#].*\\.org\\'"))
  13058. file visiting m buffer)
  13059. (catch 'found
  13060. (while (setq file (pop files))
  13061. (message "trying %s" file)
  13062. (setq visiting (org-find-base-buffer-visiting file))
  13063. (setq buffer (or visiting (find-file-noselect file)))
  13064. (setq m (org-find-exact-headline-in-buffer
  13065. heading buffer))
  13066. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13067. (and m (throw 'found m))))))
  13068. (defun org-find-entry-with-id (ident)
  13069. "Locate the entry that contains the ID property with exact value IDENT.
  13070. IDENT can be a string, a symbol or a number, this function will search for
  13071. the string representation of it.
  13072. Return the position where this entry starts, or nil if there is no such entry."
  13073. (interactive "sID: ")
  13074. (let ((id (cond
  13075. ((stringp ident) ident)
  13076. ((symbol-name ident) (symbol-name ident))
  13077. ((numberp ident) (number-to-string ident))
  13078. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13079. (case-fold-search nil))
  13080. (save-excursion
  13081. (save-restriction
  13082. (widen)
  13083. (goto-char (point-min))
  13084. (when (re-search-forward
  13085. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13086. nil t)
  13087. (org-back-to-heading t)
  13088. (point))))))
  13089. ;;;; Timestamps
  13090. (defvar org-last-changed-timestamp nil)
  13091. (defvar org-last-inserted-timestamp nil
  13092. "The last time stamp inserted with `org-insert-time-stamp'.")
  13093. (defvar org-time-was-given) ; dynamically scoped parameter
  13094. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13095. (defvar org-ts-what) ; dynamically scoped parameter
  13096. (defun org-time-stamp (arg &optional inactive)
  13097. "Prompt for a date/time and insert a time stamp.
  13098. If the user specifies a time like HH:MM, or if this command is called
  13099. with a prefix argument, the time stamp will contain date and time.
  13100. Otherwise, only the date will be included. All parts of a date not
  13101. specified by the user will be filled in from the current date/time.
  13102. So if you press just return without typing anything, the time stamp
  13103. will represent the current date/time. If there is already a timestamp
  13104. at the cursor, it will be modified."
  13105. (interactive "P")
  13106. (let* ((ts nil)
  13107. (default-time
  13108. ;; Default time is either today, or, when entering a range,
  13109. ;; the range start.
  13110. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13111. (save-excursion
  13112. (re-search-backward
  13113. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13114. (- (point) 20) t)))
  13115. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13116. (current-time)))
  13117. (default-input (and ts (org-get-compact-tod ts)))
  13118. org-time-was-given org-end-time-was-given time)
  13119. (cond
  13120. ((and (org-at-timestamp-p t)
  13121. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13122. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13123. (insert "--")
  13124. (setq time (let ((this-command this-command))
  13125. (org-read-date arg 'totime nil nil
  13126. default-time default-input)))
  13127. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13128. ((org-at-timestamp-p t)
  13129. (setq time (let ((this-command this-command))
  13130. (org-read-date arg 'totime nil nil default-time default-input)))
  13131. (when (org-at-timestamp-p t) ; just to get the match data
  13132. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13133. (replace-match "")
  13134. (setq org-last-changed-timestamp
  13135. (org-insert-time-stamp
  13136. time (or org-time-was-given arg)
  13137. inactive nil nil (list org-end-time-was-given))))
  13138. (message "Timestamp updated"))
  13139. (t
  13140. (setq time (let ((this-command this-command))
  13141. (org-read-date arg 'totime nil nil default-time default-input)))
  13142. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13143. nil nil (list org-end-time-was-given))))))
  13144. ;; FIXME: can we use this for something else, like computing time differences?
  13145. (defun org-get-compact-tod (s)
  13146. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13147. (let* ((t1 (match-string 1 s))
  13148. (h1 (string-to-number (match-string 2 s)))
  13149. (m1 (string-to-number (match-string 3 s)))
  13150. (t2 (and (match-end 4) (match-string 5 s)))
  13151. (h2 (and t2 (string-to-number (match-string 6 s))))
  13152. (m2 (and t2 (string-to-number (match-string 7 s))))
  13153. dh dm)
  13154. (if (not t2)
  13155. t1
  13156. (setq dh (- h2 h1) dm (- m2 m1))
  13157. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13158. (concat t1 "+" (number-to-string dh)
  13159. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13160. (defun org-time-stamp-inactive (&optional arg)
  13161. "Insert an inactive time stamp.
  13162. An inactive time stamp is enclosed in square brackets instead of angle
  13163. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13164. does not link to the calendar and cannot be changed with the S-cursor keys.
  13165. So these are more for recording a certain time/date."
  13166. (interactive "P")
  13167. (org-time-stamp arg 'inactive))
  13168. (defvar org-date-ovl (make-overlay 1 1))
  13169. (overlay-put org-date-ovl 'face 'org-warning)
  13170. (org-detach-overlay org-date-ovl)
  13171. (defvar org-ans1) ; dynamically scoped parameter
  13172. (defvar org-ans2) ; dynamically scoped parameter
  13173. (defvar org-plain-time-of-day-regexp) ; defined below
  13174. (defvar org-overriding-default-time nil) ; dynamically scoped
  13175. (defvar org-read-date-overlay nil)
  13176. (defvar org-dcst nil) ; dynamically scoped
  13177. (defvar org-read-date-history nil)
  13178. (defvar org-read-date-final-answer nil)
  13179. (defvar org-read-date-analyze-futurep nil)
  13180. (defvar org-read-date-analyze-forced-year nil)
  13181. (defun org-read-date (&optional with-time to-time from-string prompt
  13182. default-time default-input)
  13183. "Read a date, possibly a time, and make things smooth for the user.
  13184. The prompt will suggest to enter an ISO date, but you can also enter anything
  13185. which will at least partially be understood by `parse-time-string'.
  13186. Unrecognized parts of the date will default to the current day, month, year,
  13187. hour and minute. If this command is called to replace a timestamp at point,
  13188. of to enter the second timestamp of a range, the default time is taken
  13189. from the existing stamp. Furthermore, the command prefers the future,
  13190. so if you are giving a date where the year is not given, and the day-month
  13191. combination is already past in the current year, it will assume you
  13192. mean next year. For details, see the manual. A few examples:
  13193. 3-2-5 --> 2003-02-05
  13194. feb 15 --> currentyear-02-15
  13195. 2/15 --> currentyear-02-15
  13196. sep 12 9 --> 2009-09-12
  13197. 12:45 --> today 12:45
  13198. 22 sept 0:34 --> currentyear-09-22 0:34
  13199. 12 --> currentyear-currentmonth-12
  13200. Fri --> nearest Friday (today or later)
  13201. etc.
  13202. Furthermore you can specify a relative date by giving, as the *first* thing
  13203. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13204. change in days weeks, months, years.
  13205. With a single plus or minus, the date is relative to today. With a double
  13206. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13207. +4d --> four days from today
  13208. +4 --> same as above
  13209. +2w --> two weeks from today
  13210. ++5 --> five days from default date
  13211. The function understands only English month and weekday abbreviations,
  13212. but this can be configured with the variables `parse-time-months' and
  13213. `parse-time-weekdays'.
  13214. While prompting, a calendar is popped up - you can also select the
  13215. date with the mouse (button 1). The calendar shows a period of three
  13216. months. To scroll it to other months, use the keys `>' and `<'.
  13217. If you don't like the calendar, turn it off with
  13218. \(setq org-read-date-popup-calendar nil)
  13219. With optional argument TO-TIME, the date will immediately be converted
  13220. to an internal time.
  13221. With an optional argument WITH-TIME, the prompt will suggest to also
  13222. insert a time. Note that when WITH-TIME is not set, you can still
  13223. enter a time, and this function will inform the calling routine about
  13224. this change. The calling routine may then choose to change the format
  13225. used to insert the time stamp into the buffer to include the time.
  13226. With optional argument FROM-STRING, read from this string instead from
  13227. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13228. the time/date that is used for everything that is not specified by the
  13229. user."
  13230. (require 'parse-time)
  13231. (let* ((org-time-stamp-rounding-minutes
  13232. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13233. (org-dcst org-display-custom-times)
  13234. (ct (org-current-time))
  13235. (def (or org-overriding-default-time default-time ct))
  13236. (defdecode (decode-time def))
  13237. (dummy (progn
  13238. (when (< (nth 2 defdecode) org-extend-today-until)
  13239. (setcar (nthcdr 2 defdecode) -1)
  13240. (setcar (nthcdr 1 defdecode) 59)
  13241. (setq def (apply 'encode-time defdecode)
  13242. defdecode (decode-time def)))))
  13243. (calendar-frame-setup nil)
  13244. (calendar-setup nil)
  13245. (calendar-move-hook nil)
  13246. (calendar-view-diary-initially-flag nil)
  13247. (calendar-view-holidays-initially-flag nil)
  13248. (timestr (format-time-string
  13249. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13250. (prompt (concat (if prompt (concat prompt " ") "")
  13251. (format "Date+time [%s]: " timestr)))
  13252. ans (org-ans0 "") org-ans1 org-ans2 final)
  13253. (cond
  13254. (from-string (setq ans from-string))
  13255. (org-read-date-popup-calendar
  13256. (save-excursion
  13257. (save-window-excursion
  13258. (calendar)
  13259. (unwind-protect
  13260. (progn
  13261. (calendar-forward-day (- (time-to-days def)
  13262. (calendar-absolute-from-gregorian
  13263. (calendar-current-date))))
  13264. (org-eval-in-calendar nil t)
  13265. (let* ((old-map (current-local-map))
  13266. (map (copy-keymap calendar-mode-map))
  13267. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13268. (org-defkey map (kbd "RET") 'org-calendar-select)
  13269. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13270. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13271. (org-defkey minibuffer-local-map [(meta shift left)]
  13272. (lambda () (interactive)
  13273. (org-eval-in-calendar '(calendar-backward-month 1))))
  13274. (org-defkey minibuffer-local-map [(meta shift right)]
  13275. (lambda () (interactive)
  13276. (org-eval-in-calendar '(calendar-forward-month 1))))
  13277. (org-defkey minibuffer-local-map [(meta shift up)]
  13278. (lambda () (interactive)
  13279. (org-eval-in-calendar '(calendar-backward-year 1))))
  13280. (org-defkey minibuffer-local-map [(meta shift down)]
  13281. (lambda () (interactive)
  13282. (org-eval-in-calendar '(calendar-forward-year 1))))
  13283. (org-defkey minibuffer-local-map [?\e (shift left)]
  13284. (lambda () (interactive)
  13285. (org-eval-in-calendar '(calendar-backward-month 1))))
  13286. (org-defkey minibuffer-local-map [?\e (shift right)]
  13287. (lambda () (interactive)
  13288. (org-eval-in-calendar '(calendar-forward-month 1))))
  13289. (org-defkey minibuffer-local-map [?\e (shift up)]
  13290. (lambda () (interactive)
  13291. (org-eval-in-calendar '(calendar-backward-year 1))))
  13292. (org-defkey minibuffer-local-map [?\e (shift down)]
  13293. (lambda () (interactive)
  13294. (org-eval-in-calendar '(calendar-forward-year 1))))
  13295. (org-defkey minibuffer-local-map [(shift up)]
  13296. (lambda () (interactive)
  13297. (org-eval-in-calendar '(calendar-backward-week 1))))
  13298. (org-defkey minibuffer-local-map [(shift down)]
  13299. (lambda () (interactive)
  13300. (org-eval-in-calendar '(calendar-forward-week 1))))
  13301. (org-defkey minibuffer-local-map [(shift left)]
  13302. (lambda () (interactive)
  13303. (org-eval-in-calendar '(calendar-backward-day 1))))
  13304. (org-defkey minibuffer-local-map [(shift right)]
  13305. (lambda () (interactive)
  13306. (org-eval-in-calendar '(calendar-forward-day 1))))
  13307. (org-defkey minibuffer-local-map ">"
  13308. (lambda () (interactive)
  13309. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13310. (org-defkey minibuffer-local-map "<"
  13311. (lambda () (interactive)
  13312. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13313. (org-defkey minibuffer-local-map "\C-v"
  13314. (lambda () (interactive)
  13315. (org-eval-in-calendar
  13316. '(calendar-scroll-left-three-months 1))))
  13317. (org-defkey minibuffer-local-map "\M-v"
  13318. (lambda () (interactive)
  13319. (org-eval-in-calendar
  13320. '(calendar-scroll-right-three-months 1))))
  13321. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13322. (unwind-protect
  13323. (progn
  13324. (use-local-map map)
  13325. (add-hook 'post-command-hook 'org-read-date-display)
  13326. (setq org-ans0 (read-string prompt default-input
  13327. 'org-read-date-history nil))
  13328. ;; org-ans0: from prompt
  13329. ;; org-ans1: from mouse click
  13330. ;; org-ans2: from calendar motion
  13331. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13332. (remove-hook 'post-command-hook 'org-read-date-display)
  13333. (use-local-map old-map)
  13334. (when org-read-date-overlay
  13335. (delete-overlay org-read-date-overlay)
  13336. (setq org-read-date-overlay nil)))))
  13337. (bury-buffer "*Calendar*")))))
  13338. (t ; Naked prompt only
  13339. (unwind-protect
  13340. (setq ans (read-string prompt default-input
  13341. 'org-read-date-history timestr))
  13342. (when org-read-date-overlay
  13343. (delete-overlay org-read-date-overlay)
  13344. (setq org-read-date-overlay nil)))))
  13345. (setq final (org-read-date-analyze ans def defdecode))
  13346. (when org-read-date-analyze-forced-year
  13347. (message "Year was forced into %s"
  13348. (if org-read-date-force-compatible-dates
  13349. "compatible range (1970-2037)"
  13350. "range representable on this machine"))
  13351. (ding))
  13352. ;; One round trip to get rid of 34th of August and stuff like that....
  13353. (setq final (decode-time (apply 'encode-time final)))
  13354. (setq org-read-date-final-answer ans)
  13355. (if to-time
  13356. (apply 'encode-time final)
  13357. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13358. (format "%04d-%02d-%02d %02d:%02d"
  13359. (nth 5 final) (nth 4 final) (nth 3 final)
  13360. (nth 2 final) (nth 1 final))
  13361. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13362. (defvar def)
  13363. (defvar defdecode)
  13364. (defvar with-time)
  13365. (defun org-read-date-display ()
  13366. "Display the current date prompt interpretation in the minibuffer."
  13367. (when org-read-date-display-live
  13368. (when org-read-date-overlay
  13369. (delete-overlay org-read-date-overlay))
  13370. (let ((p (point)))
  13371. (end-of-line 1)
  13372. (while (not (equal (buffer-substring
  13373. (max (point-min) (- (point) 4)) (point))
  13374. " "))
  13375. (insert " "))
  13376. (goto-char p))
  13377. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13378. " " (or org-ans1 org-ans2)))
  13379. (org-end-time-was-given nil)
  13380. (f (org-read-date-analyze ans def defdecode))
  13381. (fmts (if org-dcst
  13382. org-time-stamp-custom-formats
  13383. org-time-stamp-formats))
  13384. (fmt (if (or with-time
  13385. (and (boundp 'org-time-was-given) org-time-was-given))
  13386. (cdr fmts)
  13387. (car fmts)))
  13388. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13389. (when (and org-end-time-was-given
  13390. (string-match org-plain-time-of-day-regexp txt))
  13391. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13392. org-end-time-was-given
  13393. (substring txt (match-end 0)))))
  13394. (when org-read-date-analyze-futurep
  13395. (setq txt (concat txt " (=>F)")))
  13396. (setq org-read-date-overlay
  13397. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13398. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13399. (defun org-read-date-analyze (ans def defdecode)
  13400. "Analyze the combined answer of the date prompt."
  13401. ;; FIXME: cleanup and comment
  13402. (let ((nowdecode (decode-time (current-time)))
  13403. delta deltan deltaw deltadef year month day
  13404. hour minute second wday pm h2 m2 tl wday1
  13405. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13406. (setq org-read-date-analyze-futurep nil
  13407. org-read-date-analyze-forced-year nil)
  13408. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13409. (setq ans "+0"))
  13410. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13411. (setq ans (replace-match "" t t ans)
  13412. deltan (car delta)
  13413. deltaw (nth 1 delta)
  13414. deltadef (nth 2 delta)))
  13415. ;; Check if there is an iso week date in there
  13416. ;; If yes, store the info and postpone interpreting it until the rest
  13417. ;; of the parsing is done
  13418. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13419. (setq iso-year (if (match-end 1)
  13420. (org-small-year-to-year
  13421. (string-to-number (match-string 1 ans))))
  13422. iso-weekday (if (match-end 3)
  13423. (string-to-number (match-string 3 ans)))
  13424. iso-week (string-to-number (match-string 2 ans)))
  13425. (setq ans (replace-match "" t t ans)))
  13426. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13427. (when (string-match
  13428. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13429. (setq year (if (match-end 2)
  13430. (string-to-number (match-string 2 ans))
  13431. (progn (setq kill-year t)
  13432. (string-to-number (format-time-string "%Y"))))
  13433. month (string-to-number (match-string 3 ans))
  13434. day (string-to-number (match-string 4 ans)))
  13435. (if (< year 100) (setq year (+ 2000 year)))
  13436. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13437. t nil ans)))
  13438. ;; Help matching dottet european dates
  13439. (when (string-match
  13440. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13441. (setq year (if (match-end 3)
  13442. (string-to-number (match-string 3 ans))
  13443. (progn (setq kill-year t)
  13444. (string-to-number (format-time-string "%Y"))))
  13445. day (string-to-number (match-string 1 ans))
  13446. month (string-to-number (match-string 2 ans))
  13447. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13448. t nil ans)))
  13449. ;; Help matching american dates, like 5/30 or 5/30/7
  13450. (when (string-match
  13451. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13452. (setq year (if (match-end 4)
  13453. (string-to-number (match-string 4 ans))
  13454. (progn (setq kill-year t)
  13455. (string-to-number (format-time-string "%Y"))))
  13456. month (string-to-number (match-string 1 ans))
  13457. day (string-to-number (match-string 2 ans)))
  13458. (if (< year 100) (setq year (+ 2000 year)))
  13459. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13460. t nil ans)))
  13461. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13462. ;; If there is a time with am/pm, and *no* time without it, we convert
  13463. ;; so that matching will be successful.
  13464. (loop for i from 1 to 2 do ; twice, for end time as well
  13465. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13466. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13467. (setq hour (string-to-number (match-string 1 ans))
  13468. minute (if (match-end 3)
  13469. (string-to-number (match-string 3 ans))
  13470. 0)
  13471. pm (equal ?p
  13472. (string-to-char (downcase (match-string 4 ans)))))
  13473. (if (and (= hour 12) (not pm))
  13474. (setq hour 0)
  13475. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13476. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13477. t t ans))))
  13478. ;; Check if a time range is given as a duration
  13479. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13480. (setq hour (string-to-number (match-string 1 ans))
  13481. h2 (+ hour (string-to-number (match-string 3 ans)))
  13482. minute (string-to-number (match-string 2 ans))
  13483. m2 (+ minute (if (match-end 5) (string-to-number
  13484. (match-string 5 ans))0)))
  13485. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13486. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13487. t t ans)))
  13488. ;; Check if there is a time range
  13489. (when (boundp 'org-end-time-was-given)
  13490. (setq org-time-was-given nil)
  13491. (when (and (string-match org-plain-time-of-day-regexp ans)
  13492. (match-end 8))
  13493. (setq org-end-time-was-given (match-string 8 ans))
  13494. (setq ans (concat (substring ans 0 (match-beginning 7))
  13495. (substring ans (match-end 7))))))
  13496. (setq tl (parse-time-string ans)
  13497. day (or (nth 3 tl) (nth 3 defdecode))
  13498. month (or (nth 4 tl)
  13499. (if (and org-read-date-prefer-future
  13500. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13501. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13502. (nth 4 defdecode)))
  13503. year (or (and (not kill-year) (nth 5 tl))
  13504. (if (and org-read-date-prefer-future
  13505. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13506. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13507. (nth 5 defdecode)))
  13508. hour (or (nth 2 tl) (nth 2 defdecode))
  13509. minute (or (nth 1 tl) (nth 1 defdecode))
  13510. second (or (nth 0 tl) 0)
  13511. wday (nth 6 tl))
  13512. (when (and (eq org-read-date-prefer-future 'time)
  13513. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13514. (equal day (nth 3 nowdecode))
  13515. (equal month (nth 4 nowdecode))
  13516. (equal year (nth 5 nowdecode))
  13517. (nth 2 tl)
  13518. (or (< (nth 2 tl) (nth 2 nowdecode))
  13519. (and (= (nth 2 tl) (nth 2 nowdecode))
  13520. (nth 1 tl)
  13521. (< (nth 1 tl) (nth 1 nowdecode)))))
  13522. (setq day (1+ day)
  13523. futurep t))
  13524. ;; Special date definitions below
  13525. (cond
  13526. (iso-week
  13527. ;; There was an iso week
  13528. (require 'cal-iso)
  13529. (setq futurep nil)
  13530. (setq year (or iso-year year)
  13531. day (or iso-weekday wday 1)
  13532. wday nil ; to make sure that the trigger below does not match
  13533. iso-date (calendar-gregorian-from-absolute
  13534. (calendar-absolute-from-iso
  13535. (list iso-week day year))))
  13536. ; FIXME: Should we also push ISO weeks into the future?
  13537. ; (when (and org-read-date-prefer-future
  13538. ; (not iso-year)
  13539. ; (< (calendar-absolute-from-gregorian iso-date)
  13540. ; (time-to-days (current-time))))
  13541. ; (setq year (1+ year)
  13542. ; iso-date (calendar-gregorian-from-absolute
  13543. ; (calendar-absolute-from-iso
  13544. ; (list iso-week day year)))))
  13545. (setq month (car iso-date)
  13546. year (nth 2 iso-date)
  13547. day (nth 1 iso-date)))
  13548. (deltan
  13549. (setq futurep nil)
  13550. (unless deltadef
  13551. (let ((now (decode-time (current-time))))
  13552. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13553. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13554. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13555. ((equal deltaw "m") (setq month (+ month deltan)))
  13556. ((equal deltaw "y") (setq year (+ year deltan)))))
  13557. ((and wday (not (nth 3 tl)))
  13558. (setq futurep nil)
  13559. ;; Weekday was given, but no day, so pick that day in the week
  13560. ;; on or after the derived date.
  13561. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13562. (unless (equal wday wday1)
  13563. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13564. (if (and (boundp 'org-time-was-given)
  13565. (nth 2 tl))
  13566. (setq org-time-was-given t))
  13567. (if (< year 100) (setq year (+ 2000 year)))
  13568. ;; Check of the date is representable
  13569. (if org-read-date-force-compatible-dates
  13570. (progn
  13571. (if (< year 1970)
  13572. (setq year 1970 org-read-date-analyze-forced-year t))
  13573. (if (> year 2037)
  13574. (setq year 2037 org-read-date-analyze-forced-year t)))
  13575. (condition-case nil
  13576. (encode-time second minute hour day month year)
  13577. (error
  13578. (setq year (nth 5 defdecode))
  13579. (setq org-read-date-analyze-forced-year t))))
  13580. (setq org-read-date-analyze-futurep futurep)
  13581. (list second minute hour day month year)))
  13582. (defvar parse-time-weekdays)
  13583. (defun org-read-date-get-relative (s today default)
  13584. "Check string S for special relative date string.
  13585. TODAY and DEFAULT are internal times, for today and for a default.
  13586. Return shift list (N what def-flag)
  13587. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13588. N is the number of WHATs to shift.
  13589. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13590. the DEFAULT date rather than TODAY."
  13591. (when (and
  13592. (string-match
  13593. (concat
  13594. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13595. "\\([0-9]+\\)?"
  13596. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13597. "\\([ \t]\\|$\\)") s)
  13598. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13599. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13600. (string-to-char (substring (match-string 1 s) -1))
  13601. ?+))
  13602. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13603. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13604. (what (if (match-end 3) (match-string 3 s) "d"))
  13605. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13606. (date (if rel default today))
  13607. (wday (nth 6 (decode-time date)))
  13608. delta)
  13609. (if wday1
  13610. (progn
  13611. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13612. (if (= dir ?-) (setq delta (- delta 7)))
  13613. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13614. (list delta "d" rel))
  13615. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13616. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13617. "Turn a user-specified date into the internal representation.
  13618. The internal representation needed by the calendar is (month day year).
  13619. This is a wrapper to handle the brain-dead convention in calendar that
  13620. user function argument order change dependent on argument order."
  13621. (if (boundp 'calendar-date-style)
  13622. (cond
  13623. ((eq calendar-date-style 'american)
  13624. (list arg1 arg2 arg3))
  13625. ((eq calendar-date-style 'european)
  13626. (list arg2 arg1 arg3))
  13627. ((eq calendar-date-style 'iso)
  13628. (list arg2 arg3 arg1)))
  13629. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13630. (if (org-bound-and-true-p european-calendar-style)
  13631. (list arg2 arg1 arg3)
  13632. (list arg1 arg2 arg3)))))
  13633. (defun org-eval-in-calendar (form &optional keepdate)
  13634. "Eval FORM in the calendar window and return to current window.
  13635. Also, store the cursor date in variable org-ans2."
  13636. (let ((sf (selected-frame))
  13637. (sw (selected-window)))
  13638. (select-window (get-buffer-window "*Calendar*" t))
  13639. (eval form)
  13640. (when (and (not keepdate) (calendar-cursor-to-date))
  13641. (let* ((date (calendar-cursor-to-date))
  13642. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13643. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13644. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13645. (select-window sw)
  13646. (org-select-frame-set-input-focus sf)))
  13647. (defun org-calendar-select ()
  13648. "Return to `org-read-date' with the date currently selected.
  13649. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13650. (interactive)
  13651. (when (calendar-cursor-to-date)
  13652. (let* ((date (calendar-cursor-to-date))
  13653. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13654. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13655. (if (active-minibuffer-window) (exit-minibuffer))))
  13656. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13657. "Insert a date stamp for the date given by the internal TIME.
  13658. WITH-HM means use the stamp format that includes the time of the day.
  13659. INACTIVE means use square brackets instead of angular ones, so that the
  13660. stamp will not contribute to the agenda.
  13661. PRE and POST are optional strings to be inserted before and after the
  13662. stamp.
  13663. The command returns the inserted time stamp."
  13664. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13665. stamp)
  13666. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13667. (insert-before-markers (or pre ""))
  13668. (when (listp extra)
  13669. (setq extra (car extra))
  13670. (if (and (stringp extra)
  13671. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13672. (setq extra (format "-%02d:%02d"
  13673. (string-to-number (match-string 1 extra))
  13674. (string-to-number (match-string 2 extra))))
  13675. (setq extra nil)))
  13676. (when extra
  13677. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13678. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13679. (insert-before-markers (or post ""))
  13680. (setq org-last-inserted-timestamp stamp)))
  13681. (defun org-toggle-time-stamp-overlays ()
  13682. "Toggle the use of custom time stamp formats."
  13683. (interactive)
  13684. (setq org-display-custom-times (not org-display-custom-times))
  13685. (unless org-display-custom-times
  13686. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13687. (while (setq p (next-single-property-change p 'display))
  13688. (if (and (get-text-property p 'display)
  13689. (eq (get-text-property p 'face) 'org-date))
  13690. (remove-text-properties
  13691. p (setq p (next-single-property-change p 'display))
  13692. '(display t))))
  13693. (set-buffer-modified-p bmp)))
  13694. (if (featurep 'xemacs)
  13695. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13696. (org-restart-font-lock)
  13697. (setq org-table-may-need-update t)
  13698. (if org-display-custom-times
  13699. (message "Time stamps are overlayed with custom format")
  13700. (message "Time stamp overlays removed")))
  13701. (defun org-display-custom-time (beg end)
  13702. "Overlay modified time stamp format over timestamp between BEG and END."
  13703. (let* ((ts (buffer-substring beg end))
  13704. t1 w1 with-hm tf time str w2 (off 0))
  13705. (save-match-data
  13706. (setq t1 (org-parse-time-string ts t))
  13707. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13708. (setq off (- (match-end 0) (match-beginning 0)))))
  13709. (setq end (- end off))
  13710. (setq w1 (- end beg)
  13711. with-hm (and (nth 1 t1) (nth 2 t1))
  13712. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13713. time (org-fix-decoded-time t1)
  13714. str (org-add-props
  13715. (format-time-string
  13716. (substring tf 1 -1) (apply 'encode-time time))
  13717. nil 'mouse-face 'highlight)
  13718. w2 (length str))
  13719. (if (not (= w2 w1))
  13720. (add-text-properties (1+ beg) (+ 2 beg)
  13721. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13722. (if (featurep 'xemacs)
  13723. (progn
  13724. (put-text-property beg end 'invisible t)
  13725. (put-text-property beg end 'end-glyph (make-glyph str)))
  13726. (put-text-property beg end 'display str))))
  13727. (defun org-translate-time (string)
  13728. "Translate all timestamps in STRING to custom format.
  13729. But do this only if the variable `org-display-custom-times' is set."
  13730. (when org-display-custom-times
  13731. (save-match-data
  13732. (let* ((start 0)
  13733. (re org-ts-regexp-both)
  13734. t1 with-hm inactive tf time str beg end)
  13735. (while (setq start (string-match re string start))
  13736. (setq beg (match-beginning 0)
  13737. end (match-end 0)
  13738. t1 (save-match-data
  13739. (org-parse-time-string (substring string beg end) t))
  13740. with-hm (and (nth 1 t1) (nth 2 t1))
  13741. inactive (equal (substring string beg (1+ beg)) "[")
  13742. tf (funcall (if with-hm 'cdr 'car)
  13743. org-time-stamp-custom-formats)
  13744. time (org-fix-decoded-time t1)
  13745. str (format-time-string
  13746. (concat
  13747. (if inactive "[" "<") (substring tf 1 -1)
  13748. (if inactive "]" ">"))
  13749. (apply 'encode-time time))
  13750. string (replace-match str t t string)
  13751. start (+ start (length str)))))))
  13752. string)
  13753. (defun org-fix-decoded-time (time)
  13754. "Set 0 instead of nil for the first 6 elements of time.
  13755. Don't touch the rest."
  13756. (let ((n 0))
  13757. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13758. (defun org-days-to-time (timestamp-string)
  13759. "Difference between TIMESTAMP-STRING and now in days."
  13760. (- (time-to-days (org-time-string-to-time timestamp-string))
  13761. (time-to-days (current-time))))
  13762. (defun org-deadline-close (timestamp-string &optional ndays)
  13763. "Is the time in TIMESTAMP-STRING close to the current date?"
  13764. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13765. (and (< (org-days-to-time timestamp-string) ndays)
  13766. (not (org-entry-is-done-p))))
  13767. (defun org-get-wdays (ts)
  13768. "Get the deadline lead time appropriate for timestring TS."
  13769. (cond
  13770. ((<= org-deadline-warning-days 0)
  13771. ;; 0 or negative, enforce this value no matter what
  13772. (- org-deadline-warning-days))
  13773. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13774. ;; lead time is specified.
  13775. (floor (* (string-to-number (match-string 1 ts))
  13776. (cdr (assoc (match-string 2 ts)
  13777. '(("d" . 1) ("w" . 7)
  13778. ("m" . 30.4) ("y" . 365.25)))))))
  13779. ;; go for the default.
  13780. (t org-deadline-warning-days)))
  13781. (defun org-calendar-select-mouse (ev)
  13782. "Return to `org-read-date' with the date currently selected.
  13783. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13784. (interactive "e")
  13785. (mouse-set-point ev)
  13786. (when (calendar-cursor-to-date)
  13787. (let* ((date (calendar-cursor-to-date))
  13788. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13789. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13790. (if (active-minibuffer-window) (exit-minibuffer))))
  13791. (defun org-check-deadlines (ndays)
  13792. "Check if there are any deadlines due or past due.
  13793. A deadline is considered due if it happens within `org-deadline-warning-days'
  13794. days from today's date. If the deadline appears in an entry marked DONE,
  13795. it is not shown. The prefix arg NDAYS can be used to test that many
  13796. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13797. (interactive "P")
  13798. (let* ((org-warn-days
  13799. (cond
  13800. ((equal ndays '(4)) 100000)
  13801. (ndays (prefix-numeric-value ndays))
  13802. (t (abs org-deadline-warning-days))))
  13803. (case-fold-search nil)
  13804. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13805. (callback
  13806. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13807. (message "%d deadlines past-due or due within %d days"
  13808. (org-occur regexp nil callback)
  13809. org-warn-days)))
  13810. (defun org-check-before-date (date)
  13811. "Check if there are deadlines or scheduled entries before DATE."
  13812. (interactive (list (org-read-date)))
  13813. (let ((case-fold-search nil)
  13814. (regexp (concat "\\<\\(" org-deadline-string
  13815. "\\|" org-scheduled-string
  13816. "\\) *<\\([^>]+\\)>"))
  13817. (callback
  13818. (lambda () (time-less-p
  13819. (org-time-string-to-time (match-string 2))
  13820. (org-time-string-to-time date)))))
  13821. (message "%d entries before %s"
  13822. (org-occur regexp nil callback) date)))
  13823. (defun org-check-after-date (date)
  13824. "Check if there are deadlines or scheduled entries after DATE."
  13825. (interactive (list (org-read-date)))
  13826. (let ((case-fold-search nil)
  13827. (regexp (concat "\\<\\(" org-deadline-string
  13828. "\\|" org-scheduled-string
  13829. "\\) *<\\([^>]+\\)>"))
  13830. (callback
  13831. (lambda () (not
  13832. (time-less-p
  13833. (org-time-string-to-time (match-string 2))
  13834. (org-time-string-to-time date))))))
  13835. (message "%d entries after %s"
  13836. (org-occur regexp nil callback) date)))
  13837. (defun org-evaluate-time-range (&optional to-buffer)
  13838. "Evaluate a time range by computing the difference between start and end.
  13839. Normally the result is just printed in the echo area, but with prefix arg
  13840. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13841. If the time range is actually in a table, the result is inserted into the
  13842. next column.
  13843. For time difference computation, a year is assumed to be exactly 365
  13844. days in order to avoid rounding problems."
  13845. (interactive "P")
  13846. (or
  13847. (org-clock-update-time-maybe)
  13848. (save-excursion
  13849. (unless (org-at-date-range-p t)
  13850. (goto-char (point-at-bol))
  13851. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13852. (if (not (org-at-date-range-p t))
  13853. (error "Not at a time-stamp range, and none found in current line")))
  13854. (let* ((ts1 (match-string 1))
  13855. (ts2 (match-string 2))
  13856. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13857. (match-end (match-end 0))
  13858. (time1 (org-time-string-to-time ts1))
  13859. (time2 (org-time-string-to-time ts2))
  13860. (t1 (org-float-time time1))
  13861. (t2 (org-float-time time2))
  13862. (diff (abs (- t2 t1)))
  13863. (negative (< (- t2 t1) 0))
  13864. ;; (ys (floor (* 365 24 60 60)))
  13865. (ds (* 24 60 60))
  13866. (hs (* 60 60))
  13867. (fy "%dy %dd %02d:%02d")
  13868. (fy1 "%dy %dd")
  13869. (fd "%dd %02d:%02d")
  13870. (fd1 "%dd")
  13871. (fh "%02d:%02d")
  13872. y d h m align)
  13873. (if havetime
  13874. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13875. y 0
  13876. d (floor (/ diff ds)) diff (mod diff ds)
  13877. h (floor (/ diff hs)) diff (mod diff hs)
  13878. m (floor (/ diff 60)))
  13879. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13880. y 0
  13881. d (floor (+ (/ diff ds) 0.5))
  13882. h 0 m 0))
  13883. (if (not to-buffer)
  13884. (message "%s" (org-make-tdiff-string y d h m))
  13885. (if (org-at-table-p)
  13886. (progn
  13887. (goto-char match-end)
  13888. (setq align t)
  13889. (and (looking-at " *|") (goto-char (match-end 0))))
  13890. (goto-char match-end))
  13891. (if (looking-at
  13892. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13893. (replace-match ""))
  13894. (if negative (insert " -"))
  13895. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13896. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13897. (insert " " (format fh h m))))
  13898. (if align (org-table-align))
  13899. (message "Time difference inserted")))))
  13900. (defun org-make-tdiff-string (y d h m)
  13901. (let ((fmt "")
  13902. (l nil))
  13903. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13904. l (push y l)))
  13905. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13906. l (push d l)))
  13907. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13908. l (push h l)))
  13909. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13910. l (push m l)))
  13911. (apply 'format fmt (nreverse l))))
  13912. (defun org-time-string-to-time (s)
  13913. (apply 'encode-time (org-parse-time-string s)))
  13914. (defun org-time-string-to-seconds (s)
  13915. (org-float-time (org-time-string-to-time s)))
  13916. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13917. "Convert a time stamp to an absolute day number.
  13918. If there is a specifier for a cyclic time stamp, get the closest date to
  13919. DAYNR.
  13920. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13921. The variable date is bound by the calendar when this is called."
  13922. (cond
  13923. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13924. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13925. daynr
  13926. (+ daynr 1000)))
  13927. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13928. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13929. (time-to-days (current-time))) (match-string 0 s)
  13930. prefer show-all))
  13931. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13932. (defun org-days-to-iso-week (days)
  13933. "Return the iso week number."
  13934. (require 'cal-iso)
  13935. (car (calendar-iso-from-absolute days)))
  13936. (defun org-small-year-to-year (year)
  13937. "Convert 2-digit years into 4-digit years.
  13938. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13939. The year 2000 cannot be abbreviated. Any year larger than 99
  13940. is returned unchanged."
  13941. (if (< year 38)
  13942. (setq year (+ 2000 year))
  13943. (if (< year 100)
  13944. (setq year (+ 1900 year))))
  13945. year)
  13946. (defun org-time-from-absolute (d)
  13947. "Return the time corresponding to date D.
  13948. D may be an absolute day number, or a calendar-type list (month day year)."
  13949. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13950. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13951. (defun org-calendar-holiday ()
  13952. "List of holidays, for Diary display in Org-mode."
  13953. (require 'holidays)
  13954. (let ((hl (funcall
  13955. (if (fboundp 'calendar-check-holidays)
  13956. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13957. (if hl (mapconcat 'identity hl "; "))))
  13958. (defun org-diary-sexp-entry (sexp entry date)
  13959. "Process a SEXP diary ENTRY for DATE."
  13960. (require 'diary-lib)
  13961. (let ((result (if calendar-debug-sexp
  13962. (let ((stack-trace-on-error t))
  13963. (eval (car (read-from-string sexp))))
  13964. (condition-case nil
  13965. (eval (car (read-from-string sexp)))
  13966. (error
  13967. (beep)
  13968. (message "Bad sexp at line %d in %s: %s"
  13969. (org-current-line)
  13970. (buffer-file-name) sexp)
  13971. (sleep-for 2))))))
  13972. (cond ((stringp result) (split-string result "; "))
  13973. ((and (consp result)
  13974. (not (consp (cdr result)))
  13975. (stringp (cdr result))) (cdr result))
  13976. ((and (consp result)
  13977. (stringp (car result))) result)
  13978. (result entry)
  13979. (t nil))))
  13980. (defun org-diary-to-ical-string (frombuf)
  13981. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13982. This uses the icalendar.el library."
  13983. (let* ((tmpdir (if (featurep 'xemacs)
  13984. (temp-directory)
  13985. temporary-file-directory))
  13986. (tmpfile (make-temp-name
  13987. (expand-file-name "orgics" tmpdir)))
  13988. buf rtn b e)
  13989. (with-current-buffer frombuf
  13990. (icalendar-export-region (point-min) (point-max) tmpfile)
  13991. (setq buf (find-buffer-visiting tmpfile))
  13992. (set-buffer buf)
  13993. (goto-char (point-min))
  13994. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13995. (setq b (match-beginning 0)))
  13996. (goto-char (point-max))
  13997. (if (re-search-backward "^END:VEVENT" nil t)
  13998. (setq e (match-end 0)))
  13999. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14000. (kill-buffer buf)
  14001. (delete-file tmpfile)
  14002. rtn))
  14003. (defun org-closest-date (start current change prefer show-all)
  14004. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14005. When PREFER is `past', return a date that is either CURRENT or past.
  14006. When PREFER is `future', return a date that is either CURRENT or future.
  14007. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14008. ;; Make the proper lists from the dates
  14009. (catch 'exit
  14010. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14011. dn dw sday cday n1 n2 n0
  14012. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14013. (setq start (org-date-to-gregorian start)
  14014. current (org-date-to-gregorian
  14015. (if show-all
  14016. current
  14017. (time-to-days (current-time))))
  14018. sday (calendar-absolute-from-gregorian start)
  14019. cday (calendar-absolute-from-gregorian current))
  14020. (if (<= cday sday) (throw 'exit sday))
  14021. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14022. (setq dn (string-to-number (match-string 1 change))
  14023. dw (cdr (assoc (match-string 2 change) a1)))
  14024. (error "Invalid change specifier: %s" change))
  14025. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14026. (cond
  14027. ((eq dw 'day)
  14028. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14029. n2 (+ n1 dn)))
  14030. ((eq dw 'year)
  14031. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14032. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14033. (setq date1 (list m d y1)
  14034. n1 (calendar-absolute-from-gregorian date1)
  14035. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14036. n2 (calendar-absolute-from-gregorian date2)))
  14037. ((eq dw 'month)
  14038. ;; approx number of month between the two dates
  14039. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14040. ;; How often does dn fit in there?
  14041. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14042. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14043. m (+ m nm)
  14044. ny (floor (/ m 12))
  14045. y (+ y ny)
  14046. m (- m (* ny 12)))
  14047. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14048. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14049. (setq m2 (+ m dn) y2 y)
  14050. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14051. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14052. (while (<= n2 cday)
  14053. (setq n1 n2 m m2 y y2)
  14054. (setq m2 (+ m dn) y2 y)
  14055. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14056. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14057. ;; Make sure n1 is the earlier date
  14058. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14059. (if show-all
  14060. (cond
  14061. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14062. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14063. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14064. (cond
  14065. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14066. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14067. (t (if (= cday n1) n1 n2)))))))
  14068. (defun org-date-to-gregorian (date)
  14069. "Turn any specification of DATE into a Gregorian date for the calendar."
  14070. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14071. ((and (listp date) (= (length date) 3)) date)
  14072. ((stringp date)
  14073. (setq date (org-parse-time-string date))
  14074. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14075. ((listp date)
  14076. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14077. (defun org-parse-time-string (s &optional nodefault)
  14078. "Parse the standard Org-mode time string.
  14079. This should be a lot faster than the normal `parse-time-string'.
  14080. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14081. hour and minute fields will be nil if not given."
  14082. (if (string-match org-ts-regexp0 s)
  14083. (list 0
  14084. (if (or (match-beginning 8) (not nodefault))
  14085. (string-to-number (or (match-string 8 s) "0")))
  14086. (if (or (match-beginning 7) (not nodefault))
  14087. (string-to-number (or (match-string 7 s) "0")))
  14088. (string-to-number (match-string 4 s))
  14089. (string-to-number (match-string 3 s))
  14090. (string-to-number (match-string 2 s))
  14091. nil nil nil)
  14092. (error "Not a standard Org-mode time string: %s" s)))
  14093. (defun org-timestamp-up (&optional arg)
  14094. "Increase the date item at the cursor by one.
  14095. If the cursor is on the year, change the year. If it is on the month or
  14096. the day, change that.
  14097. With prefix ARG, change by that many units."
  14098. (interactive "p")
  14099. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14100. (defun org-timestamp-down (&optional arg)
  14101. "Decrease the date item at the cursor by one.
  14102. If the cursor is on the year, change the year. If it is on the month or
  14103. the day, change that.
  14104. With prefix ARG, change by that many units."
  14105. (interactive "p")
  14106. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14107. (defun org-timestamp-up-day (&optional arg)
  14108. "Increase the date in the time stamp by one day.
  14109. With prefix ARG, change that many days."
  14110. (interactive "p")
  14111. (if (and (not (org-at-timestamp-p t))
  14112. (org-on-heading-p))
  14113. (org-todo 'up)
  14114. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14115. (defun org-timestamp-down-day (&optional arg)
  14116. "Decrease the date in the time stamp by one day.
  14117. With prefix ARG, change that many days."
  14118. (interactive "p")
  14119. (if (and (not (org-at-timestamp-p t))
  14120. (org-on-heading-p))
  14121. (org-todo 'down)
  14122. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14123. (defun org-at-timestamp-p (&optional inactive-ok)
  14124. "Determine if the cursor is in or at a timestamp."
  14125. (interactive)
  14126. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14127. (pos (point))
  14128. (ans (or (looking-at tsr)
  14129. (save-excursion
  14130. (skip-chars-backward "^[<\n\r\t")
  14131. (if (> (point) (point-min)) (backward-char 1))
  14132. (and (looking-at tsr)
  14133. (> (- (match-end 0) pos) -1))))))
  14134. (and ans
  14135. (boundp 'org-ts-what)
  14136. (setq org-ts-what
  14137. (cond
  14138. ((= pos (match-beginning 0)) 'bracket)
  14139. ((= pos (1- (match-end 0))) 'bracket)
  14140. ((org-pos-in-match-range pos 2) 'year)
  14141. ((org-pos-in-match-range pos 3) 'month)
  14142. ((org-pos-in-match-range pos 7) 'hour)
  14143. ((org-pos-in-match-range pos 8) 'minute)
  14144. ((or (org-pos-in-match-range pos 4)
  14145. (org-pos-in-match-range pos 5)) 'day)
  14146. ((and (> pos (or (match-end 8) (match-end 5)))
  14147. (< pos (match-end 0)))
  14148. (- pos (or (match-end 8) (match-end 5))))
  14149. (t 'day))))
  14150. ans))
  14151. (defun org-toggle-timestamp-type ()
  14152. "Toggle the type (<active> or [inactive]) of a time stamp."
  14153. (interactive)
  14154. (when (org-at-timestamp-p t)
  14155. (let ((beg (match-beginning 0)) (end (match-end 0))
  14156. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14157. (save-excursion
  14158. (goto-char beg)
  14159. (while (re-search-forward "[][<>]" end t)
  14160. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14161. t t)))
  14162. (message "Timestamp is now %sactive"
  14163. (if (equal (char-after beg) ?<) "" "in")))))
  14164. (defun org-timestamp-change (n &optional what updown)
  14165. "Change the date in the time stamp at point.
  14166. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14167. `year', `minute', `second'. If WHAT is not given, the cursor position
  14168. in the timestamp determines what will be changed."
  14169. (let ((pos (point))
  14170. with-hm inactive
  14171. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14172. org-ts-what
  14173. extra rem
  14174. ts time time0)
  14175. (if (not (org-at-timestamp-p t))
  14176. (error "Not at a timestamp"))
  14177. (if (and (not what) (eq org-ts-what 'bracket))
  14178. (org-toggle-timestamp-type)
  14179. (if (and (not what) (not (eq org-ts-what 'day))
  14180. org-display-custom-times
  14181. (get-text-property (point) 'display)
  14182. (not (get-text-property (1- (point)) 'display)))
  14183. (setq org-ts-what 'day))
  14184. (setq org-ts-what (or what org-ts-what)
  14185. inactive (= (char-after (match-beginning 0)) ?\[)
  14186. ts (match-string 0))
  14187. (replace-match "")
  14188. (if (string-match
  14189. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14190. ts)
  14191. (setq extra (match-string 1 ts)))
  14192. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14193. (setq with-hm t))
  14194. (setq time0 (org-parse-time-string ts))
  14195. (when (and updown
  14196. (eq org-ts-what 'minute)
  14197. (not current-prefix-arg))
  14198. ;; This looks like s-up and s-down. Change by one rounding step.
  14199. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14200. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14201. (setcar (cdr time0) (+ (nth 1 time0)
  14202. (if (> n 0) (- rem) (- dm rem))))))
  14203. (setq time
  14204. (encode-time (or (car time0) 0)
  14205. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14206. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14207. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14208. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14209. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14210. (nthcdr 6 time0)))
  14211. (when (and (member org-ts-what '(hour minute))
  14212. extra
  14213. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14214. (setq extra (org-modify-ts-extra
  14215. extra
  14216. (if (eq org-ts-what 'hour) 2 5)
  14217. n dm)))
  14218. (when (integerp org-ts-what)
  14219. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14220. (if (eq what 'calendar)
  14221. (let ((cal-date (org-get-date-from-calendar)))
  14222. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14223. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14224. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14225. (setcar time0 (or (car time0) 0))
  14226. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14227. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14228. (setq time (apply 'encode-time time0))))
  14229. (setq org-last-changed-timestamp
  14230. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14231. (org-clock-update-time-maybe)
  14232. (goto-char pos)
  14233. ;; Try to recenter the calendar window, if any
  14234. (if (and org-calendar-follow-timestamp-change
  14235. (get-buffer-window "*Calendar*" t)
  14236. (memq org-ts-what '(day month year)))
  14237. (org-recenter-calendar (time-to-days time))))))
  14238. (defun org-modify-ts-extra (s pos n dm)
  14239. "Change the different parts of the lead-time and repeat fields in timestamp."
  14240. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14241. ng h m new rem)
  14242. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14243. (cond
  14244. ((or (org-pos-in-match-range pos 2)
  14245. (org-pos-in-match-range pos 3))
  14246. (setq m (string-to-number (match-string 3 s))
  14247. h (string-to-number (match-string 2 s)))
  14248. (if (org-pos-in-match-range pos 2)
  14249. (setq h (+ h n))
  14250. (setq n (* dm (org-no-warnings (signum n))))
  14251. (when (not (= 0 (setq rem (% m dm))))
  14252. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14253. (setq m (+ m n)))
  14254. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14255. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14256. (setq h (min 24 (max 0 h)))
  14257. (setq ng 1 new (format "-%02d:%02d" h m)))
  14258. ((org-pos-in-match-range pos 6)
  14259. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14260. ((org-pos-in-match-range pos 5)
  14261. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14262. ((org-pos-in-match-range pos 9)
  14263. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14264. ((org-pos-in-match-range pos 8)
  14265. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14266. (when ng
  14267. (setq s (concat
  14268. (substring s 0 (match-beginning ng))
  14269. new
  14270. (substring s (match-end ng))))))
  14271. s))
  14272. (defun org-recenter-calendar (date)
  14273. "If the calendar is visible, recenter it to DATE."
  14274. (let* ((win (selected-window))
  14275. (cwin (get-buffer-window "*Calendar*" t))
  14276. (calendar-move-hook nil))
  14277. (when cwin
  14278. (select-window cwin)
  14279. (calendar-goto-date (if (listp date) date
  14280. (calendar-gregorian-from-absolute date)))
  14281. (select-window win))))
  14282. (defun org-goto-calendar (&optional arg)
  14283. "Go to the Emacs calendar at the current date.
  14284. If there is a time stamp in the current line, go to that date.
  14285. A prefix ARG can be used to force the current date."
  14286. (interactive "P")
  14287. (let ((tsr org-ts-regexp) diff
  14288. (calendar-move-hook nil)
  14289. (calendar-view-holidays-initially-flag nil)
  14290. (calendar-view-diary-initially-flag nil))
  14291. (if (or (org-at-timestamp-p)
  14292. (save-excursion
  14293. (beginning-of-line 1)
  14294. (looking-at (concat ".*" tsr))))
  14295. (let ((d1 (time-to-days (current-time)))
  14296. (d2 (time-to-days
  14297. (org-time-string-to-time (match-string 1)))))
  14298. (setq diff (- d2 d1))))
  14299. (calendar)
  14300. (calendar-goto-today)
  14301. (if (and diff (not arg)) (calendar-forward-day diff))))
  14302. (defun org-get-date-from-calendar ()
  14303. "Return a list (month day year) of date at point in calendar."
  14304. (with-current-buffer "*Calendar*"
  14305. (save-match-data
  14306. (calendar-cursor-to-date))))
  14307. (defun org-date-from-calendar ()
  14308. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14309. If there is already a time stamp at the cursor position, update it."
  14310. (interactive)
  14311. (if (org-at-timestamp-p t)
  14312. (org-timestamp-change 0 'calendar)
  14313. (let ((cal-date (org-get-date-from-calendar)))
  14314. (org-insert-time-stamp
  14315. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14316. (defun org-minutes-to-hh:mm-string (m)
  14317. "Compute H:MM from a number of minutes."
  14318. (let ((h (/ m 60)))
  14319. (setq m (- m (* 60 h)))
  14320. (format org-time-clocksum-format h m)))
  14321. (defun org-hh:mm-string-to-minutes (s)
  14322. "Convert a string H:MM to a number of minutes.
  14323. If the string is just a number, interpret it as minutes.
  14324. In fact, the first hh:mm or number in the string will be taken,
  14325. there can be extra stuff in the string.
  14326. If no number is found, the return value is 0."
  14327. (cond
  14328. ((integerp s) s)
  14329. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14330. (+ (* (string-to-number (match-string 1 s)) 60)
  14331. (string-to-number (match-string 2 s))))
  14332. ((string-match "\\([0-9]+\\)" s)
  14333. (string-to-number (match-string 1 s)))
  14334. (t 0)))
  14335. (defcustom org-effort-durations
  14336. `(("h" . 60)
  14337. ("d" . ,(* 60 8))
  14338. ("w" . ,(* 60 8 5))
  14339. ("m" . ,(* 60 8 5 4))
  14340. ("y" . ,(* 60 8 5 40)))
  14341. "Conversion factor to minutes for an effort modifier.
  14342. Each entry has the form (MODIFIER . MINUTES).
  14343. In an effort string, a number followed by MODIFIER is multiplied
  14344. by the specified number of MINUTES to obtain an effort in
  14345. minutes.
  14346. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14347. effort string \"2hours\" is equivalent to 120 minutes."
  14348. :group 'org-agenda
  14349. :type '(alist :key-type (string :tag "Modifier")
  14350. :value-type (number :tag "Minutes")))
  14351. (defun org-duration-string-to-minutes (s)
  14352. "Convert a duration string S to minutes.
  14353. A bare number is interpreted as minutes, modifiers can be set by
  14354. customizing `org-effort-durations' (which see).
  14355. Entries containing a colon are interpreted as H:MM by
  14356. `org-hh:mm-string-to-minutes'."
  14357. (let ((result 0)
  14358. (re (concat "\\([0-9]+\\) *\\("
  14359. (regexp-opt (mapcar 'car org-effort-durations))
  14360. "\\)")))
  14361. (while (string-match re s)
  14362. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14363. (string-to-number (match-string 1 s))))
  14364. (setq s (replace-match "" nil t s)))
  14365. (incf result (org-hh:mm-string-to-minutes s))
  14366. result))
  14367. ;;;; Files
  14368. (defun org-save-all-org-buffers ()
  14369. "Save all Org-mode buffers without user confirmation."
  14370. (interactive)
  14371. (message "Saving all Org-mode buffers...")
  14372. (save-some-buffers t 'org-mode-p)
  14373. (when (featurep 'org-id) (org-id-locations-save))
  14374. (message "Saving all Org-mode buffers... done"))
  14375. (defun org-revert-all-org-buffers ()
  14376. "Revert all Org-mode buffers.
  14377. Prompt for confirmation when there are unsaved changes.
  14378. Be sure you know what you are doing before letting this function
  14379. overwrite your changes.
  14380. This function is useful in a setup where one tracks org files
  14381. with a version control system, to revert on one machine after pulling
  14382. changes from another. I believe the procedure must be like this:
  14383. 1. M-x org-save-all-org-buffers
  14384. 2. Pull changes from the other machine, resolve conflicts
  14385. 3. M-x org-revert-all-org-buffers"
  14386. (interactive)
  14387. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14388. (error "Abort"))
  14389. (save-excursion
  14390. (save-window-excursion
  14391. (mapc
  14392. (lambda (b)
  14393. (when (and (with-current-buffer b (org-mode-p))
  14394. (with-current-buffer b buffer-file-name))
  14395. (switch-to-buffer b)
  14396. (revert-buffer t 'no-confirm)))
  14397. (buffer-list))
  14398. (when (and (featurep 'org-id) org-id-track-globally)
  14399. (org-id-locations-load)))))
  14400. ;;;; Agenda files
  14401. ;;;###autoload
  14402. (defun org-switchb (&optional arg)
  14403. "Switch between Org buffers.
  14404. With a prefix argument, restrict available to files.
  14405. With two prefix arguments, restrict available buffers to agenda files.
  14406. Defaults to `iswitchb' for buffer name completion.
  14407. Set `org-completion-use-ido' to make it use ido instead."
  14408. (interactive "P")
  14409. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14410. ((equal arg '(16)) (org-buffer-list 'agenda))
  14411. (t (org-buffer-list))))
  14412. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14413. (org-completion-use-ido org-completion-use-ido))
  14414. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14415. (setq org-completion-use-iswitchb t))
  14416. (switch-to-buffer
  14417. (org-icompleting-read "Org buffer: "
  14418. (mapcar 'list (mapcar 'buffer-name blist))
  14419. nil t))))
  14420. ;;; Define some older names previously used for this functionality
  14421. ;;;###autoload
  14422. (defalias 'org-ido-switchb 'org-switchb)
  14423. ;;;###autoload
  14424. (defalias 'org-iswitchb 'org-switchb)
  14425. (defun org-buffer-list (&optional predicate exclude-tmp)
  14426. "Return a list of Org buffers.
  14427. PREDICATE can be `export', `files' or `agenda'.
  14428. export restrict the list to Export buffers.
  14429. files restrict the list to buffers visiting Org files.
  14430. agenda restrict the list to buffers visiting agenda files.
  14431. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14432. (let* ((bfn nil)
  14433. (agenda-files (and (eq predicate 'agenda)
  14434. (mapcar 'file-truename (org-agenda-files t))))
  14435. (filter
  14436. (cond
  14437. ((eq predicate 'files)
  14438. (lambda (b) (with-current-buffer b (org-mode-p))))
  14439. ((eq predicate 'export)
  14440. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14441. ((eq predicate 'agenda)
  14442. (lambda (b)
  14443. (with-current-buffer b
  14444. (and (org-mode-p)
  14445. (setq bfn (buffer-file-name b))
  14446. (member (file-truename bfn) agenda-files)))))
  14447. (t (lambda (b) (with-current-buffer b
  14448. (or (org-mode-p)
  14449. (string-match "\*Org .*Export"
  14450. (buffer-name b)))))))))
  14451. (delq nil
  14452. (mapcar
  14453. (lambda(b)
  14454. (if (and (funcall filter b)
  14455. (or (not exclude-tmp)
  14456. (not (string-match "tmp" (buffer-name b)))))
  14457. b
  14458. nil))
  14459. (buffer-list)))))
  14460. (defun org-agenda-files (&optional unrestricted archives)
  14461. "Get the list of agenda files.
  14462. Optional UNRESTRICTED means return the full list even if a restriction
  14463. is currently in place.
  14464. When ARCHIVES is t, include all archive files that are really being
  14465. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14466. `org-agenda-archives-mode' is t."
  14467. (let ((files
  14468. (cond
  14469. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14470. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14471. ((listp org-agenda-files) org-agenda-files)
  14472. (t (error "Invalid value of `org-agenda-files'")))))
  14473. (setq files (apply 'append
  14474. (mapcar (lambda (f)
  14475. (if (file-directory-p f)
  14476. (directory-files
  14477. f t org-agenda-file-regexp)
  14478. (list f)))
  14479. files)))
  14480. (when org-agenda-skip-unavailable-files
  14481. (setq files (delq nil
  14482. (mapcar (function
  14483. (lambda (file)
  14484. (and (file-readable-p file) file)))
  14485. files))))
  14486. (when (or (eq archives t)
  14487. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14488. (setq files (org-add-archive-files files)))
  14489. files))
  14490. (defun org-agenda-file-p (&optional file)
  14491. "Return non-nil, if FILE is an agenda file.
  14492. If FILE is omitted, use the file associated with the current
  14493. buffer."
  14494. (member (or file (buffer-file-name))
  14495. (org-agenda-files t)))
  14496. (defun org-edit-agenda-file-list ()
  14497. "Edit the list of agenda files.
  14498. Depending on setup, this either uses customize to edit the variable
  14499. `org-agenda-files', or it visits the file that is holding the list. In the
  14500. latter case, the buffer is set up in a way that saving it automatically kills
  14501. the buffer and restores the previous window configuration."
  14502. (interactive)
  14503. (if (stringp org-agenda-files)
  14504. (let ((cw (current-window-configuration)))
  14505. (find-file org-agenda-files)
  14506. (org-set-local 'org-window-configuration cw)
  14507. (org-add-hook 'after-save-hook
  14508. (lambda ()
  14509. (set-window-configuration
  14510. (prog1 org-window-configuration
  14511. (kill-buffer (current-buffer))))
  14512. (org-install-agenda-files-menu)
  14513. (message "New agenda file list installed"))
  14514. nil 'local)
  14515. (message "%s" (substitute-command-keys
  14516. "Edit list and finish with \\[save-buffer]")))
  14517. (customize-variable 'org-agenda-files)))
  14518. (defun org-store-new-agenda-file-list (list)
  14519. "Set new value for the agenda file list and save it correctly."
  14520. (if (stringp org-agenda-files)
  14521. (let ((fe (org-read-agenda-file-list t)) b u)
  14522. (while (setq b (find-buffer-visiting org-agenda-files))
  14523. (kill-buffer b))
  14524. (with-temp-file org-agenda-files
  14525. (insert
  14526. (mapconcat
  14527. (lambda (f) ;; Keep un-expanded entries.
  14528. (if (setq u (assoc f fe))
  14529. (cdr u)
  14530. f))
  14531. list "\n")
  14532. "\n")))
  14533. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14534. (org-insert-mode-line-in-empty-file nil))
  14535. (setq org-agenda-files list)
  14536. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14537. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14538. "Read the list of agenda files from a file.
  14539. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14540. filenames, used by `org-store-new-agenda-file-list' to write back
  14541. un-expanded file names."
  14542. (when (file-directory-p org-agenda-files)
  14543. (error "`org-agenda-files' cannot be a single directory"))
  14544. (when (stringp org-agenda-files)
  14545. (with-temp-buffer
  14546. (insert-file-contents org-agenda-files)
  14547. (mapcar
  14548. (lambda (f)
  14549. (let ((e (expand-file-name (substitute-in-file-name f)
  14550. org-directory)))
  14551. (if pair-with-expansion
  14552. (cons e f)
  14553. e)))
  14554. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14555. ;;;###autoload
  14556. (defun org-cycle-agenda-files ()
  14557. "Cycle through the files in `org-agenda-files'.
  14558. If the current buffer visits an agenda file, find the next one in the list.
  14559. If the current buffer does not, find the first agenda file."
  14560. (interactive)
  14561. (let* ((fs (org-agenda-files t))
  14562. (files (append fs (list (car fs))))
  14563. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14564. file)
  14565. (unless files (error "No agenda files"))
  14566. (catch 'exit
  14567. (while (setq file (pop files))
  14568. (if (equal (file-truename file) tcf)
  14569. (when (car files)
  14570. (find-file (car files))
  14571. (throw 'exit t))))
  14572. (find-file (car fs)))
  14573. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14574. (defun org-agenda-file-to-front (&optional to-end)
  14575. "Move/add the current file to the top of the agenda file list.
  14576. If the file is not present in the list, it is added to the front. If it is
  14577. present, it is moved there. With optional argument TO-END, add/move to the
  14578. end of the list."
  14579. (interactive "P")
  14580. (let ((org-agenda-skip-unavailable-files nil)
  14581. (file-alist (mapcar (lambda (x)
  14582. (cons (file-truename x) x))
  14583. (org-agenda-files t)))
  14584. (ctf (file-truename buffer-file-name))
  14585. x had)
  14586. (setq x (assoc ctf file-alist) had x)
  14587. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14588. (if to-end
  14589. (setq file-alist (append (delq x file-alist) (list x)))
  14590. (setq file-alist (cons x (delq x file-alist))))
  14591. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14592. (org-install-agenda-files-menu)
  14593. (message "File %s to %s of agenda file list"
  14594. (if had "moved" "added") (if to-end "end" "front"))))
  14595. (defun org-remove-file (&optional file)
  14596. "Remove current file from the list of files in variable `org-agenda-files'.
  14597. These are the files which are being checked for agenda entries.
  14598. Optional argument FILE means use this file instead of the current."
  14599. (interactive)
  14600. (let* ((org-agenda-skip-unavailable-files nil)
  14601. (file (or file buffer-file-name))
  14602. (true-file (file-truename file))
  14603. (afile (abbreviate-file-name file))
  14604. (files (delq nil (mapcar
  14605. (lambda (x)
  14606. (if (equal true-file
  14607. (file-truename x))
  14608. nil x))
  14609. (org-agenda-files t)))))
  14610. (if (not (= (length files) (length (org-agenda-files t))))
  14611. (progn
  14612. (org-store-new-agenda-file-list files)
  14613. (org-install-agenda-files-menu)
  14614. (message "Removed file: %s" afile))
  14615. (message "File was not in list: %s (not removed)" afile))))
  14616. (defun org-file-menu-entry (file)
  14617. (vector file (list 'find-file file) t))
  14618. (defun org-check-agenda-file (file)
  14619. "Make sure FILE exists. If not, ask user what to do."
  14620. (when (not (file-exists-p file))
  14621. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14622. (abbreviate-file-name file))
  14623. (let ((r (downcase (read-char-exclusive))))
  14624. (cond
  14625. ((equal r ?r)
  14626. (org-remove-file file)
  14627. (throw 'nextfile t))
  14628. (t (error "Abort"))))))
  14629. (defun org-get-agenda-file-buffer (file)
  14630. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14631. it to the list of buffers which might be released later."
  14632. (let ((buf (org-find-base-buffer-visiting file)))
  14633. (if buf
  14634. buf ; just return it
  14635. ;; Make a new buffer and remember it
  14636. (setq buf (find-file-noselect file))
  14637. (if buf (push buf org-agenda-new-buffers))
  14638. buf)))
  14639. (defun org-release-buffers (blist)
  14640. "Release all buffers in list, asking the user for confirmation when needed.
  14641. When a buffer is unmodified, it is just killed. When modified, it is saved
  14642. \(if the user agrees) and then killed."
  14643. (let (buf file)
  14644. (while (setq buf (pop blist))
  14645. (setq file (buffer-file-name buf))
  14646. (when (and (buffer-modified-p buf)
  14647. file
  14648. (y-or-n-p (format "Save file %s? " file)))
  14649. (with-current-buffer buf (save-buffer)))
  14650. (kill-buffer buf))))
  14651. (defun org-prepare-agenda-buffers (files)
  14652. "Create buffers for all agenda files, protect archived trees and comments."
  14653. (interactive)
  14654. (let ((pa '(:org-archived t))
  14655. (pc '(:org-comment t))
  14656. (pall '(:org-archived t :org-comment t))
  14657. (inhibit-read-only t)
  14658. (rea (concat ":" org-archive-tag ":"))
  14659. bmp file re)
  14660. (save-excursion
  14661. (save-restriction
  14662. (while (setq file (pop files))
  14663. (catch 'nextfile
  14664. (if (bufferp file)
  14665. (set-buffer file)
  14666. (org-check-agenda-file file)
  14667. (set-buffer (org-get-agenda-file-buffer file)))
  14668. (widen)
  14669. (setq bmp (buffer-modified-p))
  14670. (org-refresh-category-properties)
  14671. (setq org-todo-keywords-for-agenda
  14672. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14673. (setq org-done-keywords-for-agenda
  14674. (append org-done-keywords-for-agenda org-done-keywords))
  14675. (setq org-todo-keyword-alist-for-agenda
  14676. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14677. (setq org-drawers-for-agenda
  14678. (append org-drawers-for-agenda org-drawers))
  14679. (setq org-tag-alist-for-agenda
  14680. (append org-tag-alist-for-agenda org-tag-alist))
  14681. (save-excursion
  14682. (remove-text-properties (point-min) (point-max) pall)
  14683. (when org-agenda-skip-archived-trees
  14684. (goto-char (point-min))
  14685. (while (re-search-forward rea nil t)
  14686. (if (org-on-heading-p t)
  14687. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14688. (goto-char (point-min))
  14689. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14690. (while (re-search-forward re nil t)
  14691. (add-text-properties
  14692. (match-beginning 0) (org-end-of-subtree t) pc)))
  14693. (set-buffer-modified-p bmp)))))
  14694. (setq org-todo-keywords-for-agenda
  14695. (org-uniquify org-todo-keywords-for-agenda))
  14696. (setq org-todo-keyword-alist-for-agenda
  14697. (org-uniquify org-todo-keyword-alist-for-agenda)
  14698. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14699. ;;;; Embedded LaTeX
  14700. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14701. "Keymap for the minor `org-cdlatex-mode'.")
  14702. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14703. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14704. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14705. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14706. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14707. (defvar org-cdlatex-texmathp-advice-is-done nil
  14708. "Flag remembering if we have applied the advice to texmathp already.")
  14709. (define-minor-mode org-cdlatex-mode
  14710. "Toggle the minor `org-cdlatex-mode'.
  14711. This mode supports entering LaTeX environment and math in LaTeX fragments
  14712. in Org-mode.
  14713. \\{org-cdlatex-mode-map}"
  14714. nil " OCDL" nil
  14715. (when org-cdlatex-mode (require 'cdlatex))
  14716. (unless org-cdlatex-texmathp-advice-is-done
  14717. (setq org-cdlatex-texmathp-advice-is-done t)
  14718. (defadvice texmathp (around org-math-always-on activate)
  14719. "Always return t in org-mode buffers.
  14720. This is because we want to insert math symbols without dollars even outside
  14721. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14722. an embedded LaTeX fragment, let texmathp do its job.
  14723. \\[org-cdlatex-mode-map]"
  14724. (interactive)
  14725. (let (p)
  14726. (cond
  14727. ((not (org-mode-p)) ad-do-it)
  14728. ((eq this-command 'cdlatex-math-symbol)
  14729. (setq ad-return-value t
  14730. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14731. (t
  14732. (let ((p (org-inside-LaTeX-fragment-p)))
  14733. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14734. (setq ad-return-value t
  14735. texmathp-why '("Org-mode embedded math" . 0))
  14736. (if p ad-do-it)))))))))
  14737. (defun turn-on-org-cdlatex ()
  14738. "Unconditionally turn on `org-cdlatex-mode'."
  14739. (org-cdlatex-mode 1))
  14740. (defun org-inside-LaTeX-fragment-p ()
  14741. "Test if point is inside a LaTeX fragment.
  14742. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14743. sequence appearing also before point.
  14744. Even though the matchers for math are configurable, this function assumes
  14745. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14746. delimiters are skipped when they have been removed by customization.
  14747. The return value is nil, or a cons cell with the delimiter and
  14748. and the position of this delimiter.
  14749. This function does a reasonably good job, but can locally be fooled by
  14750. for example currency specifications. For example it will assume being in
  14751. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14752. fragments that are properly closed, but during editing, we have to live
  14753. with the uncertainty caused by missing closing delimiters. This function
  14754. looks only before point, not after."
  14755. (catch 'exit
  14756. (let ((pos (point))
  14757. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14758. (lim (progn
  14759. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14760. (point)))
  14761. dd-on str (start 0) m re)
  14762. (goto-char pos)
  14763. (when dodollar
  14764. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14765. re (nth 1 (assoc "$" org-latex-regexps)))
  14766. (while (string-match re str start)
  14767. (cond
  14768. ((= (match-end 0) (length str))
  14769. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14770. ((= (match-end 0) (- (length str) 5))
  14771. (throw 'exit nil))
  14772. (t (setq start (match-end 0))))))
  14773. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14774. (goto-char pos)
  14775. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14776. (and (match-beginning 2) (throw 'exit nil))
  14777. ;; count $$
  14778. (while (re-search-backward "\\$\\$" lim t)
  14779. (setq dd-on (not dd-on)))
  14780. (goto-char pos)
  14781. (if dd-on (cons "$$" m))))))
  14782. (defun org-inside-latex-macro-p ()
  14783. "Is point inside a LaTeX macro or its arguments?"
  14784. (save-match-data
  14785. (org-in-regexp
  14786. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14787. (defun org-try-cdlatex-tab ()
  14788. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14789. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14790. - inside a LaTeX fragment, or
  14791. - after the first word in a line, where an abbreviation expansion could
  14792. insert a LaTeX environment."
  14793. (when org-cdlatex-mode
  14794. (cond
  14795. ((save-excursion
  14796. (skip-chars-backward "a-zA-Z0-9*")
  14797. (skip-chars-backward " \t")
  14798. (bolp))
  14799. (cdlatex-tab) t)
  14800. ((org-inside-LaTeX-fragment-p)
  14801. (cdlatex-tab) t)
  14802. (t nil))))
  14803. (defun org-cdlatex-underscore-caret (&optional arg)
  14804. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14805. Revert to the normal definition outside of these fragments."
  14806. (interactive "P")
  14807. (if (org-inside-LaTeX-fragment-p)
  14808. (call-interactively 'cdlatex-sub-superscript)
  14809. (let (org-cdlatex-mode)
  14810. (call-interactively (key-binding (vector last-input-event))))))
  14811. (defun org-cdlatex-math-modify (&optional arg)
  14812. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14813. Revert to the normal definition outside of these fragments."
  14814. (interactive "P")
  14815. (if (org-inside-LaTeX-fragment-p)
  14816. (call-interactively 'cdlatex-math-modify)
  14817. (let (org-cdlatex-mode)
  14818. (call-interactively (key-binding (vector last-input-event))))))
  14819. (defvar org-latex-fragment-image-overlays nil
  14820. "List of overlays carrying the images of latex fragments.")
  14821. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14822. (defun org-remove-latex-fragment-image-overlays ()
  14823. "Remove all overlays with LaTeX fragment images in current buffer."
  14824. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14825. (setq org-latex-fragment-image-overlays nil))
  14826. (defun org-preview-latex-fragment (&optional subtree)
  14827. "Preview the LaTeX fragment at point, or all locally or globally.
  14828. If the cursor is in a LaTeX fragment, create the image and overlay
  14829. it over the source code. If there is no fragment at point, display
  14830. all fragments in the current text, from one headline to the next. With
  14831. prefix SUBTREE, display all fragments in the current subtree. With a
  14832. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14833. the cursor is before the first headline,
  14834. display all fragments in the buffer.
  14835. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14836. (interactive "P")
  14837. (org-remove-latex-fragment-image-overlays)
  14838. (save-excursion
  14839. (save-restriction
  14840. (let (beg end at msg)
  14841. (cond
  14842. ((or (equal subtree '(16))
  14843. (not (save-excursion
  14844. (re-search-backward (concat "^" outline-regexp) nil t))))
  14845. (setq beg (point-min) end (point-max)
  14846. msg "Creating images for buffer...%s"))
  14847. ((equal subtree '(4))
  14848. (org-back-to-heading)
  14849. (setq beg (point) end (org-end-of-subtree t)
  14850. msg "Creating images for subtree...%s"))
  14851. (t
  14852. (if (setq at (org-inside-LaTeX-fragment-p))
  14853. (goto-char (max (point-min) (- (cdr at) 2)))
  14854. (org-back-to-heading))
  14855. (setq beg (point) end (progn (outline-next-heading) (point))
  14856. msg (if at "Creating image...%s"
  14857. "Creating images for entry...%s"))))
  14858. (message msg "")
  14859. (narrow-to-region beg end)
  14860. (goto-char beg)
  14861. (org-format-latex
  14862. (concat "ltxpng/" (file-name-sans-extension
  14863. (file-name-nondirectory
  14864. buffer-file-name)))
  14865. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14866. (message msg "done. Use `C-c C-c' to remove images.")))))
  14867. (defvar org-latex-regexps
  14868. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14869. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14870. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14871. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14872. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14873. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14874. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14875. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14876. "Regular expressions for matching embedded LaTeX.")
  14877. (defvar org-export-have-math nil) ;; dynamic scoping
  14878. (defun org-format-latex (prefix &optional dir overlays msg at
  14879. forbuffer processing-type)
  14880. "Replace LaTeX fragments with links to an image, and produce images.
  14881. Some of the options can be changed using the variable
  14882. `org-format-latex-options'."
  14883. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14884. (let* ((prefixnodir (file-name-nondirectory prefix))
  14885. (absprefix (expand-file-name prefix dir))
  14886. (todir (file-name-directory absprefix))
  14887. (opt org-format-latex-options)
  14888. (matchers (plist-get opt :matchers))
  14889. (re-list org-latex-regexps)
  14890. (org-format-latex-header-extra
  14891. (plist-get (org-infile-export-plist) :latex-header-extra))
  14892. (cnt 0) txt hash link beg end re e checkdir
  14893. executables-checked string
  14894. m n block linkfile movefile ov)
  14895. ;; Check the different regular expressions
  14896. (while (setq e (pop re-list))
  14897. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14898. block (if (nth 3 e) "\n\n" ""))
  14899. (when (member m matchers)
  14900. (goto-char (point-min))
  14901. (while (re-search-forward re nil t)
  14902. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14903. (not (get-text-property (match-beginning n)
  14904. 'org-protected))
  14905. (or (not overlays)
  14906. (not (eq (get-char-property (match-beginning n)
  14907. 'org-overlay-type)
  14908. 'org-latex-overlay))))
  14909. (setq org-export-have-math t)
  14910. (cond
  14911. ((eq processing-type 'verbatim)
  14912. ;; Leave the text verbatim, just protect it
  14913. (add-text-properties (match-beginning n) (match-end n)
  14914. '(org-protected t)))
  14915. ((eq processing-type 'mathjax)
  14916. ;; Prepare for MathJax processing
  14917. (setq string (match-string n))
  14918. (if (member m '("$" "$1"))
  14919. (save-excursion
  14920. (delete-region (match-beginning n) (match-end n))
  14921. (goto-char (match-beginning n))
  14922. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14923. "\\)")
  14924. '(org-protected t))))
  14925. (add-text-properties (match-beginning n) (match-end n)
  14926. '(org-protected t))))
  14927. ((or (eq processing-type 'dvipng) t)
  14928. ;; Process to an image
  14929. (setq txt (match-string n)
  14930. beg (match-beginning n) end (match-end n)
  14931. cnt (1+ cnt))
  14932. (let (print-length print-level) ; make sure full list is printed
  14933. (setq hash (sha1 (prin1-to-string
  14934. (list org-format-latex-header
  14935. org-format-latex-header-extra
  14936. org-export-latex-default-packages-alist
  14937. org-export-latex-packages-alist
  14938. org-format-latex-options
  14939. forbuffer txt)))
  14940. linkfile (format "%s_%s.png" prefix hash)
  14941. movefile (format "%s_%s.png" absprefix hash)))
  14942. (setq link (concat block "[[file:" linkfile "]]" block))
  14943. (if msg (message msg cnt))
  14944. (goto-char beg)
  14945. (unless checkdir ; make sure the directory exists
  14946. (setq checkdir t)
  14947. (or (file-directory-p todir) (make-directory todir t)))
  14948. (unless executables-checked
  14949. (org-check-external-command
  14950. "latex" "needed to convert LaTeX fragments to images")
  14951. (org-check-external-command
  14952. "dvipng" "needed to convert LaTeX fragments to images")
  14953. (setq executables-checked t))
  14954. (unless (file-exists-p movefile)
  14955. (org-create-formula-image
  14956. txt movefile opt forbuffer))
  14957. (if overlays
  14958. (progn
  14959. (mapc (lambda (o)
  14960. (if (eq (overlay-get o 'org-overlay-type)
  14961. 'org-latex-overlay)
  14962. (delete-overlay o)))
  14963. (overlays-in beg end))
  14964. (setq ov (make-overlay beg end))
  14965. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14966. (if (featurep 'xemacs)
  14967. (progn
  14968. (overlay-put ov 'invisible t)
  14969. (overlay-put
  14970. ov 'end-glyph
  14971. (make-glyph (vector 'png :file movefile))))
  14972. (overlay-put
  14973. ov 'display
  14974. (list 'image :type 'png :file movefile :ascent 'center)))
  14975. (push ov org-latex-fragment-image-overlays)
  14976. (goto-char end))
  14977. (delete-region beg end)
  14978. (insert (org-add-props link
  14979. (list 'org-latex-src
  14980. (replace-regexp-in-string
  14981. "\"" "" txt)))))))))))))
  14982. ;; This function borrows from Ganesh Swami's latex2png.el
  14983. (defun org-create-formula-image (string tofile options buffer)
  14984. "This calls dvipng."
  14985. (require 'org-latex)
  14986. (let* ((tmpdir (if (featurep 'xemacs)
  14987. (temp-directory)
  14988. temporary-file-directory))
  14989. (texfilebase (make-temp-name
  14990. (expand-file-name "orgtex" tmpdir)))
  14991. (texfile (concat texfilebase ".tex"))
  14992. (dvifile (concat texfilebase ".dvi"))
  14993. (pngfile (concat texfilebase ".png"))
  14994. (fnh (if (featurep 'xemacs)
  14995. (font-height (get-face-font 'default))
  14996. (face-attribute 'default :height nil)))
  14997. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14998. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14999. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15000. "Black"))
  15001. (bg (or (plist-get options (if buffer :background :html-background))
  15002. "Transparent")))
  15003. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15004. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15005. (with-temp-file texfile
  15006. (insert (org-splice-latex-header
  15007. org-format-latex-header
  15008. org-export-latex-default-packages-alist
  15009. org-export-latex-packages-alist t
  15010. org-format-latex-header-extra))
  15011. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15012. (require 'org-latex)
  15013. (org-export-latex-fix-inputenc))
  15014. (let ((dir default-directory))
  15015. (condition-case nil
  15016. (progn
  15017. (cd tmpdir)
  15018. (call-process "latex" nil nil nil texfile))
  15019. (error nil))
  15020. (cd dir))
  15021. (if (not (file-exists-p dvifile))
  15022. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15023. (condition-case nil
  15024. (call-process "dvipng" nil nil nil
  15025. "-fg" fg "-bg" bg
  15026. "-D" dpi
  15027. ;;"-x" scale "-y" scale
  15028. "-T" "tight"
  15029. "-o" pngfile
  15030. dvifile)
  15031. (error nil))
  15032. (if (not (file-exists-p pngfile))
  15033. (if org-format-latex-signal-error
  15034. (error "Failed to create png file from %s" texfile)
  15035. (message "Failed to create png file from %s" texfile)
  15036. nil)
  15037. ;; Use the requested file name and clean up
  15038. (copy-file pngfile tofile 'replace)
  15039. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15040. (delete-file (concat texfilebase e)))
  15041. pngfile))))
  15042. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15043. "Fill a LaTeX header template TPL.
  15044. In the template, the following place holders will be recognized:
  15045. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15046. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15047. [PACKAGES] \\usepackage statements for PKG
  15048. [NO-PACKAGES] do not include PKG
  15049. [EXTRA] the string EXTRA
  15050. [NO-EXTRA] do not include EXTRA
  15051. For backward compatibility, if both the positive and the negative place
  15052. holder is missing, the positive one (without the \"NO-\") will be
  15053. assumed to be present at the end of the template.
  15054. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15055. EXTRA is a string.
  15056. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15057. (let (rpl (end ""))
  15058. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15059. (setq rpl (if (or (match-end 1) (not def-pkg))
  15060. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15061. tpl (replace-match rpl t t tpl))
  15062. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15063. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15064. (setq rpl (if (or (match-end 1) (not pkg))
  15065. "" (org-latex-packages-to-string pkg snippets-p t))
  15066. tpl (replace-match rpl t t tpl))
  15067. (if pkg (setq end
  15068. (concat end "\n"
  15069. (org-latex-packages-to-string pkg snippets-p)))))
  15070. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15071. (setq rpl (if (or (match-end 1) (not extra))
  15072. "" (concat extra "\n"))
  15073. tpl (replace-match rpl t t tpl))
  15074. (if (and extra (string-match "\\S-" extra))
  15075. (setq end (concat end "\n" extra))))
  15076. (if (string-match "\\S-" end)
  15077. (concat tpl "\n" end)
  15078. tpl)))
  15079. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15080. "Turn an alist of packages into a string with the \\usepackage macros."
  15081. (setq pkg (mapconcat (lambda(p)
  15082. (cond
  15083. ((stringp p) p)
  15084. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15085. (format "%% Package %s omitted" (cadr p)))
  15086. ((equal "" (car p))
  15087. (format "\\usepackage{%s}" (cadr p)))
  15088. (t
  15089. (format "\\usepackage[%s]{%s}"
  15090. (car p) (cadr p)))))
  15091. pkg
  15092. "\n"))
  15093. (if newline (concat pkg "\n") pkg))
  15094. (defun org-dvipng-color (attr)
  15095. "Return an rgb color specification for dvipng."
  15096. (apply 'format "rgb %s %s %s"
  15097. (mapcar 'org-normalize-color
  15098. (color-values (face-attribute 'default attr nil)))))
  15099. (defun org-normalize-color (value)
  15100. "Return string to be used as color value for an RGB component."
  15101. (format "%g" (/ value 65535.0)))
  15102. ;; Image display
  15103. (defvar org-inline-image-overlays nil)
  15104. (make-variable-buffer-local 'org-inline-image-overlays)
  15105. (defun org-toggle-inline-images (&optional include-linked)
  15106. "Toggle the display of inline images.
  15107. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15108. (interactive "P")
  15109. (if org-inline-image-overlays
  15110. (progn
  15111. (org-remove-inline-images)
  15112. (message "Inline image display turned off"))
  15113. (org-display-inline-images include-linked)
  15114. (if org-inline-image-overlays
  15115. (message "%d images displayed inline"
  15116. (length org-inline-image-overlays))
  15117. (message "No images to display inline"))))
  15118. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15119. "Display inline images.
  15120. Normally only links without a description part are inlined, because this
  15121. is how it will work for export. When INCLUDE-LINKED is set, also links
  15122. with a description part will be inlined. This can be nice for a quick
  15123. look at those images, but it does not reflect what exported files will look
  15124. like.
  15125. When REFRESH is set, refresh existing images between BEG and END.
  15126. This will create new image displays only if necessary.
  15127. BEG and END default to the buffer boundaries."
  15128. (interactive "P")
  15129. (unless refresh
  15130. (org-remove-inline-images)
  15131. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15132. (save-excursion
  15133. (save-restriction
  15134. (widen)
  15135. (setq beg (or beg (point-min)) end (or end (point-max)))
  15136. (goto-char (point-min))
  15137. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15138. (substring (org-image-file-name-regexp) 0 -2)
  15139. "\\)\\]" (if include-linked "" "\\]")))
  15140. old file ov img)
  15141. (while (re-search-forward re end t)
  15142. (setq old (get-char-property-and-overlay (match-beginning 1)
  15143. 'org-image-overlay))
  15144. (setq file (expand-file-name
  15145. (concat (or (match-string 3) "") (match-string 4))))
  15146. (when (file-exists-p file)
  15147. (if (and (car-safe old) refresh)
  15148. (image-refresh (overlay-get (cdr old) 'display))
  15149. (setq img (save-match-data (create-image file)))
  15150. (when img
  15151. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15152. (overlay-put ov 'display img)
  15153. (overlay-put ov 'face 'default)
  15154. (overlay-put ov 'org-image-overlay t)
  15155. (overlay-put ov 'modification-hooks
  15156. (list 'org-display-inline-modification-hook))
  15157. (push ov org-inline-image-overlays)))))))))
  15158. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15159. "Remove inline-display overlay if a corresponding region is modified."
  15160. (let ((inhibit-modification-hooks t))
  15161. (when (and ov after)
  15162. (delete ov org-inline-image-overlays)
  15163. (delete-overlay ov))))
  15164. (defun org-remove-inline-images ()
  15165. "Remove inline display of images."
  15166. (interactive)
  15167. (mapc 'delete-overlay org-inline-image-overlays)
  15168. (setq org-inline-image-overlays nil))
  15169. ;;;; Key bindings
  15170. ;; Make `C-c C-x' a prefix key
  15171. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15172. ;; TAB key with modifiers
  15173. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15174. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15175. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15176. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15177. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15178. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15179. ;; The following line is necessary under Suse GNU/Linux
  15180. (unless (featurep 'xemacs)
  15181. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15182. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15183. (define-key org-mode-map [backtab] 'org-shifttab)
  15184. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15185. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15186. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15187. ;; Cursor keys with modifiers
  15188. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15189. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15190. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15191. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15192. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15193. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15194. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15195. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15196. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15197. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15198. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15199. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15200. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15201. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15202. ;; Babel keys
  15203. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15204. (mapc (lambda (pair)
  15205. (define-key org-babel-map (car pair) (cdr pair)))
  15206. org-babel-key-bindings)
  15207. ;;; Extra keys for tty access.
  15208. ;; We only set them when really needed because otherwise the
  15209. ;; menus don't show the simple keys
  15210. (when (or org-use-extra-keys
  15211. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15212. (not window-system))
  15213. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15214. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15215. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15216. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15217. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15218. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15219. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15220. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15221. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15222. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15223. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15224. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15225. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15226. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15227. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15228. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15229. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15230. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15231. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15232. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15233. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15234. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15235. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15236. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15237. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15238. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15239. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15240. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15241. ;; All the other keys
  15242. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15243. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15244. (if (boundp 'narrow-map)
  15245. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15246. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15247. (if (boundp 'narrow-map)
  15248. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15249. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15250. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15251. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15252. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15253. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15254. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15255. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15256. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15257. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15258. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15259. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15260. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15261. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15262. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15263. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15264. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15265. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15266. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15267. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15268. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15269. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15270. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15271. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15272. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15273. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15274. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15275. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15276. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15277. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15278. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15279. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15280. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15281. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15282. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15283. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15284. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15285. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15286. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15287. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15288. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15289. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15290. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15291. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15292. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15293. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15294. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15295. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15296. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15297. (org-defkey org-mode-map "\C-m" 'org-return)
  15298. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15299. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15300. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15301. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15302. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15303. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15304. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15305. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15306. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15307. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15308. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15309. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15310. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15311. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15312. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15313. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15314. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15315. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15316. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15317. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15318. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15319. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15320. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15321. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15322. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15323. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15324. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15325. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15326. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15327. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15328. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15329. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15330. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15331. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15332. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15333. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15334. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15335. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15336. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15337. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15338. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15339. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15340. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15341. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15342. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15343. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15344. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15345. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15346. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15347. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15348. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15349. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15350. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15351. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15352. (when (featurep 'xemacs)
  15353. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15354. (defconst org-speed-commands-default
  15355. '(
  15356. ("Outline Navigation")
  15357. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15358. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15359. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15360. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15361. ("u" . (org-speed-move-safe 'outline-up-heading))
  15362. ("j" . org-goto)
  15363. ("g" . (org-refile t))
  15364. ("Outline Visibility")
  15365. ("c" . org-cycle)
  15366. ("C" . org-shifttab)
  15367. (" " . org-display-outline-path)
  15368. ("Outline Structure Editing")
  15369. ("U" . org-shiftmetaup)
  15370. ("D" . org-shiftmetadown)
  15371. ("r" . org-metaright)
  15372. ("l" . org-metaleft)
  15373. ("R" . org-shiftmetaright)
  15374. ("L" . org-shiftmetaleft)
  15375. ("i" . (progn (forward-char 1) (call-interactively
  15376. 'org-insert-heading-respect-content)))
  15377. ("^" . org-sort)
  15378. ("w" . org-refile)
  15379. ("a" . org-archive-subtree-default-with-confirmation)
  15380. ("." . org-mark-subtree)
  15381. ("Clock Commands")
  15382. ("I" . org-clock-in)
  15383. ("O" . org-clock-out)
  15384. ("Meta Data Editing")
  15385. ("t" . org-todo)
  15386. ("0" . (org-priority ?\ ))
  15387. ("1" . (org-priority ?A))
  15388. ("2" . (org-priority ?B))
  15389. ("3" . (org-priority ?C))
  15390. (";" . org-set-tags-command)
  15391. ("e" . org-set-effort)
  15392. ("Agenda Views etc")
  15393. ("v" . org-agenda)
  15394. ("/" . org-sparse-tree)
  15395. ("Misc")
  15396. ("o" . org-open-at-point)
  15397. ("?" . org-speed-command-help)
  15398. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15399. (">" . (org-agenda-remove-restriction-lock))
  15400. )
  15401. "The default speed commands.")
  15402. (defun org-print-speed-command (e)
  15403. (if (> (length (car e)) 1)
  15404. (progn
  15405. (princ "\n")
  15406. (princ (car e))
  15407. (princ "\n")
  15408. (princ (make-string (length (car e)) ?-))
  15409. (princ "\n"))
  15410. (princ (car e))
  15411. (princ " ")
  15412. (if (symbolp (cdr e))
  15413. (princ (symbol-name (cdr e)))
  15414. (prin1 (cdr e)))
  15415. (princ "\n")))
  15416. (defun org-speed-command-help ()
  15417. "Show the available speed commands."
  15418. (interactive)
  15419. (if (not org-use-speed-commands)
  15420. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15421. (with-output-to-temp-buffer "*Help*"
  15422. (princ "User-defined Speed commands\n===========================\n")
  15423. (mapc 'org-print-speed-command org-speed-commands-user)
  15424. (princ "\n")
  15425. (princ "Built-in Speed commands\n=======================\n")
  15426. (mapc 'org-print-speed-command org-speed-commands-default))
  15427. (with-current-buffer "*Help*"
  15428. (setq truncate-lines t))))
  15429. (defun org-speed-move-safe (cmd)
  15430. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15431. If not, return to the original position and throw an error."
  15432. (interactive)
  15433. (let ((pos (point)))
  15434. (call-interactively cmd)
  15435. (unless (and (bolp) (org-on-heading-p))
  15436. (goto-char pos)
  15437. (error "Boundary reached while executing %s" cmd))))
  15438. (defvar org-self-insert-command-undo-counter 0)
  15439. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15440. (defvar org-speed-command nil)
  15441. (defun org-speed-command-default-hook (keys)
  15442. "Hook for activating single-letter speed commands.
  15443. `org-speed-commands-default' specifies a minimal command set. Use
  15444. `org-speed-commands-user' for further customization."
  15445. (when (or (and (bolp) (looking-at outline-regexp))
  15446. (and (functionp org-use-speed-commands)
  15447. (funcall org-use-speed-commands)))
  15448. (cdr (assoc keys (append org-speed-commands-user
  15449. org-speed-commands-default)))))
  15450. (defun org-babel-speed-command-hook (keys)
  15451. "Hook for activating single-letter code block commands."
  15452. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15453. (cdr (assoc keys org-babel-key-bindings))))
  15454. (defcustom org-speed-command-hook
  15455. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15456. "Hook for activating speed commands at strategic locations.
  15457. Hook functions are called in sequence until a valid handler is
  15458. found.
  15459. Each hook takes a single argument, a user-pressed command key
  15460. which is also a `self-insert-command' from the global map.
  15461. Within the hook, examine the cursor position and the command key
  15462. and return nil or a valid handler as appropriate. Handler could
  15463. be one of an interactive command, a function, or a form.
  15464. Set `org-use-speed-commands' to non-nil value to enable this
  15465. hook. The default setting is `org-speed-command-default-hook'."
  15466. :group 'org-structure
  15467. :type 'hook)
  15468. (defun org-self-insert-command (N)
  15469. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15470. If the cursor is in a table looking at whitespace, the whitespace is
  15471. overwritten, and the table is not marked as requiring realignment."
  15472. (interactive "p")
  15473. (cond
  15474. ((and org-use-speed-commands
  15475. (setq org-speed-command
  15476. (run-hook-with-args-until-success
  15477. 'org-speed-command-hook (this-command-keys))))
  15478. (cond
  15479. ((commandp org-speed-command)
  15480. (setq this-command org-speed-command)
  15481. (call-interactively org-speed-command))
  15482. ((functionp org-speed-command)
  15483. (funcall org-speed-command))
  15484. ((and org-speed-command (listp org-speed-command))
  15485. (eval org-speed-command))
  15486. (t (let (org-use-speed-commands)
  15487. (call-interactively 'org-self-insert-command)))))
  15488. ((and
  15489. (org-table-p)
  15490. (progn
  15491. ;; check if we blank the field, and if that triggers align
  15492. (and (featurep 'org-table) org-table-auto-blank-field
  15493. (member last-command
  15494. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15495. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15496. ;; got extra space, this field does not determine column width
  15497. (let (org-table-may-need-update) (org-table-blank-field))
  15498. ;; no extra space, this field may determine column width
  15499. (org-table-blank-field)))
  15500. t)
  15501. (eq N 1)
  15502. (looking-at "[^|\n]* |"))
  15503. (let (org-table-may-need-update)
  15504. (goto-char (1- (match-end 0)))
  15505. (delete-char -1)
  15506. (goto-char (match-beginning 0))
  15507. (self-insert-command N)))
  15508. (t
  15509. (setq org-table-may-need-update t)
  15510. (self-insert-command N)
  15511. (org-fix-tags-on-the-fly)
  15512. (if org-self-insert-cluster-for-undo
  15513. (if (not (eq last-command 'org-self-insert-command))
  15514. (setq org-self-insert-command-undo-counter 1)
  15515. (if (>= org-self-insert-command-undo-counter 20)
  15516. (setq org-self-insert-command-undo-counter 1)
  15517. (and (> org-self-insert-command-undo-counter 0)
  15518. buffer-undo-list (listp buffer-undo-list)
  15519. (not (cadr buffer-undo-list)) ; remove nil entry
  15520. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15521. (setq org-self-insert-command-undo-counter
  15522. (1+ org-self-insert-command-undo-counter))))))))
  15523. (defun org-fix-tags-on-the-fly ()
  15524. (when (and (equal (char-after (point-at-bol)) ?*)
  15525. (org-on-heading-p))
  15526. (org-align-tags-here org-tags-column)))
  15527. (defun org-delete-backward-char (N)
  15528. "Like `delete-backward-char', insert whitespace at field end in tables.
  15529. When deleting backwards, in tables this function will insert whitespace in
  15530. front of the next \"|\" separator, to keep the table aligned. The table will
  15531. still be marked for re-alignment if the field did fill the entire column,
  15532. because, in this case the deletion might narrow the column."
  15533. (interactive "p")
  15534. (if (and (org-table-p)
  15535. (eq N 1)
  15536. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15537. (looking-at ".*?|"))
  15538. (let ((pos (point))
  15539. (noalign (looking-at "[^|\n\r]* |"))
  15540. (c org-table-may-need-update))
  15541. (backward-delete-char N)
  15542. (if (not overwrite-mode)
  15543. (progn
  15544. (skip-chars-forward "^|")
  15545. (insert " ")
  15546. (goto-char (1- pos))))
  15547. ;; noalign: if there were two spaces at the end, this field
  15548. ;; does not determine the width of the column.
  15549. (if noalign (setq org-table-may-need-update c)))
  15550. (backward-delete-char N)
  15551. (org-fix-tags-on-the-fly)))
  15552. (defun org-delete-char (N)
  15553. "Like `delete-char', but insert whitespace at field end in tables.
  15554. When deleting characters, in tables this function will insert whitespace in
  15555. front of the next \"|\" separator, to keep the table aligned. The table will
  15556. still be marked for re-alignment if the field did fill the entire column,
  15557. because, in this case the deletion might narrow the column."
  15558. (interactive "p")
  15559. (if (and (org-table-p)
  15560. (not (bolp))
  15561. (not (= (char-after) ?|))
  15562. (eq N 1))
  15563. (if (looking-at ".*?|")
  15564. (let ((pos (point))
  15565. (noalign (looking-at "[^|\n\r]* |"))
  15566. (c org-table-may-need-update))
  15567. (replace-match (concat
  15568. (substring (match-string 0) 1 -1)
  15569. " |"))
  15570. (goto-char pos)
  15571. ;; noalign: if there were two spaces at the end, this field
  15572. ;; does not determine the width of the column.
  15573. (if noalign (setq org-table-may-need-update c)))
  15574. (delete-char N))
  15575. (delete-char N)
  15576. (org-fix-tags-on-the-fly)))
  15577. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15578. (put 'org-self-insert-command 'delete-selection t)
  15579. (put 'orgtbl-self-insert-command 'delete-selection t)
  15580. (put 'org-delete-char 'delete-selection 'supersede)
  15581. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15582. (put 'org-yank 'delete-selection 'yank)
  15583. ;; Make `flyspell-mode' delay after some commands
  15584. (put 'org-self-insert-command 'flyspell-delayed t)
  15585. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15586. (put 'org-delete-char 'flyspell-delayed t)
  15587. (put 'org-delete-backward-char 'flyspell-delayed t)
  15588. ;; Make pabbrev-mode expand after org-mode commands
  15589. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15590. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15591. ;; How to do this: Measure non-white length of current string
  15592. ;; If equal to column width, we should realign.
  15593. (defun org-remap (map &rest commands)
  15594. "In MAP, remap the functions given in COMMANDS.
  15595. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15596. (let (new old)
  15597. (while commands
  15598. (setq old (pop commands) new (pop commands))
  15599. (if (fboundp 'command-remapping)
  15600. (org-defkey map (vector 'remap old) new)
  15601. (substitute-key-definition old new map global-map)))))
  15602. (when (eq org-enable-table-editor 'optimized)
  15603. ;; If the user wants maximum table support, we need to hijack
  15604. ;; some standard editing functions
  15605. (org-remap org-mode-map
  15606. 'self-insert-command 'org-self-insert-command
  15607. 'delete-char 'org-delete-char
  15608. 'delete-backward-char 'org-delete-backward-char)
  15609. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15610. (defvar org-ctrl-c-ctrl-c-hook nil
  15611. "Hook for functions attaching themselves to `C-c C-c'.
  15612. This can be used to add additional functionality to the C-c C-c key which
  15613. executes context-dependent commands.
  15614. Each function will be called with no arguments. The function must check
  15615. if the context is appropriate for it to act. If yes, it should do its
  15616. thing and then return a non-nil value. If the context is wrong,
  15617. just do nothing and return nil.")
  15618. (defvar org-tab-first-hook nil
  15619. "Hook for functions to attach themselves to TAB.
  15620. See `org-ctrl-c-ctrl-c-hook' for more information.
  15621. This hook runs as the first action when TAB is pressed, even before
  15622. `org-cycle' messes around with the `outline-regexp' to cater for
  15623. inline tasks and plain list item folding.
  15624. If any function in this hook returns t, any other actions that
  15625. would have been caused by TAB (such as table field motion or visibility
  15626. cycling) will not occur.")
  15627. (defvar org-tab-after-check-for-table-hook nil
  15628. "Hook for functions to attach themselves to TAB.
  15629. See `org-ctrl-c-ctrl-c-hook' for more information.
  15630. This hook runs after it has been established that the cursor is not in a
  15631. table, but before checking if the cursor is in a headline or if global cycling
  15632. should be done.
  15633. If any function in this hook returns t, not other actions like visibility
  15634. cycling will be done.")
  15635. (defvar org-tab-after-check-for-cycling-hook nil
  15636. "Hook for functions to attach themselves to TAB.
  15637. See `org-ctrl-c-ctrl-c-hook' for more information.
  15638. This hook runs after it has been established that not table field motion and
  15639. not visibility should be done because of current context. This is probably
  15640. the place where a package like yasnippets can hook in.")
  15641. (defvar org-tab-before-tab-emulation-hook nil
  15642. "Hook for functions to attach themselves to TAB.
  15643. See `org-ctrl-c-ctrl-c-hook' for more information.
  15644. This hook runs after every other options for TAB have been exhausted, but
  15645. before indentation and \t insertion takes place.")
  15646. (defvar org-metaleft-hook nil
  15647. "Hook for functions attaching themselves to `M-left'.
  15648. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15649. (defvar org-metaright-hook nil
  15650. "Hook for functions attaching themselves to `M-right'.
  15651. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15652. (defvar org-metaup-hook nil
  15653. "Hook for functions attaching themselves to `M-up'.
  15654. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15655. (defvar org-metadown-hook nil
  15656. "Hook for functions attaching themselves to `M-down'.
  15657. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15658. (defvar org-shiftmetaleft-hook nil
  15659. "Hook for functions attaching themselves to `M-S-left'.
  15660. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15661. (defvar org-shiftmetaright-hook nil
  15662. "Hook for functions attaching themselves to `M-S-right'.
  15663. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15664. (defvar org-shiftmetaup-hook nil
  15665. "Hook for functions attaching themselves to `M-S-up'.
  15666. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15667. (defvar org-shiftmetadown-hook nil
  15668. "Hook for functions attaching themselves to `M-S-down'.
  15669. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15670. (defvar org-metareturn-hook nil
  15671. "Hook for functions attaching themselves to `M-RET'.
  15672. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15673. (defvar org-shiftup-hook nil
  15674. "Hook for functions attaching themselves to `S-up'.
  15675. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15676. (defvar org-shiftup-final-hook nil
  15677. "Hook for functions attaching themselves to `S-up'.
  15678. This one runs after all other options except shift-select have been excluded.
  15679. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15680. (defvar org-shiftdown-hook nil
  15681. "Hook for functions attaching themselves to `S-down'.
  15682. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15683. (defvar org-shiftdown-final-hook nil
  15684. "Hook for functions attaching themselves to `S-down'.
  15685. This one runs after all other options except shift-select have been excluded.
  15686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15687. (defvar org-shiftleft-hook nil
  15688. "Hook for functions attaching themselves to `S-left'.
  15689. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15690. (defvar org-shiftleft-final-hook nil
  15691. "Hook for functions attaching themselves to `S-left'.
  15692. This one runs after all other options except shift-select have been excluded.
  15693. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15694. (defvar org-shiftright-hook nil
  15695. "Hook for functions attaching themselves to `S-right'.
  15696. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15697. (defvar org-shiftright-final-hook nil
  15698. "Hook for functions attaching themselves to `S-right'.
  15699. This one runs after all other options except shift-select have been excluded.
  15700. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15701. (defun org-modifier-cursor-error ()
  15702. "Throw an error, a modified cursor command was applied in wrong context."
  15703. (error "This command is active in special context like tables, headlines or items"))
  15704. (defun org-shiftselect-error ()
  15705. "Throw an error because Shift-Cursor command was applied in wrong context."
  15706. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15707. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15708. (error "This command works only in special context like headlines or timestamps")))
  15709. (defun org-call-for-shift-select (cmd)
  15710. (let ((this-command-keys-shift-translated t))
  15711. (call-interactively cmd)))
  15712. (defun org-shifttab (&optional arg)
  15713. "Global visibility cycling or move to previous table field.
  15714. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15715. on context.
  15716. See the individual commands for more information."
  15717. (interactive "P")
  15718. (cond
  15719. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15720. ((integerp arg)
  15721. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15722. (message "Content view to level: %d" arg)
  15723. (org-content (prefix-numeric-value arg2))
  15724. (setq org-cycle-global-status 'overview)))
  15725. (t (call-interactively 'org-global-cycle))))
  15726. (defun org-shiftmetaleft ()
  15727. "Promote subtree or delete table column.
  15728. Calls `org-promote-subtree', `org-outdent-item',
  15729. or `org-table-delete-column', depending on context.
  15730. See the individual commands for more information."
  15731. (interactive)
  15732. (cond
  15733. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15734. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15735. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15736. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15737. (t (org-modifier-cursor-error))))
  15738. (defun org-shiftmetaright ()
  15739. "Demote subtree or insert table column.
  15740. Calls `org-demote-subtree', `org-indent-item',
  15741. or `org-table-insert-column', depending on context.
  15742. See the individual commands for more information."
  15743. (interactive)
  15744. (cond
  15745. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15746. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15747. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15748. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15749. (t (org-modifier-cursor-error))))
  15750. (defun org-shiftmetaup (&optional arg)
  15751. "Move subtree up or kill table row.
  15752. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15753. `org-move-item-up' depending on context. See the individual commands
  15754. for more information."
  15755. (interactive "P")
  15756. (cond
  15757. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15758. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15759. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15760. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15761. (t (org-modifier-cursor-error))))
  15762. (defun org-shiftmetadown (&optional arg)
  15763. "Move subtree down or insert table row.
  15764. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15765. `org-move-item-down', depending on context. See the individual
  15766. commands for more information."
  15767. (interactive "P")
  15768. (cond
  15769. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15770. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15771. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15772. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15773. (t (org-modifier-cursor-error))))
  15774. (defsubst org-hidden-tree-error ()
  15775. (error
  15776. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15777. (defun org-metaleft (&optional arg)
  15778. "Promote heading or move table column to left.
  15779. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15780. With no specific context, calls the Emacs default `backward-word'.
  15781. See the individual commands for more information."
  15782. (interactive "P")
  15783. (cond
  15784. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15785. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15786. ((org-with-limited-levels
  15787. (or (org-on-heading-p)
  15788. (and (org-region-active-p)
  15789. (save-excursion
  15790. (goto-char (region-beginning))
  15791. (org-on-heading-p)))))
  15792. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15793. (call-interactively 'org-do-promote))
  15794. ;; At an inline task.
  15795. ((org-on-heading-p)
  15796. (call-interactively 'org-inlinetask-promote))
  15797. ((or (org-at-item-p)
  15798. (and (org-region-active-p)
  15799. (save-excursion
  15800. (goto-char (region-beginning))
  15801. (org-at-item-p))))
  15802. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15803. (call-interactively 'org-outdent-item))
  15804. (t (call-interactively 'backward-word))))
  15805. (defun org-metaright (&optional arg)
  15806. "Demote subtree or move table column to right.
  15807. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15808. With no specific context, calls the Emacs default `forward-word'.
  15809. See the individual commands for more information."
  15810. (interactive "P")
  15811. (cond
  15812. ((run-hook-with-args-until-success 'org-metaright-hook))
  15813. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15814. ((org-with-limited-levels
  15815. (or (org-on-heading-p)
  15816. (and (org-region-active-p)
  15817. (save-excursion
  15818. (goto-char (region-beginning))
  15819. (org-on-heading-p)))))
  15820. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15821. (call-interactively 'org-do-demote))
  15822. ;; At an inline task.
  15823. ((org-on-heading-p)
  15824. (call-interactively 'org-inlinetask-demote))
  15825. ((or (org-at-item-p)
  15826. (and (org-region-active-p)
  15827. (save-excursion
  15828. (goto-char (region-beginning))
  15829. (org-at-item-p))))
  15830. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15831. (call-interactively 'org-indent-item))
  15832. (t (call-interactively 'forward-word))))
  15833. (defun org-check-for-hidden (what)
  15834. "Check if there are hidden headlines/items in the current visual line.
  15835. WHAT can be either `headlines' or `items'. If the current line is
  15836. an outline or item heading and it has a folded subtree below it,
  15837. this function returns t, nil otherwise."
  15838. (let ((re (cond
  15839. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15840. ((eq what 'items) (org-item-beginning-re))
  15841. (t (error "This should not happen"))))
  15842. beg end)
  15843. (save-excursion
  15844. (catch 'exit
  15845. (unless (org-region-active-p)
  15846. (setq beg (point-at-bol))
  15847. (beginning-of-line 2)
  15848. (while (and (not (eobp)) ;; this is like `next-line'
  15849. (get-char-property (1- (point)) 'invisible))
  15850. (beginning-of-line 2))
  15851. (setq end (point))
  15852. (goto-char beg)
  15853. (goto-char (point-at-eol))
  15854. (setq end (max end (point)))
  15855. (while (re-search-forward re end t)
  15856. (if (get-char-property (match-beginning 0) 'invisible)
  15857. (throw 'exit t))))
  15858. nil))))
  15859. (defun org-metaup (&optional arg)
  15860. "Move subtree up or move table row up.
  15861. Calls `org-move-subtree-up' or `org-table-move-row' or
  15862. `org-move-item-up', depending on context. See the individual commands
  15863. for more information."
  15864. (interactive "P")
  15865. (cond
  15866. ((run-hook-with-args-until-success 'org-metaup-hook))
  15867. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15868. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15869. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15870. (t (transpose-lines 1) (beginning-of-line -1))))
  15871. (defun org-metadown (&optional arg)
  15872. "Move subtree down or move table row down.
  15873. Calls `org-move-subtree-down' or `org-table-move-row' or
  15874. `org-move-item-down', depending on context. See the individual
  15875. commands for more information."
  15876. (interactive "P")
  15877. (cond
  15878. ((run-hook-with-args-until-success 'org-metadown-hook))
  15879. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15880. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15881. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15882. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15883. (defun org-shiftup (&optional arg)
  15884. "Increase item in timestamp or increase priority of current headline.
  15885. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15886. depending on context. See the individual commands for more information."
  15887. (interactive "P")
  15888. (cond
  15889. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15890. ((and org-support-shift-select (org-region-active-p))
  15891. (org-call-for-shift-select 'previous-line))
  15892. ((org-at-timestamp-p t)
  15893. (call-interactively (if org-edit-timestamp-down-means-later
  15894. 'org-timestamp-down 'org-timestamp-up)))
  15895. ((and (not (eq org-support-shift-select 'always))
  15896. org-enable-priority-commands
  15897. (org-on-heading-p))
  15898. (call-interactively 'org-priority-up))
  15899. ((and (not org-support-shift-select) (org-at-item-p))
  15900. (call-interactively 'org-previous-item))
  15901. ((org-clocktable-try-shift 'up arg))
  15902. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15903. (org-support-shift-select
  15904. (org-call-for-shift-select 'previous-line))
  15905. (t (org-shiftselect-error))))
  15906. (defun org-shiftdown (&optional arg)
  15907. "Decrease item in timestamp or decrease priority of current headline.
  15908. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15909. depending on context. See the individual commands for more information."
  15910. (interactive "P")
  15911. (cond
  15912. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15913. ((and org-support-shift-select (org-region-active-p))
  15914. (org-call-for-shift-select 'next-line))
  15915. ((org-at-timestamp-p t)
  15916. (call-interactively (if org-edit-timestamp-down-means-later
  15917. 'org-timestamp-up 'org-timestamp-down)))
  15918. ((and (not (eq org-support-shift-select 'always))
  15919. org-enable-priority-commands
  15920. (org-on-heading-p))
  15921. (call-interactively 'org-priority-down))
  15922. ((and (not org-support-shift-select) (org-at-item-p))
  15923. (call-interactively 'org-next-item))
  15924. ((org-clocktable-try-shift 'down arg))
  15925. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15926. (org-support-shift-select
  15927. (org-call-for-shift-select 'next-line))
  15928. (t (org-shiftselect-error))))
  15929. (defun org-shiftright (&optional arg)
  15930. "Cycle the thing at point or in the current line, depending on context.
  15931. Depending on context, this does one of the following:
  15932. - switch a timestamp at point one day into the future
  15933. - on a headline, switch to the next TODO keyword.
  15934. - on an item, switch entire list to the next bullet type
  15935. - on a property line, switch to the next allowed value
  15936. - on a clocktable definition line, move time block into the future"
  15937. (interactive "P")
  15938. (cond
  15939. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15940. ((and org-support-shift-select (org-region-active-p))
  15941. (org-call-for-shift-select 'forward-char))
  15942. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15943. ((and (not (eq org-support-shift-select 'always))
  15944. (org-on-heading-p))
  15945. (let ((org-inhibit-logging
  15946. (not org-treat-S-cursor-todo-selection-as-state-change))
  15947. (org-inhibit-blocking
  15948. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15949. (org-call-with-arg 'org-todo 'right)))
  15950. ((or (and org-support-shift-select
  15951. (not (eq org-support-shift-select 'always))
  15952. (org-at-item-bullet-p))
  15953. (and (not org-support-shift-select) (org-at-item-p)))
  15954. (org-call-with-arg 'org-cycle-list-bullet nil))
  15955. ((and (not (eq org-support-shift-select 'always))
  15956. (org-at-property-p))
  15957. (call-interactively 'org-property-next-allowed-value))
  15958. ((org-clocktable-try-shift 'right arg))
  15959. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15960. (org-support-shift-select
  15961. (org-call-for-shift-select 'forward-char))
  15962. (t (org-shiftselect-error))))
  15963. (defun org-shiftleft (&optional arg)
  15964. "Cycle the thing at point or in the current line, depending on context.
  15965. Depending on context, this does one of the following:
  15966. - switch a timestamp at point one day into the past
  15967. - on a headline, switch to the previous TODO keyword.
  15968. - on an item, switch entire list to the previous bullet type
  15969. - on a property line, switch to the previous allowed value
  15970. - on a clocktable definition line, move time block into the past"
  15971. (interactive "P")
  15972. (cond
  15973. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15974. ((and org-support-shift-select (org-region-active-p))
  15975. (org-call-for-shift-select 'backward-char))
  15976. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15977. ((and (not (eq org-support-shift-select 'always))
  15978. (org-on-heading-p))
  15979. (let ((org-inhibit-logging
  15980. (not org-treat-S-cursor-todo-selection-as-state-change))
  15981. (org-inhibit-blocking
  15982. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15983. (org-call-with-arg 'org-todo 'left)))
  15984. ((or (and org-support-shift-select
  15985. (not (eq org-support-shift-select 'always))
  15986. (org-at-item-bullet-p))
  15987. (and (not org-support-shift-select) (org-at-item-p)))
  15988. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15989. ((and (not (eq org-support-shift-select 'always))
  15990. (org-at-property-p))
  15991. (call-interactively 'org-property-previous-allowed-value))
  15992. ((org-clocktable-try-shift 'left arg))
  15993. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15994. (org-support-shift-select
  15995. (org-call-for-shift-select 'backward-char))
  15996. (t (org-shiftselect-error))))
  15997. (defun org-shiftcontrolright ()
  15998. "Switch to next TODO set."
  15999. (interactive)
  16000. (cond
  16001. ((and org-support-shift-select (org-region-active-p))
  16002. (org-call-for-shift-select 'forward-word))
  16003. ((and (not (eq org-support-shift-select 'always))
  16004. (org-on-heading-p))
  16005. (org-call-with-arg 'org-todo 'nextset))
  16006. (org-support-shift-select
  16007. (org-call-for-shift-select 'forward-word))
  16008. (t (org-shiftselect-error))))
  16009. (defun org-shiftcontrolleft ()
  16010. "Switch to previous TODO set."
  16011. (interactive)
  16012. (cond
  16013. ((and org-support-shift-select (org-region-active-p))
  16014. (org-call-for-shift-select 'backward-word))
  16015. ((and (not (eq org-support-shift-select 'always))
  16016. (org-on-heading-p))
  16017. (org-call-with-arg 'org-todo 'previousset))
  16018. (org-support-shift-select
  16019. (org-call-for-shift-select 'backward-word))
  16020. (t (org-shiftselect-error))))
  16021. (defun org-ctrl-c-ret ()
  16022. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16023. (interactive)
  16024. (cond
  16025. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16026. (t (call-interactively 'org-insert-heading))))
  16027. (defun org-copy-special ()
  16028. "Copy region in table or copy current subtree.
  16029. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16030. See the individual commands for more information."
  16031. (interactive)
  16032. (call-interactively
  16033. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16034. (defun org-cut-special ()
  16035. "Cut region in table or cut current subtree.
  16036. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16037. See the individual commands for more information."
  16038. (interactive)
  16039. (call-interactively
  16040. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16041. (defun org-paste-special (arg)
  16042. "Paste rectangular region into table, or past subtree relative to level.
  16043. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16044. See the individual commands for more information."
  16045. (interactive "P")
  16046. (if (org-at-table-p)
  16047. (org-table-paste-rectangle)
  16048. (org-paste-subtree arg)))
  16049. (defun org-edit-special (&optional arg)
  16050. "Call a special editor for the stuff at point.
  16051. When at a table, call the formula editor with `org-table-edit-formulas'.
  16052. When at the first line of an src example, call `org-edit-src-code'.
  16053. When in an #+include line, visit the include file. Otherwise call
  16054. `ffap' to visit the file at point."
  16055. (interactive)
  16056. ;; possibly prep session before editing source
  16057. (when arg
  16058. (let* ((info (org-babel-get-src-block-info))
  16059. (lang (nth 0 info))
  16060. (params (nth 2 info))
  16061. (session (cdr (assoc :session params))))
  16062. (when (and info session) ;; we are in a source-code block with a session
  16063. (funcall
  16064. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16065. (cond ;; proceed with `org-edit-special'
  16066. ((save-excursion
  16067. (beginning-of-line 1)
  16068. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16069. (find-file (org-trim (match-string 1))))
  16070. ((org-edit-src-code))
  16071. ((org-edit-fixed-width-region))
  16072. ((org-at-table.el-p)
  16073. (org-edit-src-code))
  16074. ((or (org-at-table-p)
  16075. (save-excursion
  16076. (beginning-of-line 1)
  16077. (looking-at "[ \t]*#\\+TBLFM:")))
  16078. (call-interactively 'org-table-edit-formulas))
  16079. (t (call-interactively 'ffap))))
  16080. (defun org-ctrl-c-ctrl-c (&optional arg)
  16081. "Set tags in headline, or update according to changed information at point.
  16082. This command does many different things, depending on context:
  16083. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16084. this is what we do.
  16085. - If the cursor is on a statistics cookie, update it.
  16086. - If the cursor is in a headline, prompt for tags and insert them
  16087. into the current line, aligned to `org-tags-column'. When called
  16088. with prefix arg, realign all tags in the current buffer.
  16089. - If the cursor is in one of the special #+KEYWORD lines, this
  16090. triggers scanning the buffer for these lines and updating the
  16091. information.
  16092. - If the cursor is inside a table, realign the table. This command
  16093. works even if the automatic table editor has been turned off.
  16094. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16095. the entire table.
  16096. - If the cursor is at a footnote reference or definition, jump to
  16097. the corresponding definition or references, respectively.
  16098. - If the cursor is a the beginning of a dynamic block, update it.
  16099. - If the current buffer is a capture buffer, close note and file it.
  16100. - If the cursor is on a <<<target>>>, update radio targets and
  16101. corresponding links in this buffer.
  16102. - If the cursor is on a numbered item in a plain list, renumber the
  16103. ordered list.
  16104. - If the cursor is on a checkbox, toggle it.
  16105. - If the cursor is on a code block, evaluate it. The variable
  16106. `org-confirm-babel-evaluate' can be used to control prompting
  16107. before code block evaluation, by default every code block
  16108. evaluation requires confirmation. Code block evaluation can be
  16109. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16110. (interactive "P")
  16111. (let ((org-enable-table-editor t))
  16112. (cond
  16113. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16114. org-occur-highlights
  16115. org-latex-fragment-image-overlays)
  16116. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16117. (org-remove-occur-highlights)
  16118. (org-remove-latex-fragment-image-overlays)
  16119. (message "Temporary highlights/overlays removed from current buffer"))
  16120. ((and (local-variable-p 'org-finish-function (current-buffer))
  16121. (fboundp org-finish-function))
  16122. (funcall org-finish-function))
  16123. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16124. ((or (looking-at org-property-start-re)
  16125. (org-at-property-p))
  16126. (call-interactively 'org-property-action))
  16127. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16128. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16129. (or (org-on-heading-p) (org-at-item-p)))
  16130. (call-interactively 'org-update-statistics-cookies))
  16131. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16132. ((org-at-table.el-p)
  16133. (message "Use C-c ' to edit table.el tables"))
  16134. ((org-at-table-p)
  16135. (org-table-maybe-eval-formula)
  16136. (if arg
  16137. (call-interactively 'org-table-recalculate)
  16138. (org-table-maybe-recalculate-line))
  16139. (call-interactively 'org-table-align)
  16140. (orgtbl-send-table 'maybe))
  16141. ((or (org-footnote-at-reference-p)
  16142. (org-footnote-at-definition-p))
  16143. (call-interactively 'org-footnote-action))
  16144. ((org-at-item-checkbox-p)
  16145. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16146. ;; list only if at top item.
  16147. (let* ((cbox (match-string 1))
  16148. (struct (org-list-struct))
  16149. (old-struct (copy-tree struct))
  16150. (parents (org-list-parents-alist struct))
  16151. (prevs (org-list-prevs-alist struct))
  16152. (orderedp (org-entry-get nil "ORDERED"))
  16153. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16154. block-item)
  16155. ;; Use a light version of `org-toggle-checkbox' to avoid
  16156. ;; computing list structure twice.
  16157. (org-list-set-checkbox (point-at-bol) struct
  16158. (cond
  16159. ((equal arg '(16)) "[-]")
  16160. ((equal arg '(4)) nil)
  16161. ((equal "[X]" cbox) "[ ]")
  16162. (t "[X]")))
  16163. (org-list-struct-fix-ind struct parents)
  16164. (org-list-struct-fix-bul struct prevs)
  16165. (setq block-item
  16166. (org-list-struct-fix-box struct parents prevs orderedp))
  16167. (when block-item
  16168. (message
  16169. "Checkboxes were removed due to unchecked box at line %d"
  16170. (org-current-line block-item)))
  16171. (org-list-struct-apply-struct struct old-struct)
  16172. (org-update-checkbox-count-maybe)
  16173. (when firstp (org-list-send-list 'maybe))))
  16174. ((org-at-item-p)
  16175. ;; Cursor at an item: repair list. Do checkbox related actions
  16176. ;; only if function was called with an argument. Send list only
  16177. ;; if at top item.
  16178. (let* ((struct (org-list-struct))
  16179. (old-struct (copy-tree struct))
  16180. (parents (org-list-parents-alist struct))
  16181. (prevs (org-list-prevs-alist struct))
  16182. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16183. (org-list-struct-fix-ind struct parents)
  16184. (org-list-struct-fix-bul struct prevs)
  16185. (when arg
  16186. (org-list-set-checkbox (point-at-bol) struct "[ ]")
  16187. (org-list-struct-fix-box struct parents prevs))
  16188. (org-list-struct-apply-struct struct old-struct)
  16189. (when arg (org-update-checkbox-count-maybe))
  16190. (when firstp (org-list-send-list 'maybe))))
  16191. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16192. ;; Dynamic block
  16193. (beginning-of-line 1)
  16194. (save-excursion (org-update-dblock)))
  16195. ((save-excursion
  16196. (beginning-of-line 1)
  16197. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16198. (cond
  16199. ((equal (match-string 1) "TBLFM")
  16200. ;; Recalculate the table before this line
  16201. (save-excursion
  16202. (beginning-of-line 1)
  16203. (skip-chars-backward " \r\n\t")
  16204. (if (org-at-table-p)
  16205. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16206. (t
  16207. (let ((org-inhibit-startup-visibility-stuff t)
  16208. (org-startup-align-all-tables nil))
  16209. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16210. (message "Local setup has been refreshed"))))
  16211. ((org-clock-update-time-maybe))
  16212. (t (error "C-c C-c can do nothing useful at this location")))))
  16213. (defun org-mode-restart ()
  16214. "Restart Org-mode, to scan again for special lines.
  16215. Also updates the keyword regular expressions."
  16216. (interactive)
  16217. (org-mode)
  16218. (message "Org-mode restarted"))
  16219. (defun org-kill-note-or-show-branches ()
  16220. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16221. (interactive)
  16222. (if (not org-finish-function)
  16223. (progn
  16224. (hide-subtree)
  16225. (call-interactively 'show-branches))
  16226. (let ((org-note-abort t))
  16227. (funcall org-finish-function))))
  16228. (defun org-return (&optional indent)
  16229. "Goto next table row or insert a newline.
  16230. Calls `org-table-next-row' or `newline', depending on context.
  16231. See the individual commands for more information."
  16232. (interactive)
  16233. (cond
  16234. ((bobp) (if indent (newline-and-indent) (newline)))
  16235. ((org-at-table-p)
  16236. (org-table-justify-field-maybe)
  16237. (call-interactively 'org-table-next-row))
  16238. ;; when `newline-and-indent' is called within a list, make sure
  16239. ;; text moved stays inside the item.
  16240. ((and (org-in-item-p) indent)
  16241. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16242. (progn
  16243. (newline)
  16244. (org-indent-line-to (length (match-string 0))))
  16245. (let ((ind (org-get-indentation)))
  16246. (newline)
  16247. (if (org-looking-back org-list-end-re)
  16248. (org-indent-line-function)
  16249. (org-indent-line-to ind)))))
  16250. ((and org-return-follows-link
  16251. (eq (get-text-property (point) 'face) 'org-link))
  16252. (call-interactively 'org-open-at-point))
  16253. ((and (org-at-heading-p)
  16254. (looking-at
  16255. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16256. (org-show-entry)
  16257. (end-of-line 1)
  16258. (newline))
  16259. (t (if indent (newline-and-indent) (newline)))))
  16260. (defun org-return-indent ()
  16261. "Goto next table row or insert a newline and indent.
  16262. Calls `org-table-next-row' or `newline-and-indent', depending on
  16263. context. See the individual commands for more information."
  16264. (interactive)
  16265. (org-return t))
  16266. (defun org-ctrl-c-star ()
  16267. "Compute table, or change heading status of lines.
  16268. Calls `org-table-recalculate' or `org-toggle-heading',
  16269. depending on context."
  16270. (interactive)
  16271. (cond
  16272. ((org-at-table-p)
  16273. (call-interactively 'org-table-recalculate))
  16274. (t
  16275. ;; Convert all lines in region to list items
  16276. (call-interactively 'org-toggle-heading))))
  16277. (defun org-ctrl-c-minus ()
  16278. "Insert separator line in table or modify bullet status of line.
  16279. Also turns a plain line or a region of lines into list items.
  16280. Calls `org-table-insert-hline', `org-toggle-item', or
  16281. `org-cycle-list-bullet', depending on context."
  16282. (interactive)
  16283. (cond
  16284. ((org-at-table-p)
  16285. (call-interactively 'org-table-insert-hline))
  16286. ((org-region-active-p)
  16287. (call-interactively 'org-toggle-item))
  16288. ((org-in-item-p)
  16289. (call-interactively 'org-cycle-list-bullet))
  16290. (t
  16291. (call-interactively 'org-toggle-item))))
  16292. (defun org-toggle-item (arg)
  16293. "Convert headings or normal lines to items, items to normal lines.
  16294. If there is no active region, only the current line is considered.
  16295. If the first non blank line in the region is an headline, convert
  16296. all headlines to items, shifting text accordingly.
  16297. If it is an item, convert all items to normal lines.
  16298. If it is normal text, change region into an item. With a prefix
  16299. argument ARG, change each line in region into an item."
  16300. (interactive "P")
  16301. (let ((shift-text
  16302. (function
  16303. ;; Shift text in current section to IND, from point to END.
  16304. ;; The function leaves point to END line.
  16305. (lambda (ind end)
  16306. (let ((min-i 1000) (end (copy-marker end)))
  16307. ;; First determine the minimum indentation (MIN-I) of
  16308. ;; the text.
  16309. (save-excursion
  16310. (catch 'exit
  16311. (while (< (point) end)
  16312. (let ((i (org-get-indentation)))
  16313. (cond
  16314. ;; Skip blank lines and inline tasks.
  16315. ((looking-at "^[ \t]*$"))
  16316. ((looking-at "^\\*+ "))
  16317. ;; We can't find less than 0 indentation.
  16318. ((zerop i) (throw 'exit (setq min-i 0)))
  16319. ((< i min-i) (setq min-i i))))
  16320. (forward-line))))
  16321. ;; Then indent each line so that a line indented to
  16322. ;; MIN-I becomes indented to IND. Ignore blank lines
  16323. ;; and inline tasks in the process.
  16324. (let ((delta (- ind min-i)))
  16325. (while (< (point) end)
  16326. (unless (or (looking-at "^[ \t]*$")
  16327. (looking-at "^\\*+ "))
  16328. (org-indent-line-to (+ (org-get-indentation) delta)))
  16329. (forward-line)))))))
  16330. (skip-blanks
  16331. (function
  16332. ;; Return beginning of first non-blank line, starting from
  16333. ;; line at POS.
  16334. (lambda (pos)
  16335. (save-excursion
  16336. (goto-char pos)
  16337. (skip-chars-forward " \r\t\n")
  16338. (point-at-bol)))))
  16339. beg end)
  16340. ;; Determine boundaries of changes.
  16341. (if (org-region-active-p)
  16342. (setq beg (funcall skip-blanks (region-beginning))
  16343. end (copy-marker (region-end)))
  16344. (setq beg (funcall skip-blanks (point-at-bol))
  16345. end (copy-marker (point-at-eol))))
  16346. ;; Depending on the starting line, choose an action on the text
  16347. ;; between BEG and END.
  16348. (org-with-limited-levels
  16349. (save-excursion
  16350. (goto-char beg)
  16351. (cond
  16352. ;; Case 1. Start at an item: de-itemize. Note that it only
  16353. ;; happens when a region is active: `org-ctrl-c-minus'
  16354. ;; would call `org-cycle-list-bullet' otherwise.
  16355. ((org-at-item-p)
  16356. (while (< (point) end)
  16357. (when (org-at-item-p)
  16358. (skip-chars-forward " \t")
  16359. (delete-region (point) (match-end 0)))
  16360. (forward-line)))
  16361. ;; Case 2. Start at an heading: convert to items.
  16362. ((org-on-heading-p)
  16363. (let* ((bul (org-list-bullet-string "-"))
  16364. (bul-len (length bul))
  16365. ;; Indentation of the first heading. It should be
  16366. ;; relative to the indentation of its parent, if any.
  16367. (start-ind (save-excursion
  16368. (cond
  16369. ((not org-adapt-indentation) 0)
  16370. ((not (outline-previous-heading)) 0)
  16371. (t (length (match-string 0))))))
  16372. ;; Level of first heading. Further headings will be
  16373. ;; compared to it to determine hierarchy in the list.
  16374. (ref-level (org-reduced-level (org-outline-level))))
  16375. (while (< (point) end)
  16376. (let* ((level (org-reduced-level (org-outline-level)))
  16377. (delta (max 0 (- level ref-level))))
  16378. ;; If current headline is less indented than the first
  16379. ;; one, set it as reference, in order to preserve
  16380. ;; subtrees.
  16381. (when (< level ref-level) (setq ref-level level))
  16382. (replace-match bul t t)
  16383. (org-indent-line-to (+ start-ind (* delta bul-len)))
  16384. ;; Ensure all text down to END (or SECTION-END) belongs
  16385. ;; to the newly created item.
  16386. (let ((section-end (save-excursion
  16387. (or (outline-next-heading) (point)))))
  16388. (forward-line)
  16389. (funcall shift-text
  16390. (+ start-ind (* (1+ delta) bul-len))
  16391. (min end section-end)))))))
  16392. ;; Case 3. Normal line with ARG: turn each non-item line into
  16393. ;; an item.
  16394. (arg
  16395. (while (< (point) end)
  16396. (unless (or (org-on-heading-p) (org-at-item-p))
  16397. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16398. (replace-match
  16399. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  16400. (forward-line)))
  16401. ;; Case 4. Normal line without ARG: make the first line of
  16402. ;; region an item, and shift indentation of others
  16403. ;; lines to set them as item's body.
  16404. (t (let* ((bul (org-list-bullet-string "-"))
  16405. (bul-len (length bul))
  16406. (ref-ind (org-get-indentation)))
  16407. (skip-chars-forward " \t")
  16408. (insert bul)
  16409. (forward-line)
  16410. (while (< (point) end)
  16411. ;; Ensure that lines less indented than first one
  16412. ;; still get included in item body.
  16413. (funcall shift-text
  16414. (+ ref-ind bul-len)
  16415. (min end (save-excursion (or (outline-next-heading)
  16416. (point)))))
  16417. (forward-line)))))))))
  16418. (defun org-toggle-heading (&optional nstars)
  16419. "Convert headings to normal text, or items or text to headings.
  16420. If there is no active region, only the current line is considered.
  16421. If the first non blank line is an headline, remove the stars from
  16422. all headlines in the region.
  16423. If it is a plain list item, turn all plain list items into headings.
  16424. If it is a normal line, turn each and every normal line (i.e. not
  16425. an heading or an item) in the region into a heading.
  16426. When converting a line into a heading, the number of stars is chosen
  16427. such that the lines become children of the current entry. However,
  16428. when a prefix argument is given, its value determines the number of
  16429. stars to add."
  16430. (interactive "P")
  16431. (let ((skip-blanks
  16432. (function
  16433. ;; Return beginning of first non-blank line, starting from
  16434. ;; line at POS.
  16435. (lambda (pos)
  16436. (save-excursion
  16437. (goto-char pos)
  16438. (skip-chars-forward " \r\t\n")
  16439. (point-at-bol)))))
  16440. beg end)
  16441. ;; Determine boundaries of changes. If region ends at a bol, do
  16442. ;; not consider the last line to be in the region.
  16443. (if (org-region-active-p)
  16444. (setq beg (funcall skip-blanks (region-beginning))
  16445. end (copy-marker (save-excursion
  16446. (goto-char (region-end))
  16447. (if (bolp) (point) (point-at-eol)))))
  16448. (setq beg (funcall skip-blanks (point-at-bol))
  16449. end (copy-marker (point-at-eol))))
  16450. ;; Ensure inline tasks don't count as headings.
  16451. (org-with-limited-levels
  16452. (save-excursion
  16453. (goto-char beg)
  16454. (cond
  16455. ;; Case 1. Started at an heading: de-star headings.
  16456. ((org-on-heading-p)
  16457. (while (< (point) end)
  16458. (when (org-on-heading-p t)
  16459. (looking-at outline-regexp) (replace-match ""))
  16460. (forward-line)))
  16461. ;; Case 2. Started at an item: change items into headlines.
  16462. ;; One star will be added by `org-list-to-subtree'.
  16463. ((org-at-item-p)
  16464. (let* ((stars (make-string
  16465. (if nstars
  16466. ;; subtract the star that will be added again by
  16467. ;; `org-list-to-subtree'
  16468. (1- (prefix-numeric-value current-prefix-arg))
  16469. (or (org-current-level) 0))
  16470. ?*))
  16471. (add-stars
  16472. (cond (nstars "") ; stars from prefix only
  16473. ((equal stars "") "") ; before first heading
  16474. (org-odd-levels-only "*") ; inside heading, odd
  16475. (t "")))) ; inside heading, oddeven
  16476. (while (< (point) end)
  16477. (when (org-at-item-p)
  16478. ;; Pay attention to cases when region ends before list.
  16479. (let* ((struct (org-list-struct))
  16480. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  16481. (save-restriction
  16482. (narrow-to-region (point) list-end)
  16483. (insert
  16484. (org-list-to-subtree
  16485. (org-list-parse-list t)
  16486. '(:istart (concat stars add-stars (funcall get-stars depth))
  16487. :icount (concat stars add-stars (funcall get-stars depth))))))))
  16488. (forward-line))))
  16489. ;; Case 3. Started at normal text: make every line an heading,
  16490. ;; skipping headlines and items.
  16491. (t (let* ((stars (make-string
  16492. (if nstars
  16493. (prefix-numeric-value current-prefix-arg)
  16494. (or (org-current-level) 0))
  16495. ?*))
  16496. (add-stars
  16497. (cond (nstars "") ; stars from prefix only
  16498. ((equal stars "") "*") ; before first heading
  16499. (org-odd-levels-only "**") ; inside heading, odd
  16500. (t "*"))) ; inside heading, oddeven
  16501. (rpl (concat stars add-stars " ")))
  16502. (while (< (point) end)
  16503. (when (and (not (org-on-heading-p)) (not (org-at-item-p))
  16504. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  16505. (replace-match (concat rpl (match-string 2))))
  16506. (forward-line)))))))))
  16507. (defun org-meta-return (&optional arg)
  16508. "Insert a new heading or wrap a region in a table.
  16509. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16510. See the individual commands for more information."
  16511. (interactive "P")
  16512. (cond
  16513. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16514. ((org-at-table-p)
  16515. (call-interactively 'org-table-wrap-region))
  16516. (t (call-interactively 'org-insert-heading))))
  16517. ;;; Menu entries
  16518. ;; Define the Org-mode menus
  16519. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16520. '("Tbl"
  16521. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16522. ["Next Field" org-cycle (org-at-table-p)]
  16523. ["Previous Field" org-shifttab (org-at-table-p)]
  16524. ["Next Row" org-return (org-at-table-p)]
  16525. "--"
  16526. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16527. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16528. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16529. "--"
  16530. ("Column"
  16531. ["Move Column Left" org-metaleft (org-at-table-p)]
  16532. ["Move Column Right" org-metaright (org-at-table-p)]
  16533. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16534. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16535. ("Row"
  16536. ["Move Row Up" org-metaup (org-at-table-p)]
  16537. ["Move Row Down" org-metadown (org-at-table-p)]
  16538. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16539. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16540. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16541. "--"
  16542. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16543. ("Rectangle"
  16544. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16545. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16546. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16547. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16548. "--"
  16549. ("Calculate"
  16550. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16551. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16552. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16553. "--"
  16554. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16555. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16556. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16557. "--"
  16558. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16559. "--"
  16560. ["Sum Column/Rectangle" org-table-sum
  16561. (or (org-at-table-p) (org-region-active-p))]
  16562. ["Which Column?" org-table-current-column (org-at-table-p)])
  16563. ["Debug Formulas"
  16564. org-table-toggle-formula-debugger
  16565. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16566. ["Show Col/Row Numbers"
  16567. org-table-toggle-coordinate-overlays
  16568. :style toggle
  16569. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16570. "--"
  16571. ["Create" org-table-create (and (not (org-at-table-p))
  16572. org-enable-table-editor)]
  16573. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16574. ["Import from File" org-table-import (not (org-at-table-p))]
  16575. ["Export to File" org-table-export (org-at-table-p)]
  16576. "--"
  16577. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16578. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16579. '("Org"
  16580. ("Show/Hide"
  16581. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16582. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16583. ["Sparse Tree..." org-sparse-tree t]
  16584. ["Reveal Context" org-reveal t]
  16585. ["Show All" show-all t]
  16586. "--"
  16587. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16588. "--"
  16589. ["New Heading" org-insert-heading t]
  16590. ("Navigate Headings"
  16591. ["Up" outline-up-heading t]
  16592. ["Next" outline-next-visible-heading t]
  16593. ["Previous" outline-previous-visible-heading t]
  16594. ["Next Same Level" outline-forward-same-level t]
  16595. ["Previous Same Level" outline-backward-same-level t]
  16596. "--"
  16597. ["Jump" org-goto t])
  16598. ("Edit Structure"
  16599. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16600. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16601. "--"
  16602. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16603. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16604. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16605. "--"
  16606. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16607. "--"
  16608. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16609. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16610. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16611. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16612. "--"
  16613. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16614. "--"
  16615. ["Convert to odd levels" org-convert-to-odd-levels t]
  16616. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16617. ("Editing"
  16618. ["Emphasis..." org-emphasize t]
  16619. ["Edit Source Example" org-edit-special t]
  16620. "--"
  16621. ["Footnote new/jump" org-footnote-action t]
  16622. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16623. ("Archive"
  16624. ["Archive (default method)" org-archive-subtree-default t]
  16625. "--"
  16626. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16627. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16628. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16629. )
  16630. "--"
  16631. ("Hyperlinks"
  16632. ["Store Link (Global)" org-store-link t]
  16633. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16634. ["Insert Link" org-insert-link t]
  16635. ["Follow Link" org-open-at-point t]
  16636. "--"
  16637. ["Next link" org-next-link t]
  16638. ["Previous link" org-previous-link t]
  16639. "--"
  16640. ["Descriptive Links"
  16641. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16642. :style radio
  16643. :selected (member '(org-link) buffer-invisibility-spec)]
  16644. ["Literal Links"
  16645. (progn
  16646. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16647. :style radio
  16648. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16649. "--"
  16650. ("TODO Lists"
  16651. ["TODO/DONE/-" org-todo t]
  16652. ("Select keyword"
  16653. ["Next keyword" org-shiftright (org-on-heading-p)]
  16654. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16655. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16656. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16657. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16658. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16659. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16660. "--"
  16661. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16662. :selected org-enforce-todo-dependencies :style toggle :active t]
  16663. "Settings for tree at point"
  16664. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16665. :selected (org-entry-get nil "ORDERED")
  16666. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16667. ["Do Children parallel" org-toggle-ordered-property :style radio
  16668. :selected (not (org-entry-get nil "ORDERED"))
  16669. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16670. "--"
  16671. ["Set Priority" org-priority t]
  16672. ["Priority Up" org-shiftup t]
  16673. ["Priority Down" org-shiftdown t]
  16674. "--"
  16675. ["Get news from all feeds" org-feed-update-all t]
  16676. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16677. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16678. ("TAGS and Properties"
  16679. ["Set Tags" org-set-tags-command t]
  16680. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16681. "--"
  16682. ["Set property" org-set-property t]
  16683. ["Column view of properties" org-columns t]
  16684. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16685. ("Dates and Scheduling"
  16686. ["Timestamp" org-time-stamp t]
  16687. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16688. ("Change Date"
  16689. ["1 Day Later" org-shiftright t]
  16690. ["1 Day Earlier" org-shiftleft t]
  16691. ["1 ... Later" org-shiftup t]
  16692. ["1 ... Earlier" org-shiftdown t])
  16693. ["Compute Time Range" org-evaluate-time-range t]
  16694. ["Schedule Item" org-schedule t]
  16695. ["Deadline" org-deadline t]
  16696. "--"
  16697. ["Custom time format" org-toggle-time-stamp-overlays
  16698. :style radio :selected org-display-custom-times]
  16699. "--"
  16700. ["Goto Calendar" org-goto-calendar t]
  16701. ["Date from Calendar" org-date-from-calendar t]
  16702. "--"
  16703. ["Start/Restart Timer" org-timer-start t]
  16704. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16705. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16706. ["Insert Timer String" org-timer t]
  16707. ["Insert Timer Item" org-timer-item t])
  16708. ("Logging work"
  16709. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16710. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16711. ["Clock out" org-clock-out t]
  16712. ["Clock cancel" org-clock-cancel t]
  16713. "--"
  16714. ["Mark as default task" org-clock-mark-default-task t]
  16715. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16716. ["Goto running clock" org-clock-goto t]
  16717. "--"
  16718. ["Display times" org-clock-display t]
  16719. ["Create clock table" org-clock-report t]
  16720. "--"
  16721. ["Record DONE time"
  16722. (progn (setq org-log-done (not org-log-done))
  16723. (message "Switching to %s will %s record a timestamp"
  16724. (car org-done-keywords)
  16725. (if org-log-done "automatically" "not")))
  16726. :style toggle :selected org-log-done])
  16727. "--"
  16728. ["Agenda Command..." org-agenda t]
  16729. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16730. ("File List for Agenda")
  16731. ("Special views current file"
  16732. ["TODO Tree" org-show-todo-tree t]
  16733. ["Check Deadlines" org-check-deadlines t]
  16734. ["Timeline" org-timeline t]
  16735. ["Tags/Property tree" org-match-sparse-tree t])
  16736. "--"
  16737. ["Export/Publish..." org-export t]
  16738. ("LaTeX"
  16739. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16740. :selected org-cdlatex-mode]
  16741. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16742. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16743. ["Modify math symbol" org-cdlatex-math-modify
  16744. (org-inside-LaTeX-fragment-p)]
  16745. ["Insert citation" org-reftex-citation t]
  16746. "--"
  16747. ["Template for BEAMER" org-insert-beamer-options-template t])
  16748. "--"
  16749. ("MobileOrg"
  16750. ["Push Files and Views" org-mobile-push t]
  16751. ["Get Captured and Flagged" org-mobile-pull t]
  16752. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16753. "--"
  16754. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16755. "--"
  16756. ("Documentation"
  16757. ["Show Version" org-version t]
  16758. ["Info Documentation" org-info t])
  16759. ("Customize"
  16760. ["Browse Org Group" org-customize t]
  16761. "--"
  16762. ["Expand This Menu" org-create-customize-menu
  16763. (fboundp 'customize-menu-create)])
  16764. ["Send bug report" org-submit-bug-report t]
  16765. "--"
  16766. ("Refresh/Reload"
  16767. ["Refresh setup current buffer" org-mode-restart t]
  16768. ["Reload Org (after update)" org-reload t]
  16769. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16770. ))
  16771. (defun org-info (&optional node)
  16772. "Read documentation for Org-mode in the info system.
  16773. With optional NODE, go directly to that node."
  16774. (interactive)
  16775. (info (format "(org)%s" (or node ""))))
  16776. ;;;###autoload
  16777. (defun org-submit-bug-report ()
  16778. "Submit a bug report on Org-mode via mail.
  16779. Don't hesitate to report any problems or inaccurate documentation.
  16780. If you don't have setup sending mail from (X)Emacs, please copy the
  16781. output buffer into your mail program, as it gives us important
  16782. information about your Org-mode version and configuration."
  16783. (interactive)
  16784. (require 'reporter)
  16785. (org-load-modules-maybe)
  16786. (org-require-autoloaded-modules)
  16787. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16788. (reporter-submit-bug-report
  16789. "emacs-orgmode@gnu.org"
  16790. (org-version)
  16791. (let (list)
  16792. (save-window-excursion
  16793. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16794. (delete-other-windows)
  16795. (erase-buffer)
  16796. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16797. We would like to add your full Org-mode and Outline configuration to the
  16798. bug report. This greatly simplifies the work of the maintainer and
  16799. other experts on the mailing list.
  16800. HOWEVER, some variables you have customized may contain private
  16801. information. The names of customers, colleagues, or friends, might
  16802. appear in the form of file names, tags, todo states, or search strings.
  16803. If you answer yes to the prompt, you might want to check and remove
  16804. such private information before sending the email.")
  16805. (add-text-properties (point-min) (point-max) '(face org-warning))
  16806. (when (yes-or-no-p "Include your Org-mode configuration ")
  16807. (mapatoms
  16808. (lambda (v)
  16809. (and (boundp v)
  16810. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16811. (or (and (symbol-value v)
  16812. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16813. (and
  16814. (get v 'custom-type) (get v 'standard-value)
  16815. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16816. (push v list)))))
  16817. (kill-buffer (get-buffer "*Warn about privacy*"))
  16818. list))
  16819. nil nil
  16820. "Remember to cover the basics, that is, what you expected to happen and
  16821. what in fact did happen. You don't know how to make a good report? See
  16822. http://orgmode.org/manual/Feedback.html#Feedback
  16823. Your bug report will be posted to the Org-mode mailing list.
  16824. ------------------------------------------------------------------------")
  16825. (save-excursion
  16826. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16827. (replace-match "\\1Bug: \\3 [\\2]")))))
  16828. (defun org-install-agenda-files-menu ()
  16829. (let ((bl (buffer-list)))
  16830. (save-excursion
  16831. (while bl
  16832. (set-buffer (pop bl))
  16833. (if (org-mode-p) (setq bl nil)))
  16834. (when (org-mode-p)
  16835. (easy-menu-change
  16836. '("Org") "File List for Agenda"
  16837. (append
  16838. (list
  16839. ["Edit File List" (org-edit-agenda-file-list) t]
  16840. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16841. ["Remove Current File from List" org-remove-file t]
  16842. ["Cycle through agenda files" org-cycle-agenda-files t]
  16843. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16844. "--")
  16845. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16846. ;;;; Documentation
  16847. ;;;###autoload
  16848. (defun org-require-autoloaded-modules ()
  16849. (interactive)
  16850. (mapc 'require
  16851. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16852. org-docbook org-exp org-html org-icalendar
  16853. org-id org-latex
  16854. org-publish org-remember org-table
  16855. org-timer org-xoxo)))
  16856. ;;;###autoload
  16857. (defun org-reload (&optional uncompiled)
  16858. "Reload all org lisp files.
  16859. With prefix arg UNCOMPILED, load the uncompiled versions."
  16860. (interactive "P")
  16861. (require 'find-func)
  16862. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16863. (dir-org (file-name-directory (org-find-library-name "org")))
  16864. (dir-org-contrib (ignore-errors
  16865. (file-name-directory
  16866. (org-find-library-name "org-contribdir"))))
  16867. (babel-files
  16868. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16869. (append (list nil "comint" "eval" "exp" "keys"
  16870. "lob" "ref" "table" "tangle")
  16871. (delq nil
  16872. (mapcar
  16873. (lambda (lang)
  16874. (when (cdr lang) (symbol-name (car lang))))
  16875. org-babel-load-languages)))))
  16876. (files
  16877. (append (directory-files dir-org t file-re)
  16878. babel-files
  16879. (and dir-org-contrib
  16880. (directory-files dir-org-contrib t file-re))))
  16881. (remove-re (concat (if (featurep 'xemacs)
  16882. "org-colview" "org-colview-xemacs")
  16883. "\\'")))
  16884. (setq files (mapcar 'file-name-sans-extension files))
  16885. (setq files (mapcar
  16886. (lambda (x) (if (string-match remove-re x) nil x))
  16887. files))
  16888. (setq files (delq nil files))
  16889. (mapc
  16890. (lambda (f)
  16891. (when (featurep (intern (file-name-nondirectory f)))
  16892. (if (and (not uncompiled)
  16893. (file-exists-p (concat f ".elc")))
  16894. (load (concat f ".elc") nil nil t)
  16895. (load (concat f ".el") nil nil t))))
  16896. files))
  16897. (org-version))
  16898. ;;;###autoload
  16899. (defun org-customize ()
  16900. "Call the customize function with org as argument."
  16901. (interactive)
  16902. (org-load-modules-maybe)
  16903. (org-require-autoloaded-modules)
  16904. (customize-browse 'org))
  16905. (defun org-create-customize-menu ()
  16906. "Create a full customization menu for Org-mode, insert it into the menu."
  16907. (interactive)
  16908. (org-load-modules-maybe)
  16909. (org-require-autoloaded-modules)
  16910. (if (fboundp 'customize-menu-create)
  16911. (progn
  16912. (easy-menu-change
  16913. '("Org") "Customize"
  16914. `(["Browse Org group" org-customize t]
  16915. "--"
  16916. ,(customize-menu-create 'org)
  16917. ["Set" Custom-set t]
  16918. ["Save" Custom-save t]
  16919. ["Reset to Current" Custom-reset-current t]
  16920. ["Reset to Saved" Custom-reset-saved t]
  16921. ["Reset to Standard Settings" Custom-reset-standard t]))
  16922. (message "\"Org\"-menu now contains full customization menu"))
  16923. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16924. ;;;; Miscellaneous stuff
  16925. ;;; Generally useful functions
  16926. (defun org-get-at-bol (property)
  16927. "Get text property PROPERTY at beginning of line."
  16928. (get-text-property (point-at-bol) property))
  16929. (defun org-find-text-property-in-string (prop s)
  16930. "Return the first non-nil value of property PROP in string S."
  16931. (or (get-text-property 0 prop s)
  16932. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16933. prop s)))
  16934. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16935. "Display the given MESSAGE as a warning."
  16936. (if (fboundp 'display-warning)
  16937. (display-warning 'org message
  16938. (if (featurep 'xemacs) 'warning :warning))
  16939. (let ((buf (get-buffer-create "*Org warnings*")))
  16940. (with-current-buffer buf
  16941. (goto-char (point-max))
  16942. (insert "Warning (Org): " message)
  16943. (unless (bolp)
  16944. (newline)))
  16945. (display-buffer buf)
  16946. (sit-for 0))))
  16947. (defun org-eval (form)
  16948. "Eval FORM and return result."
  16949. (condition-case error
  16950. (eval form)
  16951. (error (format "%%![Error: %s]" error))))
  16952. (defun org-in-commented-line ()
  16953. "Is point in a line starting with `#'?"
  16954. (equal (char-after (point-at-bol)) ?#))
  16955. (defun org-in-indented-comment-line ()
  16956. "Is point in a line starting with `#' after some white space?"
  16957. (save-excursion
  16958. (save-match-data
  16959. (goto-char (point-at-bol))
  16960. (looking-at "[ \t]*#"))))
  16961. (defun org-in-verbatim-emphasis ()
  16962. (save-match-data
  16963. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16964. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16965. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16966. (if (and marker (marker-buffer marker)
  16967. (buffer-live-p (marker-buffer marker)))
  16968. (progn
  16969. (switch-to-buffer (marker-buffer marker))
  16970. (if (or (> marker (point-max)) (< marker (point-min)))
  16971. (widen))
  16972. (goto-char marker)
  16973. (org-show-context 'org-goto))
  16974. (if bookmark
  16975. (bookmark-jump bookmark)
  16976. (error "Cannot find location"))))
  16977. (defun org-quote-csv-field (s)
  16978. "Quote field for inclusion in CSV material."
  16979. (if (string-match "[\",]" s)
  16980. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16981. s))
  16982. (defun org-force-self-insert (N)
  16983. "Needed to enforce self-insert under remapping."
  16984. (interactive "p")
  16985. (self-insert-command N))
  16986. (defun org-string-width (s)
  16987. "Compute width of string, ignoring invisible characters.
  16988. This ignores character with invisibility property `org-link', and also
  16989. characters with property `org-cwidth', because these will become invisible
  16990. upon the next fontification round."
  16991. (let (b l)
  16992. (when (or (eq t buffer-invisibility-spec)
  16993. (assq 'org-link buffer-invisibility-spec))
  16994. (while (setq b (text-property-any 0 (length s)
  16995. 'invisible 'org-link s))
  16996. (setq s (concat (substring s 0 b)
  16997. (substring s (or (next-single-property-change
  16998. b 'invisible s) (length s)))))))
  16999. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17000. (setq s (concat (substring s 0 b)
  17001. (substring s (or (next-single-property-change
  17002. b 'org-cwidth s) (length s))))))
  17003. (setq l (string-width s) b -1)
  17004. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17005. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17006. l))
  17007. (defun org-shorten-string (s maxlength)
  17008. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17009. If the string is shorter or has length MAXLENGTH, just return the
  17010. original string. If it is longer, the functions finds a space in the
  17011. string, breaks this string off at that locations and adds three dots
  17012. as ellipsis. Including the ellipsis, the string will not be longer
  17013. than MAXLENGTH. If finding a good breaking point in the string does
  17014. not work, the string is just chopped off in the middle of a word
  17015. if necessary."
  17016. (if (<= (length s) maxlength)
  17017. s
  17018. (let* ((n (max (- maxlength 4) 1))
  17019. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17020. (if (string-match re s)
  17021. (concat (match-string 1 s) "...")
  17022. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17023. (defun org-get-indentation (&optional line)
  17024. "Get the indentation of the current line, interpreting tabs.
  17025. When LINE is given, assume it represents a line and compute its indentation."
  17026. (if line
  17027. (if (string-match "^ *" (org-remove-tabs line))
  17028. (match-end 0))
  17029. (save-excursion
  17030. (beginning-of-line 1)
  17031. (skip-chars-forward " \t")
  17032. (current-column))))
  17033. (defun org-get-string-indentation (s)
  17034. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17035. (let ((n -1) (i 0) (w tab-width) c)
  17036. (catch 'exit
  17037. (while (< (setq n (1+ n)) (length s))
  17038. (setq c (aref s n))
  17039. (cond ((= c ?\ ) (setq i (1+ i)))
  17040. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17041. (t (throw 'exit t)))))
  17042. i))
  17043. (defun org-remove-tabs (s &optional width)
  17044. "Replace tabulators in S with spaces.
  17045. Assumes that s is a single line, starting in column 0."
  17046. (setq width (or width tab-width))
  17047. (while (string-match "\t" s)
  17048. (setq s (replace-match
  17049. (make-string
  17050. (- (* width (/ (+ (match-beginning 0) width) width))
  17051. (match-beginning 0)) ?\ )
  17052. t t s)))
  17053. s)
  17054. (defun org-fix-indentation (line ind)
  17055. "Fix indentation in LINE.
  17056. IND is a cons cell with target and minimum indentation.
  17057. If the current indentation in LINE is smaller than the minimum,
  17058. leave it alone. If it is larger than ind, set it to the target."
  17059. (let* ((l (org-remove-tabs line))
  17060. (i (org-get-indentation l))
  17061. (i1 (car ind)) (i2 (cdr ind)))
  17062. (if (>= i i2) (setq l (substring line i2)))
  17063. (if (> i1 0)
  17064. (concat (make-string i1 ?\ ) l)
  17065. l)))
  17066. (defun org-remove-indentation (code &optional n)
  17067. "Remove the maximum common indentation from the lines in CODE.
  17068. N may optionally be the number of spaces to remove."
  17069. (with-temp-buffer
  17070. (insert code)
  17071. (org-do-remove-indentation n)
  17072. (buffer-string)))
  17073. (defun org-do-remove-indentation (&optional n)
  17074. "Remove the maximum common indentation from the buffer."
  17075. (untabify (point-min) (point-max))
  17076. (let ((min 10000) re)
  17077. (if n
  17078. (setq min n)
  17079. (goto-char (point-min))
  17080. (while (re-search-forward "^ *[^ \n]" nil t)
  17081. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17082. (unless (or (= min 0) (= min 10000))
  17083. (setq re (format "^ \\{%d\\}" min))
  17084. (goto-char (point-min))
  17085. (while (re-search-forward re nil t)
  17086. (replace-match "")
  17087. (end-of-line 1))
  17088. min)))
  17089. (defun org-fill-template (template alist)
  17090. "Find each %key of ALIST in TEMPLATE and replace it."
  17091. (let ((case-fold-search nil)
  17092. entry key value)
  17093. (setq alist (sort (copy-sequence alist)
  17094. (lambda (a b) (< (length (car a)) (length (car b))))))
  17095. (while (setq entry (pop alist))
  17096. (setq template
  17097. (replace-regexp-in-string
  17098. (concat "%" (regexp-quote (car entry)))
  17099. (cdr entry) template t t)))
  17100. template))
  17101. (defun org-base-buffer (buffer)
  17102. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17103. (if (not buffer)
  17104. buffer
  17105. (or (buffer-base-buffer buffer)
  17106. buffer)))
  17107. (defun org-trim (s)
  17108. "Remove whitespace at beginning and end of string."
  17109. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17110. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17111. s)
  17112. (defun org-wrap (string &optional width lines)
  17113. "Wrap string to either a number of lines, or a width in characters.
  17114. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17115. that costs. If there is a word longer than WIDTH, the text is actually
  17116. wrapped to the length of that word.
  17117. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17118. many lines, whatever width that takes.
  17119. The return value is a list of lines, without newlines at the end."
  17120. (let* ((words (org-split-string string "[ \t\n]+"))
  17121. (maxword (apply 'max (mapcar 'org-string-width words)))
  17122. w ll)
  17123. (cond (width
  17124. (org-do-wrap words (max maxword width)))
  17125. (lines
  17126. (setq w maxword)
  17127. (setq ll (org-do-wrap words maxword))
  17128. (if (<= (length ll) lines)
  17129. ll
  17130. (setq ll words)
  17131. (while (> (length ll) lines)
  17132. (setq w (1+ w))
  17133. (setq ll (org-do-wrap words w)))
  17134. ll))
  17135. (t (error "Cannot wrap this")))))
  17136. (defun org-do-wrap (words width)
  17137. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17138. (let (lines line)
  17139. (while words
  17140. (setq line (pop words))
  17141. (while (and words (< (+ (length line) (length (car words))) width))
  17142. (setq line (concat line " " (pop words))))
  17143. (setq lines (push line lines)))
  17144. (nreverse lines)))
  17145. (defun org-split-string (string &optional separators)
  17146. "Splits STRING into substrings at SEPARATORS.
  17147. No empty strings are returned if there are matches at the beginning
  17148. and end of string."
  17149. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17150. (start 0)
  17151. notfirst
  17152. (list nil))
  17153. (while (and (string-match rexp string
  17154. (if (and notfirst
  17155. (= start (match-beginning 0))
  17156. (< start (length string)))
  17157. (1+ start) start))
  17158. (< (match-beginning 0) (length string)))
  17159. (setq notfirst t)
  17160. (or (eq (match-beginning 0) 0)
  17161. (and (eq (match-beginning 0) (match-end 0))
  17162. (eq (match-beginning 0) start))
  17163. (setq list
  17164. (cons (substring string start (match-beginning 0))
  17165. list)))
  17166. (setq start (match-end 0)))
  17167. (or (eq start (length string))
  17168. (setq list
  17169. (cons (substring string start)
  17170. list)))
  17171. (nreverse list)))
  17172. (defun org-quote-vert (s)
  17173. "Replace \"|\" with \"\\vert\"."
  17174. (while (string-match "|" s)
  17175. (setq s (replace-match "\\vert" t t s)))
  17176. s)
  17177. (defun org-uuidgen-p (s)
  17178. "Is S an ID created by UUIDGEN?"
  17179. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17180. (defun org-context ()
  17181. "Return a list of contexts of the current cursor position.
  17182. If several contexts apply, all are returned.
  17183. Each context entry is a list with a symbol naming the context, and
  17184. two positions indicating start and end of the context. Possible
  17185. contexts are:
  17186. :headline anywhere in a headline
  17187. :headline-stars on the leading stars in a headline
  17188. :todo-keyword on a TODO keyword (including DONE) in a headline
  17189. :tags on the TAGS in a headline
  17190. :priority on the priority cookie in a headline
  17191. :item on the first line of a plain list item
  17192. :item-bullet on the bullet/number of a plain list item
  17193. :checkbox on the checkbox in a plain list item
  17194. :table in an org-mode table
  17195. :table-special on a special filed in a table
  17196. :table-table in a table.el table
  17197. :link on a hyperlink
  17198. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17199. :target on a <<target>>
  17200. :radio-target on a <<<radio-target>>>
  17201. :latex-fragment on a LaTeX fragment
  17202. :latex-preview on a LaTeX fragment with overlayed preview image
  17203. This function expects the position to be visible because it uses font-lock
  17204. faces as a help to recognize the following contexts: :table-special, :link,
  17205. and :keyword."
  17206. (let* ((f (get-text-property (point) 'face))
  17207. (faces (if (listp f) f (list f)))
  17208. (p (point)) clist o)
  17209. ;; First the large context
  17210. (cond
  17211. ((org-on-heading-p t)
  17212. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17213. (when (progn
  17214. (beginning-of-line 1)
  17215. (looking-at org-todo-line-tags-regexp))
  17216. (push (org-point-in-group p 1 :headline-stars) clist)
  17217. (push (org-point-in-group p 2 :todo-keyword) clist)
  17218. (push (org-point-in-group p 4 :tags) clist))
  17219. (goto-char p)
  17220. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17221. (if (looking-at "\\[#[A-Z0-9]\\]")
  17222. (push (org-point-in-group p 0 :priority) clist)))
  17223. ((org-at-item-p)
  17224. (push (org-point-in-group p 2 :item-bullet) clist)
  17225. (push (list :item (point-at-bol)
  17226. (save-excursion (org-end-of-item) (point)))
  17227. clist)
  17228. (and (org-at-item-checkbox-p)
  17229. (push (org-point-in-group p 0 :checkbox) clist)))
  17230. ((org-at-table-p)
  17231. (push (list :table (org-table-begin) (org-table-end)) clist)
  17232. (if (memq 'org-formula faces)
  17233. (push (list :table-special
  17234. (previous-single-property-change p 'face)
  17235. (next-single-property-change p 'face)) clist)))
  17236. ((org-at-table-p 'any)
  17237. (push (list :table-table) clist)))
  17238. (goto-char p)
  17239. ;; Now the small context
  17240. (cond
  17241. ((org-at-timestamp-p)
  17242. (push (org-point-in-group p 0 :timestamp) clist))
  17243. ((memq 'org-link faces)
  17244. (push (list :link
  17245. (previous-single-property-change p 'face)
  17246. (next-single-property-change p 'face)) clist))
  17247. ((memq 'org-special-keyword faces)
  17248. (push (list :keyword
  17249. (previous-single-property-change p 'face)
  17250. (next-single-property-change p 'face)) clist))
  17251. ((org-on-target-p)
  17252. (push (org-point-in-group p 0 :target) clist)
  17253. (goto-char (1- (match-beginning 0)))
  17254. (if (looking-at org-radio-target-regexp)
  17255. (push (org-point-in-group p 0 :radio-target) clist))
  17256. (goto-char p))
  17257. ((setq o (car (delq nil
  17258. (mapcar
  17259. (lambda (x)
  17260. (if (memq x org-latex-fragment-image-overlays) x))
  17261. (overlays-at (point))))))
  17262. (push (list :latex-fragment
  17263. (overlay-start o) (overlay-end o)) clist)
  17264. (push (list :latex-preview
  17265. (overlay-start o) (overlay-end o)) clist))
  17266. ((org-inside-LaTeX-fragment-p)
  17267. ;; FIXME: positions wrong.
  17268. (push (list :latex-fragment (point) (point)) clist)))
  17269. (setq clist (nreverse (delq nil clist)))
  17270. clist))
  17271. ;; FIXME: Compare with at-regexp-p Do we need both?
  17272. (defun org-in-regexp (re &optional nlines visually)
  17273. "Check if point is inside a match of regexp.
  17274. Normally only the current line is checked, but you can include NLINES extra
  17275. lines both before and after point into the search.
  17276. If VISUALLY is set, require that the cursor is not after the match but
  17277. really on, so that the block visually is on the match."
  17278. (catch 'exit
  17279. (let ((pos (point))
  17280. (eol (point-at-eol (+ 1 (or nlines 0))))
  17281. (inc (if visually 1 0)))
  17282. (save-excursion
  17283. (beginning-of-line (- 1 (or nlines 0)))
  17284. (while (re-search-forward re eol t)
  17285. (if (and (<= (match-beginning 0) pos)
  17286. (>= (+ inc (match-end 0)) pos))
  17287. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17288. (defun org-at-regexp-p (regexp)
  17289. "Is point inside a match of REGEXP in the current line?"
  17290. (catch 'exit
  17291. (save-excursion
  17292. (let ((pos (point)) (end (point-at-eol)))
  17293. (beginning-of-line 1)
  17294. (while (re-search-forward regexp end t)
  17295. (if (and (<= (match-beginning 0) pos)
  17296. (>= (match-end 0) pos))
  17297. (throw 'exit t)))
  17298. nil))))
  17299. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  17300. "Return t if the current point is between matches of START-RE and END-RE.
  17301. This will also return t if point is on one of the two matches or
  17302. in an unfinished block. END-RE can be a string or a form
  17303. returning a string.
  17304. An optional third argument bounds the search for START-RE. It
  17305. defaults to previous heading or `point-min'."
  17306. (let ((pos (point))
  17307. (limit (or bound (save-excursion (outline-previous-heading)))))
  17308. (save-excursion
  17309. ;; we're on a block when point is on start-re...
  17310. (or (org-at-regexp-p start-re)
  17311. ;; ... or start-re can be found above...
  17312. (and (re-search-backward start-re limit t)
  17313. ;; ... but no end-re between start-re and point.
  17314. (not (re-search-forward (eval end-re) pos t)))))))
  17315. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17316. "Call `multi-occur' with buffers for all agenda files."
  17317. (interactive "sOrg-files matching: \np")
  17318. (let* ((files (org-agenda-files))
  17319. (tnames (mapcar 'file-truename files))
  17320. (extra org-agenda-text-search-extra-files)
  17321. f)
  17322. (when (eq (car extra) 'agenda-archives)
  17323. (setq extra (cdr extra))
  17324. (setq files (org-add-archive-files files)))
  17325. (while (setq f (pop extra))
  17326. (unless (member (file-truename f) tnames)
  17327. (add-to-list 'files f 'append)
  17328. (add-to-list 'tnames (file-truename f) 'append)))
  17329. (multi-occur
  17330. (mapcar (lambda (x)
  17331. (with-current-buffer
  17332. (or (get-file-buffer x) (find-file-noselect x))
  17333. (widen)
  17334. (current-buffer)))
  17335. files)
  17336. regexp)))
  17337. (if (boundp 'occur-mode-find-occurrence-hook)
  17338. ;; Emacs 23
  17339. (add-hook 'occur-mode-find-occurrence-hook
  17340. (lambda ()
  17341. (when (org-mode-p)
  17342. (org-reveal))))
  17343. ;; Emacs 22
  17344. (defadvice occur-mode-goto-occurrence
  17345. (after org-occur-reveal activate)
  17346. (and (org-mode-p) (org-reveal)))
  17347. (defadvice occur-mode-goto-occurrence-other-window
  17348. (after org-occur-reveal activate)
  17349. (and (org-mode-p) (org-reveal)))
  17350. (defadvice occur-mode-display-occurrence
  17351. (after org-occur-reveal activate)
  17352. (when (org-mode-p)
  17353. (let ((pos (occur-mode-find-occurrence)))
  17354. (with-current-buffer (marker-buffer pos)
  17355. (save-excursion
  17356. (goto-char pos)
  17357. (org-reveal)))))))
  17358. (defun org-occur-link-in-agenda-files ()
  17359. "Create a link and search for it in the agendas.
  17360. The link is not stored in `org-stored-links', it is just created
  17361. for the search purpose."
  17362. (interactive)
  17363. (let ((link (condition-case nil
  17364. (org-store-link nil)
  17365. (error "Unable to create a link to here"))))
  17366. (org-occur-in-agenda-files (regexp-quote link))))
  17367. (defun org-uniquify (list)
  17368. "Remove duplicate elements from LIST."
  17369. (let (res)
  17370. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  17371. res))
  17372. (defun org-delete-all (elts list)
  17373. "Remove all elements in ELTS from LIST."
  17374. (while elts
  17375. (setq list (delete (pop elts) list)))
  17376. list)
  17377. (defun org-count (cl-item cl-seq)
  17378. "Count the number of occurrences of ITEM in SEQ.
  17379. Taken from `count' in cl-seq.el with all keyword arguments removed."
  17380. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  17381. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  17382. (while (< cl-start cl-end)
  17383. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  17384. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  17385. (setq cl-start (1+ cl-start)))
  17386. cl-count))
  17387. (defun org-remove-if (predicate seq)
  17388. "Remove everything from SEQ that fulfills PREDICATE."
  17389. (let (res e)
  17390. (while seq
  17391. (setq e (pop seq))
  17392. (if (not (funcall predicate e)) (push e res)))
  17393. (nreverse res)))
  17394. (defun org-remove-if-not (predicate seq)
  17395. "Remove everything from SEQ that does not fulfill PREDICATE."
  17396. (let (res e)
  17397. (while seq
  17398. (setq e (pop seq))
  17399. (if (funcall predicate e) (push e res)))
  17400. (nreverse res)))
  17401. (defun org-back-over-empty-lines ()
  17402. "Move backwards over whitespace, to the beginning of the first empty line.
  17403. Returns the number of empty lines passed."
  17404. (let ((pos (point)))
  17405. (if (cdr (assoc 'heading org-blank-before-new-entry))
  17406. (skip-chars-backward " \t\n\r")
  17407. (forward-line -1))
  17408. (beginning-of-line 2)
  17409. (goto-char (min (point) pos))
  17410. (count-lines (point) pos)))
  17411. (defun org-skip-whitespace ()
  17412. (skip-chars-forward " \t\n\r"))
  17413. (defun org-point-in-group (point group &optional context)
  17414. "Check if POINT is in match-group GROUP.
  17415. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  17416. match. If the match group does not exist or point is not inside it,
  17417. return nil."
  17418. (and (match-beginning group)
  17419. (>= point (match-beginning group))
  17420. (<= point (match-end group))
  17421. (if context
  17422. (list context (match-beginning group) (match-end group))
  17423. t)))
  17424. (defun org-switch-to-buffer-other-window (&rest args)
  17425. "Switch to buffer in a second window on the current frame.
  17426. In particular, do not allow pop-up frames.
  17427. Returns the newly created buffer."
  17428. (let (pop-up-frames special-display-buffer-names special-display-regexps
  17429. special-display-function)
  17430. (apply 'switch-to-buffer-other-window args)))
  17431. (defun org-combine-plists (&rest plists)
  17432. "Create a single property list from all plists in PLISTS.
  17433. The process starts by copying the first list, and then setting properties
  17434. from the other lists. Settings in the last list are the most significant
  17435. ones and overrule settings in the other lists."
  17436. (let ((rtn (copy-sequence (pop plists)))
  17437. p v ls)
  17438. (while plists
  17439. (setq ls (pop plists))
  17440. (while ls
  17441. (setq p (pop ls) v (pop ls))
  17442. (setq rtn (plist-put rtn p v))))
  17443. rtn))
  17444. (defun org-move-line-down (arg)
  17445. "Move the current line down. With prefix argument, move it past ARG lines."
  17446. (interactive "p")
  17447. (let ((col (current-column))
  17448. beg end pos)
  17449. (beginning-of-line 1) (setq beg (point))
  17450. (beginning-of-line 2) (setq end (point))
  17451. (beginning-of-line (+ 1 arg))
  17452. (setq pos (move-marker (make-marker) (point)))
  17453. (insert (delete-and-extract-region beg end))
  17454. (goto-char pos)
  17455. (org-move-to-column col)))
  17456. (defun org-move-line-up (arg)
  17457. "Move the current line up. With prefix argument, move it past ARG lines."
  17458. (interactive "p")
  17459. (let ((col (current-column))
  17460. beg end pos)
  17461. (beginning-of-line 1) (setq beg (point))
  17462. (beginning-of-line 2) (setq end (point))
  17463. (beginning-of-line (- arg))
  17464. (setq pos (move-marker (make-marker) (point)))
  17465. (insert (delete-and-extract-region beg end))
  17466. (goto-char pos)
  17467. (org-move-to-column col)))
  17468. (defun org-replace-escapes (string table)
  17469. "Replace %-escapes in STRING with values in TABLE.
  17470. TABLE is an association list with keys like \"%a\" and string values.
  17471. The sequences in STRING may contain normal field width and padding information,
  17472. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  17473. so values can contain further %-escapes if they are define later in TABLE."
  17474. (let ((tbl (copy-alist table))
  17475. (case-fold-search nil)
  17476. (pchg 0)
  17477. e re rpl)
  17478. (while (setq e (pop tbl))
  17479. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  17480. (when (and (cdr e) (string-match re (cdr e)))
  17481. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  17482. (safe "SREF"))
  17483. (add-text-properties 0 3 (list 'sref sref) safe)
  17484. (setcdr e (replace-match safe t t (cdr e)))))
  17485. (while (string-match re string)
  17486. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  17487. (cdr e)))
  17488. (setq string (replace-match rpl t t string))))
  17489. (while (setq pchg (next-property-change pchg string))
  17490. (let ((sref (get-text-property pchg 'sref string)))
  17491. (when (and sref (string-match "SREF" string pchg))
  17492. (setq string (replace-match sref t t string)))))
  17493. string))
  17494. (defun org-sublist (list start end)
  17495. "Return a section of LIST, from START to END.
  17496. Counting starts at 1."
  17497. (let (rtn (c start))
  17498. (setq list (nthcdr (1- start) list))
  17499. (while (and list (<= c end))
  17500. (push (pop list) rtn)
  17501. (setq c (1+ c)))
  17502. (nreverse rtn)))
  17503. (defun org-find-base-buffer-visiting (file)
  17504. "Like `find-buffer-visiting' but always return the base buffer and
  17505. not an indirect buffer."
  17506. (let ((buf (or (get-file-buffer file)
  17507. (find-buffer-visiting file))))
  17508. (if buf
  17509. (or (buffer-base-buffer buf) buf)
  17510. nil)))
  17511. (defun org-image-file-name-regexp (&optional extensions)
  17512. "Return regexp matching the file names of images.
  17513. If EXTENSIONS is given, only match these."
  17514. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  17515. (image-file-name-regexp)
  17516. (let ((image-file-name-extensions
  17517. (or extensions
  17518. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17519. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  17520. (concat "\\."
  17521. (regexp-opt (nconc (mapcar 'upcase
  17522. image-file-name-extensions)
  17523. image-file-name-extensions)
  17524. t)
  17525. "\\'"))))
  17526. (defun org-file-image-p (file &optional extensions)
  17527. "Return non-nil if FILE is an image."
  17528. (save-match-data
  17529. (string-match (org-image-file-name-regexp extensions) file)))
  17530. (defun org-get-cursor-date ()
  17531. "Return the date at cursor in as a time.
  17532. This works in the calendar and in the agenda, anywhere else it just
  17533. returns the current time."
  17534. (let (date day defd)
  17535. (cond
  17536. ((eq major-mode 'calendar-mode)
  17537. (setq date (calendar-cursor-to-date)
  17538. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17539. ((eq major-mode 'org-agenda-mode)
  17540. (setq day (get-text-property (point) 'day))
  17541. (if day
  17542. (setq date (calendar-gregorian-from-absolute day)
  17543. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17544. (nth 2 date))))))
  17545. (or defd (current-time))))
  17546. (defvar org-agenda-action-marker (make-marker)
  17547. "Marker pointing to the entry for the next agenda action.")
  17548. (defun org-mark-entry-for-agenda-action ()
  17549. "Mark the current entry as target of an agenda action.
  17550. Agenda actions are actions executed from the agenda with the key `k',
  17551. which make use of the date at the cursor."
  17552. (interactive)
  17553. (move-marker org-agenda-action-marker
  17554. (save-excursion (org-back-to-heading t) (point))
  17555. (current-buffer))
  17556. (message
  17557. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17558. (defun org-mark-subtree ()
  17559. "Mark the current subtree.
  17560. This puts point at the start of the current subtree, and mark at the end.
  17561. If point is in an inline task, mark that task instead."
  17562. (interactive)
  17563. (let ((inline-task-p
  17564. (and (featurep 'org-inlinetask)
  17565. (org-inlinetask-in-task-p)))
  17566. (beg))
  17567. ;; Get beginning of subtree
  17568. (cond
  17569. (inline-task-p (org-inlinetask-goto-beginning))
  17570. ((org-at-heading-p) (beginning-of-line))
  17571. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  17572. (setq beg (point))
  17573. ;; Get end of it
  17574. (if inline-task-p
  17575. (org-inlinetask-goto-end)
  17576. (org-end-of-subtree))
  17577. ;; Mark zone
  17578. (push-mark (point) nil t)
  17579. (goto-char beg)))
  17580. ;;; Paragraph filling stuff.
  17581. ;; We want this to be just right, so use the full arsenal.
  17582. (defun org-indent-line-function ()
  17583. "Indent line depending on context."
  17584. (interactive)
  17585. (let* ((pos (point))
  17586. (itemp (org-at-item-p))
  17587. (case-fold-search t)
  17588. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17589. (inline-task-p (and (featurep 'org-inlinetask)
  17590. (org-inlinetask-in-task-p)))
  17591. (inline-re (and inline-task-p
  17592. (org-inlinetask-outline-regexp)))
  17593. column)
  17594. (beginning-of-line 1)
  17595. (cond
  17596. ;; Comments
  17597. ((looking-at "# ") (setq column 0))
  17598. ;; Headings
  17599. ((looking-at "\\*+ ") (setq column 0))
  17600. ;; Included files
  17601. ((looking-at "#\\+include:") (setq column 0))
  17602. ;; Footnote definition
  17603. ((looking-at org-footnote-definition-re) (setq column 0))
  17604. ;; Literal examples
  17605. ((looking-at "[ \t]*:[ \t]")
  17606. (setq column (org-get-indentation))) ; do nothing
  17607. ;; Lists
  17608. ((ignore-errors (goto-char (org-in-item-p)))
  17609. (setq column (if itemp
  17610. (org-get-indentation)
  17611. (org-list-item-body-column (point))))
  17612. (goto-char pos))
  17613. ;; Drawers
  17614. ((and (looking-at "[ \t]*:END:")
  17615. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17616. (save-excursion
  17617. (goto-char (1- (match-beginning 1)))
  17618. (setq column (current-column))))
  17619. ;; Special blocks
  17620. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17621. (save-excursion
  17622. (re-search-backward
  17623. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17624. (setq column (org-get-indentation (match-string 0))))
  17625. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17626. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17627. (save-excursion
  17628. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17629. (setq column
  17630. (if (equal (downcase (match-string 1)) "src")
  17631. ;; src blocks: let `org-edit-src-exit' handle them
  17632. (org-get-indentation)
  17633. (org-get-indentation (match-string 0)))))
  17634. ;; This line has nothing special, look at the previous relevant
  17635. ;; line to compute indentation
  17636. (t
  17637. (beginning-of-line 0)
  17638. (while (and (not (bobp))
  17639. (not (looking-at org-drawer-regexp))
  17640. ;; When point started in an inline task, do not move
  17641. ;; above task starting line.
  17642. (not (and inline-task-p (looking-at inline-re)))
  17643. ;; Skip drawers, blocks, empty lines, verbatim,
  17644. ;; comments, tables, footnotes definitions, lists,
  17645. ;; inline tasks.
  17646. (or (and (looking-at "[ \t]*:END:")
  17647. (re-search-backward org-drawer-regexp nil t))
  17648. (and (looking-at "[ \t]*#\\+end_")
  17649. (re-search-backward "[ \t]*#\\+begin_"nil t))
  17650. (looking-at "[ \t]*[\n:#|]")
  17651. (looking-at org-footnote-definition-re)
  17652. (and (ignore-errors (goto-char (org-in-item-p)))
  17653. (goto-char
  17654. (org-list-get-top-point (org-list-struct))))
  17655. (and (not inline-task-p)
  17656. (featurep 'org-inlinetask)
  17657. (org-inlinetask-in-task-p)
  17658. (or (org-inlinetask-goto-beginning) t))))
  17659. (beginning-of-line 0))
  17660. (cond
  17661. ;; There was an heading above.
  17662. ((looking-at "\\*+[ \t]+")
  17663. (if (not org-adapt-indentation)
  17664. (setq column 0)
  17665. (goto-char (match-end 0))
  17666. (setq column (current-column))))
  17667. ;; A drawer had started and is unfinished
  17668. ((looking-at org-drawer-regexp)
  17669. (goto-char (1- (match-beginning 1)))
  17670. (setq column (current-column)))
  17671. ;; Else, nothing noticeable found: get indentation and go on.
  17672. (t (setq column (org-get-indentation))))))
  17673. ;; Now apply indentation and move cursor accordingly
  17674. (goto-char pos)
  17675. (if (<= (current-column) (current-indentation))
  17676. (org-indent-line-to column)
  17677. (save-excursion (org-indent-line-to column)))
  17678. ;; Special polishing for properties, see `org-property-format'
  17679. (setq column (current-column))
  17680. (beginning-of-line 1)
  17681. (if (looking-at
  17682. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17683. (replace-match (concat (match-string 1)
  17684. (format org-property-format
  17685. (match-string 2) (match-string 3)))
  17686. t t))
  17687. (org-move-to-column column)))
  17688. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17689. "Variable to store copy of `adaptive-fill-regexp'.
  17690. Since `adaptive-fill-regexp' is set to never match, we need to
  17691. store a backup of its value before entering `org-mode' so that
  17692. the functionality can be provided as a fall-back.")
  17693. (defun org-set-autofill-regexps ()
  17694. (interactive)
  17695. ;; In the paragraph separator we include headlines, because filling
  17696. ;; text in a line directly attached to a headline would otherwise
  17697. ;; fill the headline as well.
  17698. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17699. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17700. ;; The paragraph starter includes hand-formatted lists.
  17701. (org-set-local
  17702. 'paragraph-start
  17703. (concat
  17704. "\f" "\\|"
  17705. "[ ]*$" "\\|"
  17706. "\\*+ " "\\|"
  17707. "[ \t]*#" "\\|"
  17708. (org-item-re) "\\|"
  17709. "[ \t]*[:|]" "\\|"
  17710. "\\$\\$" "\\|"
  17711. "\\\\\\(begin\\|end\\|[][]\\)"))
  17712. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17713. ;; But only if the user has not turned off tables or fixed-width regions
  17714. (org-set-local
  17715. 'auto-fill-inhibit-regexp
  17716. (concat "\\*+ \\|#\\+"
  17717. "\\|[ \t]*" org-keyword-time-regexp
  17718. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17719. (concat
  17720. "\\|[ \t]*["
  17721. (if org-enable-table-editor "|" "")
  17722. (if org-enable-fixed-width-editor ":" "")
  17723. "]"))))
  17724. ;; We use our own fill-paragraph function, to make sure that tables
  17725. ;; and fixed-width regions are not wrapped. That function will pass
  17726. ;; through to `fill-paragraph' when appropriate.
  17727. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17728. ;; Prevent auto-fill from inserting unwanted new items.
  17729. (org-set-local 'fill-nobreak-predicate
  17730. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  17731. fill-nobreak-predicate
  17732. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate)))
  17733. ;; Adaptive filling: To get full control, first make sure that
  17734. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17735. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17736. (org-set-local 'org-adaptive-fill-regexp-backup
  17737. adaptive-fill-regexp))
  17738. (org-set-local 'adaptive-fill-regexp "\000")
  17739. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  17740. (org-set-local 'adaptive-fill-function
  17741. 'org-adaptive-fill-function)
  17742. (org-set-local
  17743. 'align-mode-rules-list
  17744. '((org-in-buffer-settings
  17745. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17746. (modes . '(org-mode))))))
  17747. (defun org-fill-item-nobreak-p ()
  17748. "Non-nil when a line break at point would insert a new item."
  17749. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  17750. (defun org-fill-paragraph (&optional justify)
  17751. "Re-align a table, pass through to fill-paragraph if no table."
  17752. (let ((table-p (org-at-table-p))
  17753. (table.el-p (org-at-table.el-p))
  17754. (itemp (org-in-item-p)))
  17755. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17756. (save-excursion (goto-char (point-at-bol))
  17757. (looking-at outline-regexp)))
  17758. t) ; skip headlines
  17759. (table.el-p t) ; skip table.el tables
  17760. (table-p (org-table-align) t) ; align Org tables
  17761. (itemp ; align text in items
  17762. (let* ((struct (save-excursion (goto-char itemp)
  17763. (org-list-struct)))
  17764. (parents (org-list-parents-alist struct))
  17765. (children (org-list-get-children itemp struct parents))
  17766. beg end prev next prefix)
  17767. ;; Determine in which part of item point is: before
  17768. ;; first child, after last child, between two
  17769. ;; sub-lists, or simply in item if there's no child.
  17770. (cond
  17771. ((not children)
  17772. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17773. beg itemp
  17774. end (org-list-get-item-end itemp struct)))
  17775. ((< (point) (setq next (car children)))
  17776. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17777. beg itemp
  17778. end next))
  17779. ((> (point) (setq prev (car (last children))))
  17780. (setq beg (org-list-get-item-end prev struct)
  17781. end (org-list-get-item-end itemp struct)
  17782. prefix (save-excursion
  17783. (goto-char beg)
  17784. (skip-chars-forward " \t")
  17785. (make-string (current-column) ?\ ))))
  17786. (t (catch 'exit
  17787. (while (setq next (pop children))
  17788. (if (> (point) next)
  17789. (setq prev next)
  17790. (setq beg (org-list-get-item-end prev struct)
  17791. end next
  17792. prefix (save-excursion
  17793. (goto-char beg)
  17794. (skip-chars-forward " \t")
  17795. (make-string (current-column) ?\ )))
  17796. (throw 'exit nil))))))
  17797. ;; Use `fill-paragraph' with buffer narrowed to item
  17798. ;; without any child, and with our computed PREFIX.
  17799. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17800. (save-restriction
  17801. (narrow-to-region beg end)
  17802. (save-excursion (fill-paragraph justify)))) t))
  17803. ;; Special case where point is not in a list but is on
  17804. ;; a paragraph adjacent to a list: make sure this paragraph
  17805. ;; doesn't get merged with the end of the list by narrowing
  17806. ;; buffer first.
  17807. ((save-excursion (forward-paragraph -1)
  17808. (setq itemp (org-in-item-p)))
  17809. (let ((struct (save-excursion (goto-char itemp)
  17810. (org-list-struct))))
  17811. (save-restriction
  17812. (narrow-to-region (org-list-get-bottom-point struct)
  17813. (save-excursion (forward-paragraph 1)
  17814. (point)))
  17815. (fill-paragraph justify) t)))
  17816. ;; Else simply call `fill-paragraph'.
  17817. (t nil))))
  17818. ;; For reference, this is the default value of adaptive-fill-regexp
  17819. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17820. (defun org-adaptive-fill-function ()
  17821. "Return a fill prefix for org-mode files."
  17822. (let (itemp)
  17823. (save-excursion
  17824. (cond
  17825. ;; Comment line
  17826. ((looking-at "#[ \t]+")
  17827. (match-string-no-properties 0))
  17828. ;; Plain list item
  17829. ((org-at-item-p)
  17830. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  17831. ;; Point is in a list after `backward-paragraph': original
  17832. ;; point wasn't in the list, or filling would have been taken
  17833. ;; care of by `org-auto-fill-function', but the list and the
  17834. ;; real paragraph are not separated by a blank line. Thus, move
  17835. ;; point after the list to go back to real paragraph and
  17836. ;; determine fill-prefix.
  17837. ((setq itemp (org-in-item-p))
  17838. (goto-char itemp)
  17839. (let* ((struct (org-list-struct))
  17840. (bottom (org-list-get-bottom-point struct)))
  17841. (goto-char bottom)
  17842. (make-string (org-get-indentation) ?\ )))
  17843. ;; Other text
  17844. ((looking-at org-adaptive-fill-regexp-backup)
  17845. (match-string-no-properties 0))))))
  17846. (defun org-auto-fill-function ()
  17847. "Auto-fill function."
  17848. (let (itemp prefix)
  17849. ;; When in a list, compute an appropriate fill-prefix and make
  17850. ;; sure it will be used by `do-auto-fill'.
  17851. (if (setq itemp (org-in-item-p))
  17852. (progn
  17853. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  17854. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17855. (do-auto-fill)))
  17856. ;; Else just use `do-auto-fill'.
  17857. (do-auto-fill))))
  17858. ;;; Other stuff.
  17859. (defun org-toggle-fixed-width-section (arg)
  17860. "Toggle the fixed-width export.
  17861. If there is no active region, the QUOTE keyword at the current headline is
  17862. inserted or removed. When present, it causes the text between this headline
  17863. and the next to be exported as fixed-width text, and unmodified.
  17864. If there is an active region, this command adds or removes a colon as the
  17865. first character of this line. If the first character of a line is a colon,
  17866. this line is also exported in fixed-width font."
  17867. (interactive "P")
  17868. (let* ((cc 0)
  17869. (regionp (org-region-active-p))
  17870. (beg (if regionp (region-beginning) (point)))
  17871. (end (if regionp (region-end)))
  17872. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17873. (case-fold-search nil)
  17874. (re "[ \t]*\\(: \\)")
  17875. off)
  17876. (if regionp
  17877. (save-excursion
  17878. (goto-char beg)
  17879. (setq cc (current-column))
  17880. (beginning-of-line 1)
  17881. (setq off (looking-at re))
  17882. (while (> nlines 0)
  17883. (setq nlines (1- nlines))
  17884. (beginning-of-line 1)
  17885. (cond
  17886. (arg
  17887. (org-move-to-column cc t)
  17888. (insert ": \n")
  17889. (forward-line -1))
  17890. ((and off (looking-at re))
  17891. (replace-match "" t t nil 1))
  17892. ((not off) (org-move-to-column cc t) (insert ": ")))
  17893. (forward-line 1)))
  17894. (save-excursion
  17895. (org-back-to-heading)
  17896. (if (looking-at (concat outline-regexp
  17897. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17898. (replace-match "" t t nil 1)
  17899. (if (looking-at outline-regexp)
  17900. (progn
  17901. (goto-char (match-end 0))
  17902. (insert org-quote-string " "))))))))
  17903. (defun org-reftex-citation ()
  17904. "Use reftex-citation to insert a citation into the buffer.
  17905. This looks for a line like
  17906. #+BIBLIOGRAPHY: foo plain option:-d
  17907. and derives from it that foo.bib is the bibliography file relevant
  17908. for this document. It then installs the necessary environment for RefTeX
  17909. to work in this buffer and calls `reftex-citation' to insert a citation
  17910. into the buffer.
  17911. Export of such citations to both LaTeX and HTML is handled by the contributed
  17912. package org-exp-bibtex by Taru Karttunen."
  17913. (interactive)
  17914. (let ((reftex-docstruct-symbol 'rds)
  17915. (reftex-cite-format "\\cite{%l}")
  17916. rds bib)
  17917. (save-excursion
  17918. (save-restriction
  17919. (widen)
  17920. (let ((case-fold-search t)
  17921. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17922. (if (not (save-excursion
  17923. (or (re-search-forward re nil t)
  17924. (re-search-backward re nil t))))
  17925. (error "No bibliography defined in file")
  17926. (setq bib (concat (match-string 1) ".bib")
  17927. rds (list (list 'bib bib)))))))
  17928. (call-interactively 'reftex-citation)))
  17929. ;;;; Functions extending outline functionality
  17930. (defun org-beginning-of-line (&optional arg)
  17931. "Go to the beginning of the current line. If that is invisible, continue
  17932. to a visible line beginning. This makes the function of C-a more intuitive.
  17933. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17934. first attempt, and only move to after the tags when the cursor is already
  17935. beyond the end of the headline."
  17936. (interactive "P")
  17937. (let ((pos (point))
  17938. (special (if (consp org-special-ctrl-a/e)
  17939. (car org-special-ctrl-a/e)
  17940. org-special-ctrl-a/e))
  17941. refpos)
  17942. (if (org-bound-and-true-p line-move-visual)
  17943. (beginning-of-visual-line 1)
  17944. (beginning-of-line 1))
  17945. (if (and arg (fboundp 'move-beginning-of-line))
  17946. (call-interactively 'move-beginning-of-line)
  17947. (if (bobp)
  17948. nil
  17949. (backward-char 1)
  17950. (if (org-truely-invisible-p)
  17951. (while (and (not (bobp)) (org-truely-invisible-p))
  17952. (backward-char 1)
  17953. (beginning-of-line 1))
  17954. (forward-char 1))))
  17955. (when special
  17956. (cond
  17957. ((and (looking-at org-complex-heading-regexp)
  17958. (= (char-after (match-end 1)) ?\ ))
  17959. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17960. (point-at-eol)))
  17961. (goto-char
  17962. (if (eq special t)
  17963. (cond ((> pos refpos) refpos)
  17964. ((= pos (point)) refpos)
  17965. (t (point)))
  17966. (cond ((> pos (point)) (point))
  17967. ((not (eq last-command this-command)) (point))
  17968. (t refpos)))))
  17969. ((org-at-item-p)
  17970. (goto-char
  17971. (if (eq special t)
  17972. (cond ((> pos (match-end 0)) (match-end 0))
  17973. ((= pos (point)) (match-end 0))
  17974. (t (point)))
  17975. (cond ((> pos (point)) (point))
  17976. ((not (eq last-command this-command)) (point))
  17977. (t (match-end 0))))))))
  17978. (org-no-warnings
  17979. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17980. (defun org-end-of-line (&optional arg)
  17981. "Go to the end of the line.
  17982. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17983. first attempt, and only move to after the tags when the cursor is already
  17984. beyond the end of the headline."
  17985. (interactive "P")
  17986. (let ((special (if (consp org-special-ctrl-a/e)
  17987. (cdr org-special-ctrl-a/e)
  17988. org-special-ctrl-a/e)))
  17989. (if (or (not special)
  17990. (not (org-on-heading-p))
  17991. arg)
  17992. (call-interactively
  17993. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17994. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17995. (t 'end-of-line)))
  17996. (let ((pos (point)))
  17997. (beginning-of-line 1)
  17998. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  17999. (if (eq special t)
  18000. (if (or (< pos (match-beginning 1))
  18001. (= pos (match-end 0)))
  18002. (goto-char (match-beginning 1))
  18003. (goto-char (match-end 0)))
  18004. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18005. (goto-char (match-end 0))
  18006. (goto-char (match-beginning 1))))
  18007. (call-interactively (if (fboundp 'move-end-of-line)
  18008. 'move-end-of-line
  18009. 'end-of-line)))))
  18010. (org-no-warnings
  18011. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18012. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18013. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18014. (defun org-backward-sentence (&optional arg)
  18015. "Go to beginning of sentence, or beginning of table field.
  18016. This will call `backward-sentence' or `org-table-beginning-of-field',
  18017. depending on context."
  18018. (interactive "P")
  18019. (cond
  18020. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18021. (t (call-interactively 'backward-sentence))))
  18022. (defun org-forward-sentence (&optional arg)
  18023. "Go to end of sentence, or end of table field.
  18024. This will call `forward-sentence' or `org-table-end-of-field',
  18025. depending on context."
  18026. (interactive "P")
  18027. (cond
  18028. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18029. (t (call-interactively 'forward-sentence))))
  18030. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18031. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18032. (defun org-kill-line (&optional arg)
  18033. "Kill line, to tags or end of line."
  18034. (interactive "P")
  18035. (cond
  18036. ((or (not org-special-ctrl-k)
  18037. (bolp)
  18038. (not (org-on-heading-p)))
  18039. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18040. org-ctrl-k-protect-subtree)
  18041. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18042. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18043. (error "C-k aborted - would kill hidden subtree")))
  18044. (call-interactively 'kill-line))
  18045. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18046. (kill-region (point) (match-beginning 1))
  18047. (org-set-tags nil t))
  18048. (t (kill-region (point) (point-at-eol)))))
  18049. (define-key org-mode-map "\C-k" 'org-kill-line)
  18050. (defun org-yank (&optional arg)
  18051. "Yank. If the kill is a subtree, treat it specially.
  18052. This command will look at the current kill and check if is a single
  18053. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18054. cursor is at the beginning of a line or after the stars of a currently
  18055. empty headline, then the yank is handled specially. How exactly depends
  18056. on the value of the following variables, both set by default.
  18057. org-yank-folded-subtrees
  18058. When set, the subtree(s) will be folded after insertion, but only
  18059. if doing so would now swallow text after the yanked text.
  18060. org-yank-adjusted-subtrees
  18061. When set, the subtree will be promoted or demoted in order to
  18062. fit into the local outline tree structure, which means that the level
  18063. will be adjusted so that it becomes the smaller one of the two
  18064. *visible* surrounding headings.
  18065. Any prefix to this command will cause `yank' to be called directly with
  18066. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18067. will just
  18068. plainly yank the text as it is.
  18069. \[1] The test checks if the first non-white line is a heading
  18070. and if there are no other headings with fewer stars."
  18071. (interactive "P")
  18072. (org-yank-generic 'yank arg))
  18073. (defun org-yank-generic (command arg)
  18074. "Perform some yank-like command.
  18075. This function implements the behavior described in the `org-yank'
  18076. documentation. However, it has been generalized to work for any
  18077. interactive command with similar behavior."
  18078. ;; pretend to be command COMMAND
  18079. (setq this-command command)
  18080. (if arg
  18081. (call-interactively command)
  18082. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18083. (and (org-kill-is-subtree-p)
  18084. (or (bolp)
  18085. (and (looking-at "[ \t]*$")
  18086. (string-match
  18087. "\\`\\*+\\'"
  18088. (buffer-substring (point-at-bol) (point)))))))
  18089. swallowp)
  18090. (cond
  18091. ((and subtreep org-yank-folded-subtrees)
  18092. (let ((beg (point))
  18093. end)
  18094. (if (and subtreep org-yank-adjusted-subtrees)
  18095. (org-paste-subtree nil nil 'for-yank)
  18096. (call-interactively command))
  18097. (setq end (point))
  18098. (goto-char beg)
  18099. (when (and (bolp) subtreep
  18100. (not (setq swallowp
  18101. (org-yank-folding-would-swallow-text beg end))))
  18102. (or (looking-at outline-regexp)
  18103. (re-search-forward (concat "^" outline-regexp) end t))
  18104. (while (and (< (point) end) (looking-at outline-regexp))
  18105. (hide-subtree)
  18106. (org-cycle-show-empty-lines 'folded)
  18107. (condition-case nil
  18108. (outline-forward-same-level 1)
  18109. (error (goto-char end)))))
  18110. (when swallowp
  18111. (message
  18112. "Inserted text not folded because that would swallow text"))
  18113. (goto-char end)
  18114. (skip-chars-forward " \t\n\r")
  18115. (beginning-of-line 1)
  18116. (push-mark beg 'nomsg)))
  18117. ((and subtreep org-yank-adjusted-subtrees)
  18118. (let ((beg (point-at-bol)))
  18119. (org-paste-subtree nil nil 'for-yank)
  18120. (push-mark beg 'nomsg)))
  18121. (t
  18122. (call-interactively command))))))
  18123. (defun org-yank-folding-would-swallow-text (beg end)
  18124. "Would hide-subtree at BEG swallow any text after END?"
  18125. (let (level)
  18126. (save-excursion
  18127. (goto-char beg)
  18128. (when (or (looking-at outline-regexp)
  18129. (re-search-forward (concat "^" outline-regexp) end t))
  18130. (setq level (org-outline-level)))
  18131. (goto-char end)
  18132. (skip-chars-forward " \t\r\n\v\f")
  18133. (if (or (eobp)
  18134. (and (bolp) (looking-at org-outline-regexp)
  18135. (<= (org-outline-level) level)))
  18136. nil ; Nothing would be swallowed
  18137. t)))) ; something would swallow
  18138. (define-key org-mode-map "\C-y" 'org-yank)
  18139. (defun org-truely-invisible-p ()
  18140. "Check if point is at a character currently not visible.
  18141. This version does not only check the character property, but also
  18142. `visible-mode'."
  18143. ;; Early versions of noutline don't have `outline-invisible-p'.
  18144. (if (org-bound-and-true-p visible-mode)
  18145. nil
  18146. (outline-invisible-p)))
  18147. (defun org-invisible-p2 ()
  18148. "Check if point is at a character currently not visible."
  18149. (save-excursion
  18150. (if (and (eolp) (not (bobp))) (backward-char 1))
  18151. ;; Early versions of noutline don't have `outline-invisible-p'.
  18152. (outline-invisible-p)))
  18153. (defun org-back-to-heading (&optional invisible-ok)
  18154. "Call `outline-back-to-heading', but provide a better error message."
  18155. (condition-case nil
  18156. (outline-back-to-heading invisible-ok)
  18157. (error (error "Before first headline at position %d in buffer %s"
  18158. (point) (current-buffer)))))
  18159. (defun org-beginning-of-defun ()
  18160. "Go to the beginning of the subtree, i.e. back to the heading."
  18161. (org-back-to-heading))
  18162. (defun org-end-of-defun ()
  18163. "Go to the end of the subtree."
  18164. (org-end-of-subtree nil t))
  18165. (defun org-before-first-heading-p ()
  18166. "Before first heading?"
  18167. (save-excursion
  18168. (end-of-line)
  18169. (null (re-search-backward "^\\*+ " nil t))))
  18170. (defun org-on-heading-p (&optional ignored)
  18171. (outline-on-heading-p t))
  18172. (defun org-at-heading-p (&optional ignored)
  18173. (outline-on-heading-p t))
  18174. (defun org-point-at-end-of-empty-headline ()
  18175. "If point is at the end of an empty headline, return t, else nil.
  18176. If the heading only contains a TODO keyword, it is still still considered
  18177. empty."
  18178. (and (looking-at "[ \t]*$")
  18179. (save-excursion
  18180. (beginning-of-line 1)
  18181. (let ((case-fold-search nil))
  18182. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  18183. "\\)?[ \t]*$"))))))
  18184. (defun org-at-heading-or-item-p ()
  18185. (or (org-on-heading-p) (org-at-item-p)))
  18186. (defun org-on-target-p ()
  18187. (or (org-in-regexp org-radio-target-regexp)
  18188. (org-in-regexp org-target-regexp)))
  18189. (defun org-up-heading-all (arg)
  18190. "Move to the heading line of which the present line is a subheading.
  18191. This function considers both visible and invisible heading lines.
  18192. With argument, move up ARG levels."
  18193. (if (fboundp 'outline-up-heading-all)
  18194. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18195. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18196. (defun org-up-heading-safe ()
  18197. "Move to the heading line of which the present line is a subheading.
  18198. This version will not throw an error. It will return the level of the
  18199. headline found, or nil if no higher level is found.
  18200. Also, this function will be a lot faster than `outline-up-heading',
  18201. because it relies on stars being the outline starters. This can really
  18202. make a significant difference in outlines with very many siblings."
  18203. (let (start-level re)
  18204. (org-back-to-heading t)
  18205. (setq start-level (funcall outline-level))
  18206. (if (equal start-level 1)
  18207. nil
  18208. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18209. (if (re-search-backward re nil t)
  18210. (funcall outline-level)))))
  18211. (defun org-first-sibling-p ()
  18212. "Is this heading the first child of its parents?"
  18213. (interactive)
  18214. (let ((re (concat "^" outline-regexp))
  18215. level l)
  18216. (unless (org-at-heading-p t)
  18217. (error "Not at a heading"))
  18218. (setq level (funcall outline-level))
  18219. (save-excursion
  18220. (if (not (re-search-backward re nil t))
  18221. t
  18222. (setq l (funcall outline-level))
  18223. (< l level)))))
  18224. (defun org-goto-sibling (&optional previous)
  18225. "Goto the next sibling, even if it is invisible.
  18226. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18227. when a sibling was found. When none is found, return nil and don't
  18228. move point."
  18229. (let ((fun (if previous 're-search-backward 're-search-forward))
  18230. (pos (point))
  18231. (re (concat "^" outline-regexp))
  18232. level l)
  18233. (when (condition-case nil (org-back-to-heading t) (error nil))
  18234. (setq level (funcall outline-level))
  18235. (catch 'exit
  18236. (or previous (forward-char 1))
  18237. (while (funcall fun re nil t)
  18238. (setq l (funcall outline-level))
  18239. (when (< l level) (goto-char pos) (throw 'exit nil))
  18240. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18241. (goto-char pos)
  18242. nil))))
  18243. (defun org-show-siblings ()
  18244. "Show all siblings of the current headline."
  18245. (save-excursion
  18246. (while (org-goto-sibling) (org-flag-heading nil)))
  18247. (save-excursion
  18248. (while (org-goto-sibling 'previous)
  18249. (org-flag-heading nil))))
  18250. (defun org-goto-first-child ()
  18251. "Goto the first child, even if it is invisible.
  18252. Return t when a child was found. Otherwise don't move point and
  18253. return nil."
  18254. (let (level (pos (point)) (re (concat "^" outline-regexp)))
  18255. (when (condition-case nil (org-back-to-heading t) (error nil))
  18256. (setq level (outline-level))
  18257. (forward-char 1)
  18258. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18259. (progn (goto-char (match-beginning 0)) t)
  18260. (goto-char pos) nil))))
  18261. (defun org-show-hidden-entry ()
  18262. "Show an entry where even the heading is hidden."
  18263. (save-excursion
  18264. (org-show-entry)))
  18265. (defun org-flag-heading (flag &optional entry)
  18266. "Flag the current heading. FLAG non-nil means make invisible.
  18267. When ENTRY is non-nil, show the entire entry."
  18268. (save-excursion
  18269. (org-back-to-heading t)
  18270. ;; Check if we should show the entire entry
  18271. (if entry
  18272. (progn
  18273. (org-show-entry)
  18274. (save-excursion
  18275. (and (outline-next-heading)
  18276. (org-flag-heading nil))))
  18277. (outline-flag-region (max (point-min) (1- (point)))
  18278. (save-excursion (outline-end-of-heading) (point))
  18279. flag))))
  18280. (defun org-get-next-sibling ()
  18281. "Move to next heading of the same level, and return point.
  18282. If there is no such heading, return nil.
  18283. This is like outline-next-sibling, but invisible headings are ok."
  18284. (let ((level (funcall outline-level)))
  18285. (outline-next-heading)
  18286. (while (and (not (eobp)) (> (funcall outline-level) level))
  18287. (outline-next-heading))
  18288. (if (or (eobp) (< (funcall outline-level) level))
  18289. nil
  18290. (point))))
  18291. (defun org-get-last-sibling ()
  18292. "Move to previous heading of the same level, and return point.
  18293. If there is no such heading, return nil."
  18294. (let ((opoint (point))
  18295. (level (funcall outline-level)))
  18296. (outline-previous-heading)
  18297. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18298. (while (and (> (funcall outline-level) level)
  18299. (not (bobp)))
  18300. (outline-previous-heading))
  18301. (if (< (funcall outline-level) level)
  18302. nil
  18303. (point)))))
  18304. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18305. ;; This contains an exact copy of the original function, but it uses
  18306. ;; `org-back-to-heading', to make it work also in invisible
  18307. ;; trees. And is uses an invisible-OK argument.
  18308. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  18309. ;; Furthermore, when used inside Org, finding the end of a large subtree
  18310. ;; with many children and grandchildren etc, this can be much faster
  18311. ;; than the outline version.
  18312. (org-back-to-heading invisible-OK)
  18313. (let ((first t)
  18314. (level (funcall outline-level)))
  18315. (if (and (org-mode-p) (< level 1000))
  18316. ;; A true heading (not a plain list item), in Org-mode
  18317. ;; This means we can easily find the end by looking
  18318. ;; only for the right number of stars. Using a regexp to do
  18319. ;; this is so much faster than using a Lisp loop.
  18320. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  18321. (forward-char 1)
  18322. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  18323. ;; something else, do it the slow way
  18324. (while (and (not (eobp))
  18325. (or first (> (funcall outline-level) level)))
  18326. (setq first nil)
  18327. (outline-next-heading)))
  18328. (unless to-heading
  18329. (if (memq (preceding-char) '(?\n ?\^M))
  18330. (progn
  18331. ;; Go to end of line before heading
  18332. (forward-char -1)
  18333. (if (memq (preceding-char) '(?\n ?\^M))
  18334. ;; leave blank line before heading
  18335. (forward-char -1))))))
  18336. (point))
  18337. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  18338. "Use Org version in org-mode, for dramatic speed-up."
  18339. (if (org-mode-p)
  18340. (progn
  18341. (org-end-of-subtree nil t)
  18342. (unless (eobp) (backward-char 1)))
  18343. ad-do-it))
  18344. (defun org-end-of-meta-data-and-drawers ()
  18345. "Jump to the first text after meta data and drawers in the current entry.
  18346. This will move over empty lines, lines with planning time stamps,
  18347. clocking lines, and drawers."
  18348. (org-back-to-heading t)
  18349. (let ((end (save-excursion (outline-next-heading) (point)))
  18350. (re (concat "[ \t]*$"
  18351. "\\|"
  18352. "\\(" org-drawer-regexp "\\)" ; group 1 are drawers
  18353. "\\|"
  18354. "\\([ \t]*\\(" org-keyword-time-regexp "\\)\\)")))
  18355. (forward-line 1)
  18356. (while (looking-at (concat "[ \t]*\\(" org-keyword-time-regexp "\\)"))
  18357. (if (not (match-end 1))
  18358. ;; empty or planning line
  18359. (forward-line 1)
  18360. ;; a drawer, find the end
  18361. (re-search-forward "^[ \t]*:END:" end 'move)
  18362. (forward-line 1)))
  18363. (point)))
  18364. (defun org-forward-same-level (arg &optional invisible-ok)
  18365. "Move forward to the arg'th subheading at same level as this one.
  18366. Stop at the first and last subheadings of a superior heading.
  18367. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  18368. it wil also look at invisible ones."
  18369. (interactive "p")
  18370. (org-back-to-heading invisible-ok)
  18371. (org-on-heading-p)
  18372. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18373. (re (format "^\\*\\{1,%d\\} " level))
  18374. l)
  18375. (forward-char 1)
  18376. (while (> arg 0)
  18377. (while (and (re-search-forward re nil 'move)
  18378. (setq l (- (match-end 0) (match-beginning 0) 1))
  18379. (= l level)
  18380. (not invisible-ok)
  18381. (progn (backward-char 1) (outline-invisible-p)))
  18382. (if (< l level) (setq arg 1)))
  18383. (setq arg (1- arg)))
  18384. (beginning-of-line 1)))
  18385. (defun org-backward-same-level (arg &optional invisible-ok)
  18386. "Move backward to the arg'th subheading at same level as this one.
  18387. Stop at the first and last subheadings of a superior heading."
  18388. (interactive "p")
  18389. (org-back-to-heading)
  18390. (org-on-heading-p)
  18391. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18392. (re (format "^\\*\\{1,%d\\} " level))
  18393. l)
  18394. (while (> arg 0)
  18395. (while (and (re-search-backward re nil 'move)
  18396. (setq l (- (match-end 0) (match-beginning 0) 1))
  18397. (= l level)
  18398. (not invisible-ok)
  18399. (outline-invisible-p))
  18400. (if (< l level) (setq arg 1)))
  18401. (setq arg (1- arg)))))
  18402. (defun org-show-subtree ()
  18403. "Show everything after this heading at deeper levels."
  18404. (outline-flag-region
  18405. (point)
  18406. (save-excursion
  18407. (org-end-of-subtree t t))
  18408. nil))
  18409. (defun org-show-entry ()
  18410. "Show the body directly following this heading.
  18411. Show the heading too, if it is currently invisible."
  18412. (interactive)
  18413. (save-excursion
  18414. (condition-case nil
  18415. (progn
  18416. (org-back-to-heading t)
  18417. (outline-flag-region
  18418. (max (point-min) (1- (point)))
  18419. (save-excursion
  18420. (if (re-search-forward
  18421. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  18422. (match-beginning 1)
  18423. (point-max)))
  18424. nil)
  18425. (org-cycle-hide-drawers 'children))
  18426. (error nil))))
  18427. (defun org-make-options-regexp (kwds &optional extra)
  18428. "Make a regular expression for keyword lines."
  18429. (concat
  18430. "^"
  18431. "#?[ \t]*\\+\\("
  18432. (mapconcat 'regexp-quote kwds "\\|")
  18433. (if extra (concat "\\|" extra))
  18434. "\\):[ \t]*"
  18435. "\\(.*\\)"))
  18436. ;; Make isearch reveal the necessary context
  18437. (defun org-isearch-end ()
  18438. "Reveal context after isearch exits."
  18439. (when isearch-success ; only if search was successful
  18440. (if (featurep 'xemacs)
  18441. ;; Under XEmacs, the hook is run in the correct place,
  18442. ;; we directly show the context.
  18443. (org-show-context 'isearch)
  18444. ;; In Emacs the hook runs *before* restoring the overlays.
  18445. ;; So we have to use a one-time post-command-hook to do this.
  18446. ;; (Emacs 22 has a special variable, see function `org-mode')
  18447. (unless (and (boundp 'isearch-mode-end-hook-quit)
  18448. isearch-mode-end-hook-quit)
  18449. ;; Only when the isearch was not quitted.
  18450. (org-add-hook 'post-command-hook 'org-isearch-post-command
  18451. 'append 'local)))))
  18452. (defun org-isearch-post-command ()
  18453. "Remove self from hook, and show context."
  18454. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  18455. (org-show-context 'isearch))
  18456. ;;;; Integration with and fixes for other packages
  18457. ;;; Imenu support
  18458. (defvar org-imenu-markers nil
  18459. "All markers currently used by Imenu.")
  18460. (make-variable-buffer-local 'org-imenu-markers)
  18461. (defun org-imenu-new-marker (&optional pos)
  18462. "Return a new marker for use by Imenu, and remember the marker."
  18463. (let ((m (make-marker)))
  18464. (move-marker m (or pos (point)))
  18465. (push m org-imenu-markers)
  18466. m))
  18467. (defun org-imenu-get-tree ()
  18468. "Produce the index for Imenu."
  18469. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  18470. (setq org-imenu-markers nil)
  18471. (let* ((n org-imenu-depth)
  18472. (re (concat "^" outline-regexp))
  18473. (subs (make-vector (1+ n) nil))
  18474. (last-level 0)
  18475. m level head)
  18476. (save-excursion
  18477. (save-restriction
  18478. (widen)
  18479. (goto-char (point-max))
  18480. (while (re-search-backward re nil t)
  18481. (setq level (org-reduced-level (funcall outline-level)))
  18482. (when (<= level n)
  18483. (looking-at org-complex-heading-regexp)
  18484. (setq head (org-link-display-format
  18485. (org-match-string-no-properties 4))
  18486. m (org-imenu-new-marker))
  18487. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  18488. (if (>= level last-level)
  18489. (push (cons head m) (aref subs level))
  18490. (push (cons head (aref subs (1+ level))) (aref subs level))
  18491. (loop for i from (1+ level) to n do (aset subs i nil)))
  18492. (setq last-level level)))))
  18493. (aref subs 1)))
  18494. (eval-after-load "imenu"
  18495. '(progn
  18496. (add-hook 'imenu-after-jump-hook
  18497. (lambda ()
  18498. (if (org-mode-p)
  18499. (org-show-context 'org-goto))))))
  18500. (defun org-link-display-format (link)
  18501. "Replace a link with either the description, or the link target
  18502. if no description is present"
  18503. (save-match-data
  18504. (if (string-match org-bracket-link-analytic-regexp link)
  18505. (replace-match (if (match-end 5)
  18506. (match-string 5 link)
  18507. (concat (match-string 1 link)
  18508. (match-string 3 link)))
  18509. nil t link)
  18510. link)))
  18511. ;; Speedbar support
  18512. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  18513. "Overlay marking the agenda restriction line in speedbar.")
  18514. (overlay-put org-speedbar-restriction-lock-overlay
  18515. 'face 'org-agenda-restriction-lock)
  18516. (overlay-put org-speedbar-restriction-lock-overlay
  18517. 'help-echo "Agendas are currently limited to this item.")
  18518. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  18519. (defun org-speedbar-set-agenda-restriction ()
  18520. "Restrict future agenda commands to the location at point in speedbar.
  18521. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  18522. (interactive)
  18523. (require 'org-agenda)
  18524. (let (p m tp np dir txt)
  18525. (cond
  18526. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18527. 'org-imenu t))
  18528. (setq m (get-text-property p 'org-imenu-marker))
  18529. (with-current-buffer (marker-buffer m)
  18530. (goto-char m)
  18531. (org-agenda-set-restriction-lock 'subtree)))
  18532. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18533. 'speedbar-function 'speedbar-find-file))
  18534. (setq tp (previous-single-property-change
  18535. (1+ p) 'speedbar-function)
  18536. np (next-single-property-change
  18537. tp 'speedbar-function)
  18538. dir (speedbar-line-directory)
  18539. txt (buffer-substring-no-properties (or tp (point-min))
  18540. (or np (point-max))))
  18541. (with-current-buffer (find-file-noselect
  18542. (let ((default-directory dir))
  18543. (expand-file-name txt)))
  18544. (unless (org-mode-p)
  18545. (error "Cannot restrict to non-Org-mode file"))
  18546. (org-agenda-set-restriction-lock 'file)))
  18547. (t (error "Don't know how to restrict Org-mode's agenda")))
  18548. (move-overlay org-speedbar-restriction-lock-overlay
  18549. (point-at-bol) (point-at-eol))
  18550. (setq current-prefix-arg nil)
  18551. (org-agenda-maybe-redo)))
  18552. (eval-after-load "speedbar"
  18553. '(progn
  18554. (speedbar-add-supported-extension ".org")
  18555. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  18556. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  18557. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  18558. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18559. (add-hook 'speedbar-visiting-tag-hook
  18560. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  18561. ;;; Fixes and Hacks for problems with other packages
  18562. ;; Make flyspell not check words in links, to not mess up our keymap
  18563. (defun org-mode-flyspell-verify ()
  18564. "Don't let flyspell put overlays at active buttons, or on
  18565. {todo,all-time,additional-option-like}-keywords."
  18566. (let ((pos (max (1- (point)) (point-min)))
  18567. (word (thing-at-point 'word)))
  18568. (and (not (get-text-property pos 'keymap))
  18569. (not (get-text-property pos 'org-no-flyspell))
  18570. (not (member word org-todo-keywords-1))
  18571. (not (member word org-all-time-keywords))
  18572. (not (member word org-additional-option-like-keywords)))))
  18573. (defun org-remove-flyspell-overlays-in (beg end)
  18574. "Remove flyspell overlays in region."
  18575. (and (org-bound-and-true-p flyspell-mode)
  18576. (fboundp 'flyspell-delete-region-overlays)
  18577. (flyspell-delete-region-overlays beg end))
  18578. (add-text-properties beg end '(org-no-flyspell t)))
  18579. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  18580. (eval-after-load "bookmark"
  18581. '(if (boundp 'bookmark-after-jump-hook)
  18582. ;; We can use the hook
  18583. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  18584. ;; Hook not available, use advice
  18585. (defadvice bookmark-jump (after org-make-visible activate)
  18586. "Make the position visible."
  18587. (org-bookmark-jump-unhide))))
  18588. ;; Make sure saveplace shows the location if it was hidden
  18589. (eval-after-load "saveplace"
  18590. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  18591. "Make the position visible."
  18592. (org-bookmark-jump-unhide)))
  18593. ;; Make sure ecb shows the location if it was hidden
  18594. (eval-after-load "ecb"
  18595. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  18596. "Make hierarchy visible when jumping into location from ECB tree buffer."
  18597. (if (org-mode-p)
  18598. (org-show-context))))
  18599. (defun org-bookmark-jump-unhide ()
  18600. "Unhide the current position, to show the bookmark location."
  18601. (and (org-mode-p)
  18602. (or (outline-invisible-p)
  18603. (save-excursion (goto-char (max (point-min) (1- (point))))
  18604. (outline-invisible-p)))
  18605. (org-show-context 'bookmark-jump)))
  18606. ;; Make session.el ignore our circular variable
  18607. (eval-after-load "session"
  18608. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  18609. ;;;; Experimental code
  18610. (defun org-closed-in-range ()
  18611. "Sparse tree of items closed in a certain time range.
  18612. Still experimental, may disappear in the future."
  18613. (interactive)
  18614. ;; Get the time interval from the user.
  18615. (let* ((time1 (org-float-time
  18616. (org-read-date nil 'to-time nil "Starting date: ")))
  18617. (time2 (org-float-time
  18618. (org-read-date nil 'to-time nil "End date:")))
  18619. ;; callback function
  18620. (callback (lambda ()
  18621. (let ((time
  18622. (org-float-time
  18623. (apply 'encode-time
  18624. (org-parse-time-string
  18625. (match-string 1))))))
  18626. ;; check if time in interval
  18627. (and (>= time time1) (<= time time2))))))
  18628. ;; make tree, check each match with the callback
  18629. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  18630. ;;;; Finish up
  18631. (provide 'org)
  18632. (run-hooks 'org-load-hook)
  18633. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  18634. ;;; org.el ends here