org.el 731 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568
  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
  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.01trans
  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-src)
  94. (require 'org-footnote)
  95. ;; babel
  96. (require 'ob)
  97. (require 'ob-table)
  98. (require 'ob-lob)
  99. (require 'ob-ref)
  100. (require 'ob-tangle)
  101. (require 'ob-comint)
  102. (require 'ob-keys)
  103. ;; load languages based on value of `org-babel-load-languages'
  104. (defvar org-babel-load-languages)
  105. ;;;###autoload
  106. (defun org-babel-do-load-languages (sym value)
  107. "Load the languages defined in `org-babel-load-languages'."
  108. (set-default sym value)
  109. (mapc (lambda (pair)
  110. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  111. (if active
  112. (progn
  113. (require (intern (concat "ob-" lang))))
  114. (progn
  115. (funcall 'fmakunbound
  116. (intern (concat "org-babel-execute:" lang)))
  117. (funcall 'fmakunbound
  118. (intern (concat "org-babel-expand-body:" lang)))))))
  119. org-babel-load-languages))
  120. (defcustom org-babel-load-languages '((emacs-lisp . t))
  121. "Languages which can be evaluated in Org-mode buffers.
  122. This list can be used to load support for any of the languages
  123. below, note that each language will depend on a different set of
  124. system executables and/or Emacs modes. When a language is
  125. \"loaded\", then code blocks in that language can be evaluated
  126. with `org-babel-execute-src-block' bound by default to C-c
  127. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  128. be set to remove code block evaluation from the C-c C-c
  129. keybinding. By default only Emacs Lisp (which has no
  130. requirements) is loaded."
  131. :group 'org-babel
  132. :set 'org-babel-do-load-languages
  133. :type '(alist :tag "Babel Languages"
  134. :key-type
  135. (choice
  136. (const :tag "C" C)
  137. (const :tag "R" R)
  138. (const :tag "Asymptote" asymptote)
  139. (const :tag "Clojure" clojure)
  140. (const :tag "CSS" css)
  141. (const :tag "Ditaa" ditaa)
  142. (const :tag "Dot" dot)
  143. (const :tag "Emacs Lisp" emacs-lisp)
  144. (const :tag "Gnuplot" gnuplot)
  145. (const :tag "Haskell" haskell)
  146. (const :tag "Latex" latex)
  147. (const :tag "Ledger" ledger)
  148. (const :tag "Matlab" matlab)
  149. (const :tag "Mscgen" mscgen)
  150. (const :tag "Ocaml" ocaml)
  151. (const :tag "Octave" octave)
  152. (const :tag "Perl" perl)
  153. (const :tag "Python" python)
  154. (const :tag "Ruby" ruby)
  155. (const :tag "Sass" sass)
  156. (const :tag "Screen" screen)
  157. (const :tag "Shell Script" sh)
  158. (const :tag "Sql" sql)
  159. (const :tag "Sqlite" sqlite))
  160. :value-type (boolean :tag "Activate" :value t)))
  161. ;;;; Customization variables
  162. (defcustom org-clone-delete-id nil
  163. "Remove ID property of clones of a subtree.
  164. When non-nil, clones of a subtree don't inherit the ID property.
  165. Otherwise they inherit the ID property with a new unique
  166. identifier."
  167. :type 'boolean
  168. :group 'org-id)
  169. ;;; Version
  170. (defconst org-version "7.01trans"
  171. "The version number of the file org.el.")
  172. (defun org-version (&optional here)
  173. "Show the org-mode version in the echo area.
  174. With prefix arg HERE, insert it at point."
  175. (interactive "P")
  176. (let* ((origin default-directory)
  177. (version org-version)
  178. (git-version)
  179. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  180. (when (and (file-exists-p (expand-file-name ".git" dir))
  181. (executable-find "git"))
  182. (unwind-protect
  183. (progn
  184. (cd dir)
  185. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  186. (with-current-buffer "*Shell Command Output*"
  187. (goto-char (point-min))
  188. (setq git-version (buffer-substring (point) (point-at-eol))))
  189. (subst-char-in-string ?- ?. git-version t)
  190. (when (string-match "\\S-"
  191. (shell-command-to-string
  192. "git diff-index --name-only HEAD --"))
  193. (setq git-version (concat git-version ".dirty")))
  194. (setq version (concat version " (" git-version ")"))))
  195. (cd origin)))
  196. (setq version (format "Org-mode version %s" version))
  197. (if here (insert version))
  198. (message version)))
  199. ;;; Compatibility constants
  200. ;;; The custom variables
  201. (defgroup org nil
  202. "Outline-based notes management and organizer."
  203. :tag "Org"
  204. :group 'outlines
  205. :group 'calendar)
  206. (defcustom org-mode-hook nil
  207. "Mode hook for Org-mode, run after the mode was turned on."
  208. :group 'org
  209. :type 'hook)
  210. (defcustom org-load-hook nil
  211. "Hook that is run after org.el has been loaded."
  212. :group 'org
  213. :type 'hook)
  214. (defvar org-modules) ; defined below
  215. (defvar org-modules-loaded nil
  216. "Have the modules been loaded already?")
  217. (defun org-load-modules-maybe (&optional force)
  218. "Load all extensions listed in `org-modules'."
  219. (when (or force (not org-modules-loaded))
  220. (mapc (lambda (ext)
  221. (condition-case nil (require ext)
  222. (error (message "Problems while trying to load feature `%s'" ext))))
  223. org-modules)
  224. (setq org-modules-loaded t)))
  225. (defun org-set-modules (var value)
  226. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  227. (set var value)
  228. (when (featurep 'org)
  229. (org-load-modules-maybe 'force)))
  230. (when (org-bound-and-true-p org-modules)
  231. (let ((a (member 'org-infojs org-modules)))
  232. (and a (setcar a 'org-jsinfo))))
  233. (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)
  234. "Modules that should always be loaded together with org.el.
  235. If a description starts with <C>, the file is not part of Emacs
  236. and loading it will require that you have downloaded and properly installed
  237. the org-mode distribution.
  238. You can also use this system to load external packages (i.e. neither Org
  239. core modules, nor modules from the CONTRIB directory). Just add symbols
  240. to the end of the list. If the package is called org-xyz.el, then you need
  241. to add the symbol `xyz', and the package must have a call to
  242. (provide 'org-xyz)"
  243. :group 'org
  244. :set 'org-set-modules
  245. :type
  246. '(set :greedy t
  247. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  248. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  249. (const :tag " crypt: Encryption of subtrees" org-crypt)
  250. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  251. (const :tag " docview: Links to doc-view buffers" org-docview)
  252. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  253. (const :tag " id: Global IDs for identifying entries" org-id)
  254. (const :tag " info: Links to Info nodes" org-info)
  255. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  256. (const :tag " habit: Track your consistency with habits" org-habit)
  257. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  258. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  259. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  260. (const :tag " mew Links to Mew folders/messages" org-mew)
  261. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  262. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  263. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  264. (const :tag " vm: Links to VM folders/messages" org-vm)
  265. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  266. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  267. (const :tag " mouse: Additional mouse support" org-mouse)
  268. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  269. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  270. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  271. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  272. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  273. (const :tag "C collector: Collect properties into tables" org-collector)
  274. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  275. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  276. (const :tag "C eval: Include command output as text" org-eval)
  277. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  278. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  279. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  280. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  281. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  282. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  283. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  284. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  285. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  286. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  287. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  288. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  289. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  290. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  291. (const :tag "C registry: A registry for Org-mode links" org-registry)
  292. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  293. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  294. (const :tag "C secretary: Team management with org-mode" org-secretary)
  295. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  296. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  297. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  298. (const :tag "C track: Keep up with Org-mode development" org-track)
  299. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  300. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  301. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  302. (defcustom org-support-shift-select nil
  303. "Non-nil means make shift-cursor commands select text when possible.
  304. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  305. selecting a region, or enlarge regions started in this way.
  306. In Org-mode, in special contexts, these same keys are used for other
  307. purposes, important enough to compete with shift selection. Org tries
  308. to balance these needs by supporting `shift-select-mode' outside these
  309. special contexts, under control of this variable.
  310. The default of this variable is nil, to avoid confusing behavior. Shifted
  311. cursor keys will then execute Org commands in the following contexts:
  312. - on a headline, changing TODO state (left/right) and priority (up/down)
  313. - on a time stamp, changing the time
  314. - in a plain list item, changing the bullet type
  315. - in a property definition line, switching between allowed values
  316. - in the BEGIN line of a clock table (changing the time block).
  317. Outside these contexts, the commands will throw an error.
  318. When this variable is t and the cursor is not in a special context,
  319. Org-mode will support shift-selection for making and enlarging regions.
  320. To make this more effective, the bullet cycling will no longer happen
  321. anywhere in an item line, but only if the cursor is exactly on the bullet.
  322. If you set this variable to the symbol `always', then the keys
  323. will not be special in headlines, property lines, and item lines, to make
  324. shift selection work there as well. If this is what you want, you can
  325. use the following alternative commands: `C-c C-t' and `C-c ,' to
  326. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  327. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  328. edited by hand or in column view.
  329. However, when the cursor is on a timestamp, shift-cursor commands
  330. will still edit the time stamp - this is just too good to give up.
  331. XEmacs user should have this variable set to nil, because shift-select-mode
  332. is Emacs 23 only."
  333. :group 'org
  334. :type '(choice
  335. (const :tag "Never" nil)
  336. (const :tag "When outside special context" t)
  337. (const :tag "Everywhere except timestamps" always)))
  338. (defgroup org-startup nil
  339. "Options concerning startup of Org-mode."
  340. :tag "Org Startup"
  341. :group 'org)
  342. (defcustom org-startup-folded t
  343. "Non-nil means entering Org-mode will switch to OVERVIEW.
  344. This can also be configured on a per-file basis by adding one of
  345. the following lines anywhere in the buffer:
  346. #+STARTUP: fold (or `overview', this is equivalent)
  347. #+STARTUP: nofold (or `showall', this is equivalent)
  348. #+STARTUP: content
  349. #+STARTUP: showeverything"
  350. :group 'org-startup
  351. :type '(choice
  352. (const :tag "nofold: show all" nil)
  353. (const :tag "fold: overview" t)
  354. (const :tag "content: all headlines" content)
  355. (const :tag "show everything, even drawers" showeverything)))
  356. (defcustom org-startup-truncated t
  357. "Non-nil means entering Org-mode will set `truncate-lines'.
  358. This is useful since some lines containing links can be very long and
  359. uninteresting. Also tables look terrible when wrapped."
  360. :group 'org-startup
  361. :type 'boolean)
  362. (defcustom org-startup-indented nil
  363. "Non-nil means turn on `org-indent-mode' on startup.
  364. This can also be configured on a per-file basis by adding one of
  365. the following lines anywhere in the buffer:
  366. #+STARTUP: indent
  367. #+STARTUP: noindent"
  368. :group 'org-structure
  369. :type '(choice
  370. (const :tag "Not" nil)
  371. (const :tag "Globally (slow on startup in large files)" t)))
  372. (defcustom org-use-sub-superscripts t
  373. "Non-nil means interpret \"_\" and \"^\" for export.
  374. When this option is turned on, you can use TeX-like syntax for sub- and
  375. superscripts. Several characters after \"_\" or \"^\" will be
  376. considered as a single item - so grouping with {} is normally not
  377. needed. For example, the following things will be parsed as single
  378. sub- or superscripts.
  379. 10^24 or 10^tau several digits will be considered 1 item.
  380. 10^-12 or 10^-tau a leading sign with digits or a word
  381. x^2-y^3 will be read as x^2 - y^3, because items are
  382. terminated by almost any nonword/nondigit char.
  383. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  384. Still, ambiguity is possible - so when in doubt use {} to enclose the
  385. sub/superscript. If you set this variable to the symbol `{}',
  386. the braces are *required* in order to trigger interpretations as
  387. sub/superscript. This can be helpful in documents that need \"_\"
  388. frequently in plain text.
  389. Not all export backends support this, but HTML does.
  390. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  391. :group 'org-startup
  392. :group 'org-export-translation
  393. :type '(choice
  394. (const :tag "Always interpret" t)
  395. (const :tag "Only with braces" {})
  396. (const :tag "Never interpret" nil)))
  397. (if (fboundp 'defvaralias)
  398. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  399. (defcustom org-startup-with-beamer-mode nil
  400. "Non-nil means turn on `org-beamer-mode' on startup.
  401. This can also be configured on a per-file basis by adding one of
  402. the following lines anywhere in the buffer:
  403. #+STARTUP: beamer"
  404. :group 'org-startup
  405. :type 'boolean)
  406. (defcustom org-startup-align-all-tables nil
  407. "Non-nil means align all tables when visiting a file.
  408. This is useful when the column width in tables is forced with <N> cookies
  409. in table fields. Such tables will look correct only after the first re-align.
  410. This can also be configured on a per-file basis by adding one of
  411. the following lines anywhere in the buffer:
  412. #+STARTUP: align
  413. #+STARTUP: noalign"
  414. :group 'org-startup
  415. :type 'boolean)
  416. (defcustom org-insert-mode-line-in-empty-file nil
  417. "Non-nil means insert the first line setting Org-mode in empty files.
  418. When the function `org-mode' is called interactively in an empty file, this
  419. normally means that the file name does not automatically trigger Org-mode.
  420. To ensure that the file will always be in Org-mode in the future, a
  421. line enforcing Org-mode will be inserted into the buffer, if this option
  422. has been set."
  423. :group 'org-startup
  424. :type 'boolean)
  425. (defcustom org-replace-disputed-keys nil
  426. "Non-nil means use alternative key bindings for some keys.
  427. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  428. These keys are also used by other packages like shift-selection-mode'
  429. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  430. If you want to use Org-mode together with one of these other modes,
  431. or more generally if you would like to move some Org-mode commands to
  432. other keys, set this variable and configure the keys with the variable
  433. `org-disputed-keys'.
  434. This option is only relevant at load-time of Org-mode, and must be set
  435. *before* org.el is loaded. Changing it requires a restart of Emacs to
  436. become effective."
  437. :group 'org-startup
  438. :type 'boolean)
  439. (defcustom org-use-extra-keys nil
  440. "Non-nil means use extra key sequence definitions for certain commands.
  441. This happens automatically if you run XEmacs or if `window-system'
  442. is nil. This variable lets you do the same manually. You must
  443. set it before loading org.
  444. Example: on Carbon Emacs 22 running graphically, with an external
  445. keyboard on a Powerbook, the default way of setting M-left might
  446. not work for either Alt or ESC. Setting this variable will make
  447. it work for ESC."
  448. :group 'org-startup
  449. :type 'boolean)
  450. (if (fboundp 'defvaralias)
  451. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  452. (defcustom org-disputed-keys
  453. '(([(shift up)] . [(meta p)])
  454. ([(shift down)] . [(meta n)])
  455. ([(shift left)] . [(meta -)])
  456. ([(shift right)] . [(meta +)])
  457. ([(control shift right)] . [(meta shift +)])
  458. ([(control shift left)] . [(meta shift -)]))
  459. "Keys for which Org-mode and other modes compete.
  460. This is an alist, cars are the default keys, second element specifies
  461. the alternative to use when `org-replace-disputed-keys' is t.
  462. Keys can be specified in any syntax supported by `define-key'.
  463. The value of this option takes effect only at Org-mode's startup,
  464. therefore you'll have to restart Emacs to apply it after changing."
  465. :group 'org-startup
  466. :type 'alist)
  467. (defun org-key (key)
  468. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  469. Or return the original if not disputed.
  470. Also apply the translations defined in `org-xemacs-key-equivalents'."
  471. (when org-replace-disputed-keys
  472. (let* ((nkey (key-description key))
  473. (x (org-find-if (lambda (x)
  474. (equal (key-description (car x)) nkey))
  475. org-disputed-keys)))
  476. (setq key (if x (cdr x) key))))
  477. (when (featurep 'xemacs)
  478. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  479. key)
  480. (defun org-find-if (predicate seq)
  481. (catch 'exit
  482. (while seq
  483. (if (funcall predicate (car seq))
  484. (throw 'exit (car seq))
  485. (pop seq)))))
  486. (defun org-defkey (keymap key def)
  487. "Define a key, possibly translated, as returned by `org-key'."
  488. (define-key keymap (org-key key) def))
  489. (defcustom org-ellipsis nil
  490. "The ellipsis to use in the Org-mode outline.
  491. When nil, just use the standard three dots. When a string, use that instead,
  492. When a face, use the standard 3 dots, but with the specified face.
  493. The change affects only Org-mode (which will then use its own display table).
  494. Changing this requires executing `M-x org-mode' in a buffer to become
  495. effective."
  496. :group 'org-startup
  497. :type '(choice (const :tag "Default" nil)
  498. (face :tag "Face" :value org-warning)
  499. (string :tag "String" :value "...#")))
  500. (defvar org-display-table nil
  501. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  502. (defgroup org-keywords nil
  503. "Keywords in Org-mode."
  504. :tag "Org Keywords"
  505. :group 'org)
  506. (defcustom org-deadline-string "DEADLINE:"
  507. "String to mark deadline entries.
  508. A deadline is this string, followed by a time stamp. Should be a word,
  509. terminated by a colon. You can insert a schedule keyword and
  510. a timestamp with \\[org-deadline].
  511. Changes become only effective after restarting Emacs."
  512. :group 'org-keywords
  513. :type 'string)
  514. (defcustom org-scheduled-string "SCHEDULED:"
  515. "String to mark scheduled TODO entries.
  516. A schedule is this string, followed by a time stamp. Should be a word,
  517. terminated by a colon. You can insert a schedule keyword and
  518. a timestamp with \\[org-schedule].
  519. Changes become only effective after restarting Emacs."
  520. :group 'org-keywords
  521. :type 'string)
  522. (defcustom org-closed-string "CLOSED:"
  523. "String used as the prefix for timestamps logging closing a TODO entry."
  524. :group 'org-keywords
  525. :type 'string)
  526. (defcustom org-clock-string "CLOCK:"
  527. "String used as prefix for timestamps clocking work hours on an item."
  528. :group 'org-keywords
  529. :type 'string)
  530. (defcustom org-comment-string "COMMENT"
  531. "Entries starting with this keyword will never be exported.
  532. An entry can be toggled between COMMENT and normal with
  533. \\[org-toggle-comment].
  534. Changes become only effective after restarting Emacs."
  535. :group 'org-keywords
  536. :type 'string)
  537. (defcustom org-quote-string "QUOTE"
  538. "Entries starting with this keyword will be exported in fixed-width font.
  539. Quoting applies only to the text in the entry following the headline, and does
  540. not extend beyond the next headline, even if that is lower level.
  541. An entry can be toggled between QUOTE and normal with
  542. \\[org-toggle-fixed-width-section]."
  543. :group 'org-keywords
  544. :type 'string)
  545. (defconst org-repeat-re
  546. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  547. "Regular expression for specifying repeated events.
  548. After a match, group 1 contains the repeat expression.")
  549. (defgroup org-structure nil
  550. "Options concerning the general structure of Org-mode files."
  551. :tag "Org Structure"
  552. :group 'org)
  553. (defgroup org-reveal-location nil
  554. "Options about how to make context of a location visible."
  555. :tag "Org Reveal Location"
  556. :group 'org-structure)
  557. (defconst org-context-choice
  558. '(choice
  559. (const :tag "Always" t)
  560. (const :tag "Never" nil)
  561. (repeat :greedy t :tag "Individual contexts"
  562. (cons
  563. (choice :tag "Context"
  564. (const agenda)
  565. (const org-goto)
  566. (const occur-tree)
  567. (const tags-tree)
  568. (const link-search)
  569. (const mark-goto)
  570. (const bookmark-jump)
  571. (const isearch)
  572. (const default))
  573. (boolean))))
  574. "Contexts for the reveal options.")
  575. (defcustom org-show-hierarchy-above '((default . t))
  576. "Non-nil means show full hierarchy when revealing a location.
  577. Org-mode often shows locations in an org-mode file which might have
  578. been invisible before. When this is set, the hierarchy of headings
  579. above the exposed location is shown.
  580. Turning this off for example for sparse trees makes them very compact.
  581. Instead of t, this can also be an alist specifying this option for different
  582. contexts. Valid contexts are
  583. agenda when exposing an entry from the agenda
  584. org-goto when using the command `org-goto' on key C-c C-j
  585. occur-tree when using the command `org-occur' on key C-c /
  586. tags-tree when constructing a sparse tree based on tags matches
  587. link-search when exposing search matches associated with a link
  588. mark-goto when exposing the jump goal of a mark
  589. bookmark-jump when exposing a bookmark location
  590. isearch when exiting from an incremental search
  591. default default for all contexts not set explicitly"
  592. :group 'org-reveal-location
  593. :type org-context-choice)
  594. (defcustom org-show-following-heading '((default . nil))
  595. "Non-nil means show following heading when revealing a location.
  596. Org-mode often shows locations in an org-mode file which might have
  597. been invisible before. When this is set, the heading following the
  598. match is shown.
  599. Turning this off for example for sparse trees makes them very compact,
  600. but makes it harder to edit the location of the match. In such a case,
  601. use the command \\[org-reveal] to show more context.
  602. Instead of t, this can also be an alist specifying this option for different
  603. contexts. See `org-show-hierarchy-above' for valid contexts."
  604. :group 'org-reveal-location
  605. :type org-context-choice)
  606. (defcustom org-show-siblings '((default . nil) (isearch t))
  607. "Non-nil means show all sibling heading when revealing a location.
  608. Org-mode often shows locations in an org-mode file which might have
  609. been invisible before. When this is set, the sibling of the current entry
  610. heading are all made visible. If `org-show-hierarchy-above' is t,
  611. the same happens on each level of the hierarchy above the current entry.
  612. By default this is on for the isearch context, off for all other contexts.
  613. Turning this off for example for sparse trees makes them very compact,
  614. but makes it harder to edit the location of the match. In such a case,
  615. use the command \\[org-reveal] to show more context.
  616. Instead of t, this can also be an alist specifying this option for different
  617. contexts. See `org-show-hierarchy-above' for valid contexts."
  618. :group 'org-reveal-location
  619. :type org-context-choice)
  620. (defcustom org-show-entry-below '((default . nil))
  621. "Non-nil means show the entry below a headline when revealing a location.
  622. Org-mode often shows locations in an org-mode file which might have
  623. been invisible before. When this is set, the text below the headline that is
  624. exposed is also shown.
  625. By default this is off for all contexts.
  626. Instead of t, this can also be an alist specifying this option for different
  627. contexts. See `org-show-hierarchy-above' for valid contexts."
  628. :group 'org-reveal-location
  629. :type org-context-choice)
  630. (defcustom org-indirect-buffer-display 'other-window
  631. "How should indirect tree buffers be displayed?
  632. This applies to indirect buffers created with the commands
  633. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  634. Valid values are:
  635. current-window Display in the current window
  636. other-window Just display in another window.
  637. dedicated-frame Create one new frame, and re-use it each time.
  638. new-frame Make a new frame each time. Note that in this case
  639. previously-made indirect buffers are kept, and you need to
  640. kill these buffers yourself."
  641. :group 'org-structure
  642. :group 'org-agenda-windows
  643. :type '(choice
  644. (const :tag "In current window" current-window)
  645. (const :tag "In current frame, other window" other-window)
  646. (const :tag "Each time a new frame" new-frame)
  647. (const :tag "One dedicated frame" dedicated-frame)))
  648. (defcustom org-use-speed-commands nil
  649. "Non-nil means activate single letter commands at beginning of a headline.
  650. This may also be a function to test for appropriate locations where speed
  651. commands should be active."
  652. :group 'org-structure
  653. :type '(choice
  654. (const :tag "Never" nil)
  655. (const :tag "At beginning of headline stars" t)
  656. (function)))
  657. (defcustom org-speed-commands-user nil
  658. "Alist of additional speed commands.
  659. This list will be checked before `org-speed-commands-default'
  660. when the variable `org-use-speed-commands' is non-nil
  661. and when the cursor is at the beginning of a headline.
  662. The car if each entry is a string with a single letter, which must
  663. be assigned to `self-insert-command' in the global map.
  664. The cdr is either a command to be called interactively, a function
  665. to be called, or a form to be evaluated.
  666. An entry that is just a list with a single string will be interpreted
  667. as a descriptive headline that will be added when listing the speed
  668. commands in the Help buffer using the `?' speed command."
  669. :group 'org-structure
  670. :type '(repeat :value ("k" . ignore)
  671. (choice :value ("k" . ignore)
  672. (list :tag "Descriptive Headline" (string :tag "Headline"))
  673. (cons :tag "Letter and Command"
  674. (string :tag "Command letter")
  675. (choice
  676. (function)
  677. (sexp))))))
  678. (defgroup org-cycle nil
  679. "Options concerning visibility cycling in Org-mode."
  680. :tag "Org Cycle"
  681. :group 'org-structure)
  682. (defcustom org-cycle-skip-children-state-if-no-children t
  683. "Non-nil means skip CHILDREN state in entries that don't have any."
  684. :group 'org-cycle
  685. :type 'boolean)
  686. (defcustom org-cycle-max-level nil
  687. "Maximum level which should still be subject to visibility cycling.
  688. Levels higher than this will, for cycling, be treated as text, not a headline.
  689. When `org-odd-levels-only' is set, a value of N in this variable actually
  690. means 2N-1 stars as the limiting headline.
  691. When nil, cycle all levels.
  692. Note that the limiting level of cycling is also influenced by
  693. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  694. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  695. than its value."
  696. :group 'org-cycle
  697. :type '(choice
  698. (const :tag "No limit" nil)
  699. (integer :tag "Maximum level")))
  700. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  701. "Names of drawers. Drawers are not opened by cycling on the headline above.
  702. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  703. this:
  704. :DRAWERNAME:
  705. .....
  706. :END:
  707. The drawer \"PROPERTIES\" is special for capturing properties through
  708. the property API.
  709. Drawers can be defined on the per-file basis with a line like:
  710. #+DRAWERS: HIDDEN STATE PROPERTIES"
  711. :group 'org-structure
  712. :group 'org-cycle
  713. :type '(repeat (string :tag "Drawer Name")))
  714. (defcustom org-hide-block-startup nil
  715. "Non-nil means entering Org-mode will fold all blocks.
  716. This can also be set in on a per-file basis with
  717. #+STARTUP: hideblocks
  718. #+STARTUP: showblocks"
  719. :group 'org-startup
  720. :group 'org-cycle
  721. :type 'boolean)
  722. (defcustom org-cycle-global-at-bob nil
  723. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  724. This makes it possible to do global cycling without having to use S-TAB or
  725. \\[universal-argument] TAB. For this special case to work, the first line \
  726. of the buffer
  727. must not be a headline - it may be empty or some other text. When used in
  728. this way, `org-cycle-hook' is disables temporarily, to make sure the
  729. cursor stays at the beginning of the buffer.
  730. When this option is nil, don't do anything special at the beginning
  731. of the buffer."
  732. :group 'org-cycle
  733. :type 'boolean)
  734. (defcustom org-cycle-level-after-item/entry-creation t
  735. "Non-nil means cycle entry level or item indentation in new empty entries.
  736. When the cursor is at the end of an empty headline, i.e with only stars
  737. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  738. and then all possible ancestor states, before returning to the original state.
  739. This makes data entry extremely fast: M-RET to create a new headline,
  740. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  741. When the cursor is at the end of an empty plain list item, one TAB will
  742. make it a subitem, two or more tabs will back up to make this an item
  743. higher up in the item hierarchy."
  744. :group 'org-cycle
  745. :type 'boolean)
  746. (defcustom org-cycle-emulate-tab t
  747. "Where should `org-cycle' emulate TAB.
  748. nil Never
  749. white Only in completely white lines
  750. whitestart Only at the beginning of lines, before the first non-white char
  751. t Everywhere except in headlines
  752. exc-hl-bol Everywhere except at the start of a headline
  753. If TAB is used in a place where it does not emulate TAB, the current subtree
  754. visibility is cycled."
  755. :group 'org-cycle
  756. :type '(choice (const :tag "Never" nil)
  757. (const :tag "Only in completely white lines" white)
  758. (const :tag "Before first char in a line" whitestart)
  759. (const :tag "Everywhere except in headlines" t)
  760. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  761. ))
  762. (defcustom org-cycle-separator-lines 2
  763. "Number of empty lines needed to keep an empty line between collapsed trees.
  764. If you leave an empty line between the end of a subtree and the following
  765. headline, this empty line is hidden when the subtree is folded.
  766. Org-mode will leave (exactly) one empty line visible if the number of
  767. empty lines is equal or larger to the number given in this variable.
  768. So the default 2 means at least 2 empty lines after the end of a subtree
  769. are needed to produce free space between a collapsed subtree and the
  770. following headline.
  771. If the number is negative, and the number of empty lines is at least -N,
  772. all empty lines are shown.
  773. Special case: when 0, never leave empty lines in collapsed view."
  774. :group 'org-cycle
  775. :type 'integer)
  776. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  777. (defcustom org-pre-cycle-hook nil
  778. "Hook that is run before visibility cycling is happening.
  779. The function(s) in this hook must accept a single argument which indicates
  780. the new state that will be set right after running this hook. The
  781. argument is a symbol. Before a global state change, it can have the values
  782. `overview', `content', or `all'. Before a local state change, it can have
  783. the values `folded', `children', or `subtree'."
  784. :group 'org-cycle
  785. :type 'hook)
  786. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  787. org-cycle-hide-drawers
  788. org-cycle-show-empty-lines
  789. org-optimize-window-after-visibility-change)
  790. "Hook that is run after `org-cycle' has changed the buffer visibility.
  791. The function(s) in this hook must accept a single argument which indicates
  792. the new state that was set by the most recent `org-cycle' command. The
  793. argument is a symbol. After a global state change, it can have the values
  794. `overview', `content', or `all'. After a local state change, it can have
  795. the values `folded', `children', or `subtree'."
  796. :group 'org-cycle
  797. :type 'hook)
  798. (defgroup org-edit-structure nil
  799. "Options concerning structure editing in Org-mode."
  800. :tag "Org Edit Structure"
  801. :group 'org-structure)
  802. (defcustom org-odd-levels-only nil
  803. "Non-nil means skip even levels and only use odd levels for the outline.
  804. This has the effect that two stars are being added/taken away in
  805. promotion/demotion commands. It also influences how levels are
  806. handled by the exporters.
  807. Changing it requires restart of `font-lock-mode' to become effective
  808. for fontification also in regions already fontified.
  809. You may also set this on a per-file basis by adding one of the following
  810. lines to the buffer:
  811. #+STARTUP: odd
  812. #+STARTUP: oddeven"
  813. :group 'org-edit-structure
  814. :group 'org-appearance
  815. :type 'boolean)
  816. (defcustom org-adapt-indentation t
  817. "Non-nil means adapt indentation to outline node level.
  818. When this variable is set, Org assumes that you write outlines by
  819. indenting text in each node to align with the headline (after the stars).
  820. The following issues are influenced by this variable:
  821. - When this is set and the *entire* text in an entry is indented, the
  822. indentation is increased by one space in a demotion command, and
  823. decreased by one in a promotion command. If any line in the entry
  824. body starts with text at column 0, indentation is not changed at all.
  825. - Property drawers and planning information is inserted indented when
  826. this variable s set. When nil, they will not be indented.
  827. - TAB indents a line relative to context. The lines below a headline
  828. will be indented when this variable is set.
  829. Note that this is all about true indentation, by adding and removing
  830. space characters. See also `org-indent.el' which does level-dependent
  831. indentation in a virtual way, i.e. at display time in Emacs."
  832. :group 'org-edit-structure
  833. :type 'boolean)
  834. (defcustom org-special-ctrl-a/e nil
  835. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  836. When t, `C-a' will bring back the cursor to the beginning of the
  837. headline text, i.e. after the stars and after a possible TODO keyword.
  838. In an item, this will be the position after the bullet.
  839. When the cursor is already at that position, another `C-a' will bring
  840. it to the beginning of the line.
  841. `C-e' will jump to the end of the headline, ignoring the presence of tags
  842. in the headline. A second `C-e' will then jump to the true end of the
  843. line, after any tags. This also means that, when this variable is
  844. non-nil, `C-e' also will never jump beyond the end of the heading of a
  845. folded section, i.e. not after the ellipses.
  846. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  847. going to the true line boundary first. Only a directly following, identical
  848. keypress will bring the cursor to the special positions.
  849. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  850. set separately."
  851. :group 'org-edit-structure
  852. :type '(choice
  853. (const :tag "off" nil)
  854. (const :tag "on: after stars/bullet and before tags first" t)
  855. (const :tag "reversed: true line boundary first" reversed)
  856. (cons :tag "Set C-a and C-e separately"
  857. (choice :tag "Special C-a"
  858. (const :tag "off" nil)
  859. (const :tag "on: after stars/bullet first" t)
  860. (const :tag "reversed: before stars/bullet first" reversed))
  861. (choice :tag "Special C-e"
  862. (const :tag "off" nil)
  863. (const :tag "on: before tags first" t)
  864. (const :tag "reversed: after tags first" reversed)))))
  865. (if (fboundp 'defvaralias)
  866. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  867. (defcustom org-special-ctrl-k nil
  868. "Non-nil means `C-k' will behave specially in headlines.
  869. When nil, `C-k' will call the default `kill-line' command.
  870. When t, the following will happen while the cursor is in the headline:
  871. - When the cursor is at the beginning of a headline, kill the entire
  872. line and possible the folded subtree below the line.
  873. - When in the middle of the headline text, kill the headline up to the tags.
  874. - When after the headline text, kill the tags."
  875. :group 'org-edit-structure
  876. :type 'boolean)
  877. (defcustom org-ctrl-k-protect-subtree nil
  878. "Non-nil means, do not delete a hidden subtree with C-k.
  879. When set to the symbol `error', simply throw an error when C-k is
  880. used to kill (part-of) a headline that has hidden text behind it.
  881. Any other non-nil value will result in a query to the user, if it is
  882. OK to kill that hidden subtree. When nil, kill without remorse."
  883. :group 'org-edit-structure
  884. :type '(choice
  885. (const :tag "Do not protect hidden subtrees" nil)
  886. (const :tag "Protect hidden subtrees with a security query" t)
  887. (const :tag "Never kill a hidden subtree with C-k" error)))
  888. (defcustom org-yank-folded-subtrees t
  889. "Non-nil means when yanking subtrees, fold them.
  890. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  891. it starts with a heading and all other headings in it are either children
  892. or siblings, then fold all the subtrees. However, do this only if no
  893. text after the yank would be swallowed into a folded tree by this action."
  894. :group 'org-edit-structure
  895. :type 'boolean)
  896. (defcustom org-yank-adjusted-subtrees nil
  897. "Non-nil means when yanking subtrees, adjust the level.
  898. With this setting, `org-paste-subtree' is used to insert the subtree, see
  899. this function for details."
  900. :group 'org-edit-structure
  901. :type 'boolean)
  902. (defcustom org-M-RET-may-split-line '((default . t))
  903. "Non-nil means M-RET will split the line at the cursor position.
  904. When nil, it will go to the end of the line before making a
  905. new line.
  906. You may also set this option in a different way for different
  907. contexts. Valid contexts are:
  908. headline when creating a new headline
  909. item when creating a new item
  910. table in a table field
  911. default the value to be used for all contexts not explicitly
  912. customized"
  913. :group 'org-structure
  914. :group 'org-table
  915. :type '(choice
  916. (const :tag "Always" t)
  917. (const :tag "Never" nil)
  918. (repeat :greedy t :tag "Individual contexts"
  919. (cons
  920. (choice :tag "Context"
  921. (const headline)
  922. (const item)
  923. (const table)
  924. (const default))
  925. (boolean)))))
  926. (defcustom org-insert-heading-respect-content nil
  927. "Non-nil means insert new headings after the current subtree.
  928. When nil, the new heading is created directly after the current line.
  929. The commands \\[org-insert-heading-respect-content] and
  930. \\[org-insert-todo-heading-respect-content] turn this variable on
  931. for the duration of the command."
  932. :group 'org-structure
  933. :type 'boolean)
  934. (defcustom org-blank-before-new-entry '((heading . auto)
  935. (plain-list-item . auto))
  936. "Should `org-insert-heading' leave a blank line before new heading/item?
  937. The value is an alist, with `heading' and `plain-list-item' as car,
  938. and a boolean flag as cdr. For plain lists, if the variable
  939. `org-empty-line-terminates-plain-lists' is set, the setting here
  940. is ignored and no empty line is inserted, to keep the list in tact."
  941. :group 'org-edit-structure
  942. :type '(list
  943. (cons (const heading)
  944. (choice (const :tag "Never" nil)
  945. (const :tag "Always" t)
  946. (const :tag "Auto" auto)))
  947. (cons (const plain-list-item)
  948. (choice (const :tag "Never" nil)
  949. (const :tag "Always" t)
  950. (const :tag "Auto" auto)))))
  951. (defcustom org-insert-heading-hook nil
  952. "Hook being run after inserting a new heading."
  953. :group 'org-edit-structure
  954. :type 'hook)
  955. (defcustom org-enable-fixed-width-editor t
  956. "Non-nil means lines starting with \":\" are treated as fixed-width.
  957. This currently only means they are never auto-wrapped.
  958. When nil, such lines will be treated like ordinary lines.
  959. See also the QUOTE keyword."
  960. :group 'org-edit-structure
  961. :type 'boolean)
  962. (defcustom org-goto-auto-isearch t
  963. "Non-nil means typing characters in `org-goto' starts incremental search."
  964. :group 'org-edit-structure
  965. :type 'boolean)
  966. (defgroup org-sparse-trees nil
  967. "Options concerning sparse trees in Org-mode."
  968. :tag "Org Sparse Trees"
  969. :group 'org-structure)
  970. (defcustom org-highlight-sparse-tree-matches t
  971. "Non-nil means highlight all matches that define a sparse tree.
  972. The highlights will automatically disappear the next time the buffer is
  973. changed by an edit command."
  974. :group 'org-sparse-trees
  975. :type 'boolean)
  976. (defcustom org-remove-highlights-with-change t
  977. "Non-nil means any change to the buffer will remove temporary highlights.
  978. Such highlights are created by `org-occur' and `org-clock-display'.
  979. When nil, `C-c C-c needs to be used to get rid of the highlights.
  980. The highlights created by `org-preview-latex-fragment' always need
  981. `C-c C-c' to be removed."
  982. :group 'org-sparse-trees
  983. :group 'org-time
  984. :type 'boolean)
  985. (defcustom org-occur-hook '(org-first-headline-recenter)
  986. "Hook that is run after `org-occur' has constructed a sparse tree.
  987. This can be used to recenter the window to show as much of the structure
  988. as possible."
  989. :group 'org-sparse-trees
  990. :type 'hook)
  991. (defgroup org-imenu-and-speedbar nil
  992. "Options concerning imenu and speedbar in Org-mode."
  993. :tag "Org Imenu and Speedbar"
  994. :group 'org-structure)
  995. (defcustom org-imenu-depth 2
  996. "The maximum level for Imenu access to Org-mode headlines.
  997. This also applied for speedbar access."
  998. :group 'org-imenu-and-speedbar
  999. :type 'integer)
  1000. (defgroup org-table nil
  1001. "Options concerning tables in Org-mode."
  1002. :tag "Org Table"
  1003. :group 'org)
  1004. (defcustom org-enable-table-editor 'optimized
  1005. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1006. When nil, such lines will be treated like ordinary lines.
  1007. When equal to the symbol `optimized', the table editor will be optimized to
  1008. do the following:
  1009. - Automatic overwrite mode in front of whitespace in table fields.
  1010. This makes the structure of the table stay in tact as long as the edited
  1011. field does not exceed the column width.
  1012. - Minimize the number of realigns. Normally, the table is aligned each time
  1013. TAB or RET are pressed to move to another field. With optimization this
  1014. happens only if changes to a field might have changed the column width.
  1015. Optimization requires replacing the functions `self-insert-command',
  1016. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1017. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1018. very good at guessing when a re-align will be necessary, but you can always
  1019. force one with \\[org-ctrl-c-ctrl-c].
  1020. If you would like to use the optimized version in Org-mode, but the
  1021. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1022. This variable can be used to turn on and off the table editor during a session,
  1023. but in order to toggle optimization, a restart is required.
  1024. See also the variable `org-table-auto-blank-field'."
  1025. :group 'org-table
  1026. :type '(choice
  1027. (const :tag "off" nil)
  1028. (const :tag "on" t)
  1029. (const :tag "on, optimized" optimized)))
  1030. (defcustom org-self-insert-cluster-for-undo t
  1031. "Non-nil means cluster self-insert commands for undo when possible.
  1032. If this is set, then, like in the Emacs command loop, 20 consecutive
  1033. characters will be undone together.
  1034. This is configurable, because there is some impact on typing performance."
  1035. :group 'org-table
  1036. :type 'boolean)
  1037. (defcustom org-table-tab-recognizes-table.el t
  1038. "Non-nil means TAB will automatically notice a table.el table.
  1039. When it sees such a table, it moves point into it and - if necessary -
  1040. calls `table-recognize-table'."
  1041. :group 'org-table-editing
  1042. :type 'boolean)
  1043. (defgroup org-link nil
  1044. "Options concerning links in Org-mode."
  1045. :tag "Org Link"
  1046. :group 'org)
  1047. (defvar org-link-abbrev-alist-local nil
  1048. "Buffer-local version of `org-link-abbrev-alist', which see.
  1049. The value of this is taken from the #+LINK lines.")
  1050. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1051. (defcustom org-link-abbrev-alist nil
  1052. "Alist of link abbreviations.
  1053. The car of each element is a string, to be replaced at the start of a link.
  1054. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1055. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1056. [[linkkey:tag][description]]
  1057. The 'linkkey' must be a word word, starting with a letter, followed
  1058. by letters, numbers, '-' or '_'.
  1059. If REPLACE is a string, the tag will simply be appended to create the link.
  1060. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1061. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1062. be inserted at that point (see the function `url-hexify-string').
  1063. REPLACE may also be a function that will be called with the tag as the
  1064. only argument to create the link, which should be returned as a string.
  1065. See the manual for examples."
  1066. :group 'org-link
  1067. :type '(repeat
  1068. (cons
  1069. (string :tag "Protocol")
  1070. (choice
  1071. (string :tag "Format")
  1072. (function)))))
  1073. (defcustom org-descriptive-links t
  1074. "Non-nil means hide link part and only show description of bracket links.
  1075. Bracket links are like [[link][description]]. This variable sets the initial
  1076. state in new org-mode buffers. The setting can then be toggled on a
  1077. per-buffer basis from the Org->Hyperlinks menu."
  1078. :group 'org-link
  1079. :type 'boolean)
  1080. (defcustom org-link-file-path-type 'adaptive
  1081. "How the path name in file links should be stored.
  1082. Valid values are:
  1083. relative Relative to the current directory, i.e. the directory of the file
  1084. into which the link is being inserted.
  1085. absolute Absolute path, if possible with ~ for home directory.
  1086. noabbrev Absolute path, no abbreviation of home directory.
  1087. adaptive Use relative path for files in the current directory and sub-
  1088. directories of it. For other files, use an absolute path."
  1089. :group 'org-link
  1090. :type '(choice
  1091. (const relative)
  1092. (const absolute)
  1093. (const noabbrev)
  1094. (const adaptive)))
  1095. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1096. "Types of links that should be activated in Org-mode files.
  1097. This is a list of symbols, each leading to the activation of a certain link
  1098. type. In principle, it does not hurt to turn on most link types - there may
  1099. be a small gain when turning off unused link types. The types are:
  1100. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1101. angular Links in angular brackets that may contain whitespace like
  1102. <bbdb:Carsten Dominik>.
  1103. plain Plain links in normal text, no whitespace, like http://google.com.
  1104. radio Text that is matched by a radio target, see manual for details.
  1105. tag Tag settings in a headline (link to tag search).
  1106. date Time stamps (link to calendar).
  1107. footnote Footnote labels.
  1108. Changing this variable requires a restart of Emacs to become effective."
  1109. :group 'org-link
  1110. :type '(set :greedy t
  1111. (const :tag "Double bracket links (new style)" bracket)
  1112. (const :tag "Angular bracket links (old style)" angular)
  1113. (const :tag "Plain text links" plain)
  1114. (const :tag "Radio target matches" radio)
  1115. (const :tag "Tags" tag)
  1116. (const :tag "Timestamps" date)
  1117. (const :tag "Footnotes" footnote)))
  1118. (defcustom org-make-link-description-function nil
  1119. "Function to use to generate link descriptions from links.
  1120. If nil the link location will be used. This function must take
  1121. two parameters; the first is the link and the second the
  1122. description `org-insert-link' has generated, and should return the
  1123. description to use."
  1124. :group 'org-link
  1125. :type 'function)
  1126. (defgroup org-link-store nil
  1127. "Options concerning storing links in Org-mode."
  1128. :tag "Org Store Link"
  1129. :group 'org-link)
  1130. (defcustom org-email-link-description-format "Email %c: %.30s"
  1131. "Format of the description part of a link to an email or usenet message.
  1132. The following %-escapes will be replaced by corresponding information:
  1133. %F full \"From\" field
  1134. %f name, taken from \"From\" field, address if no name
  1135. %T full \"To\" field
  1136. %t first name in \"To\" field, address if no name
  1137. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1138. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1139. %s subject
  1140. %m message-id.
  1141. You may use normal field width specification between the % and the letter.
  1142. This is for example useful to limit the length of the subject.
  1143. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1144. :group 'org-link-store
  1145. :type 'string)
  1146. (defcustom org-from-is-user-regexp
  1147. (let (r1 r2)
  1148. (when (and user-mail-address (not (string= user-mail-address "")))
  1149. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1150. (when (and user-full-name (not (string= user-full-name "")))
  1151. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1152. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1153. "Regexp matched against the \"From:\" header of an email or usenet message.
  1154. It should match if the message is from the user him/herself."
  1155. :group 'org-link-store
  1156. :type 'regexp)
  1157. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1158. "Non-nil means storing a link to an Org file will use entry IDs.
  1159. Note that before this variable is even considered, org-id must be loaded,
  1160. so please customize `org-modules' and turn it on.
  1161. The variable can have the following values:
  1162. t Create an ID if needed to make a link to the current entry.
  1163. create-if-interactive
  1164. If `org-store-link' is called directly (interactively, as a user
  1165. command), do create an ID to support the link. But when doing the
  1166. job for remember, only use the ID if it already exists. The
  1167. purpose of this setting is to avoid proliferation of unwanted
  1168. IDs, just because you happen to be in an Org file when you
  1169. call `org-remember' that automatically and preemptively
  1170. creates a link. If you do want to get an ID link in a remember
  1171. template to an entry not having an ID, create it first by
  1172. explicitly creating a link to it, using `C-c C-l' first.
  1173. create-if-interactive-and-no-custom-id
  1174. Like create-if-interactive, but do not create an ID if there is
  1175. a CUSTOM_ID property defined in the entry. This is the default.
  1176. use-existing
  1177. Use existing ID, do not create one.
  1178. nil Never use an ID to make a link, instead link using a text search for
  1179. the headline text."
  1180. :group 'org-link-store
  1181. :type '(choice
  1182. (const :tag "Create ID to make link" t)
  1183. (const :tag "Create if storing link interactively"
  1184. create-if-interactive)
  1185. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1186. create-if-interactive-and-no-custom-id)
  1187. (const :tag "Only use existing" use-existing)
  1188. (const :tag "Do not use ID to create link" nil)))
  1189. (defcustom org-context-in-file-links t
  1190. "Non-nil means file links from `org-store-link' contain context.
  1191. A search string will be added to the file name with :: as separator and
  1192. used to find the context when the link is activated by the command
  1193. `org-open-at-point'.
  1194. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1195. negates this setting for the duration of the command."
  1196. :group 'org-link-store
  1197. :type 'boolean)
  1198. (defcustom org-keep-stored-link-after-insertion nil
  1199. "Non-nil means keep link in list for entire session.
  1200. The command `org-store-link' adds a link pointing to the current
  1201. location to an internal list. These links accumulate during a session.
  1202. The command `org-insert-link' can be used to insert links into any
  1203. Org-mode file (offering completion for all stored links). When this
  1204. option is nil, every link which has been inserted once using \\[org-insert-link]
  1205. will be removed from the list, to make completing the unused links
  1206. more efficient."
  1207. :group 'org-link-store
  1208. :type 'boolean)
  1209. (defgroup org-link-follow nil
  1210. "Options concerning following links in Org-mode."
  1211. :tag "Org Follow Link"
  1212. :group 'org-link)
  1213. (defcustom org-link-translation-function nil
  1214. "Function to translate links with different syntax to Org syntax.
  1215. This can be used to translate links created for example by the Planner
  1216. or emacs-wiki packages to Org syntax.
  1217. The function must accept two parameters, a TYPE containing the link
  1218. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1219. which is everything after the link protocol. It should return a cons
  1220. with possibly modified values of type and path.
  1221. Org contains a function for this, so if you set this variable to
  1222. `org-translate-link-from-planner', you should be able follow many
  1223. links created by planner."
  1224. :group 'org-link-follow
  1225. :type 'function)
  1226. (defcustom org-follow-link-hook nil
  1227. "Hook that is run after a link has been followed."
  1228. :group 'org-link-follow
  1229. :type 'hook)
  1230. (defcustom org-tab-follows-link nil
  1231. "Non-nil means on links TAB will follow the link.
  1232. Needs to be set before org.el is loaded.
  1233. This really should not be used, it does not make sense, and the
  1234. implementation is bad."
  1235. :group 'org-link-follow
  1236. :type 'boolean)
  1237. (defcustom org-return-follows-link nil
  1238. "Non-nil means on links RET will follow the link."
  1239. :group 'org-link-follow
  1240. :type 'boolean)
  1241. (defcustom org-mouse-1-follows-link
  1242. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1243. "Non-nil means mouse-1 on a link will follow the link.
  1244. A longer mouse click will still set point. Does not work on XEmacs.
  1245. Needs to be set before org.el is loaded."
  1246. :group 'org-link-follow
  1247. :type 'boolean)
  1248. (defcustom org-mark-ring-length 4
  1249. "Number of different positions to be recorded in the ring.
  1250. Changing this requires a restart of Emacs to work correctly."
  1251. :group 'org-link-follow
  1252. :type 'integer)
  1253. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1254. "Non-nil means internal links in Org files must exactly match a headline.
  1255. When nil, the link search tries to match a phrase will all words
  1256. in the search text."
  1257. :group 'org-link-follow
  1258. :type '(choice
  1259. (const :tag "Use fuzy text search" nil)
  1260. (const :tag "Match only exact headline" t)
  1261. (const :tag "Match extact headline or query to create it"
  1262. query-to-create)))
  1263. (defcustom org-link-frame-setup
  1264. '((vm . vm-visit-folder-other-frame)
  1265. (gnus . org-gnus-no-new-news)
  1266. (file . find-file-other-window)
  1267. (wl . wl-other-frame))
  1268. "Setup the frame configuration for following links.
  1269. When following a link with Emacs, it may often be useful to display
  1270. this link in another window or frame. This variable can be used to
  1271. set this up for the different types of links.
  1272. For VM, use any of
  1273. `vm-visit-folder'
  1274. `vm-visit-folder-other-frame'
  1275. For Gnus, use any of
  1276. `gnus'
  1277. `gnus-other-frame'
  1278. `org-gnus-no-new-news'
  1279. For FILE, use any of
  1280. `find-file'
  1281. `find-file-other-window'
  1282. `find-file-other-frame'
  1283. For Wanderlust use any of
  1284. `wl'
  1285. `wl-other-frame'
  1286. For the calendar, use the variable `calendar-setup'.
  1287. For BBDB, it is currently only possible to display the matches in
  1288. another window."
  1289. :group 'org-link-follow
  1290. :type '(list
  1291. (cons (const vm)
  1292. (choice
  1293. (const vm-visit-folder)
  1294. (const vm-visit-folder-other-window)
  1295. (const vm-visit-folder-other-frame)))
  1296. (cons (const gnus)
  1297. (choice
  1298. (const gnus)
  1299. (const gnus-other-frame)
  1300. (const org-gnus-no-new-news)))
  1301. (cons (const file)
  1302. (choice
  1303. (const find-file)
  1304. (const find-file-other-window)
  1305. (const find-file-other-frame)))
  1306. (cons (const wl)
  1307. (choice
  1308. (const wl)
  1309. (const wl-other-frame)))))
  1310. (defcustom org-display-internal-link-with-indirect-buffer nil
  1311. "Non-nil means use indirect buffer to display infile links.
  1312. Activating internal links (from one location in a file to another location
  1313. in the same file) normally just jumps to the location. When the link is
  1314. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1315. is displayed in
  1316. another window. When this option is set, the other window actually displays
  1317. an indirect buffer clone of the current buffer, to avoid any visibility
  1318. changes to the current buffer."
  1319. :group 'org-link-follow
  1320. :type 'boolean)
  1321. (defcustom org-open-non-existing-files nil
  1322. "Non-nil means `org-open-file' will open non-existing files.
  1323. When nil, an error will be generated.
  1324. This variable applies only to external applications because they
  1325. might choke on non-existing files. If the link is to a file that
  1326. will be opened in Emacs, the variable is ignored."
  1327. :group 'org-link-follow
  1328. :type 'boolean)
  1329. (defcustom org-open-directory-means-index-dot-org nil
  1330. "Non-nil means a link to a directory really means to index.org.
  1331. When nil, following a directory link will run dired or open a finder/explorer
  1332. window on that directory."
  1333. :group 'org-link-follow
  1334. :type 'boolean)
  1335. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1336. "Function and arguments to call for following mailto links.
  1337. This is a list with the first element being a Lisp function, and the
  1338. remaining elements being arguments to the function. In string arguments,
  1339. %a will be replaced by the address, and %s will be replaced by the subject
  1340. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1341. :group 'org-link-follow
  1342. :type '(choice
  1343. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1344. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1345. (const :tag "message-mail" (message-mail "%a" "%s"))
  1346. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1347. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1348. "Non-nil means ask for confirmation before executing shell links.
  1349. Shell links can be dangerous: just think about a link
  1350. [[shell:rm -rf ~/*][Google Search]]
  1351. This link would show up in your Org-mode document as \"Google Search\",
  1352. but really it would remove your entire home directory.
  1353. Therefore we advise against setting this variable to nil.
  1354. Just change it to `y-or-n-p' if you want to confirm with a
  1355. single keystroke rather than having to type \"yes\"."
  1356. :group 'org-link-follow
  1357. :type '(choice
  1358. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1359. (const :tag "with y-or-n (faster)" y-or-n-p)
  1360. (const :tag "no confirmation (dangerous)" nil)))
  1361. (put 'org-confirm-shell-link-function
  1362. 'safe-local-variable
  1363. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1364. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1365. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1366. Elisp links can be dangerous: just think about a link
  1367. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1368. This link would show up in your Org-mode document as \"Google Search\",
  1369. but really it would remove your entire home directory.
  1370. Therefore we advise against setting this variable to nil.
  1371. Just change it to `y-or-n-p' if you want to confirm with a
  1372. single keystroke rather than having to type \"yes\"."
  1373. :group 'org-link-follow
  1374. :type '(choice
  1375. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1376. (const :tag "with y-or-n (faster)" y-or-n-p)
  1377. (const :tag "no confirmation (dangerous)" nil)))
  1378. (put 'org-confirm-shell-link-function
  1379. 'safe-local-variable
  1380. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1381. (defconst org-file-apps-defaults-gnu
  1382. '((remote . emacs)
  1383. (system . mailcap)
  1384. (t . mailcap))
  1385. "Default file applications on a UNIX or GNU/Linux system.
  1386. See `org-file-apps'.")
  1387. (defconst org-file-apps-defaults-macosx
  1388. '((remote . emacs)
  1389. (t . "open %s")
  1390. (system . "open %s")
  1391. ("ps.gz" . "gv %s")
  1392. ("eps.gz" . "gv %s")
  1393. ("dvi" . "xdvi %s")
  1394. ("fig" . "xfig %s"))
  1395. "Default file applications on a MacOS X system.
  1396. The system \"open\" is known as a default, but we use X11 applications
  1397. for some files for which the OS does not have a good default.
  1398. See `org-file-apps'.")
  1399. (defconst org-file-apps-defaults-windowsnt
  1400. (list
  1401. '(remote . emacs)
  1402. (cons t
  1403. (list (if (featurep 'xemacs)
  1404. 'mswindows-shell-execute
  1405. 'w32-shell-execute)
  1406. "open" 'file))
  1407. (cons 'system
  1408. (list (if (featurep 'xemacs)
  1409. 'mswindows-shell-execute
  1410. 'w32-shell-execute)
  1411. "open" 'file)))
  1412. "Default file applications on a Windows NT system.
  1413. The system \"open\" is used for most files.
  1414. See `org-file-apps'.")
  1415. (defcustom org-file-apps
  1416. '(
  1417. (auto-mode . emacs)
  1418. ("\\.mm\\'" . default)
  1419. ("\\.x?html?\\'" . default)
  1420. ("\\.pdf\\'" . default)
  1421. )
  1422. "External applications for opening `file:path' items in a document.
  1423. Org-mode uses system defaults for different file types, but
  1424. you can use this variable to set the application for a given file
  1425. extension. The entries in this list are cons cells where the car identifies
  1426. files and the cdr the corresponding command. Possible values for the
  1427. file identifier are
  1428. \"string\" A string as a file identifier can be interpreted in different
  1429. ways, depending on its contents:
  1430. - Alphanumeric characters only:
  1431. Match links with this file extension.
  1432. Example: (\"pdf\" . \"evince %s\")
  1433. to open PDFs with evince.
  1434. - Regular expression: Match links where the
  1435. filename matches the regexp. If you want to
  1436. use groups here, use shy groups.
  1437. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1438. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1439. to open *.html and *.xhtml with firefox.
  1440. - Regular expression which contains (non-shy) groups:
  1441. Match links where the whole link, including \"::\", and
  1442. anything after that, matches the regexp.
  1443. In a custom command string, %1, %2, etc. are replaced with
  1444. the parts of the link that were matched by the groups.
  1445. For backwards compatibility, if a command string is given
  1446. that does not use any of the group matches, this case is
  1447. handled identically to the second one (i.e. match against
  1448. file name only).
  1449. In a custom lisp form, you can access the group matches with
  1450. (match-string n link).
  1451. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1452. to open [[file:document.pdf::5]] with evince at page 5.
  1453. `directory' Matches a directory
  1454. `remote' Matches a remote file, accessible through tramp or efs.
  1455. Remote files most likely should be visited through Emacs
  1456. because external applications cannot handle such paths.
  1457. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1458. so all files Emacs knows how to handle. Using this with
  1459. command `emacs' will open most files in Emacs. Beware that this
  1460. will also open html files inside Emacs, unless you add
  1461. (\"html\" . default) to the list as well.
  1462. t Default for files not matched by any of the other options.
  1463. `system' The system command to open files, like `open' on Windows
  1464. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1465. that will be selected if you call `C-c C-o' with a double
  1466. \\[universal-argument] \\[universal-argument] prefix.
  1467. Possible values for the command are:
  1468. `emacs' The file will be visited by the current Emacs process.
  1469. `default' Use the default application for this file type, which is the
  1470. association for t in the list, most likely in the system-specific
  1471. part.
  1472. This can be used to overrule an unwanted setting in the
  1473. system-specific variable.
  1474. `system' Use the system command for opening files, like \"open\".
  1475. This command is specified by the entry whose car is `system'.
  1476. Most likely, the system-specific version of this variable
  1477. does define this command, but you can overrule/replace it
  1478. here.
  1479. string A command to be executed by a shell; %s will be replaced
  1480. by the path to the file.
  1481. sexp A Lisp form which will be evaluated. The file path will
  1482. be available in the Lisp variable `file'.
  1483. For more examples, see the system specific constants
  1484. `org-file-apps-defaults-macosx'
  1485. `org-file-apps-defaults-windowsnt'
  1486. `org-file-apps-defaults-gnu'."
  1487. :group 'org-link-follow
  1488. :type '(repeat
  1489. (cons (choice :value ""
  1490. (string :tag "Extension")
  1491. (const :tag "System command to open files" system)
  1492. (const :tag "Default for unrecognized files" t)
  1493. (const :tag "Remote file" remote)
  1494. (const :tag "Links to a directory" directory)
  1495. (const :tag "Any files that have Emacs modes"
  1496. auto-mode))
  1497. (choice :value ""
  1498. (const :tag "Visit with Emacs" emacs)
  1499. (const :tag "Use default" default)
  1500. (const :tag "Use the system command" system)
  1501. (string :tag "Command")
  1502. (sexp :tag "Lisp form")))))
  1503. (defgroup org-refile nil
  1504. "Options concerning refiling entries in Org-mode."
  1505. :tag "Org Refile"
  1506. :group 'org)
  1507. (defcustom org-directory "~/org"
  1508. "Directory with org files.
  1509. This is just a default location to look for Org files. There is no need
  1510. at all to put your files into this directory. It is only used in the
  1511. following situations:
  1512. 1. When a remember template specifies a target file that is not an
  1513. absolute path. The path will then be interpreted relative to
  1514. `org-directory'
  1515. 2. When a remember note is filed away in an interactive way (when exiting the
  1516. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1517. with `org-directory' as the default path."
  1518. :group 'org-refile
  1519. :group 'org-remember
  1520. :type 'directory)
  1521. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1522. "Default target for storing notes.
  1523. Used as a fall back file for org-remember.el and org-capture.el, for
  1524. templates that do not specify a target file."
  1525. :group 'org-refile
  1526. :group 'org-remember
  1527. :type '(choice
  1528. (const :tag "Default from remember-data-file" nil)
  1529. file))
  1530. (defcustom org-goto-interface 'outline
  1531. "The default interface to be used for `org-goto'.
  1532. Allowed values are:
  1533. outline The interface shows an outline of the relevant file
  1534. and the correct heading is found by moving through
  1535. the outline or by searching with incremental search.
  1536. outline-path-completion Headlines in the current buffer are offered via
  1537. completion. This is the interface also used by
  1538. the refile command."
  1539. :group 'org-refile
  1540. :type '(choice
  1541. (const :tag "Outline" outline)
  1542. (const :tag "Outline-path-completion" outline-path-completion)))
  1543. (defcustom org-goto-max-level 5
  1544. "Maximum target level when running `org-goto' with refile interface."
  1545. :group 'org-refile
  1546. :type 'integer)
  1547. (defcustom org-reverse-note-order nil
  1548. "Non-nil means store new notes at the beginning of a file or entry.
  1549. When nil, new notes will be filed to the end of a file or entry.
  1550. This can also be a list with cons cells of regular expressions that
  1551. are matched against file names, and values."
  1552. :group 'org-remember
  1553. :group 'org-refile
  1554. :type '(choice
  1555. (const :tag "Reverse always" t)
  1556. (const :tag "Reverse never" nil)
  1557. (repeat :tag "By file name regexp"
  1558. (cons regexp boolean))))
  1559. (defcustom org-log-refile nil
  1560. "Information to record when a task is refiled.
  1561. Possible values are:
  1562. nil Don't add anything
  1563. time Add a time stamp to the task
  1564. note Prompt for a note and add it with template `org-log-note-headings'
  1565. This option can also be set with on a per-file-basis with
  1566. #+STARTUP: nologrefile
  1567. #+STARTUP: logrefile
  1568. #+STARTUP: lognoterefile
  1569. You can have local logging settings for a subtree by setting the LOGGING
  1570. property to one or more of these keywords.
  1571. When bulk-refiling from the agenda, the value `note' is forbidden and
  1572. will temporarily be changed to `time'."
  1573. :group 'org-refile
  1574. :group 'org-progress
  1575. :type '(choice
  1576. (const :tag "No logging" nil)
  1577. (const :tag "Record timestamp" time)
  1578. (const :tag "Record timestamp with note." note)))
  1579. (defcustom org-refile-targets nil
  1580. "Targets for refiling entries with \\[org-refile].
  1581. This is list of cons cells. Each cell contains:
  1582. - a specification of the files to be considered, either a list of files,
  1583. or a symbol whose function or variable value will be used to retrieve
  1584. a file name or a list of file names. If you use `org-agenda-files' for
  1585. that, all agenda files will be scanned for targets. Nil means consider
  1586. headings in the current buffer.
  1587. - A specification of how to find candidate refile targets. This may be
  1588. any of:
  1589. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1590. This tag has to be present in all target headlines, inheritance will
  1591. not be considered.
  1592. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1593. todo keyword.
  1594. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1595. headlines that are refiling targets.
  1596. - a cons cell (:level . N). Any headline of level N is considered a target.
  1597. Note that, when `org-odd-levels-only' is set, level corresponds to
  1598. order in hierarchy, not to the number of stars.
  1599. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1600. Note that, when `org-odd-levels-only' is set, level corresponds to
  1601. order in hierarchy, not to the number of stars.
  1602. You can set the variable `org-refile-target-verify-function' to a function
  1603. to verify each headline found by the simple criteria above.
  1604. When this variable is nil, all top-level headlines in the current buffer
  1605. are used, equivalent to the value `((nil . (:level . 1))'."
  1606. :group 'org-refile
  1607. :type '(repeat
  1608. (cons
  1609. (choice :value org-agenda-files
  1610. (const :tag "All agenda files" org-agenda-files)
  1611. (const :tag "Current buffer" nil)
  1612. (function) (variable) (file))
  1613. (choice :tag "Identify target headline by"
  1614. (cons :tag "Specific tag" (const :value :tag) (string))
  1615. (cons :tag "TODO keyword" (const :value :todo) (string))
  1616. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1617. (cons :tag "Level number" (const :value :level) (integer))
  1618. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1619. (defcustom org-refile-target-verify-function nil
  1620. "Function to verify if the headline at point should be a refile target.
  1621. The function will be called without arguments, with point at the
  1622. beginning of the headline. It should return t and leave point
  1623. where it is if the headline is a valid target for refiling.
  1624. If the target should not be selected, the function must return nil.
  1625. In addition to this, it may move point to a place from where the search
  1626. should be continued. For example, the function may decide that the entire
  1627. subtree of the current entry should be excluded and move point to the end
  1628. of the subtree."
  1629. :group 'org-refile
  1630. :type 'function)
  1631. (defcustom org-refile-use-cache nil
  1632. "Non-nil means cache refile targets to speed up the process.
  1633. The cache for a particular file will be updated automatically when
  1634. the buffer has been killed, or when any of the marker used for flagging
  1635. refile targets no longer points at a live buffer.
  1636. If you have added new entries to a buffer that might themselves be targets,
  1637. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1638. find that easier, `C-u C-u C-u C-c C-w'."
  1639. :group 'org-refile
  1640. :type 'boolean)
  1641. (defcustom org-refile-use-outline-path nil
  1642. "Non-nil means provide refile targets as paths.
  1643. So a level 3 headline will be available as level1/level2/level3.
  1644. When the value is `file', also include the file name (without directory)
  1645. into the path. In this case, you can also stop the completion after
  1646. the file name, to get entries inserted as top level in the file.
  1647. When `full-file-path', include the full file path."
  1648. :group 'org-refile
  1649. :type '(choice
  1650. (const :tag "Not" nil)
  1651. (const :tag "Yes" t)
  1652. (const :tag "Start with file name" file)
  1653. (const :tag "Start with full file path" full-file-path)))
  1654. (defcustom org-outline-path-complete-in-steps t
  1655. "Non-nil means complete the outline path in hierarchical steps.
  1656. When Org-mode uses the refile interface to select an outline path
  1657. \(see variable `org-refile-use-outline-path'), the completion of
  1658. the path can be done is a single go, or if can be done in steps down
  1659. the headline hierarchy. Going in steps is probably the best if you
  1660. do not use a special completion package like `ido' or `icicles'.
  1661. However, when using these packages, going in one step can be very
  1662. fast, while still showing the whole path to the entry."
  1663. :group 'org-refile
  1664. :type 'boolean)
  1665. (defcustom org-refile-allow-creating-parent-nodes nil
  1666. "Non-nil means allow to create new nodes as refile targets.
  1667. New nodes are then created by adding \"/new node name\" to the completion
  1668. of an existing node. When the value of this variable is `confirm',
  1669. new node creation must be confirmed by the user (recommended)
  1670. When nil, the completion must match an existing entry.
  1671. Note that, if the new heading is not seen by the criteria
  1672. listed in `org-refile-targets', multiple instances of the same
  1673. heading would be created by trying again to file under the new
  1674. heading."
  1675. :group 'org-refile
  1676. :type '(choice
  1677. (const :tag "Never" nil)
  1678. (const :tag "Always" t)
  1679. (const :tag "Prompt for confirmation" confirm)))
  1680. (defgroup org-todo nil
  1681. "Options concerning TODO items in Org-mode."
  1682. :tag "Org TODO"
  1683. :group 'org)
  1684. (defgroup org-progress nil
  1685. "Options concerning Progress logging in Org-mode."
  1686. :tag "Org Progress"
  1687. :group 'org-time)
  1688. (defvar org-todo-interpretation-widgets
  1689. '(
  1690. (:tag "Sequence (cycling hits every state)" sequence)
  1691. (:tag "Type (cycling directly to DONE)" type))
  1692. "The available interpretation symbols for customizing `org-todo-keywords'.
  1693. Interested libraries should add to this list.")
  1694. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1695. "List of TODO entry keyword sequences and their interpretation.
  1696. \\<org-mode-map>This is a list of sequences.
  1697. Each sequence starts with a symbol, either `sequence' or `type',
  1698. indicating if the keywords should be interpreted as a sequence of
  1699. action steps, or as different types of TODO items. The first
  1700. keywords are states requiring action - these states will select a headline
  1701. for inclusion into the global TODO list Org-mode produces. If one of
  1702. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1703. signify that no further action is necessary. If \"|\" is not found,
  1704. the last keyword is treated as the only DONE state of the sequence.
  1705. The command \\[org-todo] cycles an entry through these states, and one
  1706. additional state where no keyword is present. For details about this
  1707. cycling, see the manual.
  1708. TODO keywords and interpretation can also be set on a per-file basis with
  1709. the special #+SEQ_TODO and #+TYP_TODO lines.
  1710. Each keyword can optionally specify a character for fast state selection
  1711. \(in combination with the variable `org-use-fast-todo-selection')
  1712. and specifiers for state change logging, using the same syntax
  1713. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1714. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1715. indicates to record a time stamp each time this state is selected.
  1716. Each keyword may also specify if a timestamp or a note should be
  1717. recorded when entering or leaving the state, by adding additional
  1718. characters in the parenthesis after the keyword. This looks like this:
  1719. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1720. record only the time of the state change. With X and Y being either
  1721. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1722. Y when leaving the state if and only if the *target* state does not
  1723. define X. You may omit any of the fast-selection key or X or /Y,
  1724. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1725. For backward compatibility, this variable may also be just a list
  1726. of keywords - in this case the interpretation (sequence or type) will be
  1727. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1728. :group 'org-todo
  1729. :group 'org-keywords
  1730. :type '(choice
  1731. (repeat :tag "Old syntax, just keywords"
  1732. (string :tag "Keyword"))
  1733. (repeat :tag "New syntax"
  1734. (cons
  1735. (choice
  1736. :tag "Interpretation"
  1737. ;;Quick and dirty way to see
  1738. ;;`org-todo-interpretations'. This takes the
  1739. ;;place of item arguments
  1740. :convert-widget
  1741. (lambda (widget)
  1742. (widget-put widget
  1743. :args (mapcar
  1744. #'(lambda (x)
  1745. (widget-convert
  1746. (cons 'const x)))
  1747. org-todo-interpretation-widgets))
  1748. widget))
  1749. (repeat
  1750. (string :tag "Keyword"))))))
  1751. (defvar org-todo-keywords-1 nil
  1752. "All TODO and DONE keywords active in a buffer.")
  1753. (make-variable-buffer-local 'org-todo-keywords-1)
  1754. (defvar org-todo-keywords-for-agenda nil)
  1755. (defvar org-done-keywords-for-agenda nil)
  1756. (defvar org-drawers-for-agenda nil)
  1757. (defvar org-todo-keyword-alist-for-agenda nil)
  1758. (defvar org-tag-alist-for-agenda nil)
  1759. (defvar org-agenda-contributing-files nil)
  1760. (defvar org-not-done-keywords nil)
  1761. (make-variable-buffer-local 'org-not-done-keywords)
  1762. (defvar org-done-keywords nil)
  1763. (make-variable-buffer-local 'org-done-keywords)
  1764. (defvar org-todo-heads nil)
  1765. (make-variable-buffer-local 'org-todo-heads)
  1766. (defvar org-todo-sets nil)
  1767. (make-variable-buffer-local 'org-todo-sets)
  1768. (defvar org-todo-log-states nil)
  1769. (make-variable-buffer-local 'org-todo-log-states)
  1770. (defvar org-todo-kwd-alist nil)
  1771. (make-variable-buffer-local 'org-todo-kwd-alist)
  1772. (defvar org-todo-key-alist nil)
  1773. (make-variable-buffer-local 'org-todo-key-alist)
  1774. (defvar org-todo-key-trigger nil)
  1775. (make-variable-buffer-local 'org-todo-key-trigger)
  1776. (defcustom org-todo-interpretation 'sequence
  1777. "Controls how TODO keywords are interpreted.
  1778. This variable is in principle obsolete and is only used for
  1779. backward compatibility, if the interpretation of todo keywords is
  1780. not given already in `org-todo-keywords'. See that variable for
  1781. more information."
  1782. :group 'org-todo
  1783. :group 'org-keywords
  1784. :type '(choice (const sequence)
  1785. (const type)))
  1786. (defcustom org-use-fast-todo-selection t
  1787. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1788. This variable describes if and under what circumstances the cycling
  1789. mechanism for TODO keywords will be replaced by a single-key, direct
  1790. selection scheme.
  1791. When nil, fast selection is never used.
  1792. When the symbol `prefix', it will be used when `org-todo' is called with
  1793. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1794. in an agenda buffer.
  1795. When t, fast selection is used by default. In this case, the prefix
  1796. argument forces cycling instead.
  1797. In all cases, the special interface is only used if access keys have actually
  1798. been assigned by the user, i.e. if keywords in the configuration are followed
  1799. by a letter in parenthesis, like TODO(t)."
  1800. :group 'org-todo
  1801. :type '(choice
  1802. (const :tag "Never" nil)
  1803. (const :tag "By default" t)
  1804. (const :tag "Only with C-u C-c C-t" prefix)))
  1805. (defcustom org-provide-todo-statistics t
  1806. "Non-nil means update todo statistics after insert and toggle.
  1807. ALL-HEADLINES means update todo statistics by including headlines
  1808. with no TODO keyword as well, counting them as not done.
  1809. A list of TODO keywords means the same, but skip keywords that are
  1810. not in this list.
  1811. When this is set, todo statistics is updated in the parent of the
  1812. current entry each time a todo state is changed."
  1813. :group 'org-todo
  1814. :type '(choice
  1815. (const :tag "Yes, only for TODO entries" t)
  1816. (const :tag "Yes, including all entries" 'all-headlines)
  1817. (repeat :tag "Yes, for TODOs in this list"
  1818. (string :tag "TODO keyword"))
  1819. (other :tag "No TODO statistics" nil)))
  1820. (defcustom org-hierarchical-todo-statistics t
  1821. "Non-nil means TODO statistics covers just direct children.
  1822. When nil, all entries in the subtree are considered.
  1823. This has only an effect if `org-provide-todo-statistics' is set.
  1824. To set this to nil for only a single subtree, use a COOKIE_DATA
  1825. property and include the word \"recursive\" into the value."
  1826. :group 'org-todo
  1827. :type 'boolean)
  1828. (defcustom org-after-todo-state-change-hook nil
  1829. "Hook which is run after the state of a TODO item was changed.
  1830. The new state (a string with a TODO keyword, or nil) is available in the
  1831. Lisp variable `state'."
  1832. :group 'org-todo
  1833. :type 'hook)
  1834. (defvar org-blocker-hook nil
  1835. "Hook for functions that are allowed to block a state change.
  1836. Each function gets as its single argument a property list, see
  1837. `org-trigger-hook' for more information about this list.
  1838. If any of the functions in this hook returns nil, the state change
  1839. is blocked.")
  1840. (defvar org-trigger-hook nil
  1841. "Hook for functions that are triggered by a state change.
  1842. Each function gets as its single argument a property list with at least
  1843. the following elements:
  1844. (:type type-of-change :position pos-at-entry-start
  1845. :from old-state :to new-state)
  1846. Depending on the type, more properties may be present.
  1847. This mechanism is currently implemented for:
  1848. TODO state changes
  1849. ------------------
  1850. :type todo-state-change
  1851. :from previous state (keyword as a string), or nil, or a symbol
  1852. 'todo' or 'done', to indicate the general type of state.
  1853. :to new state, like in :from")
  1854. (defcustom org-enforce-todo-dependencies nil
  1855. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1856. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1857. be blocked if any prior sibling is not yet done.
  1858. Finally, if the parent is blocked because of ordered siblings of its own,
  1859. the child will also be blocked.
  1860. This variable needs to be set before org.el is loaded, and you need to
  1861. restart Emacs after a change to make the change effective. The only way
  1862. to change is while Emacs is running is through the customize interface."
  1863. :set (lambda (var val)
  1864. (set var val)
  1865. (if val
  1866. (add-hook 'org-blocker-hook
  1867. 'org-block-todo-from-children-or-siblings-or-parent)
  1868. (remove-hook 'org-blocker-hook
  1869. 'org-block-todo-from-children-or-siblings-or-parent)))
  1870. :group 'org-todo
  1871. :type 'boolean)
  1872. (defcustom org-enforce-todo-checkbox-dependencies nil
  1873. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1874. When this is nil, checkboxes have no influence on switching TODO states.
  1875. When non-nil, you first need to check off all check boxes before the TODO
  1876. entry can be switched to DONE.
  1877. This variable needs to be set before org.el is loaded, and you need to
  1878. restart Emacs after a change to make the change effective. The only way
  1879. to change is while Emacs is running is through the customize interface."
  1880. :set (lambda (var val)
  1881. (set var val)
  1882. (if val
  1883. (add-hook 'org-blocker-hook
  1884. 'org-block-todo-from-checkboxes)
  1885. (remove-hook 'org-blocker-hook
  1886. 'org-block-todo-from-checkboxes)))
  1887. :group 'org-todo
  1888. :type 'boolean)
  1889. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1890. "Non-nil means inserting a TODO heading is treated as state change.
  1891. So when the command \\[org-insert-todo-heading] is used, state change
  1892. logging will apply if appropriate. When nil, the new TODO item will
  1893. be inserted directly, and no logging will take place."
  1894. :group 'org-todo
  1895. :type 'boolean)
  1896. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1897. "Non-nil means switching TODO states with S-cursor counts as state change.
  1898. This is the default behavior. However, setting this to nil allows a
  1899. convenient way to select a TODO state and bypass any logging associated
  1900. with that."
  1901. :group 'org-todo
  1902. :type 'boolean)
  1903. (defcustom org-todo-state-tags-triggers nil
  1904. "Tag changes that should be triggered by TODO state changes.
  1905. This is a list. Each entry is
  1906. (state-change (tag . flag) .......)
  1907. State-change can be a string with a state, and empty string to indicate the
  1908. state that has no TODO keyword, or it can be one of the symbols `todo'
  1909. or `done', meaning any not-done or done state, respectively."
  1910. :group 'org-todo
  1911. :group 'org-tags
  1912. :type '(repeat
  1913. (cons (choice :tag "When changing to"
  1914. (const :tag "Not-done state" todo)
  1915. (const :tag "Done state" done)
  1916. (string :tag "State"))
  1917. (repeat
  1918. (cons :tag "Tag action"
  1919. (string :tag "Tag")
  1920. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1921. (defcustom org-log-done nil
  1922. "Information to record when a task moves to the DONE state.
  1923. Possible values are:
  1924. nil Don't add anything, just change the keyword
  1925. time Add a time stamp to the task
  1926. note Prompt for a note and add it with template `org-log-note-headings'
  1927. This option can also be set with on a per-file-basis with
  1928. #+STARTUP: nologdone
  1929. #+STARTUP: logdone
  1930. #+STARTUP: lognotedone
  1931. You can have local logging settings for a subtree by setting the LOGGING
  1932. property to one or more of these keywords."
  1933. :group 'org-todo
  1934. :group 'org-progress
  1935. :type '(choice
  1936. (const :tag "No logging" nil)
  1937. (const :tag "Record CLOSED timestamp" time)
  1938. (const :tag "Record CLOSED timestamp with note." note)))
  1939. ;; Normalize old uses of org-log-done.
  1940. (cond
  1941. ((eq org-log-done t) (setq org-log-done 'time))
  1942. ((and (listp org-log-done) (memq 'done org-log-done))
  1943. (setq org-log-done 'note)))
  1944. (defcustom org-log-reschedule nil
  1945. "Information to record when the scheduling date of a tasks is modified.
  1946. Possible values are:
  1947. nil Don't add anything, just change the date
  1948. time Add a time stamp to the task
  1949. note Prompt for a note and add it with template `org-log-note-headings'
  1950. This option can also be set with on a per-file-basis with
  1951. #+STARTUP: nologreschedule
  1952. #+STARTUP: logreschedule
  1953. #+STARTUP: lognotereschedule"
  1954. :group 'org-todo
  1955. :group 'org-progress
  1956. :type '(choice
  1957. (const :tag "No logging" nil)
  1958. (const :tag "Record timestamp" time)
  1959. (const :tag "Record timestamp with note." note)))
  1960. (defcustom org-log-redeadline nil
  1961. "Information to record when the deadline date of a tasks is modified.
  1962. Possible values are:
  1963. nil Don't add anything, just change the date
  1964. time Add a time stamp to the task
  1965. note Prompt for a note and add it with template `org-log-note-headings'
  1966. This option can also be set with on a per-file-basis with
  1967. #+STARTUP: nologredeadline
  1968. #+STARTUP: logredeadline
  1969. #+STARTUP: lognoteredeadline
  1970. You can have local logging settings for a subtree by setting the LOGGING
  1971. property to one or more of these keywords."
  1972. :group 'org-todo
  1973. :group 'org-progress
  1974. :type '(choice
  1975. (const :tag "No logging" nil)
  1976. (const :tag "Record timestamp" time)
  1977. (const :tag "Record timestamp with note." note)))
  1978. (defcustom org-log-note-clock-out nil
  1979. "Non-nil means record a note when clocking out of an item.
  1980. This can also be configured on a per-file basis by adding one of
  1981. the following lines anywhere in the buffer:
  1982. #+STARTUP: lognoteclock-out
  1983. #+STARTUP: nolognoteclock-out"
  1984. :group 'org-todo
  1985. :group 'org-progress
  1986. :type 'boolean)
  1987. (defcustom org-log-done-with-time t
  1988. "Non-nil means the CLOSED time stamp will contain date and time.
  1989. When nil, only the date will be recorded."
  1990. :group 'org-progress
  1991. :type 'boolean)
  1992. (defcustom org-log-note-headings
  1993. '((done . "CLOSING NOTE %t")
  1994. (state . "State %-12s from %-12S %t")
  1995. (note . "Note taken on %t")
  1996. (reschedule . "Rescheduled from %S on %t")
  1997. (delschedule . "Not scheduled, was %S on %t")
  1998. (redeadline . "New deadline from %S on %t")
  1999. (deldeadline . "Removed deadline, was %S on %t")
  2000. (refile . "Refiled on %t")
  2001. (clock-out . ""))
  2002. "Headings for notes added to entries.
  2003. The value is an alist, with the car being a symbol indicating the note
  2004. context, and the cdr is the heading to be used. The heading may also be the
  2005. empty string.
  2006. %t in the heading will be replaced by a time stamp.
  2007. %T will be an active time stamp instead the default inactive one
  2008. %s will be replaced by the new TODO state, in double quotes.
  2009. %S will be replaced by the old TODO state, in double quotes.
  2010. %u will be replaced by the user name.
  2011. %U will be replaced by the full user name.
  2012. In fact, it is not a good idea to change the `state' entry, because
  2013. agenda log mode depends on the format of these entries."
  2014. :group 'org-todo
  2015. :group 'org-progress
  2016. :type '(list :greedy t
  2017. (cons (const :tag "Heading when closing an item" done) string)
  2018. (cons (const :tag
  2019. "Heading when changing todo state (todo sequence only)"
  2020. state) string)
  2021. (cons (const :tag "Heading when just taking a note" note) string)
  2022. (cons (const :tag "Heading when clocking out" clock-out) string)
  2023. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2024. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2025. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2026. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2027. (cons (const :tag "Heading when refiling" refile) string)))
  2028. (unless (assq 'note org-log-note-headings)
  2029. (push '(note . "%t") org-log-note-headings))
  2030. (defcustom org-log-into-drawer nil
  2031. "Non-nil means insert state change notes and time stamps into a drawer.
  2032. When nil, state changes notes will be inserted after the headline and
  2033. any scheduling and clock lines, but not inside a drawer.
  2034. The value of this variable should be the name of the drawer to use.
  2035. LOGBOOK is proposed at the default drawer for this purpose, you can
  2036. also set this to a string to define the drawer of your choice.
  2037. A value of t is also allowed, representing \"LOGBOOK\".
  2038. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2039. will be ignored.
  2040. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2041. a subtree."
  2042. :group 'org-todo
  2043. :group 'org-progress
  2044. :type '(choice
  2045. (const :tag "Not into a drawer" nil)
  2046. (const :tag "LOGBOOK" t)
  2047. (string :tag "Other")))
  2048. (if (fboundp 'defvaralias)
  2049. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2050. (defun org-log-into-drawer ()
  2051. "Return the value of `org-log-into-drawer', but let properties overrule.
  2052. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2053. used instead of the default value."
  2054. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  2055. (cond
  2056. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2057. ((equal p "t") "LOGBOOK")
  2058. (t p))))
  2059. (defcustom org-log-state-notes-insert-after-drawers nil
  2060. "Non-nil means insert state change notes after any drawers in entry.
  2061. Only the drawers that *immediately* follow the headline and the
  2062. deadline/scheduled line are skipped.
  2063. When nil, insert notes right after the heading and perhaps the line
  2064. with deadline/scheduling if present.
  2065. This variable will have no effect if `org-log-into-drawer' is
  2066. set."
  2067. :group 'org-todo
  2068. :group 'org-progress
  2069. :type 'boolean)
  2070. (defcustom org-log-states-order-reversed t
  2071. "Non-nil means the latest state note will be directly after heading.
  2072. When nil, the state change notes will be ordered according to time."
  2073. :group 'org-todo
  2074. :group 'org-progress
  2075. :type 'boolean)
  2076. (defcustom org-todo-repeat-to-state nil
  2077. "The TODO state to which a repeater should return the repeating task.
  2078. By default this is the first task in a TODO sequence, or the previous state
  2079. in a TODO_TYP set. But you can specify another task here.
  2080. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2081. :group 'org-todo
  2082. :type '(choice (const :tag "Head of sequence" nil)
  2083. (string :tag "Specific state")))
  2084. (defcustom org-log-repeat 'time
  2085. "Non-nil means record moving through the DONE state when triggering repeat.
  2086. An auto-repeating task is immediately switched back to TODO when
  2087. marked DONE. If you are not logging state changes (by adding \"@\"
  2088. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2089. record a closing note, there will be no record of the task moving
  2090. through DONE. This variable forces taking a note anyway.
  2091. nil Don't force a record
  2092. time Record a time stamp
  2093. note Record a note
  2094. This option can also be set with on a per-file-basis with
  2095. #+STARTUP: logrepeat
  2096. #+STARTUP: lognoterepeat
  2097. #+STARTUP: nologrepeat
  2098. You can have local logging settings for a subtree by setting the LOGGING
  2099. property to one or more of these keywords."
  2100. :group 'org-todo
  2101. :group 'org-progress
  2102. :type '(choice
  2103. (const :tag "Don't force a record" nil)
  2104. (const :tag "Force recording the DONE state" time)
  2105. (const :tag "Force recording a note with the DONE state" note)))
  2106. (defgroup org-priorities nil
  2107. "Priorities in Org-mode."
  2108. :tag "Org Priorities"
  2109. :group 'org-todo)
  2110. (defcustom org-enable-priority-commands t
  2111. "Non-nil means priority commands are active.
  2112. When nil, these commands will be disabled, so that you never accidentally
  2113. set a priority."
  2114. :group 'org-priorities
  2115. :type 'boolean)
  2116. (defcustom org-highest-priority ?A
  2117. "The highest priority of TODO items. A character like ?A, ?B etc.
  2118. Must have a smaller ASCII number than `org-lowest-priority'."
  2119. :group 'org-priorities
  2120. :type 'character)
  2121. (defcustom org-lowest-priority ?C
  2122. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2123. Must have a larger ASCII number than `org-highest-priority'."
  2124. :group 'org-priorities
  2125. :type 'character)
  2126. (defcustom org-default-priority ?B
  2127. "The default priority of TODO items.
  2128. This is the priority an item get if no explicit priority is given."
  2129. :group 'org-priorities
  2130. :type 'character)
  2131. (defcustom org-priority-start-cycle-with-default t
  2132. "Non-nil means start with default priority when starting to cycle.
  2133. When this is nil, the first step in the cycle will be (depending on the
  2134. command used) one higher or lower that the default priority."
  2135. :group 'org-priorities
  2136. :type 'boolean)
  2137. (defgroup org-time nil
  2138. "Options concerning time stamps and deadlines in Org-mode."
  2139. :tag "Org Time"
  2140. :group 'org)
  2141. (defcustom org-insert-labeled-timestamps-at-point nil
  2142. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2143. When nil, these labeled time stamps are forces into the second line of an
  2144. entry, just after the headline. When scheduling from the global TODO list,
  2145. the time stamp will always be forced into the second line."
  2146. :group 'org-time
  2147. :type 'boolean)
  2148. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2149. "Formats for `format-time-string' which are used for time stamps.
  2150. It is not recommended to change this constant.")
  2151. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2152. "Number of minutes to round time stamps to.
  2153. These are two values, the first applies when first creating a time stamp.
  2154. The second applies when changing it with the commands `S-up' and `S-down'.
  2155. When changing the time stamp, this means that it will change in steps
  2156. of N minutes, as given by the second value.
  2157. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2158. numbers should be factors of 60, so for example 5, 10, 15.
  2159. When this is larger than 1, you can still force an exact time stamp by using
  2160. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2161. and by using a prefix arg to `S-up/down' to specify the exact number
  2162. of minutes to shift."
  2163. :group 'org-time
  2164. :get '(lambda (var) ; Make sure both elements are there
  2165. (if (integerp (default-value var))
  2166. (list (default-value var) 5)
  2167. (default-value var)))
  2168. :type '(list
  2169. (integer :tag "when inserting times")
  2170. (integer :tag "when modifying times")))
  2171. ;; Normalize old customizations of this variable.
  2172. (when (integerp org-time-stamp-rounding-minutes)
  2173. (setq org-time-stamp-rounding-minutes
  2174. (list org-time-stamp-rounding-minutes
  2175. org-time-stamp-rounding-minutes)))
  2176. (defcustom org-display-custom-times nil
  2177. "Non-nil means overlay custom formats over all time stamps.
  2178. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2179. To turn this on on a per-file basis, insert anywhere in the file:
  2180. #+STARTUP: customtime"
  2181. :group 'org-time
  2182. :set 'set-default
  2183. :type 'sexp)
  2184. (make-variable-buffer-local 'org-display-custom-times)
  2185. (defcustom org-time-stamp-custom-formats
  2186. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2187. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2188. These are overlayed over the default ISO format if the variable
  2189. `org-display-custom-times' is set. Time like %H:%M should be at the
  2190. end of the second format. The custom formats are also honored by export
  2191. commands, if custom time display is turned on at the time of export."
  2192. :group 'org-time
  2193. :type 'sexp)
  2194. (defun org-time-stamp-format (&optional long inactive)
  2195. "Get the right format for a time string."
  2196. (let ((f (if long (cdr org-time-stamp-formats)
  2197. (car org-time-stamp-formats))))
  2198. (if inactive
  2199. (concat "[" (substring f 1 -1) "]")
  2200. f)))
  2201. (defcustom org-time-clocksum-format "%d:%02d"
  2202. "The format string used when creating CLOCKSUM lines.
  2203. This is also used when org-mode generates a time duration."
  2204. :group 'org-time
  2205. :type 'string)
  2206. (defcustom org-time-clocksum-use-fractional nil
  2207. "If non-nil, \\[org-clock-display] uses fractional times.
  2208. org-mode generates a time duration."
  2209. :group 'org-time
  2210. :type 'boolean)
  2211. (defcustom org-time-clocksum-fractional-format "%.2f"
  2212. "The format string used when creating CLOCKSUM lines, or when
  2213. org-mode generates a time duration."
  2214. :group 'org-time
  2215. :type 'string)
  2216. (defcustom org-deadline-warning-days 14
  2217. "No. of days before expiration during which a deadline becomes active.
  2218. This variable governs the display in sparse trees and in the agenda.
  2219. When 0 or negative, it means use this number (the absolute value of it)
  2220. even if a deadline has a different individual lead time specified.
  2221. Custom commands can set this variable in the options section."
  2222. :group 'org-time
  2223. :group 'org-agenda-daily/weekly
  2224. :type 'integer)
  2225. (defcustom org-read-date-prefer-future t
  2226. "Non-nil means assume future for incomplete date input from user.
  2227. This affects the following situations:
  2228. 1. The user gives a month but not a year.
  2229. For example, if it is April and you enter \"feb 2\", this will be read
  2230. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2231. 2. The user gives a day, but no month.
  2232. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2233. read this as the third of *next* month. However, if you enter \"17\",
  2234. it will be considered as *this* month.
  2235. If you set this variable to the symbol `time', then also the following
  2236. will work:
  2237. 3. If the user gives a time, but no day. If the time is before now,
  2238. to will be interpreted as tomorrow.
  2239. Currently none of this works for ISO week specifications.
  2240. When this option is nil, the current day, month and year will always be
  2241. used as defaults."
  2242. :group 'org-time
  2243. :type '(choice
  2244. (const :tag "Never" nil)
  2245. (const :tag "Check month and day" t)
  2246. (const :tag "Check month, day, and time" time)))
  2247. (defcustom org-read-date-display-live t
  2248. "Non-nil means display current interpretation of date prompt live.
  2249. This display will be in an overlay, in the minibuffer."
  2250. :group 'org-time
  2251. :type 'boolean)
  2252. (defcustom org-read-date-popup-calendar t
  2253. "Non-nil means pop up a calendar when prompting for a date.
  2254. In the calendar, the date can be selected with mouse-1. However, the
  2255. minibuffer will also be active, and you can simply enter the date as well.
  2256. When nil, only the minibuffer will be available."
  2257. :group 'org-time
  2258. :type 'boolean)
  2259. (if (fboundp 'defvaralias)
  2260. (defvaralias 'org-popup-calendar-for-date-prompt
  2261. 'org-read-date-popup-calendar))
  2262. (defcustom org-read-date-minibuffer-setup-hook nil
  2263. "Hook to be used to set up keys for the date/time interface.
  2264. Add key definitions to `minibuffer-local-map', which will be a temporary
  2265. copy."
  2266. :group 'org-time
  2267. :type 'hook)
  2268. (defcustom org-extend-today-until 0
  2269. "The hour when your day really ends. Must be an integer.
  2270. This has influence for the following applications:
  2271. - When switching the agenda to \"today\". It it is still earlier than
  2272. the time given here, the day recognized as TODAY is actually yesterday.
  2273. - When a date is read from the user and it is still before the time given
  2274. here, the current date and time will be assumed to be yesterday, 23:59.
  2275. Also, timestamps inserted in remember templates follow this rule.
  2276. IMPORTANT: This is a feature whose implementation is and likely will
  2277. remain incomplete. Really, it is only here because past midnight seems to
  2278. be the favorite working time of John Wiegley :-)"
  2279. :group 'org-time
  2280. :type 'integer)
  2281. (defcustom org-edit-timestamp-down-means-later nil
  2282. "Non-nil means S-down will increase the time in a time stamp.
  2283. When nil, S-up will increase."
  2284. :group 'org-time
  2285. :type 'boolean)
  2286. (defcustom org-calendar-follow-timestamp-change t
  2287. "Non-nil means make the calendar window follow timestamp changes.
  2288. When a timestamp is modified and the calendar window is visible, it will be
  2289. moved to the new date."
  2290. :group 'org-time
  2291. :type 'boolean)
  2292. (defgroup org-tags nil
  2293. "Options concerning tags in Org-mode."
  2294. :tag "Org Tags"
  2295. :group 'org)
  2296. (defcustom org-tag-alist nil
  2297. "List of tags allowed in Org-mode files.
  2298. When this list is nil, Org-mode will base TAG input on what is already in the
  2299. buffer.
  2300. The value of this variable is an alist, the car of each entry must be a
  2301. keyword as a string, the cdr may be a character that is used to select
  2302. that tag through the fast-tag-selection interface.
  2303. See the manual for details."
  2304. :group 'org-tags
  2305. :type '(repeat
  2306. (choice
  2307. (cons (string :tag "Tag name")
  2308. (character :tag "Access char"))
  2309. (list :tag "Start radio group"
  2310. (const :startgroup)
  2311. (option (string :tag "Group description")))
  2312. (list :tag "End radio group"
  2313. (const :endgroup)
  2314. (option (string :tag "Group description")))
  2315. (const :tag "New line" (:newline)))))
  2316. (defcustom org-tag-persistent-alist nil
  2317. "List of tags that will always appear in all Org-mode files.
  2318. This is in addition to any in buffer settings or customizations
  2319. of `org-tag-alist'.
  2320. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2321. The value of this variable is an alist, the car of each entry must be a
  2322. keyword as a string, the cdr may be a character that is used to select
  2323. that tag through the fast-tag-selection interface.
  2324. See the manual for details.
  2325. To disable these tags on a per-file basis, insert anywhere in the file:
  2326. #+STARTUP: noptag"
  2327. :group 'org-tags
  2328. :type '(repeat
  2329. (choice
  2330. (cons (string :tag "Tag name")
  2331. (character :tag "Access char"))
  2332. (const :tag "Start radio group" (:startgroup))
  2333. (const :tag "End radio group" (:endgroup))
  2334. (const :tag "New line" (:newline)))))
  2335. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2336. "If non-nil, always offer completion for all tags of all agenda files.
  2337. Instead of customizing this variable directly, you might want to
  2338. set it locally for remember buffers, because there no list of
  2339. tags in that file can be created dynamically (there are none).
  2340. (add-hook 'org-remember-mode-hook
  2341. (lambda ()
  2342. (set (make-local-variable
  2343. 'org-complete-tags-always-offer-all-agenda-tags)
  2344. t)))"
  2345. :group 'org-tags
  2346. :type 'boolean)
  2347. (defvar org-file-tags nil
  2348. "List of tags that can be inherited by all entries in the file.
  2349. The tags will be inherited if the variable `org-use-tag-inheritance'
  2350. says they should be.
  2351. This variable is populated from #+FILETAGS lines.")
  2352. (defcustom org-use-fast-tag-selection 'auto
  2353. "Non-nil means use fast tag selection scheme.
  2354. This is a special interface to select and deselect tags with single keys.
  2355. When nil, fast selection is never used.
  2356. When the symbol `auto', fast selection is used if and only if selection
  2357. characters for tags have been configured, either through the variable
  2358. `org-tag-alist' or through a #+TAGS line in the buffer.
  2359. When t, fast selection is always used and selection keys are assigned
  2360. automatically if necessary."
  2361. :group 'org-tags
  2362. :type '(choice
  2363. (const :tag "Always" t)
  2364. (const :tag "Never" nil)
  2365. (const :tag "When selection characters are configured" 'auto)))
  2366. (defcustom org-fast-tag-selection-single-key nil
  2367. "Non-nil means fast tag selection exits after first change.
  2368. When nil, you have to press RET to exit it.
  2369. During fast tag selection, you can toggle this flag with `C-c'.
  2370. This variable can also have the value `expert'. In this case, the window
  2371. displaying the tags menu is not even shown, until you press C-c again."
  2372. :group 'org-tags
  2373. :type '(choice
  2374. (const :tag "No" nil)
  2375. (const :tag "Yes" t)
  2376. (const :tag "Expert" expert)))
  2377. (defvar org-fast-tag-selection-include-todo nil
  2378. "Non-nil means fast tags selection interface will also offer TODO states.
  2379. This is an undocumented feature, you should not rely on it.")
  2380. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2381. "The column to which tags should be indented in a headline.
  2382. If this number is positive, it specifies the column. If it is negative,
  2383. it means that the tags should be flushright to that column. For example,
  2384. -80 works well for a normal 80 character screen."
  2385. :group 'org-tags
  2386. :type 'integer)
  2387. (defcustom org-auto-align-tags t
  2388. "Non-nil means realign tags after pro/demotion of TODO state change.
  2389. These operations change the length of a headline and therefore shift
  2390. the tags around. With this options turned on, after each such operation
  2391. the tags are again aligned to `org-tags-column'."
  2392. :group 'org-tags
  2393. :type 'boolean)
  2394. (defcustom org-use-tag-inheritance t
  2395. "Non-nil means tags in levels apply also for sublevels.
  2396. When nil, only the tags directly given in a specific line apply there.
  2397. This may also be a list of tags that should be inherited, or a regexp that
  2398. matches tags that should be inherited. Additional control is possible
  2399. with the variable `org-tags-exclude-from-inheritance' which gives an
  2400. explicit list of tags to be excluded from inheritance., even if the value of
  2401. `org-use-tag-inheritance' would select it for inheritance.
  2402. If this option is t, a match early-on in a tree can lead to a large
  2403. number of matches in the subtree when constructing the agenda or creating
  2404. a sparse tree. If you only want to see the first match in a tree during
  2405. a search, check out the variable `org-tags-match-list-sublevels'."
  2406. :group 'org-tags
  2407. :type '(choice
  2408. (const :tag "Not" nil)
  2409. (const :tag "Always" t)
  2410. (repeat :tag "Specific tags" (string :tag "Tag"))
  2411. (regexp :tag "Tags matched by regexp")))
  2412. (defcustom org-tags-exclude-from-inheritance nil
  2413. "List of tags that should never be inherited.
  2414. This is a way to exclude a few tags from inheritance. For way to do
  2415. the opposite, to actively allow inheritance for selected tags,
  2416. see the variable `org-use-tag-inheritance'."
  2417. :group 'org-tags
  2418. :type '(repeat (string :tag "Tag")))
  2419. (defun org-tag-inherit-p (tag)
  2420. "Check if TAG is one that should be inherited."
  2421. (cond
  2422. ((member tag org-tags-exclude-from-inheritance) nil)
  2423. ((eq org-use-tag-inheritance t) t)
  2424. ((not org-use-tag-inheritance) nil)
  2425. ((stringp org-use-tag-inheritance)
  2426. (string-match org-use-tag-inheritance tag))
  2427. ((listp org-use-tag-inheritance)
  2428. (member tag org-use-tag-inheritance))
  2429. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2430. (defcustom org-tags-match-list-sublevels t
  2431. "Non-nil means list also sublevels of headlines matching a search.
  2432. This variable applies to tags/property searches, and also to stuck
  2433. projects because this search is based on a tags match as well.
  2434. When set to the symbol `indented', sublevels are indented with
  2435. leading dots.
  2436. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2437. the sublevels of a headline matching a tag search often also match
  2438. the same search. Listing all of them can create very long lists.
  2439. Setting this variable to nil causes subtrees of a match to be skipped.
  2440. This variable is semi-obsolete and probably should always be true. It
  2441. is better to limit inheritance to certain tags using the variables
  2442. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2443. :group 'org-tags
  2444. :type '(choice
  2445. (const :tag "No, don't list them" nil)
  2446. (const :tag "Yes, do list them" t)
  2447. (const :tag "List them, indented with leading dots" indented)))
  2448. (defcustom org-tags-sort-function nil
  2449. "When set, tags are sorted using this function as a comparator."
  2450. :group 'org-tags
  2451. :type '(choice
  2452. (const :tag "No sorting" nil)
  2453. (const :tag "Alphabetical" string<)
  2454. (const :tag "Reverse alphabetical" string>)
  2455. (function :tag "Custom function" nil)))
  2456. (defvar org-tags-history nil
  2457. "History of minibuffer reads for tags.")
  2458. (defvar org-last-tags-completion-table nil
  2459. "The last used completion table for tags.")
  2460. (defvar org-after-tags-change-hook nil
  2461. "Hook that is run after the tags in a line have changed.")
  2462. (defgroup org-properties nil
  2463. "Options concerning properties in Org-mode."
  2464. :tag "Org Properties"
  2465. :group 'org)
  2466. (defcustom org-property-format "%-10s %s"
  2467. "How property key/value pairs should be formatted by `indent-line'.
  2468. When `indent-line' hits a property definition, it will format the line
  2469. according to this format, mainly to make sure that the values are
  2470. lined-up with respect to each other."
  2471. :group 'org-properties
  2472. :type 'string)
  2473. (defcustom org-use-property-inheritance nil
  2474. "Non-nil means properties apply also for sublevels.
  2475. This setting is chiefly used during property searches. Turning it on can
  2476. cause significant overhead when doing a search, which is why it is not
  2477. on by default.
  2478. When nil, only the properties directly given in the current entry count.
  2479. When t, every property is inherited. The value may also be a list of
  2480. properties that should have inheritance, or a regular expression matching
  2481. properties that should be inherited.
  2482. However, note that some special properties use inheritance under special
  2483. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2484. and the properties ending in \"_ALL\" when they are used as descriptor
  2485. for valid values of a property.
  2486. Note for programmers:
  2487. When querying an entry with `org-entry-get', you can control if inheritance
  2488. should be used. By default, `org-entry-get' looks only at the local
  2489. properties. You can request inheritance by setting the inherit argument
  2490. to t (to force inheritance) or to `selective' (to respect the setting
  2491. in this variable)."
  2492. :group 'org-properties
  2493. :type '(choice
  2494. (const :tag "Not" nil)
  2495. (const :tag "Always" t)
  2496. (repeat :tag "Specific properties" (string :tag "Property"))
  2497. (regexp :tag "Properties matched by regexp")))
  2498. (defun org-property-inherit-p (property)
  2499. "Check if PROPERTY is one that should be inherited."
  2500. (cond
  2501. ((eq org-use-property-inheritance t) t)
  2502. ((not org-use-property-inheritance) nil)
  2503. ((stringp org-use-property-inheritance)
  2504. (string-match org-use-property-inheritance property))
  2505. ((listp org-use-property-inheritance)
  2506. (member property org-use-property-inheritance))
  2507. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2508. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2509. "The default column format, if no other format has been defined.
  2510. This variable can be set on the per-file basis by inserting a line
  2511. #+COLUMNS: %25ITEM ....."
  2512. :group 'org-properties
  2513. :type 'string)
  2514. (defcustom org-columns-ellipses ".."
  2515. "The ellipses to be used when a field in column view is truncated.
  2516. When this is the empty string, as many characters as possible are shown,
  2517. but then there will be no visual indication that the field has been truncated.
  2518. When this is a string of length N, the last N characters of a truncated
  2519. field are replaced by this string. If the column is narrower than the
  2520. ellipses string, only part of the ellipses string will be shown."
  2521. :group 'org-properties
  2522. :type 'string)
  2523. (defcustom org-columns-modify-value-for-display-function nil
  2524. "Function that modifies values for display in column view.
  2525. For example, it can be used to cut out a certain part from a time stamp.
  2526. The function must take 2 arguments:
  2527. column-title The title of the column (*not* the property name)
  2528. value The value that should be modified.
  2529. The function should return the value that should be displayed,
  2530. or nil if the normal value should be used."
  2531. :group 'org-properties
  2532. :type 'function)
  2533. (defcustom org-effort-property "Effort"
  2534. "The property that is being used to keep track of effort estimates.
  2535. Effort estimates given in this property need to have the format H:MM."
  2536. :group 'org-properties
  2537. :group 'org-progress
  2538. :type '(string :tag "Property"))
  2539. (defconst org-global-properties-fixed
  2540. '(("VISIBILITY_ALL" . "folded children content all")
  2541. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2542. "List of property/value pairs that can be inherited by any entry.
  2543. These are fixed values, for the preset properties. The user variable
  2544. that can be used to add to this list is `org-global-properties'.
  2545. The entries in this list are cons cells where the car is a property
  2546. name and cdr is a string with the value. If the value represents
  2547. multiple items like an \"_ALL\" property, separate the items by
  2548. spaces.")
  2549. (defcustom org-global-properties nil
  2550. "List of property/value pairs that can be inherited by any entry.
  2551. This list will be combined with the constant `org-global-properties-fixed'.
  2552. The entries in this list are cons cells where the car is a property
  2553. name and cdr is a string with the value.
  2554. You can set buffer-local values for the same purpose in the variable
  2555. `org-file-properties' this by adding lines like
  2556. #+PROPERTY: NAME VALUE"
  2557. :group 'org-properties
  2558. :type '(repeat
  2559. (cons (string :tag "Property")
  2560. (string :tag "Value"))))
  2561. (defvar org-file-properties nil
  2562. "List of property/value pairs that can be inherited by any entry.
  2563. Valid for the current buffer.
  2564. This variable is populated from #+PROPERTY lines.")
  2565. (make-variable-buffer-local 'org-file-properties)
  2566. (defgroup org-agenda nil
  2567. "Options concerning agenda views in Org-mode."
  2568. :tag "Org Agenda"
  2569. :group 'org)
  2570. (defvar org-category nil
  2571. "Variable used by org files to set a category for agenda display.
  2572. Such files should use a file variable to set it, for example
  2573. # -*- mode: org; org-category: \"ELisp\"
  2574. or contain a special line
  2575. #+CATEGORY: ELisp
  2576. If the file does not specify a category, then file's base name
  2577. is used instead.")
  2578. (make-variable-buffer-local 'org-category)
  2579. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2580. (defcustom org-agenda-files nil
  2581. "The files to be used for agenda display.
  2582. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2583. \\[org-remove-file]. You can also use customize to edit the list.
  2584. If an entry is a directory, all files in that directory that are matched by
  2585. `org-agenda-file-regexp' will be part of the file list.
  2586. If the value of the variable is not a list but a single file name, then
  2587. the list of agenda files is actually stored and maintained in that file, one
  2588. agenda file per line. In this file paths can be given relative to
  2589. `org-directory'. Tilde expansion and environment variable substitution
  2590. are also made."
  2591. :group 'org-agenda
  2592. :type '(choice
  2593. (repeat :tag "List of files and directories" file)
  2594. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2595. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2596. "Regular expression to match files for `org-agenda-files'.
  2597. If any element in the list in that variable contains a directory instead
  2598. of a normal file, all files in that directory that are matched by this
  2599. regular expression will be included."
  2600. :group 'org-agenda
  2601. :type 'regexp)
  2602. (defcustom org-agenda-text-search-extra-files nil
  2603. "List of extra files to be searched by text search commands.
  2604. These files will be search in addition to the agenda files by the
  2605. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2606. Note that these files will only be searched for text search commands,
  2607. not for the other agenda views like todo lists, tag searches or the weekly
  2608. agenda. This variable is intended to list notes and possibly archive files
  2609. that should also be searched by these two commands.
  2610. In fact, if the first element in the list is the symbol `agenda-archives',
  2611. than all archive files of all agenda files will be added to the search
  2612. scope."
  2613. :group 'org-agenda
  2614. :type '(set :greedy t
  2615. (const :tag "Agenda Archives" agenda-archives)
  2616. (repeat :inline t (file))))
  2617. (if (fboundp 'defvaralias)
  2618. (defvaralias 'org-agenda-multi-occur-extra-files
  2619. 'org-agenda-text-search-extra-files))
  2620. (defcustom org-agenda-skip-unavailable-files nil
  2621. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2622. A nil value means to remove them, after a query, from the list."
  2623. :group 'org-agenda
  2624. :type 'boolean)
  2625. (defcustom org-calendar-to-agenda-key [?c]
  2626. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2627. The command `org-calendar-goto-agenda' will be bound to this key. The
  2628. default is the character `c' because then `c' can be used to switch back and
  2629. forth between agenda and calendar."
  2630. :group 'org-agenda
  2631. :type 'sexp)
  2632. (defcustom org-calendar-agenda-action-key [?k]
  2633. "The key to be installed in `calendar-mode-map' for agenda-action.
  2634. The command `org-agenda-action' will be bound to this key. The
  2635. default is the character `k' because we use the same key in the agenda."
  2636. :group 'org-agenda
  2637. :type 'sexp)
  2638. (defcustom org-calendar-insert-diary-entry-key [?i]
  2639. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2640. This option is irrelevant until `org-agenda-diary-file' has been configured
  2641. to point to an Org-mode file. When that is the case, the command
  2642. `org-agenda-diary-entry' will be bound to the key given here, by default
  2643. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2644. if you want to continue doing this, you need to change this to a different
  2645. key."
  2646. :group 'org-agenda
  2647. :type 'sexp)
  2648. (defcustom org-agenda-diary-file 'diary-file
  2649. "File to which to add new entries with the `i' key in agenda and calendar.
  2650. When this is the symbol `diary-file', the functionality in the Emacs
  2651. calendar will be used to add entries to the `diary-file'. But when this
  2652. points to a file, `org-agenda-diary-entry' will be used instead."
  2653. :group 'org-agenda
  2654. :type '(choice
  2655. (const :tag "The standard Emacs diary file" diary-file)
  2656. (file :tag "Special Org file diary entries")))
  2657. (eval-after-load "calendar"
  2658. '(progn
  2659. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2660. 'org-calendar-goto-agenda)
  2661. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2662. 'org-agenda-action)
  2663. (add-hook 'calendar-mode-hook
  2664. (lambda ()
  2665. (unless (eq org-agenda-diary-file 'diary-file)
  2666. (define-key calendar-mode-map
  2667. org-calendar-insert-diary-entry-key
  2668. 'org-agenda-diary-entry))))))
  2669. (defgroup org-latex nil
  2670. "Options for embedding LaTeX code into Org-mode."
  2671. :tag "Org LaTeX"
  2672. :group 'org)
  2673. (defcustom org-format-latex-options
  2674. '(:foreground default :background default :scale 1.0
  2675. :html-foreground "Black" :html-background "Transparent"
  2676. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2677. "Options for creating images from LaTeX fragments.
  2678. This is a property list with the following properties:
  2679. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2680. `default' means use the foreground of the default face.
  2681. :background the background color, or \"Transparent\".
  2682. `default' means use the background of the default face.
  2683. :scale a scaling factor for the size of the images, to get more pixels
  2684. :html-foreground, :html-background, :html-scale
  2685. the same numbers for HTML export.
  2686. :matchers a list indicating which matchers should be used to
  2687. find LaTeX fragments. Valid members of this list are:
  2688. \"begin\" find environments
  2689. \"$1\" find single characters surrounded by $.$
  2690. \"$\" find math expressions surrounded by $...$
  2691. \"$$\" find math expressions surrounded by $$....$$
  2692. \"\\(\" find math expressions surrounded by \\(...\\)
  2693. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2694. :group 'org-latex
  2695. :type 'plist)
  2696. (defcustom org-format-latex-signal-error t
  2697. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2698. When nil, just push out a message."
  2699. :group 'org-latex
  2700. :type 'boolean)
  2701. (defcustom org-format-latex-header "\\documentclass{article}
  2702. \\usepackage[usenames]{color}
  2703. \\usepackage{amsmath}
  2704. \\usepackage[mathscr]{eucal}
  2705. \\pagestyle{empty} % do not remove
  2706. \[PACKAGES]
  2707. \[DEFAULT-PACKAGES]
  2708. % The settings below are copied from fullpage.sty
  2709. \\setlength{\\textwidth}{\\paperwidth}
  2710. \\addtolength{\\textwidth}{-3cm}
  2711. \\setlength{\\oddsidemargin}{1.5cm}
  2712. \\addtolength{\\oddsidemargin}{-2.54cm}
  2713. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2714. \\setlength{\\textheight}{\\paperheight}
  2715. \\addtolength{\\textheight}{-\\headheight}
  2716. \\addtolength{\\textheight}{-\\headsep}
  2717. \\addtolength{\\textheight}{-\\footskip}
  2718. \\addtolength{\\textheight}{-3cm}
  2719. \\setlength{\\topmargin}{1.5cm}
  2720. \\addtolength{\\topmargin}{-2.54cm}"
  2721. "The document header used for processing LaTeX fragments.
  2722. It is imperative that this header make sure that no page number
  2723. appears on the page. The package defined in the variables
  2724. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2725. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2726. will be appended."
  2727. :group 'org-latex
  2728. :type 'string)
  2729. (defvar org-format-latex-header-extra nil)
  2730. (defun org-set-packages-alist (var val)
  2731. "Set the packages alist and make sure it has 3 elements per entry."
  2732. (set var (mapcar (lambda (x)
  2733. (if (and (consp x) (= (length x) 2))
  2734. (list (car x) (nth 1 x) t)
  2735. x))
  2736. val)))
  2737. (defun org-get-packages-alist (var)
  2738. "Get the packages alist and make sure it has 3 elements per entry."
  2739. (mapcar (lambda (x)
  2740. (if (and (consp x) (= (length x) 2))
  2741. (list (car x) (nth 1 x) t)
  2742. x))
  2743. (default-value var)))
  2744. ;; The following variables are defined here because is it also used
  2745. ;; when formatting latex fragments. Originally it was part of the
  2746. ;; LaTeX exporter, which is why the name includes "export".
  2747. (defcustom org-export-latex-default-packages-alist
  2748. '(("AUTO" "inputenc" t)
  2749. ("T1" "fontenc" t)
  2750. ("" "fixltx2e" nil)
  2751. ("" "graphicx" t)
  2752. ("" "longtable" nil)
  2753. ("" "float" nil)
  2754. ("" "wrapfig" nil)
  2755. ("" "soul" t)
  2756. ("" "t1enc" t)
  2757. ("" "textcomp" t)
  2758. ("" "marvosym" t)
  2759. ("" "wasysym" t)
  2760. ("" "latexsym" t)
  2761. ("" "amssymb" t)
  2762. ("" "hyperref" nil)
  2763. "\\tolerance=1000"
  2764. )
  2765. "Alist of default packages to be inserted in the header.
  2766. Change this only if one of the packages here causes an incompatibility
  2767. with another package you are using.
  2768. The packages in this list are needed by one part or another of Org-mode
  2769. to function properly.
  2770. - inputenc, fontenc, t1enc: for basic font and character selection
  2771. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2772. for interpreting the entities in `org-entities'. You can skip some of these
  2773. packages if you don't use any of the symbols in it.
  2774. - graphicx: for including images
  2775. - float, wrapfig: for figure placement
  2776. - longtable: for long tables
  2777. - hyperref: for cross references
  2778. Therefore you should not modify this variable unless you know what you
  2779. are doing. The one reason to change it anyway is that you might be loading
  2780. some other package that conflicts with one of the default packages.
  2781. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2782. If SNIPPET-FLAG is t, the package also needs to be included when
  2783. compiling LaTeX snippets into images for inclusion into HTML."
  2784. :group 'org-export-latex
  2785. :set 'org-set-packages-alist
  2786. :get 'org-get-packages-alist
  2787. :type '(repeat
  2788. (choice
  2789. (list :tag "options/package pair"
  2790. (string :tag "options")
  2791. (string :tag "package")
  2792. (boolean :tag "Snippet"))
  2793. (string :tag "A line of LaTeX"))))
  2794. (defcustom org-export-latex-packages-alist nil
  2795. "Alist of packages to be inserted in every LaTeX header.
  2796. These will be inserted after `org-export-latex-default-packages-alist'.
  2797. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2798. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2799. turning LaTeX snippets into images for inclusion into HTML.
  2800. Make sure that you only list packages here which:
  2801. - you want in every file
  2802. - do not conflict with the default packages in
  2803. `org-export-latex-default-packages-alist'
  2804. - do not conflict with the setup in `org-format-latex-header'."
  2805. :group 'org-export-latex
  2806. :set 'org-set-packages-alist
  2807. :get 'org-get-packages-alist
  2808. :type '(repeat
  2809. (choice
  2810. (list :tag "options/package pair"
  2811. (string :tag "options")
  2812. (string :tag "package")
  2813. (boolean :tag "Snippet"))
  2814. (string :tag "A line of LaTeX"))))
  2815. (defgroup org-appearance nil
  2816. "Settings for Org-mode appearance."
  2817. :tag "Org Appearance"
  2818. :group 'org)
  2819. (defcustom org-level-color-stars-only nil
  2820. "Non-nil means fontify only the stars in each headline.
  2821. When nil, the entire headline is fontified.
  2822. Changing it requires restart of `font-lock-mode' to become effective
  2823. also in regions already fontified."
  2824. :group 'org-appearance
  2825. :type 'boolean)
  2826. (defcustom org-hide-leading-stars nil
  2827. "Non-nil means hide the first N-1 stars in a headline.
  2828. This works by using the face `org-hide' for these stars. This
  2829. face is white for a light background, and black for a dark
  2830. background. You may have to customize the face `org-hide' to
  2831. make this work.
  2832. Changing it requires restart of `font-lock-mode' to become effective
  2833. also in regions already fontified.
  2834. You may also set this on a per-file basis by adding one of the following
  2835. lines to the buffer:
  2836. #+STARTUP: hidestars
  2837. #+STARTUP: showstars"
  2838. :group 'org-appearance
  2839. :type 'boolean)
  2840. (defcustom org-hidden-keywords nil
  2841. "List of keywords that should be hidden when typed in the org buffer.
  2842. For example, add #+TITLE to this list in order to make the
  2843. document title appear in the buffer without the initial #+TITLE:
  2844. keyword."
  2845. :group 'org-appearance
  2846. :type '(set (const :tag "#+AUTHOR" author)
  2847. (const :tag "#+DATE" date)
  2848. (const :tag "#+EMAIL" email)
  2849. (const :tag "#+TITLE" title)))
  2850. (defcustom org-fontify-done-headline nil
  2851. "Non-nil means change the face of a headline if it is marked DONE.
  2852. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2853. When this is non-nil, the headline after the keyword is set to the
  2854. `org-headline-done' as an additional indication."
  2855. :group 'org-appearance
  2856. :type 'boolean)
  2857. (defcustom org-fontify-emphasized-text t
  2858. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2859. Changing this variable requires a restart of Emacs to take effect."
  2860. :group 'org-appearance
  2861. :type 'boolean)
  2862. (defcustom org-fontify-whole-heading-line nil
  2863. "Non-nil means fontify the whole line for headings.
  2864. This is useful when setting a background color for the
  2865. org-level-* faces."
  2866. :group 'org-appearance
  2867. :type 'boolean)
  2868. (defcustom org-highlight-latex-fragments-and-specials nil
  2869. "Non-nil means fontify what is treated specially by the exporters."
  2870. :group 'org-appearance
  2871. :type 'boolean)
  2872. (defcustom org-hide-emphasis-markers nil
  2873. "Non-nil mean font-lock should hide the emphasis marker characters."
  2874. :group 'org-appearance
  2875. :type 'boolean)
  2876. (defcustom org-pretty-entities nil
  2877. "Non-nil means show entities as UTF8 characters.
  2878. When nil, the \\name form remains in the buffer."
  2879. :group 'org-appearance
  2880. :type 'boolean)
  2881. (defcustom org-pretty-entities-include-sub-superscripts t
  2882. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2883. :group 'org-appearance
  2884. :type 'boolean)
  2885. (defvar org-emph-re nil
  2886. "Regular expression for matching emphasis.
  2887. After a match, the match groups contain these elements:
  2888. 0 The match of the full regular expression, including the characters
  2889. before and after the proper match
  2890. 1 The character before the proper match, or empty at beginning of line
  2891. 2 The proper match, including the leading and trailing markers
  2892. 3 The leading marker like * or /, indicating the type of highlighting
  2893. 4 The text between the emphasis markers, not including the markers
  2894. 5 The character after the match, empty at the end of a line")
  2895. (defvar org-verbatim-re nil
  2896. "Regular expression for matching verbatim text.")
  2897. (defvar org-emphasis-regexp-components) ; defined just below
  2898. (defvar org-emphasis-alist) ; defined just below
  2899. (defun org-set-emph-re (var val)
  2900. "Set variable and compute the emphasis regular expression."
  2901. (set var val)
  2902. (when (and (boundp 'org-emphasis-alist)
  2903. (boundp 'org-emphasis-regexp-components)
  2904. org-emphasis-alist org-emphasis-regexp-components)
  2905. (let* ((e org-emphasis-regexp-components)
  2906. (pre (car e))
  2907. (post (nth 1 e))
  2908. (border (nth 2 e))
  2909. (body (nth 3 e))
  2910. (nl (nth 4 e))
  2911. (body1 (concat body "*?"))
  2912. (markers (mapconcat 'car org-emphasis-alist ""))
  2913. (vmarkers (mapconcat
  2914. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2915. org-emphasis-alist "")))
  2916. ;; make sure special characters appear at the right position in the class
  2917. (if (string-match "\\^" markers)
  2918. (setq markers (concat (replace-match "" t t markers) "^")))
  2919. (if (string-match "-" markers)
  2920. (setq markers (concat (replace-match "" t t markers) "-")))
  2921. (if (string-match "\\^" vmarkers)
  2922. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2923. (if (string-match "-" vmarkers)
  2924. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2925. (if (> nl 0)
  2926. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2927. (int-to-string nl) "\\}")))
  2928. ;; Make the regexp
  2929. (setq org-emph-re
  2930. (concat "\\([" pre "]\\|^\\)"
  2931. "\\("
  2932. "\\([" markers "]\\)"
  2933. "\\("
  2934. "[^" border "]\\|"
  2935. "[^" border "]"
  2936. body1
  2937. "[^" border "]"
  2938. "\\)"
  2939. "\\3\\)"
  2940. "\\([" post "]\\|$\\)"))
  2941. (setq org-verbatim-re
  2942. (concat "\\([" pre "]\\|^\\)"
  2943. "\\("
  2944. "\\([" vmarkers "]\\)"
  2945. "\\("
  2946. "[^" border "]\\|"
  2947. "[^" border "]"
  2948. body1
  2949. "[^" border "]"
  2950. "\\)"
  2951. "\\3\\)"
  2952. "\\([" post "]\\|$\\)")))))
  2953. (defcustom org-emphasis-regexp-components
  2954. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2955. "Components used to build the regular expression for emphasis.
  2956. This is a list with 6 entries. Terminology: In an emphasis string
  2957. like \" *strong word* \", we call the initial space PREMATCH, the final
  2958. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2959. and \"trong wor\" is the body. The different components in this variable
  2960. specify what is allowed/forbidden in each part:
  2961. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2962. post Chars allowed as postmatch. End of line will be allowed too.
  2963. border The chars *forbidden* as border characters.
  2964. body-regexp A regexp like \".\" to match a body character. Don't use
  2965. non-shy groups here, and don't allow newline here.
  2966. newline The maximum number of newlines allowed in an emphasis exp.
  2967. Use customize to modify this, or restart Emacs after changing it."
  2968. :group 'org-appearance
  2969. :set 'org-set-emph-re
  2970. :type '(list
  2971. (sexp :tag "Allowed chars in pre ")
  2972. (sexp :tag "Allowed chars in post ")
  2973. (sexp :tag "Forbidden chars in border ")
  2974. (sexp :tag "Regexp for body ")
  2975. (integer :tag "number of newlines allowed")
  2976. (option (boolean :tag "Please ignore this button"))))
  2977. (defcustom org-emphasis-alist
  2978. `(("*" bold "<b>" "</b>")
  2979. ("/" italic "<i>" "</i>")
  2980. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2981. ("=" org-code "<code>" "</code>" verbatim)
  2982. ("~" org-verbatim "<code>" "</code>" verbatim)
  2983. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2984. "<del>" "</del>")
  2985. )
  2986. "Special syntax for emphasized text.
  2987. Text starting and ending with a special character will be emphasized, for
  2988. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2989. characters, the face to be used by font-lock for highlighting in Org-mode
  2990. Emacs buffers, and the HTML tags to be used for this.
  2991. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2992. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  2993. Use customize to modify this, or restart Emacs after changing it."
  2994. :group 'org-appearance
  2995. :set 'org-set-emph-re
  2996. :type '(repeat
  2997. (list
  2998. (string :tag "Marker character")
  2999. (choice
  3000. (face :tag "Font-lock-face")
  3001. (plist :tag "Face property list"))
  3002. (string :tag "HTML start tag")
  3003. (string :tag "HTML end tag")
  3004. (option (const verbatim)))))
  3005. (defvar org-protecting-blocks
  3006. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3007. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3008. This is needed for font-lock setup.")
  3009. ;;; Miscellaneous options
  3010. (defgroup org-completion nil
  3011. "Completion in Org-mode."
  3012. :tag "Org Completion"
  3013. :group 'org)
  3014. (defcustom org-completion-use-ido nil
  3015. "Non-nil means use ido completion wherever possible.
  3016. Note that `ido-mode' must be active for this variable to be relevant.
  3017. If you decide to turn this variable on, you might well want to turn off
  3018. `org-outline-path-complete-in-steps'.
  3019. See also `org-completion-use-iswitchb'."
  3020. :group 'org-completion
  3021. :type 'boolean)
  3022. (defcustom org-completion-use-iswitchb nil
  3023. "Non-nil means use iswitchb completion wherever possible.
  3024. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3025. If you decide to turn this variable on, you might well want to turn off
  3026. `org-outline-path-complete-in-steps'.
  3027. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3028. :group 'org-completion
  3029. :type 'boolean)
  3030. (defcustom org-completion-fallback-command 'hippie-expand
  3031. "The expansion command called by \\[org-complete] in normal context.
  3032. Normal means no org-mode-specific context."
  3033. :group 'org-completion
  3034. :type 'function)
  3035. ;;; Functions and variables from their packages
  3036. ;; Declared here to avoid compiler warnings
  3037. ;; XEmacs only
  3038. (defvar outline-mode-menu-heading)
  3039. (defvar outline-mode-menu-show)
  3040. (defvar outline-mode-menu-hide)
  3041. (defvar zmacs-regions) ; XEmacs regions
  3042. ;; Emacs only
  3043. (defvar mark-active)
  3044. ;; Various packages
  3045. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3046. (declare-function calendar-forward-day "cal-move" (arg))
  3047. (declare-function calendar-goto-date "cal-move" (date))
  3048. (declare-function calendar-goto-today "cal-move" ())
  3049. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3050. (defvar calc-embedded-close-formula)
  3051. (defvar calc-embedded-open-formula)
  3052. (declare-function cdlatex-tab "ext:cdlatex" ())
  3053. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3054. (defvar font-lock-unfontify-region-function)
  3055. (declare-function iswitchb-read-buffer "iswitchb"
  3056. (prompt &optional default require-match start matches-set))
  3057. (defvar iswitchb-temp-buflist)
  3058. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3059. (defvar org-agenda-tags-todo-honor-ignore-options)
  3060. (declare-function org-agenda-skip "org-agenda" ())
  3061. (declare-function
  3062. org-format-agenda-item "org-agenda"
  3063. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3064. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3065. (declare-function org-agenda-change-all-lines "org-agenda"
  3066. (newhead hdmarker &optional fixface just-this))
  3067. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3068. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3069. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3070. (beg end))
  3071. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3072. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3073. "org-agenda" (&optional end))
  3074. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3075. (declare-function org-indent-mode "org-indent" (&optional arg))
  3076. (declare-function parse-time-string "parse-time" (string))
  3077. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3078. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3079. (defvar remember-data-file)
  3080. (defvar texmathp-why)
  3081. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3082. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3083. (defvar w3m-current-url)
  3084. (defvar w3m-current-title)
  3085. (defvar org-latex-regexps)
  3086. ;;; Autoload and prepare some org modules
  3087. ;; Some table stuff that needs to be defined here, because it is used
  3088. ;; by the functions setting up org-mode or checking for table context.
  3089. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3090. "Detect an org-type or table-type table.")
  3091. (defconst org-table-line-regexp "^[ \t]*|"
  3092. "Detect an org-type table line.")
  3093. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3094. "Detect an org-type table line.")
  3095. (defconst org-table-hline-regexp "^[ \t]*|-"
  3096. "Detect an org-type table hline.")
  3097. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3098. "Detect a table-type table hline.")
  3099. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3100. "Detect the first line outside a table when searching from within it.
  3101. This works for both table types.")
  3102. ;; Autoload the functions in org-table.el that are needed by functions here.
  3103. (eval-and-compile
  3104. (org-autoload "org-table"
  3105. '(org-table-align org-table-begin org-table-blank-field
  3106. org-table-convert org-table-convert-region org-table-copy-down
  3107. org-table-copy-region org-table-create
  3108. org-table-create-or-convert-from-region
  3109. org-table-create-with-table.el org-table-current-dline
  3110. org-table-cut-region org-table-delete-column org-table-edit-field
  3111. org-table-edit-formulas org-table-end org-table-eval-formula
  3112. org-table-export org-table-field-info
  3113. org-table-get-stored-formulas org-table-goto-column
  3114. org-table-hline-and-move org-table-import org-table-insert-column
  3115. org-table-insert-hline org-table-insert-row org-table-iterate
  3116. org-table-justify-field-maybe org-table-kill-row
  3117. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3118. org-table-move-column org-table-move-column-left
  3119. org-table-move-column-right org-table-move-row
  3120. org-table-move-row-down org-table-move-row-up
  3121. org-table-next-field org-table-next-row org-table-paste-rectangle
  3122. org-table-previous-field org-table-recalculate
  3123. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3124. org-table-toggle-coordinate-overlays
  3125. org-table-toggle-formula-debugger org-table-wrap-region
  3126. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3127. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3128. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3129. (defun org-at-table-p (&optional table-type)
  3130. "Return t if the cursor is inside an org-type table.
  3131. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3132. (if org-enable-table-editor
  3133. (save-excursion
  3134. (beginning-of-line 1)
  3135. (looking-at (if table-type org-table-any-line-regexp
  3136. org-table-line-regexp)))
  3137. nil))
  3138. (defsubst org-table-p () (org-at-table-p))
  3139. (defun org-at-table.el-p ()
  3140. "Return t if and only if we are at a table.el table."
  3141. (and (org-at-table-p 'any)
  3142. (save-excursion
  3143. (goto-char (org-table-begin 'any))
  3144. (looking-at org-table1-hline-regexp))))
  3145. (defun org-table-recognize-table.el ()
  3146. "If there is a table.el table nearby, recognize it and move into it."
  3147. (if org-table-tab-recognizes-table.el
  3148. (if (org-at-table.el-p)
  3149. (progn
  3150. (beginning-of-line 1)
  3151. (if (looking-at org-table-dataline-regexp)
  3152. nil
  3153. (if (looking-at org-table1-hline-regexp)
  3154. (progn
  3155. (beginning-of-line 2)
  3156. (if (looking-at org-table-any-border-regexp)
  3157. (beginning-of-line -1)))))
  3158. (if (re-search-forward "|" (org-table-end t) t)
  3159. (progn
  3160. (require 'table)
  3161. (if (table--at-cell-p (point))
  3162. t
  3163. (message "recognizing table.el table...")
  3164. (table-recognize-table)
  3165. (message "recognizing table.el table...done")))
  3166. (error "This should not happen"))
  3167. t)
  3168. nil)
  3169. nil))
  3170. (defun org-at-table-hline-p ()
  3171. "Return t if the cursor is inside a hline in a table."
  3172. (if org-enable-table-editor
  3173. (save-excursion
  3174. (beginning-of-line 1)
  3175. (looking-at org-table-hline-regexp))
  3176. nil))
  3177. (defvar org-table-clean-did-remove-column nil)
  3178. (defun org-table-map-tables (function &optional quietly)
  3179. "Apply FUNCTION to the start of all tables in the buffer."
  3180. (save-excursion
  3181. (save-restriction
  3182. (widen)
  3183. (goto-char (point-min))
  3184. (while (re-search-forward org-table-any-line-regexp nil t)
  3185. (unless quietly
  3186. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3187. (beginning-of-line 1)
  3188. (when (looking-at org-table-line-regexp)
  3189. (save-excursion (funcall function))
  3190. (or (looking-at org-table-line-regexp)
  3191. (forward-char 1)))
  3192. (re-search-forward org-table-any-border-regexp nil 1))))
  3193. (unless quietly (message "Mapping tables: done")))
  3194. ;; Declare and autoload functions from org-exp.el & Co
  3195. (declare-function org-default-export-plist "org-exp")
  3196. (declare-function org-infile-export-plist "org-exp")
  3197. (declare-function org-get-current-options "org-exp")
  3198. (eval-and-compile
  3199. (org-autoload "org-exp"
  3200. '(org-export org-export-visible
  3201. org-insert-export-options-template
  3202. org-table-clean-before-export))
  3203. (org-autoload "org-ascii"
  3204. '(org-export-as-ascii org-export-ascii-preprocess
  3205. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3206. org-export-region-as-ascii))
  3207. (org-autoload "org-latex"
  3208. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3209. org-replace-region-by-latex org-export-region-as-latex
  3210. org-export-as-latex org-export-as-pdf
  3211. org-export-as-pdf-and-open))
  3212. (org-autoload "org-html"
  3213. '(org-export-as-html-and-open
  3214. org-export-as-html-batch org-export-as-html-to-buffer
  3215. org-replace-region-by-html org-export-region-as-html
  3216. org-export-as-html))
  3217. (org-autoload "org-docbook"
  3218. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3219. org-replace-region-by-docbook org-export-region-as-docbook
  3220. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3221. org-export-as-docbook))
  3222. (org-autoload "org-icalendar"
  3223. '(org-export-icalendar-this-file
  3224. org-export-icalendar-all-agenda-files
  3225. org-export-icalendar-combine-agenda-files))
  3226. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3227. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3228. ;; Declare and autoload functions from org-agenda.el
  3229. (eval-and-compile
  3230. (org-autoload "org-agenda"
  3231. '(org-agenda org-agenda-list org-search-view
  3232. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3233. org-diary org-agenda-to-appt
  3234. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3235. ;; Autoload org-remember
  3236. (eval-and-compile
  3237. (org-autoload "org-remember"
  3238. '(org-remember-insinuate org-remember-annotation
  3239. org-remember-apply-template org-remember org-remember-handler)))
  3240. (eval-and-compile
  3241. (org-autoload "org-capture"
  3242. '(org-capture org-capture-insert-template-here
  3243. org-capture-import-remember-templates)))
  3244. ;; Autoload org-clock.el
  3245. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3246. (beg end))
  3247. (declare-function org-clock-update-mode-line "org-clock" ())
  3248. (declare-function org-resolve-clocks "org-clock"
  3249. (&optional also-non-dangling-p prompt last-valid))
  3250. (defvar org-clock-start-time)
  3251. (defvar org-clock-marker (make-marker)
  3252. "Marker recording the last clock-in.")
  3253. (defvar org-clock-hd-marker (make-marker)
  3254. "Marker recording the last clock-in, but the headline position.")
  3255. (defvar org-clock-heading ""
  3256. "The heading of the current clock entry.")
  3257. (defun org-clock-is-active ()
  3258. "Return non-nil if clock is currently running.
  3259. The return value is actually the clock marker."
  3260. (marker-buffer org-clock-marker))
  3261. (eval-and-compile
  3262. (org-autoload
  3263. "org-clock"
  3264. '(org-clock-in org-clock-out org-clock-cancel
  3265. org-clock-goto org-clock-sum org-clock-display
  3266. org-clock-remove-overlays org-clock-report
  3267. org-clocktable-shift org-dblock-write:clocktable
  3268. org-get-clocktable org-resolve-clocks)))
  3269. (defun org-clock-update-time-maybe ()
  3270. "If this is a CLOCK line, update it and return t.
  3271. Otherwise, return nil."
  3272. (interactive)
  3273. (save-excursion
  3274. (beginning-of-line 1)
  3275. (skip-chars-forward " \t")
  3276. (when (looking-at org-clock-string)
  3277. (let ((re (concat "[ \t]*" org-clock-string
  3278. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3279. "\\([ \t]*=>.*\\)?\\)?"))
  3280. ts te h m s neg)
  3281. (cond
  3282. ((not (looking-at re))
  3283. nil)
  3284. ((not (match-end 2))
  3285. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3286. (> org-clock-marker (point))
  3287. (<= org-clock-marker (point-at-eol)))
  3288. ;; The clock is running here
  3289. (setq org-clock-start-time
  3290. (apply 'encode-time
  3291. (org-parse-time-string (match-string 1))))
  3292. (org-clock-update-mode-line)))
  3293. (t
  3294. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3295. (end-of-line 1)
  3296. (setq ts (match-string 1)
  3297. te (match-string 3))
  3298. (setq s (- (org-float-time
  3299. (apply 'encode-time (org-parse-time-string te)))
  3300. (org-float-time
  3301. (apply 'encode-time (org-parse-time-string ts))))
  3302. neg (< s 0)
  3303. s (abs s)
  3304. h (floor (/ s 3600))
  3305. s (- s (* 3600 h))
  3306. m (floor (/ s 60))
  3307. s (- s (* 60 s)))
  3308. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3309. t))))))
  3310. (defun org-check-running-clock ()
  3311. "Check if the current buffer contains the running clock.
  3312. If yes, offer to stop it and to save the buffer with the changes."
  3313. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3314. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3315. (buffer-name))))
  3316. (org-clock-out)
  3317. (when (y-or-n-p "Save changed buffer?")
  3318. (save-buffer))))
  3319. (defun org-clocktable-try-shift (dir n)
  3320. "Check if this line starts a clock table, if yes, shift the time block."
  3321. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3322. (org-clocktable-shift dir n)))
  3323. ;; Autoload org-timer.el
  3324. (eval-and-compile
  3325. (org-autoload
  3326. "org-timer"
  3327. '(org-timer-start org-timer org-timer-item
  3328. org-timer-change-times-in-region
  3329. org-timer-set-timer
  3330. org-timer-reset-timers
  3331. org-timer-show-remaining-time)))
  3332. ;; Autoload org-feed.el
  3333. (eval-and-compile
  3334. (org-autoload
  3335. "org-feed"
  3336. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3337. ;; Autoload org-indent.el
  3338. ;; Define the variable already here, to make sure we have it.
  3339. (defvar org-indent-mode nil
  3340. "Non-nil if Org-Indent mode is enabled.
  3341. Use the command `org-indent-mode' to change this variable.")
  3342. (eval-and-compile
  3343. (org-autoload
  3344. "org-indent"
  3345. '(org-indent-mode)))
  3346. ;; Autoload org-mobile.el
  3347. (eval-and-compile
  3348. (org-autoload
  3349. "org-mobile"
  3350. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3351. ;; Autoload archiving code
  3352. ;; The stuff that is needed for cycling and tags has to be defined here.
  3353. (defgroup org-archive nil
  3354. "Options concerning archiving in Org-mode."
  3355. :tag "Org Archive"
  3356. :group 'org-structure)
  3357. (defcustom org-archive-location "%s_archive::"
  3358. "The location where subtrees should be archived.
  3359. The value of this variable is a string, consisting of two parts,
  3360. separated by a double-colon. The first part is a filename and
  3361. the second part is a headline.
  3362. When the filename is omitted, archiving happens in the same file.
  3363. %s in the filename will be replaced by the current file
  3364. name (without the directory part). Archiving to a different file
  3365. is useful to keep archived entries from contributing to the
  3366. Org-mode Agenda.
  3367. The archived entries will be filed as subtrees of the specified
  3368. headline. When the headline is omitted, the subtrees are simply
  3369. filed away at the end of the file, as top-level entries. Also in
  3370. the heading you can use %s to represent the file name, this can be
  3371. useful when using the same archive for a number of different files.
  3372. Here are a few examples:
  3373. \"%s_archive::\"
  3374. If the current file is Projects.org, archive in file
  3375. Projects.org_archive, as top-level trees. This is the default.
  3376. \"::* Archived Tasks\"
  3377. Archive in the current file, under the top-level headline
  3378. \"* Archived Tasks\".
  3379. \"~/org/archive.org::\"
  3380. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3381. \"~/org/archive.org::From %s\"
  3382. Archive in file ~/org/archive.org (absolute path), under headlines
  3383. \"From FILENAME\" where file name is the current file name.
  3384. \"basement::** Finished Tasks\"
  3385. Archive in file ./basement (relative path), as level 3 trees
  3386. below the level 2 heading \"** Finished Tasks\".
  3387. You may set this option on a per-file basis by adding to the buffer a
  3388. line like
  3389. #+ARCHIVE: basement::** Finished Tasks
  3390. You may also define it locally for a subtree by setting an ARCHIVE property
  3391. in the entry. If such a property is found in an entry, or anywhere up
  3392. the hierarchy, it will be used."
  3393. :group 'org-archive
  3394. :type 'string)
  3395. (defcustom org-archive-tag "ARCHIVE"
  3396. "The tag that marks a subtree as archived.
  3397. An archived subtree does not open during visibility cycling, and does
  3398. not contribute to the agenda listings.
  3399. After changing this, font-lock must be restarted in the relevant buffers to
  3400. get the proper fontification."
  3401. :group 'org-archive
  3402. :group 'org-keywords
  3403. :type 'string)
  3404. (defcustom org-agenda-skip-archived-trees t
  3405. "Non-nil means the agenda will skip any items located in archived trees.
  3406. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3407. variable is no longer recommended, you should leave it at the value t.
  3408. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3409. :group 'org-archive
  3410. :group 'org-agenda-skip
  3411. :type 'boolean)
  3412. (defcustom org-columns-skip-archived-trees t
  3413. "Non-nil means ignore archived trees when creating column view."
  3414. :group 'org-archive
  3415. :group 'org-properties
  3416. :type 'boolean)
  3417. (defcustom org-cycle-open-archived-trees nil
  3418. "Non-nil means `org-cycle' will open archived trees.
  3419. An archived tree is a tree marked with the tag ARCHIVE.
  3420. When nil, archived trees will stay folded. You can still open them with
  3421. normal outline commands like `show-all', but not with the cycling commands."
  3422. :group 'org-archive
  3423. :group 'org-cycle
  3424. :type 'boolean)
  3425. (defcustom org-sparse-tree-open-archived-trees nil
  3426. "Non-nil means sparse tree construction shows matches in archived trees.
  3427. When nil, matches in these trees are highlighted, but the trees are kept in
  3428. collapsed state."
  3429. :group 'org-archive
  3430. :group 'org-sparse-trees
  3431. :type 'boolean)
  3432. (defun org-cycle-hide-archived-subtrees (state)
  3433. "Re-hide all archived subtrees after a visibility state change."
  3434. (when (and (not org-cycle-open-archived-trees)
  3435. (not (memq state '(overview folded))))
  3436. (save-excursion
  3437. (let* ((globalp (memq state '(contents all)))
  3438. (beg (if globalp (point-min) (point)))
  3439. (end (if globalp (point-max) (org-end-of-subtree t))))
  3440. (org-hide-archived-subtrees beg end)
  3441. (goto-char beg)
  3442. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3443. (message "%s" (substitute-command-keys
  3444. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3445. (defun org-force-cycle-archived ()
  3446. "Cycle subtree even if it is archived."
  3447. (interactive)
  3448. (setq this-command 'org-cycle)
  3449. (let ((org-cycle-open-archived-trees t))
  3450. (call-interactively 'org-cycle)))
  3451. (defun org-hide-archived-subtrees (beg end)
  3452. "Re-hide all archived subtrees after a visibility state change."
  3453. (save-excursion
  3454. (let* ((re (concat ":" org-archive-tag ":")))
  3455. (goto-char beg)
  3456. (while (re-search-forward re end t)
  3457. (when (org-on-heading-p)
  3458. (org-flag-subtree t)
  3459. (org-end-of-subtree t))))))
  3460. (defun org-flag-subtree (flag)
  3461. (save-excursion
  3462. (org-back-to-heading t)
  3463. (outline-end-of-heading)
  3464. (outline-flag-region (point)
  3465. (progn (org-end-of-subtree t) (point))
  3466. flag)))
  3467. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3468. (eval-and-compile
  3469. (org-autoload "org-archive"
  3470. '(org-add-archive-files org-archive-subtree
  3471. org-archive-to-archive-sibling org-toggle-archive-tag
  3472. org-archive-subtree-default
  3473. org-archive-subtree-default-with-confirmation)))
  3474. ;; Autoload Column View Code
  3475. (declare-function org-columns-number-to-string "org-colview")
  3476. (declare-function org-columns-get-format-and-top-level "org-colview")
  3477. (declare-function org-columns-compute "org-colview")
  3478. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3479. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3480. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3481. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3482. ;; Autoload ID code
  3483. (declare-function org-id-store-link "org-id")
  3484. (declare-function org-id-locations-load "org-id")
  3485. (declare-function org-id-locations-save "org-id")
  3486. (defvar org-id-track-globally)
  3487. (org-autoload "org-id"
  3488. '(org-id-get-create org-id-new org-id-copy org-id-get
  3489. org-id-get-with-outline-path-completion
  3490. org-id-get-with-outline-drilling org-id-store-link
  3491. org-id-goto org-id-find org-id-store-link))
  3492. ;; Autoload Plotting Code
  3493. (org-autoload "org-plot"
  3494. '(org-plot/gnuplot))
  3495. ;;; Variables for pre-computed regular expressions, all buffer local
  3496. (defvar org-drawer-regexp nil
  3497. "Matches first line of a hidden block.")
  3498. (make-variable-buffer-local 'org-drawer-regexp)
  3499. (defvar org-todo-regexp nil
  3500. "Matches any of the TODO state keywords.")
  3501. (make-variable-buffer-local 'org-todo-regexp)
  3502. (defvar org-not-done-regexp nil
  3503. "Matches any of the TODO state keywords except the last one.")
  3504. (make-variable-buffer-local 'org-not-done-regexp)
  3505. (defvar org-not-done-heading-regexp nil
  3506. "Matches a TODO headline that is not done.")
  3507. (make-variable-buffer-local 'org-not-done-regexp)
  3508. (defvar org-todo-line-regexp nil
  3509. "Matches a headline and puts TODO state into group 2 if present.")
  3510. (make-variable-buffer-local 'org-todo-line-regexp)
  3511. (defvar org-complex-heading-regexp nil
  3512. "Matches a headline and puts everything into groups:
  3513. group 1: the stars
  3514. group 2: The todo keyword, maybe
  3515. group 3: Priority cookie
  3516. group 4: True headline
  3517. group 5: Tags")
  3518. (make-variable-buffer-local 'org-complex-heading-regexp)
  3519. (defvar org-complex-heading-regexp-format nil
  3520. "Printf format to make regexp to match an exact headline.
  3521. This regexp will match the headline of any node which hase the exact
  3522. headline text that is put into the format, but may have any TODO state,
  3523. priority and tags.")
  3524. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3525. (defvar org-todo-line-tags-regexp nil
  3526. "Matches a headline and puts TODO state into group 2 if present.
  3527. Also put tags into group 4 if tags are present.")
  3528. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3529. (defvar org-nl-done-regexp nil
  3530. "Matches newline followed by a headline with the DONE keyword.")
  3531. (make-variable-buffer-local 'org-nl-done-regexp)
  3532. (defvar org-looking-at-done-regexp nil
  3533. "Matches the DONE keyword a point.")
  3534. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3535. (defvar org-ds-keyword-length 12
  3536. "Maximum length of the Deadline and SCHEDULED keywords.")
  3537. (make-variable-buffer-local 'org-ds-keyword-length)
  3538. (defvar org-deadline-regexp nil
  3539. "Matches the DEADLINE keyword.")
  3540. (make-variable-buffer-local 'org-deadline-regexp)
  3541. (defvar org-deadline-time-regexp nil
  3542. "Matches the DEADLINE keyword together with a time stamp.")
  3543. (make-variable-buffer-local 'org-deadline-time-regexp)
  3544. (defvar org-deadline-line-regexp nil
  3545. "Matches the DEADLINE keyword and the rest of the line.")
  3546. (make-variable-buffer-local 'org-deadline-line-regexp)
  3547. (defvar org-scheduled-regexp nil
  3548. "Matches the SCHEDULED keyword.")
  3549. (make-variable-buffer-local 'org-scheduled-regexp)
  3550. (defvar org-scheduled-time-regexp nil
  3551. "Matches the SCHEDULED keyword together with a time stamp.")
  3552. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3553. (defvar org-closed-time-regexp nil
  3554. "Matches the CLOSED keyword together with a time stamp.")
  3555. (make-variable-buffer-local 'org-closed-time-regexp)
  3556. (defvar org-keyword-time-regexp nil
  3557. "Matches any of the 4 keywords, together with the time stamp.")
  3558. (make-variable-buffer-local 'org-keyword-time-regexp)
  3559. (defvar org-keyword-time-not-clock-regexp nil
  3560. "Matches any of the 3 keywords, together with the time stamp.")
  3561. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3562. (defvar org-maybe-keyword-time-regexp nil
  3563. "Matches a timestamp, possibly preceded by a keyword.")
  3564. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3565. (defvar org-planning-or-clock-line-re nil
  3566. "Matches a line with planning or clock info.")
  3567. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3568. (defvar org-all-time-keywords nil
  3569. "List of time keywords.")
  3570. (make-variable-buffer-local 'org-all-time-keywords)
  3571. (defconst org-plain-time-of-day-regexp
  3572. (concat
  3573. "\\(\\<[012]?[0-9]"
  3574. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3575. "\\(--?"
  3576. "\\(\\<[012]?[0-9]"
  3577. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3578. "\\)?")
  3579. "Regular expression to match a plain time or time range.
  3580. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3581. groups carry important information:
  3582. 0 the full match
  3583. 1 the first time, range or not
  3584. 8 the second time, if it is a range.")
  3585. (defconst org-plain-time-extension-regexp
  3586. (concat
  3587. "\\(\\<[012]?[0-9]"
  3588. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3589. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3590. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3591. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3592. groups carry important information:
  3593. 0 the full match
  3594. 7 hours of duration
  3595. 9 minutes of duration")
  3596. (defconst org-stamp-time-of-day-regexp
  3597. (concat
  3598. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3599. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3600. "\\(--?"
  3601. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3602. "Regular expression to match a timestamp time or time range.
  3603. After a match, the following groups carry important information:
  3604. 0 the full match
  3605. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3606. 2 the first time, range or not
  3607. 4 the second time, if it is a range.")
  3608. (defconst org-startup-options
  3609. '(("fold" org-startup-folded t)
  3610. ("overview" org-startup-folded t)
  3611. ("nofold" org-startup-folded nil)
  3612. ("showall" org-startup-folded nil)
  3613. ("showeverything" org-startup-folded showeverything)
  3614. ("content" org-startup-folded content)
  3615. ("indent" org-startup-indented t)
  3616. ("noindent" org-startup-indented nil)
  3617. ("hidestars" org-hide-leading-stars t)
  3618. ("showstars" org-hide-leading-stars nil)
  3619. ("odd" org-odd-levels-only t)
  3620. ("oddeven" org-odd-levels-only nil)
  3621. ("align" org-startup-align-all-tables t)
  3622. ("noalign" org-startup-align-all-tables nil)
  3623. ("customtime" org-display-custom-times t)
  3624. ("logdone" org-log-done time)
  3625. ("lognotedone" org-log-done note)
  3626. ("nologdone" org-log-done nil)
  3627. ("lognoteclock-out" org-log-note-clock-out t)
  3628. ("nolognoteclock-out" org-log-note-clock-out nil)
  3629. ("logrepeat" org-log-repeat state)
  3630. ("lognoterepeat" org-log-repeat note)
  3631. ("nologrepeat" org-log-repeat nil)
  3632. ("logreschedule" org-log-reschedule time)
  3633. ("lognotereschedule" org-log-reschedule note)
  3634. ("nologreschedule" org-log-reschedule nil)
  3635. ("logredeadline" org-log-redeadline time)
  3636. ("lognoteredeadline" org-log-redeadline note)
  3637. ("nologredeadline" org-log-redeadline nil)
  3638. ("logrefile" org-log-refile time)
  3639. ("lognoterefile" org-log-refile note)
  3640. ("nologrefile" org-log-refile nil)
  3641. ("fninline" org-footnote-define-inline t)
  3642. ("nofninline" org-footnote-define-inline nil)
  3643. ("fnlocal" org-footnote-section nil)
  3644. ("fnauto" org-footnote-auto-label t)
  3645. ("fnprompt" org-footnote-auto-label nil)
  3646. ("fnconfirm" org-footnote-auto-label confirm)
  3647. ("fnplain" org-footnote-auto-label plain)
  3648. ("fnadjust" org-footnote-auto-adjust t)
  3649. ("nofnadjust" org-footnote-auto-adjust nil)
  3650. ("constcgs" constants-unit-system cgs)
  3651. ("constSI" constants-unit-system SI)
  3652. ("noptag" org-tag-persistent-alist nil)
  3653. ("hideblocks" org-hide-block-startup t)
  3654. ("nohideblocks" org-hide-block-startup nil)
  3655. ("beamer" org-startup-with-beamer-mode t)
  3656. ("entitiespretty" org-pretty-entities t)
  3657. ("entitiesplain" org-pretty-entities nil))
  3658. "Variable associated with STARTUP options for org-mode.
  3659. Each element is a list of three items: The startup options as written
  3660. in the #+STARTUP line, the corresponding variable, and the value to
  3661. set this variable to if the option is found. An optional forth element PUSH
  3662. means to push this value onto the list in the variable.")
  3663. (defun org-set-regexps-and-options ()
  3664. "Precompute regular expressions for current buffer."
  3665. (when (org-mode-p)
  3666. (org-set-local 'org-todo-kwd-alist nil)
  3667. (org-set-local 'org-todo-key-alist nil)
  3668. (org-set-local 'org-todo-key-trigger nil)
  3669. (org-set-local 'org-todo-keywords-1 nil)
  3670. (org-set-local 'org-done-keywords nil)
  3671. (org-set-local 'org-todo-heads nil)
  3672. (org-set-local 'org-todo-sets nil)
  3673. (org-set-local 'org-todo-log-states nil)
  3674. (org-set-local 'org-file-properties nil)
  3675. (org-set-local 'org-file-tags nil)
  3676. (let ((re (org-make-options-regexp
  3677. '("CATEGORY" "TODO" "COLUMNS"
  3678. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3679. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3680. "OPTIONS")
  3681. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3682. (splitre "[ \t]+")
  3683. (scripts org-use-sub-superscripts)
  3684. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3685. tail sep kws1 prio props ftags drawers beamer-p
  3686. ext-setup-or-nil setup-contents (start 0))
  3687. (save-excursion
  3688. (save-restriction
  3689. (widen)
  3690. (goto-char (point-min))
  3691. (while (or (and ext-setup-or-nil
  3692. (string-match re ext-setup-or-nil start)
  3693. (setq start (match-end 0)))
  3694. (and (setq ext-setup-or-nil nil start 0)
  3695. (re-search-forward re nil t)))
  3696. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3697. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3698. (if (stringp value) (setq value (org-trim value)))
  3699. (cond
  3700. ((equal key "CATEGORY")
  3701. (setq cat value))
  3702. ((member key '("SEQ_TODO" "TODO"))
  3703. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3704. ((equal key "TYP_TODO")
  3705. (push (cons 'type (org-split-string value splitre)) kwds))
  3706. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3707. ;; general TODO-like setup
  3708. (push (cons (intern (downcase (match-string 1 key)))
  3709. (org-split-string value splitre)) kwds))
  3710. ((equal key "TAGS")
  3711. (setq tags (append tags (if tags '("\\n") nil)
  3712. (org-split-string value splitre))))
  3713. ((equal key "COLUMNS")
  3714. (org-set-local 'org-columns-default-format value))
  3715. ((equal key "LINK")
  3716. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3717. (push (cons (match-string 1 value)
  3718. (org-trim (match-string 2 value)))
  3719. links)))
  3720. ((equal key "PRIORITIES")
  3721. (setq prio (org-split-string value " +")))
  3722. ((equal key "PROPERTY")
  3723. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3724. (push (cons (match-string 1 value) (match-string 2 value))
  3725. props)))
  3726. ((equal key "FILETAGS")
  3727. (when (string-match "\\S-" value)
  3728. (setq ftags
  3729. (append
  3730. ftags
  3731. (apply 'append
  3732. (mapcar (lambda (x) (org-split-string x ":"))
  3733. (org-split-string value)))))))
  3734. ((equal key "DRAWERS")
  3735. (setq drawers (org-split-string value splitre)))
  3736. ((equal key "CONSTANTS")
  3737. (setq const (append const (org-split-string value splitre))))
  3738. ((equal key "STARTUP")
  3739. (let ((opts (org-split-string value splitre))
  3740. l var val)
  3741. (while (setq l (pop opts))
  3742. (when (setq l (assoc l org-startup-options))
  3743. (setq var (nth 1 l) val (nth 2 l))
  3744. (if (not (nth 3 l))
  3745. (set (make-local-variable var) val)
  3746. (if (not (listp (symbol-value var)))
  3747. (set (make-local-variable var) nil))
  3748. (set (make-local-variable var) (symbol-value var))
  3749. (add-to-list var val))))))
  3750. ((equal key "ARCHIVE")
  3751. (setq arch value)
  3752. (remove-text-properties 0 (length arch)
  3753. '(face t fontified t) arch))
  3754. ((equal key "LATEX_CLASS")
  3755. (setq beamer-p (equal value "beamer")))
  3756. ((equal key "OPTIONS")
  3757. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3758. (setq scripts (read (match-string 2 value)))))
  3759. ((equal key "SETUPFILE")
  3760. (setq setup-contents (org-file-contents
  3761. (expand-file-name
  3762. (org-remove-double-quotes value))
  3763. 'noerror))
  3764. (if (not ext-setup-or-nil)
  3765. (setq ext-setup-or-nil setup-contents start 0)
  3766. (setq ext-setup-or-nil
  3767. (concat (substring ext-setup-or-nil 0 start)
  3768. "\n" setup-contents "\n"
  3769. (substring ext-setup-or-nil start)))))
  3770. ))))
  3771. (org-set-local 'org-use-sub-superscripts scripts)
  3772. (when cat
  3773. (org-set-local 'org-category (intern cat))
  3774. (push (cons "CATEGORY" cat) props))
  3775. (when prio
  3776. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3777. (setq prio (mapcar 'string-to-char prio))
  3778. (org-set-local 'org-highest-priority (nth 0 prio))
  3779. (org-set-local 'org-lowest-priority (nth 1 prio))
  3780. (org-set-local 'org-default-priority (nth 2 prio)))
  3781. (and props (org-set-local 'org-file-properties (nreverse props)))
  3782. (and ftags (org-set-local 'org-file-tags
  3783. (mapcar 'org-add-prop-inherited ftags)))
  3784. (and drawers (org-set-local 'org-drawers drawers))
  3785. (and arch (org-set-local 'org-archive-location arch))
  3786. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3787. ;; Process the TODO keywords
  3788. (unless kwds
  3789. ;; Use the global values as if they had been given locally.
  3790. (setq kwds (default-value 'org-todo-keywords))
  3791. (if (stringp (car kwds))
  3792. (setq kwds (list (cons org-todo-interpretation
  3793. (default-value 'org-todo-keywords)))))
  3794. (setq kwds (reverse kwds)))
  3795. (setq kwds (nreverse kwds))
  3796. (let (inter kws kw)
  3797. (while (setq kws (pop kwds))
  3798. (let ((kws (or
  3799. (run-hook-with-args-until-success
  3800. 'org-todo-setup-filter-hook kws)
  3801. kws)))
  3802. (setq inter (pop kws) sep (member "|" kws)
  3803. kws0 (delete "|" (copy-sequence kws))
  3804. kwsa nil
  3805. kws1 (mapcar
  3806. (lambda (x)
  3807. ;; 1 2
  3808. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3809. (progn
  3810. (setq kw (match-string 1 x)
  3811. key (and (match-end 2) (match-string 2 x))
  3812. log (org-extract-log-state-settings x))
  3813. (push (cons kw (and key (string-to-char key))) kwsa)
  3814. (and log (push log org-todo-log-states))
  3815. kw)
  3816. (error "Invalid TODO keyword %s" x)))
  3817. kws0)
  3818. kwsa (if kwsa (append '((:startgroup))
  3819. (nreverse kwsa)
  3820. '((:endgroup))))
  3821. hw (car kws1)
  3822. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3823. tail (list inter hw (car dws) (org-last dws))))
  3824. (add-to-list 'org-todo-heads hw 'append)
  3825. (push kws1 org-todo-sets)
  3826. (setq org-done-keywords (append org-done-keywords dws nil))
  3827. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3828. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3829. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3830. (setq org-todo-sets (nreverse org-todo-sets)
  3831. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3832. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3833. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3834. ;; Process the constants
  3835. (when const
  3836. (let (e cst)
  3837. (while (setq e (pop const))
  3838. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3839. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3840. (setq org-table-formula-constants-local cst)))
  3841. ;; Process the tags.
  3842. (when tags
  3843. (let (e tgs)
  3844. (while (setq e (pop tags))
  3845. (cond
  3846. ((equal e "{") (push '(:startgroup) tgs))
  3847. ((equal e "}") (push '(:endgroup) tgs))
  3848. ((equal e "\\n") (push '(:newline) tgs))
  3849. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3850. (push (cons (match-string 1 e)
  3851. (string-to-char (match-string 2 e)))
  3852. tgs))
  3853. (t (push (list e) tgs))))
  3854. (org-set-local 'org-tag-alist nil)
  3855. (while (setq e (pop tgs))
  3856. (or (and (stringp (car e))
  3857. (assoc (car e) org-tag-alist))
  3858. (push e org-tag-alist)))))
  3859. ;; Compute the regular expressions and other local variables
  3860. (if (not org-done-keywords)
  3861. (setq org-done-keywords (and org-todo-keywords-1
  3862. (list (org-last org-todo-keywords-1)))))
  3863. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3864. (length org-scheduled-string)
  3865. (length org-clock-string)
  3866. (length org-closed-string)))
  3867. org-drawer-regexp
  3868. (concat "^[ \t]*:\\("
  3869. (mapconcat 'regexp-quote org-drawers "\\|")
  3870. "\\):[ \t]*$")
  3871. org-not-done-keywords
  3872. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3873. org-todo-regexp
  3874. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3875. "\\|") "\\)\\>")
  3876. org-not-done-regexp
  3877. (concat "\\<\\("
  3878. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3879. "\\)\\>")
  3880. org-not-done-heading-regexp
  3881. (concat "^\\(\\*+\\)[ \t]+\\("
  3882. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3883. "\\)\\>")
  3884. org-todo-line-regexp
  3885. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3886. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3887. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3888. org-complex-heading-regexp
  3889. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3890. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3891. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3892. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3893. org-complex-heading-regexp-format
  3894. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3895. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3896. "\\)\\>\\)?"
  3897. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3898. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3899. "[ \t]*\\(%s\\)"
  3900. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3901. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3902. org-nl-done-regexp
  3903. (concat "\n\\*+[ \t]+"
  3904. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3905. "\\)" "\\>")
  3906. org-todo-line-tags-regexp
  3907. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3908. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3909. (org-re
  3910. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3911. org-looking-at-done-regexp
  3912. (concat "^" "\\(?:"
  3913. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3914. "\\>")
  3915. org-deadline-regexp (concat "\\<" org-deadline-string)
  3916. org-deadline-time-regexp
  3917. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3918. org-deadline-line-regexp
  3919. (concat "\\<\\(" org-deadline-string "\\).*")
  3920. org-scheduled-regexp
  3921. (concat "\\<" org-scheduled-string)
  3922. org-scheduled-time-regexp
  3923. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3924. org-closed-time-regexp
  3925. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3926. org-keyword-time-regexp
  3927. (concat "\\<\\(" org-scheduled-string
  3928. "\\|" org-deadline-string
  3929. "\\|" org-closed-string
  3930. "\\|" org-clock-string "\\)"
  3931. " *[[<]\\([^]>]+\\)[]>]")
  3932. org-keyword-time-not-clock-regexp
  3933. (concat "\\<\\(" org-scheduled-string
  3934. "\\|" org-deadline-string
  3935. "\\|" org-closed-string
  3936. "\\)"
  3937. " *[[<]\\([^]>]+\\)[]>]")
  3938. org-maybe-keyword-time-regexp
  3939. (concat "\\(\\<\\(" org-scheduled-string
  3940. "\\|" org-deadline-string
  3941. "\\|" org-closed-string
  3942. "\\|" org-clock-string "\\)\\)?"
  3943. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3944. org-planning-or-clock-line-re
  3945. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3946. "\\|" org-deadline-string
  3947. "\\|" org-closed-string "\\|" org-clock-string
  3948. "\\)\\>\\)")
  3949. org-all-time-keywords
  3950. (mapcar (lambda (w) (substring w 0 -1))
  3951. (list org-scheduled-string org-deadline-string
  3952. org-clock-string org-closed-string))
  3953. )
  3954. (org-compute-latex-and-specials-regexp)
  3955. (org-set-font-lock-defaults))))
  3956. (defun org-file-contents (file &optional noerror)
  3957. "Return the contents of FILE, as a string."
  3958. (if (or (not file)
  3959. (not (file-readable-p file)))
  3960. (if noerror
  3961. (progn
  3962. (message "Cannot read file \"%s\"" file)
  3963. (ding) (sit-for 2)
  3964. "")
  3965. (error "Cannot read file \"%s\"" file))
  3966. (with-temp-buffer
  3967. (insert-file-contents file)
  3968. (buffer-string))))
  3969. (defun org-extract-log-state-settings (x)
  3970. "Extract the log state setting from a TODO keyword string.
  3971. This will extract info from a string like \"WAIT(w@/!)\"."
  3972. (let (kw key log1 log2)
  3973. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3974. (setq kw (match-string 1 x)
  3975. key (and (match-end 2) (match-string 2 x))
  3976. log1 (and (match-end 3) (match-string 3 x))
  3977. log2 (and (match-end 4) (match-string 4 x)))
  3978. (and (or log1 log2)
  3979. (list kw
  3980. (and log1 (if (equal log1 "!") 'time 'note))
  3981. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3982. (defun org-remove-keyword-keys (list)
  3983. "Remove a pair of parenthesis at the end of each string in LIST."
  3984. (mapcar (lambda (x)
  3985. (if (string-match "(.*)$" x)
  3986. (substring x 0 (match-beginning 0))
  3987. x))
  3988. list))
  3989. (defun org-assign-fast-keys (alist)
  3990. "Assign fast keys to a keyword-key alist.
  3991. Respect keys that are already there."
  3992. (let (new e (alt ?0))
  3993. (while (setq e (pop alist))
  3994. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3995. (cdr e)) ;; Key already assigned.
  3996. (push e new)
  3997. (let ((clist (string-to-list (downcase (car e))))
  3998. (used (append new alist)))
  3999. (when (= (car clist) ?@)
  4000. (pop clist))
  4001. (while (and clist (rassoc (car clist) used))
  4002. (pop clist))
  4003. (unless clist
  4004. (while (rassoc alt used)
  4005. (incf alt)))
  4006. (push (cons (car e) (or (car clist) alt)) new))))
  4007. (nreverse new)))
  4008. ;;; Some variables used in various places
  4009. (defvar org-window-configuration nil
  4010. "Used in various places to store a window configuration.")
  4011. (defvar org-selected-window nil
  4012. "Used in various places to store a window configuration.")
  4013. (defvar org-finish-function nil
  4014. "Function to be called when `C-c C-c' is used.
  4015. This is for getting out of special buffers like remember.")
  4016. ;; FIXME: Occasionally check by commenting these, to make sure
  4017. ;; no other functions uses these, forgetting to let-bind them.
  4018. (defvar entry)
  4019. (defvar last-state)
  4020. (defvar date)
  4021. ;; Defined somewhere in this file, but used before definition.
  4022. (defvar org-entities) ;; defined in org-entities.el
  4023. (defvar org-struct-menu)
  4024. (defvar org-org-menu)
  4025. (defvar org-tbl-menu)
  4026. ;;;; Define the Org-mode
  4027. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4028. (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"))
  4029. ;; We use a before-change function to check if a table might need
  4030. ;; an update.
  4031. (defvar org-table-may-need-update t
  4032. "Indicates that a table might need an update.
  4033. This variable is set by `org-before-change-function'.
  4034. `org-table-align' sets it back to nil.")
  4035. (defun org-before-change-function (beg end)
  4036. "Every change indicates that a table might need an update."
  4037. (setq org-table-may-need-update t))
  4038. (defvar org-mode-map)
  4039. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4040. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4041. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4042. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4043. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4044. (defvar org-table-buffer-is-an nil)
  4045. (defconst org-outline-regexp "\\*+ ")
  4046. ;;;###autoload
  4047. (define-derived-mode org-mode outline-mode "Org"
  4048. "Outline-based notes management and organizer, alias
  4049. \"Carsten's outline-mode for keeping track of everything.\"
  4050. Org-mode develops organizational tasks around a NOTES file which
  4051. contains information about projects as plain text. Org-mode is
  4052. implemented on top of outline-mode, which is ideal to keep the content
  4053. of large files well structured. It supports ToDo items, deadlines and
  4054. time stamps, which magically appear in the diary listing of the Emacs
  4055. calendar. Tables are easily created with a built-in table editor.
  4056. Plain text URL-like links connect to websites, emails (VM), Usenet
  4057. messages (Gnus), BBDB entries, and any files related to the project.
  4058. For printing and sharing of notes, an Org-mode file (or a part of it)
  4059. can be exported as a structured ASCII or HTML file.
  4060. The following commands are available:
  4061. \\{org-mode-map}"
  4062. ;; Get rid of Outline menus, they are not needed
  4063. ;; Need to do this here because define-derived-mode sets up
  4064. ;; the keymap so late. Still, it is a waste to call this each time
  4065. ;; we switch another buffer into org-mode.
  4066. (if (featurep 'xemacs)
  4067. (when (boundp 'outline-mode-menu-heading)
  4068. ;; Assume this is Greg's port, it uses easymenu
  4069. (easy-menu-remove outline-mode-menu-heading)
  4070. (easy-menu-remove outline-mode-menu-show)
  4071. (easy-menu-remove outline-mode-menu-hide))
  4072. (define-key org-mode-map [menu-bar headings] 'undefined)
  4073. (define-key org-mode-map [menu-bar hide] 'undefined)
  4074. (define-key org-mode-map [menu-bar show] 'undefined))
  4075. (org-load-modules-maybe)
  4076. (easy-menu-add org-org-menu)
  4077. (easy-menu-add org-tbl-menu)
  4078. (org-install-agenda-files-menu)
  4079. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4080. (add-to-invisibility-spec '(org-cwidth))
  4081. (add-to-invisibility-spec '(org-hide-block . t))
  4082. (when (featurep 'xemacs)
  4083. (org-set-local 'line-move-ignore-invisible t))
  4084. (org-set-local 'outline-regexp org-outline-regexp)
  4085. (org-set-local 'outline-level 'org-outline-level)
  4086. (when (and org-ellipsis
  4087. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4088. (fboundp 'make-glyph-code))
  4089. (unless org-display-table
  4090. (setq org-display-table (make-display-table)))
  4091. (set-display-table-slot
  4092. org-display-table 4
  4093. (vconcat (mapcar
  4094. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4095. org-ellipsis)))
  4096. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4097. (setq buffer-display-table org-display-table))
  4098. (org-set-regexps-and-options)
  4099. (when (and org-tag-faces (not org-tags-special-faces-re))
  4100. ;; tag faces set outside customize.... force initialization.
  4101. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4102. ;; Calc embedded
  4103. (org-set-local 'calc-embedded-open-mode "# ")
  4104. (modify-syntax-entry ?@ "w")
  4105. (if org-startup-truncated (setq truncate-lines t))
  4106. (org-set-local 'font-lock-unfontify-region-function
  4107. 'org-unfontify-region)
  4108. ;; Activate before-change-function
  4109. (org-set-local 'org-table-may-need-update t)
  4110. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4111. 'local)
  4112. ;; Check for running clock before killing a buffer
  4113. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4114. ;; Paragraphs and auto-filling
  4115. (org-set-autofill-regexps)
  4116. (setq indent-line-function 'org-indent-line-function)
  4117. (org-update-radio-target-regexp)
  4118. ;; Beginning/end of defun
  4119. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4120. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4121. ;; Make sure dependence stuff works reliably, even for users who set it
  4122. ;; too late :-(
  4123. (if org-enforce-todo-dependencies
  4124. (add-hook 'org-blocker-hook
  4125. 'org-block-todo-from-children-or-siblings-or-parent)
  4126. (remove-hook 'org-blocker-hook
  4127. 'org-block-todo-from-children-or-siblings-or-parent))
  4128. (if org-enforce-todo-checkbox-dependencies
  4129. (add-hook 'org-blocker-hook
  4130. 'org-block-todo-from-checkboxes)
  4131. (remove-hook 'org-blocker-hook
  4132. 'org-block-todo-from-checkboxes))
  4133. ;; Comment characters
  4134. (org-set-local 'comment-start "#")
  4135. (org-set-local 'comment-padding " ")
  4136. ;; Align options lines
  4137. (org-set-local
  4138. 'align-mode-rules-list
  4139. '((org-in-buffer-settings
  4140. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4141. (modes . '(org-mode)))))
  4142. ;; Imenu
  4143. (org-set-local 'imenu-create-index-function
  4144. 'org-imenu-get-tree)
  4145. ;; Make isearch reveal context
  4146. (if (or (featurep 'xemacs)
  4147. (not (boundp 'outline-isearch-open-invisible-function)))
  4148. ;; Emacs 21 and XEmacs make use of the hook
  4149. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4150. ;; Emacs 22 deals with this through a special variable
  4151. (org-set-local 'outline-isearch-open-invisible-function
  4152. (lambda (&rest ignore) (org-show-context 'isearch))))
  4153. ;; Turn on org-beamer-mode?
  4154. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4155. ;; If empty file that did not turn on org-mode automatically, make it to.
  4156. (if (and org-insert-mode-line-in-empty-file
  4157. (interactive-p)
  4158. (= (point-min) (point-max)))
  4159. (insert "# -*- mode: org -*-\n\n"))
  4160. (unless org-inhibit-startup
  4161. (when org-startup-align-all-tables
  4162. (let ((bmp (buffer-modified-p)))
  4163. (org-table-map-tables 'org-table-align 'quietly)
  4164. (set-buffer-modified-p bmp)))
  4165. (when org-startup-indented
  4166. (require 'org-indent)
  4167. (org-indent-mode 1))
  4168. (unless org-inhibit-startup-visibility-stuff
  4169. (org-set-startup-visibility))))
  4170. (when (fboundp 'abbrev-table-put)
  4171. (abbrev-table-put org-mode-abbrev-table
  4172. :parents (list text-mode-abbrev-table)))
  4173. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4174. (defun org-current-time ()
  4175. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4176. (if (> (car org-time-stamp-rounding-minutes) 1)
  4177. (let ((r (car org-time-stamp-rounding-minutes))
  4178. (time (decode-time)))
  4179. (apply 'encode-time
  4180. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4181. (nthcdr 2 time))))
  4182. (current-time)))
  4183. ;;;; Font-Lock stuff, including the activators
  4184. (defvar org-mouse-map (make-sparse-keymap))
  4185. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4186. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4187. (when org-mouse-1-follows-link
  4188. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4189. (when org-tab-follows-link
  4190. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4191. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4192. (require 'font-lock)
  4193. (defconst org-non-link-chars "]\t\n\r<>")
  4194. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4195. "shell" "elisp" "doi"))
  4196. (defvar org-link-types-re nil
  4197. "Matches a link that has a url-like prefix like \"http:\"")
  4198. (defvar org-link-re-with-space nil
  4199. "Matches a link with spaces, optional angular brackets around it.")
  4200. (defvar org-link-re-with-space2 nil
  4201. "Matches a link with spaces, optional angular brackets around it.")
  4202. (defvar org-link-re-with-space3 nil
  4203. "Matches a link with spaces, only for internal part in bracket links.")
  4204. (defvar org-angle-link-re nil
  4205. "Matches link with angular brackets, spaces are allowed.")
  4206. (defvar org-plain-link-re nil
  4207. "Matches plain link, without spaces.")
  4208. (defvar org-bracket-link-regexp nil
  4209. "Matches a link in double brackets.")
  4210. (defvar org-bracket-link-analytic-regexp nil
  4211. "Regular expression used to analyze links.
  4212. Here is what the match groups contain after a match:
  4213. 1: http:
  4214. 2: http
  4215. 3: path
  4216. 4: [desc]
  4217. 5: desc")
  4218. (defvar org-bracket-link-analytic-regexp++ nil
  4219. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4220. (defvar org-any-link-re nil
  4221. "Regular expression matching any link.")
  4222. (defcustom org-match-sexp-depth 3
  4223. "Number of stacked braces for sub/superscript matching.
  4224. This has to be set before loading org.el to be effective."
  4225. :group 'org-export-translation ; ??????????????????????????/
  4226. :type 'integer)
  4227. (defun org-create-multibrace-regexp (left right n)
  4228. "Create a regular expression which will match a balanced sexp.
  4229. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4230. as single character strings.
  4231. The regexp returned will match the entire expression including the
  4232. delimiters. It will also define a single group which contains the
  4233. match except for the outermost delimiters. The maximum depth of
  4234. stacked delimiters is N. Escaping delimiters is not possible."
  4235. (let* ((nothing (concat "[^" left right "]*?"))
  4236. (or "\\|")
  4237. (re nothing)
  4238. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4239. (while (> n 1)
  4240. (setq n (1- n)
  4241. re (concat re or next)
  4242. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4243. (concat left "\\(" re "\\)" right)))
  4244. (defvar org-match-substring-regexp
  4245. (concat
  4246. "\\([^\\]\\)\\([_^]\\)\\("
  4247. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4248. "\\|"
  4249. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4250. "\\|"
  4251. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4252. "The regular expression matching a sub- or superscript.")
  4253. (defvar org-match-substring-with-braces-regexp
  4254. (concat
  4255. "\\([^\\]\\)\\([_^]\\)\\("
  4256. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4257. "\\)")
  4258. "The regular expression matching a sub- or superscript, forcing braces.")
  4259. (defun org-make-link-regexps ()
  4260. "Update the link regular expressions.
  4261. This should be called after the variable `org-link-types' has changed."
  4262. (setq org-link-types-re
  4263. (concat
  4264. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4265. org-link-re-with-space
  4266. (concat
  4267. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4268. "\\([^" org-non-link-chars " ]"
  4269. "[^" org-non-link-chars "]*"
  4270. "[^" org-non-link-chars " ]\\)>?")
  4271. org-link-re-with-space2
  4272. (concat
  4273. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4274. "\\([^" org-non-link-chars " ]"
  4275. "[^\t\n\r]*"
  4276. "[^" org-non-link-chars " ]\\)>?")
  4277. org-link-re-with-space3
  4278. (concat
  4279. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4280. "\\([^" org-non-link-chars " ]"
  4281. "[^\t\n\r]*\\)")
  4282. org-angle-link-re
  4283. (concat
  4284. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4285. "\\([^" org-non-link-chars " ]"
  4286. "[^" org-non-link-chars "]*"
  4287. "\\)>")
  4288. org-plain-link-re
  4289. (concat
  4290. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4291. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4292. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4293. org-bracket-link-regexp
  4294. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4295. org-bracket-link-analytic-regexp
  4296. (concat
  4297. "\\[\\["
  4298. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4299. "\\([^]]+\\)"
  4300. "\\]"
  4301. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4302. "\\]")
  4303. org-bracket-link-analytic-regexp++
  4304. (concat
  4305. "\\[\\["
  4306. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4307. "\\([^]]+\\)"
  4308. "\\]"
  4309. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4310. "\\]")
  4311. org-any-link-re
  4312. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4313. org-angle-link-re "\\)\\|\\("
  4314. org-plain-link-re "\\)")))
  4315. (org-make-link-regexps)
  4316. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4317. "Regular expression for fast time stamp matching.")
  4318. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4319. "Regular expression for fast time stamp matching.")
  4320. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4321. "Regular expression matching time strings for analysis.
  4322. This one does not require the space after the date, so it can be used
  4323. on a string that terminates immediately after the date.")
  4324. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4325. "Regular expression matching time strings for analysis.")
  4326. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4327. "Regular expression matching time stamps, with groups.")
  4328. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4329. "Regular expression matching time stamps (also [..]), with groups.")
  4330. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4331. "Regular expression matching a time stamp range.")
  4332. (defconst org-tr-regexp-both
  4333. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4334. "Regular expression matching a time stamp range.")
  4335. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4336. org-ts-regexp "\\)?")
  4337. "Regular expression matching a time stamp or time stamp range.")
  4338. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4339. org-ts-regexp-both "\\)?")
  4340. "Regular expression matching a time stamp or time stamp range.
  4341. The time stamps may be either active or inactive.")
  4342. (defvar org-emph-face nil)
  4343. (defun org-do-emphasis-faces (limit)
  4344. "Run through the buffer and add overlays to links."
  4345. (let (rtn a)
  4346. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4347. (if (not (= (char-after (match-beginning 3))
  4348. (char-after (match-beginning 4))))
  4349. (progn
  4350. (setq rtn t)
  4351. (setq a (assoc (match-string 3) org-emphasis-alist))
  4352. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4353. 'face
  4354. (nth 1 a))
  4355. (and (nth 4 a)
  4356. (org-remove-flyspell-overlays-in
  4357. (match-beginning 0) (match-end 0)))
  4358. (add-text-properties (match-beginning 2) (match-end 2)
  4359. '(font-lock-multiline t org-emphasis t))
  4360. (when org-hide-emphasis-markers
  4361. (add-text-properties (match-end 4) (match-beginning 5)
  4362. '(invisible org-link))
  4363. (add-text-properties (match-beginning 3) (match-end 3)
  4364. '(invisible org-link)))))
  4365. (backward-char 1))
  4366. rtn))
  4367. (defun org-emphasize (&optional char)
  4368. "Insert or change an emphasis, i.e. a font like bold or italic.
  4369. If there is an active region, change that region to a new emphasis.
  4370. If there is no region, just insert the marker characters and position
  4371. the cursor between them.
  4372. CHAR should be either the marker character, or the first character of the
  4373. HTML tag associated with that emphasis. If CHAR is a space, the means
  4374. to remove the emphasis of the selected region.
  4375. If char is not given (for example in an interactive call) it
  4376. will be prompted for."
  4377. (interactive)
  4378. (let ((eal org-emphasis-alist) e det
  4379. (erc org-emphasis-regexp-components)
  4380. (prompt "")
  4381. (string "") beg end move tag c s)
  4382. (if (org-region-active-p)
  4383. (setq beg (region-beginning) end (region-end)
  4384. string (buffer-substring beg end))
  4385. (setq move t))
  4386. (while (setq e (pop eal))
  4387. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4388. c (aref tag 0))
  4389. (push (cons c (string-to-char (car e))) det)
  4390. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4391. (substring tag 1)))))
  4392. (setq det (nreverse det))
  4393. (unless char
  4394. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4395. (setq char (read-char-exclusive)))
  4396. (setq char (or (cdr (assoc char det)) char))
  4397. (if (equal char ?\ )
  4398. (setq s "" move nil)
  4399. (unless (assoc (char-to-string char) org-emphasis-alist)
  4400. (error "No such emphasis marker: \"%c\"" char))
  4401. (setq s (char-to-string char)))
  4402. (while (and (> (length string) 1)
  4403. (equal (substring string 0 1) (substring string -1))
  4404. (assoc (substring string 0 1) org-emphasis-alist))
  4405. (setq string (substring string 1 -1)))
  4406. (setq string (concat s string s))
  4407. (if beg (delete-region beg end))
  4408. (unless (or (bolp)
  4409. (string-match (concat "[" (nth 0 erc) "\n]")
  4410. (char-to-string (char-before (point)))))
  4411. (insert " "))
  4412. (unless (or (eobp)
  4413. (string-match (concat "[" (nth 1 erc) "\n]")
  4414. (char-to-string (char-after (point)))))
  4415. (insert " ") (backward-char 1))
  4416. (insert string)
  4417. (and move (backward-char 1))))
  4418. (defconst org-nonsticky-props
  4419. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4420. (defsubst org-rear-nonsticky-at (pos)
  4421. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4422. (defun org-activate-plain-links (limit)
  4423. "Run through the buffer and add overlays to links."
  4424. (catch 'exit
  4425. (let (f)
  4426. (if (re-search-forward org-plain-link-re limit t)
  4427. (progn
  4428. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4429. (setq f (get-text-property (match-beginning 0) 'face))
  4430. (if (or (eq f 'org-tag)
  4431. (and (listp f) (memq 'org-tag f)))
  4432. nil
  4433. (add-text-properties (match-beginning 0) (match-end 0)
  4434. (list 'mouse-face 'highlight
  4435. 'face 'org-link
  4436. 'keymap org-mouse-map))
  4437. (org-rear-nonsticky-at (match-end 0)))
  4438. t)))))
  4439. (defun org-activate-code (limit)
  4440. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4441. (progn
  4442. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4443. (remove-text-properties (match-beginning 0) (match-end 0)
  4444. '(display t invisible t intangible t))
  4445. t)))
  4446. (defun org-fontify-meta-lines-and-blocks (limit)
  4447. "Fontify #+ lines and blocks, in the correct ways."
  4448. (let ((case-fold-search t))
  4449. (if (re-search-forward
  4450. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4451. limit t)
  4452. (let ((beg (match-beginning 0))
  4453. (beg1 (line-beginning-position 2))
  4454. (dc1 (downcase (match-string 2)))
  4455. (dc3 (downcase (match-string 3)))
  4456. end end1 quoting block-type)
  4457. (cond
  4458. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4459. ;; a single line of backend-specific content
  4460. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4461. (remove-text-properties (match-beginning 0) (match-end 0)
  4462. '(display t invisible t intangible t))
  4463. (add-text-properties (match-beginning 1) (match-end 3)
  4464. '(font-lock-fontified t face org-meta-line))
  4465. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4466. '(font-lock-fontified t face org-block))
  4467. ; for backend-specific code
  4468. t)
  4469. ((and (match-end 4) (equal dc3 "begin"))
  4470. ;; Truly a block
  4471. (setq block-type (downcase (match-string 5))
  4472. quoting (member block-type org-protecting-blocks))
  4473. (when (re-search-forward
  4474. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4475. nil t) ;; on purpose, we look further than LIMIT
  4476. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4477. (when quoting
  4478. (remove-text-properties beg end
  4479. '(display t invisible t intangible t)))
  4480. (add-text-properties
  4481. beg end
  4482. '(font-lock-fontified t font-lock-multiline t))
  4483. (add-text-properties beg beg1 '(face org-meta-line))
  4484. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4485. ; for end_src
  4486. (cond
  4487. (quoting
  4488. (add-text-properties beg1 (+ end1 1) '(face
  4489. org-block)))
  4490. ; end of source block
  4491. ((not org-fontify-quote-and-verse-blocks))
  4492. ((string= block-type "quote")
  4493. (add-text-properties beg1 end1 '(face org-quote)))
  4494. ((string= block-type "verse")
  4495. (add-text-properties beg1 end1 '(face org-verse))))
  4496. t))
  4497. ((member dc1 '("title:" "author:" "email:" "date:"))
  4498. (add-text-properties
  4499. beg (match-end 3)
  4500. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4501. '(font-lock-fontified t invisible t)
  4502. '(font-lock-fontified t face org-document-info-keyword)))
  4503. (add-text-properties
  4504. (match-beginning 6) (match-end 6)
  4505. (if (string-equal dc1 "title:")
  4506. '(font-lock-fontified t face org-document-title)
  4507. '(font-lock-fontified t face org-document-info))))
  4508. ((not (member (char-after beg) '(?\ ?\t)))
  4509. ;; just any other in-buffer setting, but not indented
  4510. (add-text-properties
  4511. beg (match-end 0)
  4512. '(font-lock-fontified t face org-meta-line))
  4513. t)
  4514. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4515. "orgtbl:" "tblfm:" "tblname:" "result:"
  4516. "results:" "source:" "srcname:" "call:"))
  4517. (and (match-end 4) (equal dc3 "attr")))
  4518. (add-text-properties
  4519. beg (match-end 0)
  4520. '(font-lock-fontified t face org-meta-line))
  4521. t)
  4522. ((member dc3 '(" " ""))
  4523. (add-text-properties
  4524. beg (match-end 0)
  4525. '(font-lock-fontified t face font-lock-comment-face)))
  4526. (t nil))))))
  4527. (defun org-activate-angle-links (limit)
  4528. "Run through the buffer and add overlays to links."
  4529. (if (re-search-forward org-angle-link-re limit t)
  4530. (progn
  4531. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4532. (add-text-properties (match-beginning 0) (match-end 0)
  4533. (list 'mouse-face 'highlight
  4534. 'keymap org-mouse-map))
  4535. (org-rear-nonsticky-at (match-end 0))
  4536. t)))
  4537. (defun org-activate-footnote-links (limit)
  4538. "Run through the buffer and add overlays to links."
  4539. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4540. limit t)
  4541. (progn
  4542. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4543. (add-text-properties (match-beginning 2) (match-end 2)
  4544. (list 'mouse-face 'highlight
  4545. 'keymap org-mouse-map
  4546. 'help-echo
  4547. (if (= (point-at-bol) (match-beginning 2))
  4548. "Footnote definition"
  4549. "Footnote reference")
  4550. ))
  4551. (org-rear-nonsticky-at (match-end 2))
  4552. t)))
  4553. (defun org-activate-bracket-links (limit)
  4554. "Run through the buffer and add overlays to bracketed links."
  4555. (if (re-search-forward org-bracket-link-regexp limit t)
  4556. (let* ((help (concat "LINK: "
  4557. (org-match-string-no-properties 1)))
  4558. ;; FIXME: above we should remove the escapes.
  4559. ;; but that requires another match, protecting match data,
  4560. ;; a lot of overhead for font-lock.
  4561. (ip (org-maybe-intangible
  4562. (list 'invisible 'org-link
  4563. 'keymap org-mouse-map 'mouse-face 'highlight
  4564. 'font-lock-multiline t 'help-echo help)))
  4565. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4566. 'font-lock-multiline t 'help-echo help)))
  4567. ;; We need to remove the invisible property here. Table narrowing
  4568. ;; may have made some of this invisible.
  4569. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4570. (remove-text-properties (match-beginning 0) (match-end 0)
  4571. '(invisible nil))
  4572. (if (match-end 3)
  4573. (progn
  4574. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4575. (org-rear-nonsticky-at (match-beginning 3))
  4576. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4577. (org-rear-nonsticky-at (match-end 3))
  4578. (add-text-properties (match-end 3) (match-end 0) ip)
  4579. (org-rear-nonsticky-at (match-end 0)))
  4580. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4581. (org-rear-nonsticky-at (match-beginning 1))
  4582. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4583. (org-rear-nonsticky-at (match-end 1))
  4584. (add-text-properties (match-end 1) (match-end 0) ip)
  4585. (org-rear-nonsticky-at (match-end 0)))
  4586. t)))
  4587. (defun org-activate-dates (limit)
  4588. "Run through the buffer and add overlays to dates."
  4589. (if (re-search-forward org-tsr-regexp-both limit t)
  4590. (progn
  4591. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4592. (add-text-properties (match-beginning 0) (match-end 0)
  4593. (list 'mouse-face 'highlight
  4594. 'keymap org-mouse-map))
  4595. (org-rear-nonsticky-at (match-end 0))
  4596. (when org-display-custom-times
  4597. (if (match-end 3)
  4598. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4599. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4600. t)))
  4601. (defvar org-target-link-regexp nil
  4602. "Regular expression matching radio targets in plain text.")
  4603. (make-variable-buffer-local 'org-target-link-regexp)
  4604. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4605. "Regular expression matching a link target.")
  4606. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4607. "Regular expression matching a radio target.")
  4608. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4609. "Regular expression matching any target.")
  4610. (defun org-activate-target-links (limit)
  4611. "Run through the buffer and add overlays to target matches."
  4612. (when org-target-link-regexp
  4613. (let ((case-fold-search t))
  4614. (if (re-search-forward org-target-link-regexp limit t)
  4615. (progn
  4616. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4617. (add-text-properties (match-beginning 0) (match-end 0)
  4618. (list 'mouse-face 'highlight
  4619. 'keymap org-mouse-map
  4620. 'help-echo "Radio target link"
  4621. 'org-linked-text t))
  4622. (org-rear-nonsticky-at (match-end 0))
  4623. t)))))
  4624. (defun org-update-radio-target-regexp ()
  4625. "Find all radio targets in this file and update the regular expression."
  4626. (interactive)
  4627. (when (memq 'radio org-activate-links)
  4628. (setq org-target-link-regexp
  4629. (org-make-target-link-regexp (org-all-targets 'radio)))
  4630. (org-restart-font-lock)))
  4631. (defun org-hide-wide-columns (limit)
  4632. (let (s e)
  4633. (setq s (text-property-any (point) (or limit (point-max))
  4634. 'org-cwidth t))
  4635. (when s
  4636. (setq e (next-single-property-change s 'org-cwidth))
  4637. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4638. (goto-char e)
  4639. t)))
  4640. (defvar org-latex-and-specials-regexp nil
  4641. "Regular expression for highlighting export special stuff.")
  4642. (defvar org-match-substring-regexp)
  4643. (defvar org-match-substring-with-braces-regexp)
  4644. ;; This should be with the exporter code, but we also use if for font-locking
  4645. (defconst org-export-html-special-string-regexps
  4646. '(("\\\\-" . "&shy;")
  4647. ("---\\([^-]\\)" . "&mdash;\\1")
  4648. ("--\\([^-]\\)" . "&ndash;\\1")
  4649. ("\\.\\.\\." . "&hellip;"))
  4650. "Regular expressions for special string conversion.")
  4651. (defun org-compute-latex-and-specials-regexp ()
  4652. "Compute regular expression for stuff treated specially by exporters."
  4653. (if (not org-highlight-latex-fragments-and-specials)
  4654. (org-set-local 'org-latex-and-specials-regexp nil)
  4655. (require 'org-exp)
  4656. (let*
  4657. ((matchers (plist-get org-format-latex-options :matchers))
  4658. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4659. org-latex-regexps)))
  4660. (org-export-allow-BIND nil)
  4661. (options (org-combine-plists (org-default-export-plist)
  4662. (org-infile-export-plist)))
  4663. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4664. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4665. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4666. (org-export-html-expand (plist-get options :expand-quoted-html))
  4667. (org-export-with-special-strings (plist-get options :special-strings))
  4668. (re-sub
  4669. (cond
  4670. ((equal org-export-with-sub-superscripts '{})
  4671. (list org-match-substring-with-braces-regexp))
  4672. (org-export-with-sub-superscripts
  4673. (list org-match-substring-regexp))
  4674. (t nil)))
  4675. (re-latex
  4676. (if org-export-with-LaTeX-fragments
  4677. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4678. (re-macros
  4679. (if org-export-with-TeX-macros
  4680. (list (concat "\\\\"
  4681. (regexp-opt
  4682. (append
  4683. (delq nil
  4684. (mapcar 'car-safe
  4685. (append org-entities-user
  4686. org-entities)))
  4687. (if (boundp 'org-latex-entities)
  4688. (mapcar (lambda (x)
  4689. (or (car-safe x) x))
  4690. org-latex-entities)
  4691. nil))
  4692. 'words))) ; FIXME
  4693. ))
  4694. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4695. (re-special (if org-export-with-special-strings
  4696. (mapcar (lambda (x) (car x))
  4697. org-export-html-special-string-regexps)))
  4698. (re-rest
  4699. (delq nil
  4700. (list
  4701. (if org-export-html-expand "@<[^>\n]+>")
  4702. ))))
  4703. (org-set-local
  4704. 'org-latex-and-specials-regexp
  4705. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4706. re-rest) "\\|")))))
  4707. (defun org-do-latex-and-special-faces (limit)
  4708. "Run through the buffer and add overlays to links."
  4709. (when org-latex-and-specials-regexp
  4710. (let (rtn d)
  4711. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4712. limit t))
  4713. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4714. 'face))
  4715. '(org-code org-verbatim underline)))
  4716. (progn
  4717. (setq rtn t
  4718. d (cond ((member (char-after (1+ (match-beginning 0)))
  4719. '(?_ ?^)) 1)
  4720. (t 0)))
  4721. (font-lock-prepend-text-property
  4722. (+ d (match-beginning 0)) (match-end 0)
  4723. 'face 'org-latex-and-export-specials)
  4724. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4725. '(font-lock-multiline t)))))
  4726. rtn)))
  4727. (defun org-restart-font-lock ()
  4728. "Restart `font-lock-mode', to force refontification."
  4729. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4730. (font-lock-mode -1)
  4731. (font-lock-mode 1)))
  4732. (defun org-all-targets (&optional radio)
  4733. "Return a list of all targets in this file.
  4734. With optional argument RADIO, only find radio targets."
  4735. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4736. rtn)
  4737. (save-excursion
  4738. (goto-char (point-min))
  4739. (while (re-search-forward re nil t)
  4740. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4741. rtn)))
  4742. (defun org-make-target-link-regexp (targets)
  4743. "Make regular expression matching all strings in TARGETS.
  4744. The regular expression finds the targets also if there is a line break
  4745. between words."
  4746. (and targets
  4747. (concat
  4748. "\\<\\("
  4749. (mapconcat
  4750. (lambda (x)
  4751. (while (string-match " +" x)
  4752. (setq x (replace-match "\\s-+" t t x)))
  4753. x)
  4754. targets
  4755. "\\|")
  4756. "\\)\\>")))
  4757. (defun org-activate-tags (limit)
  4758. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4759. (progn
  4760. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4761. (add-text-properties (match-beginning 1) (match-end 1)
  4762. (list 'mouse-face 'highlight
  4763. 'keymap org-mouse-map))
  4764. (org-rear-nonsticky-at (match-end 1))
  4765. t)))
  4766. (defun org-outline-level ()
  4767. "Compute the outline level of the heading at point.
  4768. This function assumes that the cursor is at the beginning of a line matched
  4769. by `outline-regexp'. Otherwise it returns garbage.
  4770. If this is called at a normal headline, the level is the number of stars.
  4771. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4772. For plain list items, if they are matched by `outline-regexp', this returns
  4773. 1000 plus the line indentation."
  4774. (save-excursion
  4775. (looking-at outline-regexp)
  4776. (if (match-beginning 1)
  4777. (+ (org-get-string-indentation (match-string 1)) 1000)
  4778. (1- (- (match-end 0) (match-beginning 0))))))
  4779. (defvar org-font-lock-keywords nil)
  4780. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4781. "Regular expression matching a property line.")
  4782. (defvar org-font-lock-hook nil
  4783. "Functions to be called for special font lock stuff.")
  4784. (defvar org-font-lock-set-keywords-hook nil
  4785. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4786. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4787. it is installed to be used by font lock. This can be useful if something
  4788. needs to be inserted at a specific position in the font-lock sequence.")
  4789. (defun org-font-lock-hook (limit)
  4790. (run-hook-with-args 'org-font-lock-hook limit))
  4791. (defun org-set-font-lock-defaults ()
  4792. (let* ((em org-fontify-emphasized-text)
  4793. (lk org-activate-links)
  4794. (org-font-lock-extra-keywords
  4795. (list
  4796. ;; Call the hook
  4797. '(org-font-lock-hook)
  4798. ;; Headlines
  4799. `(,(if org-fontify-whole-heading-line
  4800. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4801. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4802. (1 (org-get-level-face 1))
  4803. (2 (org-get-level-face 2))
  4804. (3 (org-get-level-face 3)))
  4805. ;; Table lines
  4806. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4807. (1 'org-table t))
  4808. ;; Table internals
  4809. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4810. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4811. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4812. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4813. ;; Drawers
  4814. (list org-drawer-regexp '(0 'org-special-keyword t))
  4815. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4816. ;; Properties
  4817. (list org-property-re
  4818. '(1 'org-special-keyword t)
  4819. '(3 'org-property-value t))
  4820. ;; Links
  4821. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4822. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4823. (if (memq 'plain lk) '(org-activate-plain-links))
  4824. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4825. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4826. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4827. (if (memq 'footnote lk) '(org-activate-footnote-links
  4828. (2 'org-footnote t)))
  4829. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4830. '(org-hide-wide-columns (0 nil append))
  4831. ;; TODO lines
  4832. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4833. '(1 (org-get-todo-face 1) t))
  4834. ;; DONE
  4835. (if org-fontify-done-headline
  4836. (list (concat "^[*]+ +\\<\\("
  4837. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4838. "\\)\\(.*\\)")
  4839. '(2 'org-headline-done t))
  4840. nil)
  4841. ;; Priorities
  4842. '(org-font-lock-add-priority-faces)
  4843. ;; Tags
  4844. '(org-font-lock-add-tag-faces)
  4845. ;; Special keywords
  4846. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4847. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4848. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4849. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4850. ;; Emphasis
  4851. (if em
  4852. (if (featurep 'xemacs)
  4853. '(org-do-emphasis-faces (0 nil append))
  4854. '(org-do-emphasis-faces)))
  4855. ;; Checkboxes
  4856. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4857. 2 'org-checkbox prepend)
  4858. (if org-provide-checkbox-statistics
  4859. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4860. (0 (org-get-checkbox-statistics-face) t)))
  4861. ;; Description list items
  4862. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4863. 2 'bold prepend)
  4864. ;; ARCHIVEd headings
  4865. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4866. '(1 'org-archived prepend))
  4867. ;; Specials
  4868. '(org-do-latex-and-special-faces)
  4869. '(org-fontify-entities)
  4870. '(org-raise-scripts)
  4871. ;; Code
  4872. '(org-activate-code (1 'org-code t))
  4873. ;; COMMENT
  4874. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4875. "\\|" org-quote-string "\\)\\>")
  4876. '(1 'org-special-keyword t))
  4877. '("^#.*" (0 'font-lock-comment-face t))
  4878. ;; Blocks and meta lines
  4879. '(org-fontify-meta-lines-and-blocks)
  4880. )))
  4881. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4882. (run-hooks 'org-font-lock-set-keywords-hook)
  4883. ;; Now set the full font-lock-keywords
  4884. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4885. (org-set-local 'font-lock-defaults
  4886. '(org-font-lock-keywords t nil nil backward-paragraph))
  4887. (kill-local-variable 'font-lock-keywords) nil))
  4888. (defun org-toggle-pretty-entities ()
  4889. "Toggle the composition display of entities as UTF8 characters."
  4890. (interactive)
  4891. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4892. (org-restart-font-lock)
  4893. (if org-pretty-entities
  4894. (message "Entities are displayed as UTF8 characers")
  4895. (save-restriction
  4896. (widen)
  4897. (decompose-region (point-min) (point-max))
  4898. (message "Entities are displayed plain"))))
  4899. (defun org-fontify-entities (limit)
  4900. "Find an entity to fontify."
  4901. (let (ee)
  4902. (when org-pretty-entities
  4903. (catch 'match
  4904. (while (re-search-forward
  4905. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  4906. limit t)
  4907. (if (and (not (org-in-indented-comment-line))
  4908. (setq ee (org-entity-get (match-string 1)))
  4909. (= (length (nth 6 ee)) 1))
  4910. (progn
  4911. (add-text-properties
  4912. (match-beginning 0) (match-end 1)
  4913. (list 'font-lock-fontified t))
  4914. (compose-region (match-beginning 0) (match-end 1)
  4915. (nth 6 ee) nil)
  4916. (backward-char 1)
  4917. (throw 'match t))))
  4918. nil))))
  4919. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4920. "Fontify string S like in Org-mode."
  4921. (with-temp-buffer
  4922. (insert s)
  4923. (let ((org-odd-levels-only odd-levels))
  4924. (org-mode)
  4925. (font-lock-fontify-buffer)
  4926. (buffer-string))))
  4927. (defvar org-m nil)
  4928. (defvar org-l nil)
  4929. (defvar org-f nil)
  4930. (defun org-get-level-face (n)
  4931. "Get the right face for match N in font-lock matching of headlines."
  4932. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4933. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4934. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4935. (cond
  4936. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4937. ((eq n 2) org-f)
  4938. (t (if org-level-color-stars-only nil org-f))))
  4939. (defun org-get-todo-face (kwd)
  4940. "Get the right face for a TODO keyword KWD.
  4941. If KWD is a number, get the corresponding match group."
  4942. (if (numberp kwd) (setq kwd (match-string kwd)))
  4943. (or (org-face-from-face-or-color
  4944. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4945. (and (member kwd org-done-keywords) 'org-done)
  4946. 'org-todo))
  4947. (defun org-face-from-face-or-color (context inherit face-or-color)
  4948. "Create a face list that inherits INHERIT, but sets the foreground color.
  4949. When FACE-OR-COLOR is not a string, just return it."
  4950. (if (stringp face-or-color)
  4951. (list :inherit inherit
  4952. (cdr (assoc context org-faces-easy-properties))
  4953. face-or-color)
  4954. face-or-color))
  4955. (defun org-font-lock-add-tag-faces (limit)
  4956. "Add the special tag faces."
  4957. (when (and org-tag-faces org-tags-special-faces-re)
  4958. (while (re-search-forward org-tags-special-faces-re limit t)
  4959. (add-text-properties (match-beginning 1) (match-end 1)
  4960. (list 'face (org-get-tag-face 1)
  4961. 'font-lock-fontified t))
  4962. (backward-char 1))))
  4963. (defun org-font-lock-add-priority-faces (limit)
  4964. "Add the special priority faces."
  4965. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4966. (add-text-properties
  4967. (match-beginning 0) (match-end 0)
  4968. (list 'face (or (org-face-from-face-or-color
  4969. 'priority 'org-special-keyword
  4970. (cdr (assoc (char-after (match-beginning 1))
  4971. org-priority-faces)))
  4972. 'org-special-keyword)
  4973. 'font-lock-fontified t))))
  4974. (defun org-get-tag-face (kwd)
  4975. "Get the right face for a TODO keyword KWD.
  4976. If KWD is a number, get the corresponding match group."
  4977. (if (numberp kwd) (setq kwd (match-string kwd)))
  4978. (or (org-face-from-face-or-color
  4979. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4980. 'org-tag))
  4981. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4982. "Remove fontification and activation overlays from links."
  4983. (font-lock-default-unfontify-region beg end)
  4984. (let* ((buffer-undo-list t)
  4985. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4986. (inhibit-modification-hooks t)
  4987. deactivate-mark buffer-file-name buffer-file-truename)
  4988. (decompose-region beg end)
  4989. (remove-text-properties
  4990. beg end
  4991. (if org-indent-mode
  4992. ;; also remove line-prefix and wrap-prefix properties
  4993. '(mouse-face t keymap t org-linked-text t
  4994. invisible t intangible t
  4995. line-prefix t wrap-prefix t
  4996. org-no-flyspell t org-emphasis t)
  4997. '(mouse-face t keymap t org-linked-text t
  4998. invisible t intangible t
  4999. org-no-flyspell t org-emphasis t)))
  5000. (org-remove-font-lock-display-properties beg end)))
  5001. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5002. ((raise 0.3) (height 0.7))
  5003. ((raise -0.5))
  5004. ((raise 0.5)))
  5005. "Display properties for showing superscripts and subscripts.")
  5006. (defun org-remove-font-lock-display-properties (beg end)
  5007. "Remove specific display properties that have been added by font lock.
  5008. The will remove the raise properties that are used to show superscripts
  5009. and subscripts."
  5010. (let (next prop)
  5011. (while (< beg end)
  5012. (setq next (next-single-property-change beg 'display nil end)
  5013. prop (get-text-property beg 'display))
  5014. (if (member prop org-script-display)
  5015. (put-text-property beg next 'display nil))
  5016. (setq beg next))))
  5017. (defun org-raise-scripts (limit)
  5018. "Add raise properties to sub/superscripts."
  5019. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5020. (if (re-search-forward
  5021. (if (eq org-use-sub-superscripts t)
  5022. org-match-substring-regexp
  5023. org-match-substring-with-braces-regexp)
  5024. limit t)
  5025. (let* ((pos (point)) table-p comment-p
  5026. (mpos (match-beginning 3))
  5027. (emph-p (get-text-property mpos 'org-emphasis))
  5028. (link-p (get-text-property mpos 'mouse-face))
  5029. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5030. (goto-char (point-at-bol))
  5031. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5032. comment-p (org-looking-at-p "[ \t]*#"))
  5033. (goto-char pos)
  5034. ;; FIXME: Should we go back one character here, for a_b^c
  5035. ;; (goto-char (1- pos)) ;????????????????????
  5036. (if (or comment-p emph-p link-p keyw-p)
  5037. t
  5038. (put-text-property (match-beginning 3) (match-end 0)
  5039. 'display
  5040. (if (equal (char-after (match-beginning 2)) ?^)
  5041. (nth (if table-p 3 1) org-script-display)
  5042. (nth (if table-p 2 0) org-script-display)))
  5043. (add-text-properties (match-beginning 2) (match-end 2)
  5044. (list 'invisible t
  5045. 'org-dwidth t 'org-dwidth-n 1))
  5046. (if (and (eq (char-after (match-beginning 3)) ?{)
  5047. (eq (char-before (match-end 3)) ?}))
  5048. (progn
  5049. (add-text-properties
  5050. (match-beginning 3) (1+ (match-beginning 3))
  5051. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5052. (add-text-properties
  5053. (1- (match-end 3)) (match-end 3)
  5054. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5055. t)))))
  5056. ;;;; Visibility cycling, including org-goto and indirect buffer
  5057. ;;; Cycling
  5058. (defvar org-cycle-global-status nil)
  5059. (make-variable-buffer-local 'org-cycle-global-status)
  5060. (defvar org-cycle-subtree-status nil)
  5061. (make-variable-buffer-local 'org-cycle-subtree-status)
  5062. ;;;###autoload
  5063. (defvar org-inlinetask-min-level)
  5064. (defun org-cycle (&optional arg)
  5065. "TAB-action and visibility cycling for Org-mode.
  5066. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5067. is outline visibility cycling, but it also invokes other actions
  5068. in special contexts.
  5069. - When this function is called with a prefix argument, rotate the entire
  5070. buffer through 3 states (global cycling)
  5071. 1. OVERVIEW: Show only top-level headlines.
  5072. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5073. 3. SHOW ALL: Show everything.
  5074. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5075. determined by the variable `org-startup-folded', and by any VISIBILITY
  5076. properties in the buffer.
  5077. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5078. including any drawers.
  5079. - When inside a table, re-align the table and move to the next field.
  5080. - When point is at the beginning of a headline, rotate the subtree started
  5081. by this line through 3 different states (local cycling)
  5082. 1. FOLDED: Only the main headline is shown.
  5083. 2. CHILDREN: The main headline and the direct children are shown.
  5084. From this state, you can move to one of the children
  5085. and zoom in further.
  5086. 3. SUBTREE: Show the entire subtree, including body text.
  5087. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5088. - When point is at the beginning of an empty headline and the variable
  5089. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5090. of the headline by demoting and promoting it to likely levels. This
  5091. speeds up creation document structure by pressing TAB once or several
  5092. times right after creating a new headline.
  5093. - When there is a numeric prefix, go up to a heading with level ARG, do
  5094. a `show-subtree' and return to the previous cursor position. If ARG
  5095. is negative, go up that many levels.
  5096. - When point is not at the beginning of a headline, execute the global
  5097. binding for TAB, which is re-indenting the line. See the option
  5098. `org-cycle-emulate-tab' for details.
  5099. - Special case: if point is at the beginning of the buffer and there is
  5100. no headline in line 1, this function will act as if called with prefix arg.
  5101. But only if also the variable `org-cycle-global-at-bob' is t."
  5102. (interactive "P")
  5103. (org-load-modules-maybe)
  5104. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5105. (and org-cycle-level-after-item/entry-creation
  5106. (or (org-cycle-level)
  5107. (org-cycle-item-indentation))))
  5108. (let* ((limit-level
  5109. (or org-cycle-max-level
  5110. (and (boundp 'org-inlinetask-min-level)
  5111. org-inlinetask-min-level
  5112. (1- org-inlinetask-min-level))))
  5113. (nstars (and limit-level
  5114. (if org-odd-levels-only
  5115. (and limit-level (1- (* limit-level 2)))
  5116. limit-level)))
  5117. (outline-regexp
  5118. (cond
  5119. ((not (org-mode-p)) outline-regexp)
  5120. ((or (eq org-cycle-include-plain-lists 'integrate)
  5121. (and org-cycle-include-plain-lists (org-at-item-p)))
  5122. (concat "\\(?:\\*"
  5123. (if nstars (format "\\{1,%d\\}" nstars) "+")
  5124. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  5125. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  5126. (bob-special (and org-cycle-global-at-bob (bobp)
  5127. (not (looking-at outline-regexp))))
  5128. (org-cycle-hook
  5129. (if bob-special
  5130. (delq 'org-optimize-window-after-visibility-change
  5131. (copy-sequence org-cycle-hook))
  5132. org-cycle-hook))
  5133. (pos (point)))
  5134. (if (or bob-special (equal arg '(4)))
  5135. ;; special case: use global cycling
  5136. (setq arg t))
  5137. (cond
  5138. ((equal arg '(16))
  5139. (org-set-startup-visibility)
  5140. (message "Startup visibility, plus VISIBILITY properties"))
  5141. ((equal arg '(64))
  5142. (show-all)
  5143. (message "Entire buffer visible, including drawers"))
  5144. ((org-at-table-p 'any)
  5145. ;; Enter the table or move to the next field in the table
  5146. (if (org-at-table.el-p)
  5147. (message "Use C-c ' to edit table.el tables")
  5148. (if arg (org-table-edit-field t)
  5149. (org-table-justify-field-maybe)
  5150. (call-interactively 'org-table-next-field))))
  5151. ((run-hook-with-args-until-success
  5152. 'org-tab-after-check-for-table-hook))
  5153. ((eq arg t) ;; Global cycling
  5154. (org-cycle-internal-global))
  5155. ((and org-drawers org-drawer-regexp
  5156. (save-excursion
  5157. (beginning-of-line 1)
  5158. (looking-at org-drawer-regexp)))
  5159. ;; Toggle block visibility
  5160. (org-flag-drawer
  5161. (not (get-char-property (match-end 0) 'invisible))))
  5162. ((integerp arg)
  5163. ;; Show-subtree, ARG levels up from here.
  5164. (save-excursion
  5165. (org-back-to-heading)
  5166. (outline-up-heading (if (< arg 0) (- arg)
  5167. (- (funcall outline-level) arg)))
  5168. (org-show-subtree)))
  5169. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5170. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5171. (org-cycle-internal-local))
  5172. ;; TAB emulation and template completion
  5173. (buffer-read-only (org-back-to-heading))
  5174. ((run-hook-with-args-until-success
  5175. 'org-tab-after-check-for-cycling-hook))
  5176. ((org-try-structure-completion))
  5177. ((org-try-cdlatex-tab))
  5178. ((run-hook-with-args-until-success
  5179. 'org-tab-before-tab-emulation-hook))
  5180. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5181. (or (not (bolp))
  5182. (not (looking-at outline-regexp))))
  5183. (call-interactively (global-key-binding "\t")))
  5184. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5185. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5186. (or (and (eq org-cycle-emulate-tab 'white)
  5187. (= (match-end 0) (point-at-eol)))
  5188. (and (eq org-cycle-emulate-tab 'whitestart)
  5189. (>= (match-end 0) pos))))
  5190. t
  5191. (eq org-cycle-emulate-tab t))
  5192. (call-interactively (global-key-binding "\t")))
  5193. (t (save-excursion
  5194. (org-back-to-heading)
  5195. (org-cycle)))))))
  5196. (defun org-cycle-internal-global ()
  5197. "Do the global cycling action."
  5198. (cond
  5199. ((and (eq last-command this-command)
  5200. (eq org-cycle-global-status 'overview))
  5201. ;; We just created the overview - now do table of contents
  5202. ;; This can be slow in very large buffers, so indicate action
  5203. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5204. (message "CONTENTS...")
  5205. (org-content)
  5206. (message "CONTENTS...done")
  5207. (setq org-cycle-global-status 'contents)
  5208. (run-hook-with-args 'org-cycle-hook 'contents))
  5209. ((and (eq last-command this-command)
  5210. (eq org-cycle-global-status 'contents))
  5211. ;; We just showed the table of contents - now show everything
  5212. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5213. (show-all)
  5214. (message "SHOW ALL")
  5215. (setq org-cycle-global-status 'all)
  5216. (run-hook-with-args 'org-cycle-hook 'all))
  5217. (t
  5218. ;; Default action: go to overview
  5219. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5220. (org-overview)
  5221. (message "OVERVIEW")
  5222. (setq org-cycle-global-status 'overview)
  5223. (run-hook-with-args 'org-cycle-hook 'overview))))
  5224. (defun org-cycle-internal-local ()
  5225. "Do the local cycling action."
  5226. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  5227. ;; First, some boundaries
  5228. (save-excursion
  5229. (org-back-to-heading)
  5230. (setq level (funcall outline-level))
  5231. (save-excursion
  5232. (beginning-of-line 2)
  5233. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  5234. ; XEmacs does not have `next-single-char-property-change'
  5235. ; I'm not sure about Emacs 21.
  5236. (while (and (not (eobp)) ;; this is like `next-line'
  5237. (get-char-property (1- (point)) 'invisible))
  5238. (beginning-of-line 2))
  5239. (while (and (not (eobp)) ;; this is like `next-line'
  5240. (get-char-property (1- (point)) 'invisible))
  5241. (goto-char (next-single-char-property-change (point) 'invisible))
  5242. (and (eolp) (beginning-of-line 2))))
  5243. (setq eol (point)))
  5244. (outline-end-of-heading) (setq eoh (point))
  5245. (save-excursion
  5246. (outline-next-heading)
  5247. (setq has-children (and (org-at-heading-p t)
  5248. (> (funcall outline-level) level))))
  5249. (org-end-of-subtree t)
  5250. (unless (eobp)
  5251. (skip-chars-forward " \t\n")
  5252. (beginning-of-line 1) ; in case this is an item
  5253. )
  5254. (setq eos (if (eobp) (point) (1- (point)))))
  5255. ;; Find out what to do next and set `this-command'
  5256. (cond
  5257. ((= eos eoh)
  5258. ;; Nothing is hidden behind this heading
  5259. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5260. (message "EMPTY ENTRY")
  5261. (setq org-cycle-subtree-status nil)
  5262. (save-excursion
  5263. (goto-char eos)
  5264. (outline-next-heading)
  5265. (if (org-invisible-p) (org-flag-heading nil))))
  5266. ((and (or (>= eol eos)
  5267. (not (string-match "\\S-" (buffer-substring eol eos))))
  5268. (or has-children
  5269. (not (setq children-skipped
  5270. org-cycle-skip-children-state-if-no-children))))
  5271. ;; Entire subtree is hidden in one line: children view
  5272. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5273. (org-show-entry)
  5274. (show-children)
  5275. (message "CHILDREN")
  5276. (save-excursion
  5277. (goto-char eos)
  5278. (outline-next-heading)
  5279. (if (org-invisible-p) (org-flag-heading nil)))
  5280. (setq org-cycle-subtree-status 'children)
  5281. (run-hook-with-args 'org-cycle-hook 'children))
  5282. ((or children-skipped
  5283. (and (eq last-command this-command)
  5284. (eq org-cycle-subtree-status 'children)))
  5285. ;; We just showed the children, or no children are there,
  5286. ;; now show everything.
  5287. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5288. (org-show-subtree)
  5289. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5290. (setq org-cycle-subtree-status 'subtree)
  5291. (run-hook-with-args 'org-cycle-hook 'subtree))
  5292. (t
  5293. ;; Default action: hide the subtree.
  5294. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5295. (hide-subtree)
  5296. (message "FOLDED")
  5297. (setq org-cycle-subtree-status 'folded)
  5298. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5299. ;;;###autoload
  5300. (defun org-global-cycle (&optional arg)
  5301. "Cycle the global visibility. For details see `org-cycle'.
  5302. With \\[universal-argument] prefix arg, switch to startup visibility.
  5303. With a numeric prefix, show all headlines up to that level."
  5304. (interactive "P")
  5305. (let ((org-cycle-include-plain-lists
  5306. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5307. (cond
  5308. ((integerp arg)
  5309. (show-all)
  5310. (hide-sublevels arg)
  5311. (setq org-cycle-global-status 'contents))
  5312. ((equal arg '(4))
  5313. (org-set-startup-visibility)
  5314. (message "Startup visibility, plus VISIBILITY properties."))
  5315. (t
  5316. (org-cycle '(4))))))
  5317. (defun org-set-startup-visibility ()
  5318. "Set the visibility required by startup options and properties."
  5319. (cond
  5320. ((eq org-startup-folded t)
  5321. (org-cycle '(4)))
  5322. ((eq org-startup-folded 'content)
  5323. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5324. (org-cycle '(4)) (org-cycle '(4)))))
  5325. (unless (eq org-startup-folded 'showeverything)
  5326. (if org-hide-block-startup (org-hide-block-all))
  5327. (org-set-visibility-according-to-property 'no-cleanup)
  5328. (org-cycle-hide-archived-subtrees 'all)
  5329. (org-cycle-hide-drawers 'all)
  5330. (org-cycle-show-empty-lines t)))
  5331. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5332. "Switch subtree visibilities according to :VISIBILITY: property."
  5333. (interactive)
  5334. (let (org-show-entry-below state)
  5335. (save-excursion
  5336. (goto-char (point-max))
  5337. (while (re-search-backward
  5338. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5339. nil t)
  5340. (setq state (match-string 1))
  5341. (save-excursion
  5342. (org-back-to-heading t)
  5343. (hide-subtree)
  5344. (org-reveal)
  5345. (cond
  5346. ((equal state '("fold" "folded"))
  5347. (hide-subtree))
  5348. ((equal state "children")
  5349. (org-show-hidden-entry)
  5350. (show-children))
  5351. ((equal state "content")
  5352. (save-excursion
  5353. (save-restriction
  5354. (org-narrow-to-subtree)
  5355. (org-content))))
  5356. ((member state '("all" "showall"))
  5357. (show-subtree)))))
  5358. (unless no-cleanup
  5359. (org-cycle-hide-archived-subtrees 'all)
  5360. (org-cycle-hide-drawers 'all)
  5361. (org-cycle-show-empty-lines 'all)))))
  5362. (defun org-overview ()
  5363. "Switch to overview mode, showing only top-level headlines.
  5364. Really, this shows all headlines with level equal or greater than the level
  5365. of the first headline in the buffer. This is important, because if the
  5366. first headline is not level one, then (hide-sublevels 1) gives confusing
  5367. results."
  5368. (interactive)
  5369. (let ((level (save-excursion
  5370. (goto-char (point-min))
  5371. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5372. (progn
  5373. (goto-char (match-beginning 0))
  5374. (funcall outline-level))))))
  5375. (and level (hide-sublevels level))))
  5376. (defun org-content (&optional arg)
  5377. "Show all headlines in the buffer, like a table of contents.
  5378. With numerical argument N, show content up to level N."
  5379. (interactive "P")
  5380. (save-excursion
  5381. ;; Visit all headings and show their offspring
  5382. (and (integerp arg) (org-overview))
  5383. (goto-char (point-max))
  5384. (catch 'exit
  5385. (while (and (progn (condition-case nil
  5386. (outline-previous-visible-heading 1)
  5387. (error (goto-char (point-min))))
  5388. t)
  5389. (looking-at outline-regexp))
  5390. (if (integerp arg)
  5391. (show-children (1- arg))
  5392. (show-branches))
  5393. (if (bobp) (throw 'exit nil))))))
  5394. (defun org-optimize-window-after-visibility-change (state)
  5395. "Adjust the window after a change in outline visibility.
  5396. This function is the default value of the hook `org-cycle-hook'."
  5397. (when (get-buffer-window (current-buffer))
  5398. (cond
  5399. ((eq state 'content) nil)
  5400. ((eq state 'all) nil)
  5401. ((eq state 'folded) nil)
  5402. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5403. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5404. (defun org-remove-empty-overlays-at (pos)
  5405. "Remove outline overlays that do not contain non-white stuff."
  5406. (mapc
  5407. (lambda (o)
  5408. (and (eq 'outline (overlay-get o 'invisible))
  5409. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5410. (overlay-end o))))
  5411. (delete-overlay o)))
  5412. (overlays-at pos)))
  5413. (defun org-clean-visibility-after-subtree-move ()
  5414. "Fix visibility issues after moving a subtree."
  5415. ;; First, find a reasonable region to look at:
  5416. ;; Start two siblings above, end three below
  5417. (let* ((beg (save-excursion
  5418. (and (org-get-last-sibling)
  5419. (org-get-last-sibling))
  5420. (point)))
  5421. (end (save-excursion
  5422. (and (org-get-next-sibling)
  5423. (org-get-next-sibling)
  5424. (org-get-next-sibling))
  5425. (if (org-at-heading-p)
  5426. (point-at-eol)
  5427. (point))))
  5428. (level (looking-at "\\*+"))
  5429. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5430. (save-excursion
  5431. (save-restriction
  5432. (narrow-to-region beg end)
  5433. (when re
  5434. ;; Properly fold already folded siblings
  5435. (goto-char (point-min))
  5436. (while (re-search-forward re nil t)
  5437. (if (and (not (org-invisible-p))
  5438. (save-excursion
  5439. (goto-char (point-at-eol)) (org-invisible-p)))
  5440. (hide-entry))))
  5441. (org-cycle-show-empty-lines 'overview)
  5442. (org-cycle-hide-drawers 'overview)))))
  5443. (defun org-cycle-show-empty-lines (state)
  5444. "Show empty lines above all visible headlines.
  5445. The region to be covered depends on STATE when called through
  5446. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5447. entire buffer covered. Note that an empty line is only shown if there
  5448. are at least `org-cycle-separator-lines' empty lines before the headline."
  5449. (when (not (= org-cycle-separator-lines 0))
  5450. (save-excursion
  5451. (let* ((n (abs org-cycle-separator-lines))
  5452. (re (cond
  5453. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5454. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5455. (t (let ((ns (number-to-string (- n 2))))
  5456. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5457. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5458. beg end b e)
  5459. (cond
  5460. ((memq state '(overview contents t))
  5461. (setq beg (point-min) end (point-max)))
  5462. ((memq state '(children folded))
  5463. (setq beg (point) end (progn (org-end-of-subtree t t)
  5464. (beginning-of-line 2)
  5465. (point)))))
  5466. (when beg
  5467. (goto-char beg)
  5468. (while (re-search-forward re end t)
  5469. (unless (get-char-property (match-end 1) 'invisible)
  5470. (setq e (match-end 1))
  5471. (if (< org-cycle-separator-lines 0)
  5472. (setq b (save-excursion
  5473. (goto-char (match-beginning 0))
  5474. (org-back-over-empty-lines)
  5475. (if (save-excursion
  5476. (goto-char (max (point-min) (1- (point))))
  5477. (org-on-heading-p))
  5478. (1- (point))
  5479. (point))))
  5480. (setq b (match-beginning 1)))
  5481. (outline-flag-region b e nil)))))))
  5482. ;; Never hide empty lines at the end of the file.
  5483. (save-excursion
  5484. (goto-char (point-max))
  5485. (outline-previous-heading)
  5486. (outline-end-of-heading)
  5487. (if (and (looking-at "[ \t\n]+")
  5488. (= (match-end 0) (point-max)))
  5489. (outline-flag-region (point) (match-end 0) nil))))
  5490. (defun org-show-empty-lines-in-parent ()
  5491. "Move to the parent and re-show empty lines before visible headlines."
  5492. (save-excursion
  5493. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5494. (org-cycle-show-empty-lines context))))
  5495. (defun org-files-list ()
  5496. "Return `org-agenda-files' list, plus all open org-mode files.
  5497. This is useful for operations that need to scan all of a user's
  5498. open and agenda-wise Org files."
  5499. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5500. (dolist (buf (buffer-list))
  5501. (with-current-buffer buf
  5502. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5503. (let ((file (expand-file-name (buffer-file-name))))
  5504. (unless (member file files)
  5505. (push file files))))))
  5506. files))
  5507. (defsubst org-entry-beginning-position ()
  5508. "Return the beginning position of the current entry."
  5509. (save-excursion (outline-back-to-heading t) (point)))
  5510. (defsubst org-entry-end-position ()
  5511. "Return the end position of the current entry."
  5512. (save-excursion (outline-next-heading) (point)))
  5513. (defun org-cycle-hide-drawers (state)
  5514. "Re-hide all drawers after a visibility state change."
  5515. (when (and (org-mode-p)
  5516. (not (memq state '(overview folded contents))))
  5517. (save-excursion
  5518. (let* ((globalp (memq state '(contents all)))
  5519. (beg (if globalp (point-min) (point)))
  5520. (end (if globalp (point-max)
  5521. (if (eq state 'children)
  5522. (save-excursion (outline-next-heading) (point))
  5523. (org-end-of-subtree t)))))
  5524. (goto-char beg)
  5525. (while (re-search-forward org-drawer-regexp end t)
  5526. (org-flag-drawer t))))))
  5527. (defun org-flag-drawer (flag)
  5528. (save-excursion
  5529. (beginning-of-line 1)
  5530. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5531. (let ((b (match-end 0))
  5532. (outline-regexp org-outline-regexp))
  5533. (if (re-search-forward
  5534. "^[ \t]*:END:"
  5535. (save-excursion (outline-next-heading) (point)) t)
  5536. (outline-flag-region b (point-at-eol) flag)
  5537. (error ":END: line missing at position %s" b))))))
  5538. (defun org-subtree-end-visible-p ()
  5539. "Is the end of the current subtree visible?"
  5540. (pos-visible-in-window-p
  5541. (save-excursion (org-end-of-subtree t) (point))))
  5542. (defun org-first-headline-recenter (&optional N)
  5543. "Move cursor to the first headline and recenter the headline.
  5544. Optional argument N means put the headline into the Nth line of the window."
  5545. (goto-char (point-min))
  5546. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5547. (beginning-of-line)
  5548. (recenter (prefix-numeric-value N))))
  5549. ;;; Saving and restoring visibility
  5550. (defun org-outline-overlay-data (&optional use-markers)
  5551. "Return a list of the locations of all outline overlays.
  5552. The are overlays with the `invisible' property value `outline'.
  5553. The return values is a list of cons cells, with start and stop
  5554. positions for each overlay.
  5555. If USE-MARKERS is set, return the positions as markers."
  5556. (let (beg end)
  5557. (save-excursion
  5558. (save-restriction
  5559. (widen)
  5560. (delq nil
  5561. (mapcar (lambda (o)
  5562. (when (eq (overlay-get o 'invisible) 'outline)
  5563. (setq beg (overlay-start o)
  5564. end (overlay-end o))
  5565. (and beg end (> end beg)
  5566. (if use-markers
  5567. (cons (move-marker (make-marker) beg)
  5568. (move-marker (make-marker) end))
  5569. (cons beg end)))))
  5570. (overlays-in (point-min) (point-max))))))))
  5571. (defun org-set-outline-overlay-data (data)
  5572. "Create visibility overlays for all positions in DATA.
  5573. DATA should have been made by `org-outline-overlay-data'."
  5574. (let (o)
  5575. (save-excursion
  5576. (save-restriction
  5577. (widen)
  5578. (show-all)
  5579. (mapc (lambda (c)
  5580. (setq o (make-overlay (car c) (cdr c)))
  5581. (overlay-put o 'invisible 'outline))
  5582. data)))))
  5583. (defmacro org-save-outline-visibility (use-markers &rest body)
  5584. "Save and restore outline visibility around BODY.
  5585. If USE-MARKERS is non-nil, use markers for the positions.
  5586. This means that the buffer may change while running BODY,
  5587. but it also means that the buffer should stay alive
  5588. during the operation, because otherwise all these markers will
  5589. point nowhere."
  5590. (declare (indent 1))
  5591. `(let ((data (org-outline-overlay-data ,use-markers)))
  5592. (unwind-protect
  5593. (progn
  5594. ,@body
  5595. (org-set-outline-overlay-data data))
  5596. (when ,use-markers
  5597. (mapc (lambda (c)
  5598. (and (markerp (car c)) (move-marker (car c) nil))
  5599. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5600. data)))))
  5601. ;;; Folding of blocks
  5602. (defconst org-block-regexp
  5603. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5604. "Regular expression for hiding blocks.")
  5605. (defvar org-hide-block-overlays nil
  5606. "Overlays hiding blocks.")
  5607. (make-variable-buffer-local 'org-hide-block-overlays)
  5608. (defun org-block-map (function &optional start end)
  5609. "Call FUNCTION at the head of all source blocks in the current buffer.
  5610. Optional arguments START and END can be used to limit the range."
  5611. (let ((start (or start (point-min)))
  5612. (end (or end (point-max))))
  5613. (save-excursion
  5614. (goto-char start)
  5615. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5616. (save-excursion
  5617. (save-match-data
  5618. (goto-char (match-beginning 0))
  5619. (funcall function)))))))
  5620. (defun org-hide-block-toggle-all ()
  5621. "Toggle the visibility of all blocks in the current buffer."
  5622. (org-block-map #'org-hide-block-toggle))
  5623. (defun org-hide-block-all ()
  5624. "Fold all blocks in the current buffer."
  5625. (interactive)
  5626. (org-show-block-all)
  5627. (org-block-map #'org-hide-block-toggle-maybe))
  5628. (defun org-show-block-all ()
  5629. "Unfold all blocks in the current buffer."
  5630. (interactive)
  5631. (mapc 'delete-overlay org-hide-block-overlays)
  5632. (setq org-hide-block-overlays nil))
  5633. (defun org-hide-block-toggle-maybe ()
  5634. "Toggle visibility of block at point."
  5635. (interactive)
  5636. (let ((case-fold-search t))
  5637. (if (save-excursion
  5638. (beginning-of-line 1)
  5639. (looking-at org-block-regexp))
  5640. (progn (org-hide-block-toggle)
  5641. t) ;; to signal that we took action
  5642. nil))) ;; to signal that we did not
  5643. (defun org-hide-block-toggle (&optional force)
  5644. "Toggle the visibility of the current block."
  5645. (interactive)
  5646. (save-excursion
  5647. (beginning-of-line)
  5648. (if (re-search-forward org-block-regexp nil t)
  5649. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5650. (end (match-end 0)) ;; end of entire body
  5651. ov)
  5652. (if (memq t (mapcar (lambda (overlay)
  5653. (eq (overlay-get overlay 'invisible)
  5654. 'org-hide-block))
  5655. (overlays-at start)))
  5656. (if (or (not force) (eq force 'off))
  5657. (mapc (lambda (ov)
  5658. (when (member ov org-hide-block-overlays)
  5659. (setq org-hide-block-overlays
  5660. (delq ov org-hide-block-overlays)))
  5661. (when (eq (overlay-get ov 'invisible)
  5662. 'org-hide-block)
  5663. (delete-overlay ov)))
  5664. (overlays-at start)))
  5665. (setq ov (make-overlay start end))
  5666. (overlay-put ov 'invisible 'org-hide-block)
  5667. ;; make the block accessible to isearch
  5668. (overlay-put
  5669. ov 'isearch-open-invisible
  5670. (lambda (ov)
  5671. (when (member ov org-hide-block-overlays)
  5672. (setq org-hide-block-overlays
  5673. (delq ov org-hide-block-overlays)))
  5674. (when (eq (overlay-get ov 'invisible)
  5675. 'org-hide-block)
  5676. (delete-overlay ov))))
  5677. (push ov org-hide-block-overlays)))
  5678. (error "Not looking at a source block"))))
  5679. ;; org-tab-after-check-for-cycling-hook
  5680. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5681. ;; Remove overlays when changing major mode
  5682. (add-hook 'org-mode-hook
  5683. (lambda () (org-add-hook 'change-major-mode-hook
  5684. 'org-show-block-all 'append 'local)))
  5685. ;;; Org-goto
  5686. (defvar org-goto-window-configuration nil)
  5687. (defvar org-goto-marker nil)
  5688. (defvar org-goto-map
  5689. (let ((map (make-sparse-keymap)))
  5690. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5691. (while (setq cmd (pop cmds))
  5692. (substitute-key-definition cmd cmd map global-map)))
  5693. (suppress-keymap map)
  5694. (org-defkey map "\C-m" 'org-goto-ret)
  5695. (org-defkey map [(return)] 'org-goto-ret)
  5696. (org-defkey map [(left)] 'org-goto-left)
  5697. (org-defkey map [(right)] 'org-goto-right)
  5698. (org-defkey map [(control ?g)] 'org-goto-quit)
  5699. (org-defkey map "\C-i" 'org-cycle)
  5700. (org-defkey map [(tab)] 'org-cycle)
  5701. (org-defkey map [(down)] 'outline-next-visible-heading)
  5702. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5703. (if org-goto-auto-isearch
  5704. (if (fboundp 'define-key-after)
  5705. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5706. nil)
  5707. (org-defkey map "q" 'org-goto-quit)
  5708. (org-defkey map "n" 'outline-next-visible-heading)
  5709. (org-defkey map "p" 'outline-previous-visible-heading)
  5710. (org-defkey map "f" 'outline-forward-same-level)
  5711. (org-defkey map "b" 'outline-backward-same-level)
  5712. (org-defkey map "u" 'outline-up-heading))
  5713. (org-defkey map "/" 'org-occur)
  5714. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5715. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5716. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5717. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5718. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5719. map))
  5720. (defconst org-goto-help
  5721. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5722. RET=jump to location [Q]uit and return to previous location
  5723. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5724. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5725. ;; FIXME: Docstring does not mention both interfaces
  5726. (defun org-goto (&optional alternative-interface)
  5727. "Look up a different location in the current file, keeping current visibility.
  5728. When you want look-up or go to a different location in a document, the
  5729. fastest way is often to fold the entire buffer and then dive into the tree.
  5730. This method has the disadvantage, that the previous location will be folded,
  5731. which may not be what you want.
  5732. This command works around this by showing a copy of the current buffer
  5733. in an indirect buffer, in overview mode. You can dive into the tree in
  5734. that copy, use org-occur and incremental search to find a location.
  5735. When pressing RET or `Q', the command returns to the original buffer in
  5736. which the visibility is still unchanged. After RET is will also jump to
  5737. the location selected in the indirect buffer and expose the
  5738. the headline hierarchy above."
  5739. (interactive "P")
  5740. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5741. (org-refile-use-outline-path t)
  5742. (org-refile-target-verify-function nil)
  5743. (interface
  5744. (if (not alternative-interface)
  5745. org-goto-interface
  5746. (if (eq org-goto-interface 'outline)
  5747. 'outline-path-completion
  5748. 'outline)))
  5749. (org-goto-start-pos (point))
  5750. (selected-point
  5751. (if (eq interface 'outline)
  5752. (car (org-get-location (current-buffer) org-goto-help))
  5753. (let ((pa (org-refile-get-location "Goto: ")))
  5754. (org-refile-check-position pa)
  5755. (nth 3 pa)))))
  5756. (if selected-point
  5757. (progn
  5758. (org-mark-ring-push org-goto-start-pos)
  5759. (goto-char selected-point)
  5760. (if (or (org-invisible-p) (org-invisible-p2))
  5761. (org-show-context 'org-goto)))
  5762. (message "Quit"))))
  5763. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5764. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5765. (defvar org-goto-local-auto-isearch-map) ; defined below
  5766. (defun org-get-location (buf help)
  5767. "Let the user select a location in the Org-mode buffer BUF.
  5768. This function uses a recursive edit. It returns the selected position
  5769. or nil."
  5770. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5771. (isearch-hide-immediately nil)
  5772. (isearch-search-fun-function
  5773. (lambda () 'org-goto-local-search-headings))
  5774. (org-goto-selected-point org-goto-exit-command)
  5775. (pop-up-frames nil)
  5776. (special-display-buffer-names nil)
  5777. (special-display-regexps nil)
  5778. (special-display-function nil))
  5779. (save-excursion
  5780. (save-window-excursion
  5781. (delete-other-windows)
  5782. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5783. (switch-to-buffer
  5784. (condition-case nil
  5785. (make-indirect-buffer (current-buffer) "*org-goto*")
  5786. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5787. (with-output-to-temp-buffer "*Help*"
  5788. (princ help))
  5789. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5790. (setq buffer-read-only nil)
  5791. (let ((org-startup-truncated t)
  5792. (org-startup-folded nil)
  5793. (org-startup-align-all-tables nil))
  5794. (org-mode)
  5795. (org-overview))
  5796. (setq buffer-read-only t)
  5797. (if (and (boundp 'org-goto-start-pos)
  5798. (integer-or-marker-p org-goto-start-pos))
  5799. (let ((org-show-hierarchy-above t)
  5800. (org-show-siblings t)
  5801. (org-show-following-heading t))
  5802. (goto-char org-goto-start-pos)
  5803. (and (org-invisible-p) (org-show-context)))
  5804. (goto-char (point-min)))
  5805. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5806. (message "Select location and press RET")
  5807. (use-local-map org-goto-map)
  5808. (recursive-edit)
  5809. ))
  5810. (kill-buffer "*org-goto*")
  5811. (cons org-goto-selected-point org-goto-exit-command)))
  5812. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5813. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5814. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5815. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5816. (defun org-goto-local-search-headings (string bound noerror)
  5817. "Search and make sure that any matches are in headlines."
  5818. (catch 'return
  5819. (while (if isearch-forward
  5820. (search-forward string bound noerror)
  5821. (search-backward string bound noerror))
  5822. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5823. (and (member :headline context)
  5824. (not (member :tags context))))
  5825. (throw 'return (point))))))
  5826. (defun org-goto-local-auto-isearch ()
  5827. "Start isearch."
  5828. (interactive)
  5829. (goto-char (point-min))
  5830. (let ((keys (this-command-keys)))
  5831. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5832. (isearch-mode t)
  5833. (isearch-process-search-char (string-to-char keys)))))
  5834. (defun org-goto-ret (&optional arg)
  5835. "Finish `org-goto' by going to the new location."
  5836. (interactive "P")
  5837. (setq org-goto-selected-point (point)
  5838. org-goto-exit-command 'return)
  5839. (throw 'exit nil))
  5840. (defun org-goto-left ()
  5841. "Finish `org-goto' by going to the new location."
  5842. (interactive)
  5843. (if (org-on-heading-p)
  5844. (progn
  5845. (beginning-of-line 1)
  5846. (setq org-goto-selected-point (point)
  5847. org-goto-exit-command 'left)
  5848. (throw 'exit nil))
  5849. (error "Not on a heading")))
  5850. (defun org-goto-right ()
  5851. "Finish `org-goto' by going to the new location."
  5852. (interactive)
  5853. (if (org-on-heading-p)
  5854. (progn
  5855. (setq org-goto-selected-point (point)
  5856. org-goto-exit-command 'right)
  5857. (throw 'exit nil))
  5858. (error "Not on a heading")))
  5859. (defun org-goto-quit ()
  5860. "Finish `org-goto' without cursor motion."
  5861. (interactive)
  5862. (setq org-goto-selected-point nil)
  5863. (setq org-goto-exit-command 'quit)
  5864. (throw 'exit nil))
  5865. ;;; Indirect buffer display of subtrees
  5866. (defvar org-indirect-dedicated-frame nil
  5867. "This is the frame being used for indirect tree display.")
  5868. (defvar org-last-indirect-buffer nil)
  5869. (defun org-tree-to-indirect-buffer (&optional arg)
  5870. "Create indirect buffer and narrow it to current subtree.
  5871. With numerical prefix ARG, go up to this level and then take that tree.
  5872. If ARG is negative, go up that many levels.
  5873. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5874. indirect buffer previously made with this command, to avoid proliferation of
  5875. indirect buffers. However, when you call the command with a \
  5876. \\[universal-argument] prefix, or
  5877. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5878. is kept so that you can work with several indirect buffers at the same time.
  5879. If `org-indirect-buffer-display' is `dedicated-frame', the \
  5880. \\[universal-argument] prefix also
  5881. requests that a new frame be made for the new buffer, so that the dedicated
  5882. frame is not changed."
  5883. (interactive "P")
  5884. (let ((cbuf (current-buffer))
  5885. (cwin (selected-window))
  5886. (pos (point))
  5887. beg end level heading ibuf)
  5888. (save-excursion
  5889. (org-back-to-heading t)
  5890. (when (numberp arg)
  5891. (setq level (org-outline-level))
  5892. (if (< arg 0) (setq arg (+ level arg)))
  5893. (while (> (setq level (org-outline-level)) arg)
  5894. (outline-up-heading 1 t)))
  5895. (setq beg (point)
  5896. heading (org-get-heading))
  5897. (org-end-of-subtree t t)
  5898. (if (org-on-heading-p) (backward-char 1))
  5899. (setq end (point)))
  5900. (if (and (buffer-live-p org-last-indirect-buffer)
  5901. (not (eq org-indirect-buffer-display 'new-frame))
  5902. (not arg))
  5903. (kill-buffer org-last-indirect-buffer))
  5904. (setq ibuf (org-get-indirect-buffer cbuf)
  5905. org-last-indirect-buffer ibuf)
  5906. (cond
  5907. ((or (eq org-indirect-buffer-display 'new-frame)
  5908. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5909. (select-frame (make-frame))
  5910. (delete-other-windows)
  5911. (switch-to-buffer ibuf)
  5912. (org-set-frame-title heading))
  5913. ((eq org-indirect-buffer-display 'dedicated-frame)
  5914. (raise-frame
  5915. (select-frame (or (and org-indirect-dedicated-frame
  5916. (frame-live-p org-indirect-dedicated-frame)
  5917. org-indirect-dedicated-frame)
  5918. (setq org-indirect-dedicated-frame (make-frame)))))
  5919. (delete-other-windows)
  5920. (switch-to-buffer ibuf)
  5921. (org-set-frame-title (concat "Indirect: " heading)))
  5922. ((eq org-indirect-buffer-display 'current-window)
  5923. (switch-to-buffer ibuf))
  5924. ((eq org-indirect-buffer-display 'other-window)
  5925. (pop-to-buffer ibuf))
  5926. (t (error "Invalid value")))
  5927. (if (featurep 'xemacs)
  5928. (save-excursion (org-mode) (turn-on-font-lock)))
  5929. (narrow-to-region beg end)
  5930. (show-all)
  5931. (goto-char pos)
  5932. (and (window-live-p cwin) (select-window cwin))))
  5933. (defun org-get-indirect-buffer (&optional buffer)
  5934. (setq buffer (or buffer (current-buffer)))
  5935. (let ((n 1) (base (buffer-name buffer)) bname)
  5936. (while (buffer-live-p
  5937. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5938. (setq n (1+ n)))
  5939. (condition-case nil
  5940. (make-indirect-buffer buffer bname 'clone)
  5941. (error (make-indirect-buffer buffer bname)))))
  5942. (defun org-set-frame-title (title)
  5943. "Set the title of the current frame to the string TITLE."
  5944. ;; FIXME: how to name a single frame in XEmacs???
  5945. (unless (featurep 'xemacs)
  5946. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5947. ;;;; Structure editing
  5948. ;;; Inserting headlines
  5949. (defun org-previous-line-empty-p ()
  5950. (save-excursion
  5951. (and (not (bobp))
  5952. (or (beginning-of-line 0) t)
  5953. (save-match-data
  5954. (looking-at "[ \t]*$")))))
  5955. (defun org-insert-heading (&optional force-heading invisible-ok)
  5956. "Insert a new heading or item with same depth at point.
  5957. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5958. If point is at the beginning of a headline, insert a sibling before the
  5959. current headline. If point is not at the beginning, do not split the line,
  5960. but create the new headline after the current line.
  5961. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5962. This is important for non-interactive uses of the command."
  5963. (interactive "P")
  5964. (if (or (= (buffer-size) 0)
  5965. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5966. (org-on-heading-p))))
  5967. (not (org-in-item-p))))
  5968. (insert "\n* ")
  5969. (when (or force-heading (not (org-insert-item)))
  5970. (let* ((empty-line-p nil)
  5971. (head (save-excursion
  5972. (condition-case nil
  5973. (progn
  5974. (org-back-to-heading invisible-ok)
  5975. (setq empty-line-p (org-previous-line-empty-p))
  5976. (match-string 0))
  5977. (error "*"))))
  5978. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5979. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5980. pos hide-previous previous-pos)
  5981. (cond
  5982. ((and (org-on-heading-p) (bolp)
  5983. (or (bobp)
  5984. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5985. ;; insert before the current line
  5986. (open-line (if blank 2 1)))
  5987. ((and (bolp)
  5988. (not org-insert-heading-respect-content)
  5989. (or (bobp)
  5990. (save-excursion
  5991. (backward-char 1) (not (org-invisible-p)))))
  5992. ;; insert right here
  5993. nil)
  5994. (t
  5995. ;; somewhere in the line
  5996. (save-excursion
  5997. (setq previous-pos (point-at-bol))
  5998. (end-of-line)
  5999. (setq hide-previous (org-invisible-p)))
  6000. (and org-insert-heading-respect-content (org-show-subtree))
  6001. (let ((split
  6002. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6003. (save-excursion
  6004. (let ((p (point)))
  6005. (goto-char (point-at-bol))
  6006. (and (looking-at org-complex-heading-regexp)
  6007. (> p (match-beginning 4)))))))
  6008. tags pos)
  6009. (cond
  6010. (org-insert-heading-respect-content
  6011. (org-end-of-subtree nil t)
  6012. (or (bolp) (newline))
  6013. (or (org-previous-line-empty-p)
  6014. (and blank (newline)))
  6015. (open-line 1))
  6016. ((org-on-heading-p)
  6017. (when hide-previous
  6018. (show-children)
  6019. (org-show-entry))
  6020. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  6021. (setq tags (and (match-end 2) (match-string 2)))
  6022. (and (match-end 1)
  6023. (delete-region (match-beginning 1) (match-end 1)))
  6024. (setq pos (point-at-bol))
  6025. (or split (end-of-line 1))
  6026. (delete-horizontal-space)
  6027. (if (string-match "\\`\\*+\\'"
  6028. (buffer-substring (point-at-bol) (point)))
  6029. (insert " "))
  6030. (newline (if blank 2 1))
  6031. (when tags
  6032. (save-excursion
  6033. (goto-char pos)
  6034. (end-of-line 1)
  6035. (insert " " tags)
  6036. (org-set-tags nil 'align))))
  6037. (t
  6038. (or split (end-of-line 1))
  6039. (newline (if blank 2 1)))))))
  6040. (insert head) (just-one-space)
  6041. (setq pos (point))
  6042. (end-of-line 1)
  6043. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6044. (when (and org-insert-heading-respect-content hide-previous)
  6045. (save-excursion
  6046. (goto-char previous-pos)
  6047. (hide-subtree)))
  6048. (run-hooks 'org-insert-heading-hook)))))
  6049. (defun org-get-heading (&optional no-tags)
  6050. "Return the heading of the current entry, without the stars."
  6051. (save-excursion
  6052. (org-back-to-heading t)
  6053. (if (looking-at
  6054. (if no-tags
  6055. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  6056. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6057. (match-string 1) "")))
  6058. (defun org-heading-components ()
  6059. "Return the components of the current heading.
  6060. This is a list with the following elements:
  6061. - the level as an integer
  6062. - the reduced level, different if `org-odd-levels-only' is set.
  6063. - the TODO keyword, or nil
  6064. - the priority character, like ?A, or nil if no priority is given
  6065. - the headline text itself, or the tags string if no headline text
  6066. - the tags string, or nil."
  6067. (save-excursion
  6068. (org-back-to-heading t)
  6069. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6070. (list (length (match-string 1))
  6071. (org-reduced-level (length (match-string 1)))
  6072. (org-match-string-no-properties 2)
  6073. (and (match-end 3) (aref (match-string 3) 2))
  6074. (org-match-string-no-properties 4)
  6075. (org-match-string-no-properties 5)))))
  6076. (defun org-get-entry ()
  6077. "Get the entry text, after heading, entire subtree."
  6078. (save-excursion
  6079. (org-back-to-heading t)
  6080. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6081. (defun org-insert-heading-after-current ()
  6082. "Insert a new heading with same level as current, after current subtree."
  6083. (interactive)
  6084. (org-back-to-heading)
  6085. (org-insert-heading)
  6086. (org-move-subtree-down)
  6087. (end-of-line 1))
  6088. (defun org-insert-heading-respect-content ()
  6089. (interactive)
  6090. (let ((org-insert-heading-respect-content t))
  6091. (org-insert-heading t)))
  6092. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6093. (interactive "P")
  6094. (let ((org-insert-heading-respect-content t))
  6095. (org-insert-todo-heading force-state t)))
  6096. (defun org-insert-todo-heading (arg &optional force-heading)
  6097. "Insert a new heading with the same level and TODO state as current heading.
  6098. If the heading has no TODO state, or if the state is DONE, use the first
  6099. state (TODO by default). Also with prefix arg, force first state."
  6100. (interactive "P")
  6101. (when (or force-heading (not (org-insert-item 'checkbox)))
  6102. (org-insert-heading force-heading)
  6103. (save-excursion
  6104. (org-back-to-heading)
  6105. (outline-previous-heading)
  6106. (looking-at org-todo-line-regexp))
  6107. (let*
  6108. ((new-mark-x
  6109. (if (or arg
  6110. (not (match-beginning 2))
  6111. (member (match-string 2) org-done-keywords))
  6112. (car org-todo-keywords-1)
  6113. (match-string 2)))
  6114. (new-mark
  6115. (or
  6116. (run-hook-with-args-until-success
  6117. 'org-todo-get-default-hook new-mark-x nil)
  6118. new-mark-x)))
  6119. (beginning-of-line 1)
  6120. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6121. (if org-treat-insert-todo-heading-as-state-change
  6122. (org-todo new-mark)
  6123. (insert new-mark " "))))
  6124. (when org-provide-todo-statistics
  6125. (org-update-parent-todo-statistics))))
  6126. (defun org-insert-subheading (arg)
  6127. "Insert a new subheading and demote it.
  6128. Works for outline headings and for plain lists alike."
  6129. (interactive "P")
  6130. (org-insert-heading arg)
  6131. (cond
  6132. ((org-on-heading-p) (org-do-demote))
  6133. ((org-at-item-p) (org-indent-item 1))))
  6134. (defun org-insert-todo-subheading (arg)
  6135. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6136. Works for outline headings and for plain lists alike."
  6137. (interactive "P")
  6138. (org-insert-todo-heading arg)
  6139. (cond
  6140. ((org-on-heading-p) (org-do-demote))
  6141. ((org-at-item-p) (org-indent-item 1))))
  6142. ;;; Promotion and Demotion
  6143. (defvar org-after-demote-entry-hook nil
  6144. "Hook run after an entry has been demoted.
  6145. The cursor will be at the beginning of the entry.
  6146. When a subtree is being demoted, the hook will be called for each node.")
  6147. (defvar org-after-promote-entry-hook nil
  6148. "Hook run after an entry has been promoted.
  6149. The cursor will be at the beginning of the entry.
  6150. When a subtree is being promoted, the hook will be called for each node.")
  6151. (defun org-promote-subtree ()
  6152. "Promote the entire subtree.
  6153. See also `org-promote'."
  6154. (interactive)
  6155. (save-excursion
  6156. (org-map-tree 'org-promote))
  6157. (org-fix-position-after-promote))
  6158. (defun org-demote-subtree ()
  6159. "Demote the entire subtree. See `org-demote'.
  6160. See also `org-promote'."
  6161. (interactive)
  6162. (save-excursion
  6163. (org-map-tree 'org-demote))
  6164. (org-fix-position-after-promote))
  6165. (defun org-do-promote ()
  6166. "Promote the current heading higher up the tree.
  6167. If the region is active in `transient-mark-mode', promote all headings
  6168. in the region."
  6169. (interactive)
  6170. (save-excursion
  6171. (if (org-region-active-p)
  6172. (org-map-region 'org-promote (region-beginning) (region-end))
  6173. (org-promote)))
  6174. (org-fix-position-after-promote))
  6175. (defun org-do-demote ()
  6176. "Demote the current heading lower down the tree.
  6177. If the region is active in `transient-mark-mode', demote all headings
  6178. in the region."
  6179. (interactive)
  6180. (save-excursion
  6181. (if (org-region-active-p)
  6182. (org-map-region 'org-demote (region-beginning) (region-end))
  6183. (org-demote)))
  6184. (org-fix-position-after-promote))
  6185. (defun org-fix-position-after-promote ()
  6186. "Make sure that after pro/demotion cursor position is right."
  6187. (let ((pos (point)))
  6188. (when (save-excursion
  6189. (beginning-of-line 1)
  6190. (looking-at org-todo-line-regexp)
  6191. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6192. (cond ((eobp) (insert " "))
  6193. ((eolp) (insert " "))
  6194. ((equal (char-after) ?\ ) (forward-char 1))))))
  6195. (defun org-current-level ()
  6196. "Return the level of the current entry, or nil if before the first headline.
  6197. The level is the number of stars at the beginning of the headline."
  6198. (save-excursion
  6199. (condition-case nil
  6200. (progn
  6201. (org-back-to-heading t)
  6202. (funcall outline-level))
  6203. (error nil))))
  6204. (defun org-get-previous-line-level ()
  6205. "Return the outline depth of the last headline before the current line.
  6206. Returns 0 for the first headline in the buffer, and nil if before the
  6207. first headline."
  6208. (let ((current-level (org-current-level))
  6209. (prev-level (when (> (line-number-at-pos) 1)
  6210. (save-excursion
  6211. (beginning-of-line 0)
  6212. (org-current-level)))))
  6213. (cond ((null current-level) nil) ; Before first headline
  6214. ((null prev-level) 0) ; At first headline
  6215. (prev-level))))
  6216. (defun org-reduced-level (l)
  6217. "Compute the effective level of a heading.
  6218. This takes into account the setting of `org-odd-levels-only'."
  6219. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6220. (defun org-level-increment ()
  6221. "Return the number of stars that will be added or removed at a
  6222. time to headlines when structure editing, based on the value of
  6223. `org-odd-levels-only'."
  6224. (if org-odd-levels-only 2 1))
  6225. (defun org-get-valid-level (level &optional change)
  6226. "Rectify a level change under the influence of `org-odd-levels-only'
  6227. LEVEL is a current level, CHANGE is by how much the level should be
  6228. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6229. even level numbers will become the next higher odd number."
  6230. (if org-odd-levels-only
  6231. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6232. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6233. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6234. (max 1 (+ level (or change 0)))))
  6235. (if (boundp 'define-obsolete-function-alias)
  6236. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6237. (define-obsolete-function-alias 'org-get-legal-level
  6238. 'org-get-valid-level)
  6239. (define-obsolete-function-alias 'org-get-legal-level
  6240. 'org-get-valid-level "23.1")))
  6241. (defun org-promote ()
  6242. "Promote the current heading higher up the tree.
  6243. If the region is active in `transient-mark-mode', promote all headings
  6244. in the region."
  6245. (org-back-to-heading t)
  6246. (let* ((level (save-match-data (funcall outline-level)))
  6247. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6248. (diff (abs (- level (length up-head) -1))))
  6249. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6250. (replace-match up-head nil t)
  6251. ;; Fixup tag positioning
  6252. (and org-auto-align-tags (org-set-tags nil t))
  6253. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6254. (run-hooks 'org-after-promote-entry-hook)))
  6255. (defun org-demote ()
  6256. "Demote the current heading lower down the tree.
  6257. If the region is active in `transient-mark-mode', demote all headings
  6258. in the region."
  6259. (org-back-to-heading t)
  6260. (let* ((level (save-match-data (funcall outline-level)))
  6261. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6262. (diff (abs (- level (length down-head) -1))))
  6263. (replace-match down-head nil t)
  6264. ;; Fixup tag positioning
  6265. (and org-auto-align-tags (org-set-tags nil t))
  6266. (if org-adapt-indentation (org-fixup-indentation diff))
  6267. (run-hooks 'org-after-demote-entry-hook)))
  6268. (defun org-cycle-level ()
  6269. "Cycle the level of an empty headline through possible states.
  6270. This goes first to child, then to parent, level, then up the hierarchy.
  6271. After top level, it switches back to sibling level."
  6272. (interactive)
  6273. (let ((org-adapt-indentation nil))
  6274. (when (org-point-at-end-of-empty-headline)
  6275. (setq this-command 'org-cycle-level) ; Only needed for caching
  6276. (let ((cur-level (org-current-level))
  6277. (prev-level (org-get-previous-line-level)))
  6278. (cond
  6279. ;; If first headline in file, promote to top-level.
  6280. ((= prev-level 0)
  6281. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6282. do (org-do-promote)))
  6283. ;; If same level as prev, demote one.
  6284. ((= prev-level cur-level)
  6285. (org-do-demote))
  6286. ;; If parent is top-level, promote to top level if not already.
  6287. ((= prev-level 1)
  6288. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6289. do (org-do-promote)))
  6290. ;; If top-level, return to prev-level.
  6291. ((= cur-level 1)
  6292. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6293. do (org-do-demote)))
  6294. ;; If less than prev-level, promote one.
  6295. ((< cur-level prev-level)
  6296. (org-do-promote))
  6297. ;; If deeper than prev-level, promote until higher than
  6298. ;; prev-level.
  6299. ((> cur-level prev-level)
  6300. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6301. do (org-do-promote))))
  6302. t))))
  6303. (defun org-map-tree (fun)
  6304. "Call FUN for every heading underneath the current one."
  6305. (org-back-to-heading)
  6306. (let ((level (funcall outline-level)))
  6307. (save-excursion
  6308. (funcall fun)
  6309. (while (and (progn
  6310. (outline-next-heading)
  6311. (> (funcall outline-level) level))
  6312. (not (eobp)))
  6313. (funcall fun)))))
  6314. (defun org-map-region (fun beg end)
  6315. "Call FUN for every heading between BEG and END."
  6316. (let ((org-ignore-region t))
  6317. (save-excursion
  6318. (setq end (copy-marker end))
  6319. (goto-char beg)
  6320. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6321. (< (point) end))
  6322. (funcall fun))
  6323. (while (and (progn
  6324. (outline-next-heading)
  6325. (< (point) end))
  6326. (not (eobp)))
  6327. (funcall fun)))))
  6328. (defun org-fixup-indentation (diff)
  6329. "Change the indentation in the current entry by DIFF.
  6330. However, if any line in the current entry has no indentation, or if it
  6331. would end up with no indentation after the change, nothing at all is done."
  6332. (save-excursion
  6333. (let ((end (save-excursion (outline-next-heading)
  6334. (point-marker)))
  6335. (prohibit (if (> diff 0)
  6336. "^\\S-"
  6337. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6338. col)
  6339. (unless (save-excursion (end-of-line 1)
  6340. (re-search-forward prohibit end t))
  6341. (while (and (< (point) end)
  6342. (re-search-forward "^[ \t]+" end t))
  6343. (goto-char (match-end 0))
  6344. (setq col (current-column))
  6345. (if (< diff 0) (replace-match ""))
  6346. (org-indent-to-column (+ diff col))))
  6347. (move-marker end nil))))
  6348. (defun org-convert-to-odd-levels ()
  6349. "Convert an org-mode file with all levels allowed to one with odd levels.
  6350. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6351. level 5 etc."
  6352. (interactive)
  6353. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6354. (let ((outline-regexp org-outline-regexp)
  6355. (outline-level 'org-outline-level)
  6356. (org-odd-levels-only nil) n)
  6357. (save-excursion
  6358. (goto-char (point-min))
  6359. (while (re-search-forward "^\\*\\*+ " nil t)
  6360. (setq n (- (length (match-string 0)) 2))
  6361. (while (>= (setq n (1- n)) 0)
  6362. (org-demote))
  6363. (end-of-line 1))))))
  6364. (defun org-convert-to-oddeven-levels ()
  6365. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6366. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6367. file contains a section with an even level, conversion would
  6368. destroy the structure of the file. An error is signaled in this
  6369. case."
  6370. (interactive)
  6371. (goto-char (point-min))
  6372. ;; First check if there are no even levels
  6373. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6374. (org-show-context t)
  6375. (error "Not all levels are odd in this file. Conversion not possible"))
  6376. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6377. (let ((outline-regexp org-outline-regexp)
  6378. (outline-level 'org-outline-level)
  6379. (org-odd-levels-only nil) n)
  6380. (save-excursion
  6381. (goto-char (point-min))
  6382. (while (re-search-forward "^\\*\\*+ " nil t)
  6383. (setq n (/ (1- (length (match-string 0))) 2))
  6384. (while (>= (setq n (1- n)) 0)
  6385. (org-promote))
  6386. (end-of-line 1))))))
  6387. (defun org-tr-level (n)
  6388. "Make N odd if required."
  6389. (if org-odd-levels-only (1+ (/ n 2)) n))
  6390. ;;; Vertical tree motion, cutting and pasting of subtrees
  6391. (defun org-move-subtree-up (&optional arg)
  6392. "Move the current subtree up past ARG headlines of the same level."
  6393. (interactive "p")
  6394. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6395. (defun org-move-subtree-down (&optional arg)
  6396. "Move the current subtree down past ARG headlines of the same level."
  6397. (interactive "p")
  6398. (setq arg (prefix-numeric-value arg))
  6399. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6400. 'org-get-last-sibling))
  6401. (ins-point (make-marker))
  6402. (cnt (abs arg))
  6403. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6404. ;; Select the tree
  6405. (org-back-to-heading)
  6406. (setq beg0 (point))
  6407. (save-excursion
  6408. (setq ne-beg (org-back-over-empty-lines))
  6409. (setq beg (point)))
  6410. (save-match-data
  6411. (save-excursion (outline-end-of-heading)
  6412. (setq folded (org-invisible-p)))
  6413. (outline-end-of-subtree))
  6414. (outline-next-heading)
  6415. (setq ne-end (org-back-over-empty-lines))
  6416. (setq end (point))
  6417. (goto-char beg0)
  6418. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6419. ;; include less whitespace
  6420. (save-excursion
  6421. (goto-char beg)
  6422. (forward-line (- ne-beg ne-end))
  6423. (setq beg (point))))
  6424. ;; Find insertion point, with error handling
  6425. (while (> cnt 0)
  6426. (or (and (funcall movfunc) (looking-at outline-regexp))
  6427. (progn (goto-char beg0)
  6428. (error "Cannot move past superior level or buffer limit")))
  6429. (setq cnt (1- cnt)))
  6430. (if (> arg 0)
  6431. ;; Moving forward - still need to move over subtree
  6432. (progn (org-end-of-subtree t t)
  6433. (save-excursion
  6434. (org-back-over-empty-lines)
  6435. (or (bolp) (newline)))))
  6436. (setq ne-ins (org-back-over-empty-lines))
  6437. (move-marker ins-point (point))
  6438. (setq txt (buffer-substring beg end))
  6439. (org-save-markers-in-region beg end)
  6440. (delete-region beg end)
  6441. (org-remove-empty-overlays-at beg)
  6442. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6443. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6444. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6445. (let ((bbb (point)))
  6446. (insert-before-markers txt)
  6447. (org-reinstall-markers-in-region bbb)
  6448. (move-marker ins-point bbb))
  6449. (or (bolp) (insert "\n"))
  6450. (setq ins-end (point))
  6451. (goto-char ins-point)
  6452. (org-skip-whitespace)
  6453. (when (and (< arg 0)
  6454. (org-first-sibling-p)
  6455. (> ne-ins ne-beg))
  6456. ;; Move whitespace back to beginning
  6457. (save-excursion
  6458. (goto-char ins-end)
  6459. (let ((kill-whole-line t))
  6460. (kill-line (- ne-ins ne-beg)) (point)))
  6461. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6462. (move-marker ins-point nil)
  6463. (if folded
  6464. (hide-subtree)
  6465. (org-show-entry)
  6466. (show-children)
  6467. (org-cycle-hide-drawers 'children))
  6468. (org-clean-visibility-after-subtree-move)))
  6469. (defvar org-subtree-clip ""
  6470. "Clipboard for cut and paste of subtrees.
  6471. This is actually only a copy of the kill, because we use the normal kill
  6472. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6473. (defvar org-subtree-clip-folded nil
  6474. "Was the last copied subtree folded?
  6475. This is used to fold the tree back after pasting.")
  6476. (defun org-cut-subtree (&optional n)
  6477. "Cut the current subtree into the clipboard.
  6478. With prefix arg N, cut this many sequential subtrees.
  6479. This is a short-hand for marking the subtree and then cutting it."
  6480. (interactive "p")
  6481. (org-copy-subtree n 'cut))
  6482. (defun org-copy-subtree (&optional n cut force-store-markers)
  6483. "Cut the current subtree into the clipboard.
  6484. With prefix arg N, cut this many sequential subtrees.
  6485. This is a short-hand for marking the subtree and then copying it.
  6486. If CUT is non-nil, actually cut the subtree.
  6487. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6488. of some markers in the region, even if CUT is non-nil. This is
  6489. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6490. (interactive "p")
  6491. (let (beg end folded (beg0 (point)))
  6492. (if (interactive-p)
  6493. (org-back-to-heading nil) ; take what looks like a subtree
  6494. (org-back-to-heading t)) ; take what is really there
  6495. (org-back-over-empty-lines)
  6496. (setq beg (point))
  6497. (skip-chars-forward " \t\r\n")
  6498. (save-match-data
  6499. (save-excursion (outline-end-of-heading)
  6500. (setq folded (org-invisible-p)))
  6501. (condition-case nil
  6502. (org-forward-same-level (1- n) t)
  6503. (error nil))
  6504. (org-end-of-subtree t t))
  6505. (org-back-over-empty-lines)
  6506. (setq end (point))
  6507. (goto-char beg0)
  6508. (when (> end beg)
  6509. (setq org-subtree-clip-folded folded)
  6510. (when (or cut force-store-markers)
  6511. (org-save-markers-in-region beg end))
  6512. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6513. (setq org-subtree-clip (current-kill 0))
  6514. (message "%s: Subtree(s) with %d characters"
  6515. (if cut "Cut" "Copied")
  6516. (length org-subtree-clip)))))
  6517. (defun org-paste-subtree (&optional level tree for-yank)
  6518. "Paste the clipboard as a subtree, with modification of headline level.
  6519. The entire subtree is promoted or demoted in order to match a new headline
  6520. level.
  6521. If the cursor is at the beginning of a headline, the same level as
  6522. that headline is used to paste the tree
  6523. If not, the new level is derived from the *visible* headings
  6524. before and after the insertion point, and taken to be the inferior headline
  6525. level of the two. So if the previous visible heading is level 3 and the
  6526. next is level 4 (or vice versa), level 4 will be used for insertion.
  6527. This makes sure that the subtree remains an independent subtree and does
  6528. not swallow low level entries.
  6529. You can also force a different level, either by using a numeric prefix
  6530. argument, or by inserting the heading marker by hand. For example, if the
  6531. cursor is after \"*****\", then the tree will be shifted to level 5.
  6532. If optional TREE is given, use this text instead of the kill ring.
  6533. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6534. move back over whitespace before inserting, and move point to the end of
  6535. the inserted text when done."
  6536. (interactive "P")
  6537. (setq tree (or tree (and kill-ring (current-kill 0))))
  6538. (unless (org-kill-is-subtree-p tree)
  6539. (error "%s"
  6540. (substitute-command-keys
  6541. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6542. (let* ((visp (not (org-invisible-p)))
  6543. (txt tree)
  6544. (^re (concat "^\\(" outline-regexp "\\)"))
  6545. (re (concat "\\(" outline-regexp "\\)"))
  6546. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6547. (old-level (if (string-match ^re txt)
  6548. (- (match-end 0) (match-beginning 0) 1)
  6549. -1))
  6550. (force-level (cond (level (prefix-numeric-value level))
  6551. ((and (looking-at "[ \t]*$")
  6552. (string-match
  6553. ^re_ (buffer-substring
  6554. (point-at-bol) (point))))
  6555. (- (match-end 1) (match-beginning 1)))
  6556. ((and (bolp)
  6557. (looking-at org-outline-regexp))
  6558. (- (match-end 0) (point) 1))
  6559. (t nil)))
  6560. (previous-level (save-excursion
  6561. (condition-case nil
  6562. (progn
  6563. (outline-previous-visible-heading 1)
  6564. (if (looking-at re)
  6565. (- (match-end 0) (match-beginning 0) 1)
  6566. 1))
  6567. (error 1))))
  6568. (next-level (save-excursion
  6569. (condition-case nil
  6570. (progn
  6571. (or (looking-at outline-regexp)
  6572. (outline-next-visible-heading 1))
  6573. (if (looking-at re)
  6574. (- (match-end 0) (match-beginning 0) 1)
  6575. 1))
  6576. (error 1))))
  6577. (new-level (or force-level (max previous-level next-level)))
  6578. (shift (if (or (= old-level -1)
  6579. (= new-level -1)
  6580. (= old-level new-level))
  6581. 0
  6582. (- new-level old-level)))
  6583. (delta (if (> shift 0) -1 1))
  6584. (func (if (> shift 0) 'org-demote 'org-promote))
  6585. (org-odd-levels-only nil)
  6586. beg end newend)
  6587. ;; Remove the forced level indicator
  6588. (if force-level
  6589. (delete-region (point-at-bol) (point)))
  6590. ;; Paste
  6591. (beginning-of-line 1)
  6592. (unless for-yank (org-back-over-empty-lines))
  6593. (setq beg (point))
  6594. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6595. (insert-before-markers txt)
  6596. (unless (string-match "\n\\'" txt) (insert "\n"))
  6597. (setq newend (point))
  6598. (org-reinstall-markers-in-region beg)
  6599. (setq end (point))
  6600. (goto-char beg)
  6601. (skip-chars-forward " \t\n\r")
  6602. (setq beg (point))
  6603. (if (and (org-invisible-p) visp)
  6604. (save-excursion (outline-show-heading)))
  6605. ;; Shift if necessary
  6606. (unless (= shift 0)
  6607. (save-restriction
  6608. (narrow-to-region beg end)
  6609. (while (not (= shift 0))
  6610. (org-map-region func (point-min) (point-max))
  6611. (setq shift (+ delta shift)))
  6612. (goto-char (point-min))
  6613. (setq newend (point-max))))
  6614. (when (or (interactive-p) for-yank)
  6615. (message "Clipboard pasted as level %d subtree" new-level))
  6616. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6617. kill-ring
  6618. (eq org-subtree-clip (current-kill 0))
  6619. org-subtree-clip-folded)
  6620. ;; The tree was folded before it was killed/copied
  6621. (hide-subtree))
  6622. (and for-yank (goto-char newend))))
  6623. (defun org-kill-is-subtree-p (&optional txt)
  6624. "Check if the current kill is an outline subtree, or a set of trees.
  6625. Returns nil if kill does not start with a headline, or if the first
  6626. headline level is not the largest headline level in the tree.
  6627. So this will actually accept several entries of equal levels as well,
  6628. which is OK for `org-paste-subtree'.
  6629. If optional TXT is given, check this string instead of the current kill."
  6630. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6631. (start-level (and kill
  6632. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6633. org-outline-regexp "\\)")
  6634. kill)
  6635. (- (match-end 2) (match-beginning 2) 1)))
  6636. (re (concat "^" org-outline-regexp))
  6637. (start (1+ (or (match-beginning 2) -1))))
  6638. (if (not start-level)
  6639. (progn
  6640. nil) ;; does not even start with a heading
  6641. (catch 'exit
  6642. (while (setq start (string-match re kill (1+ start)))
  6643. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6644. (throw 'exit nil)))
  6645. t))))
  6646. (defvar org-markers-to-move nil
  6647. "Markers that should be moved with a cut-and-paste operation.
  6648. Those markers are stored together with their positions relative to
  6649. the start of the region.")
  6650. (defun org-save-markers-in-region (beg end)
  6651. "Check markers in region.
  6652. If these markers are between BEG and END, record their position relative
  6653. to BEG, so that after moving the block of text, we can put the markers back
  6654. into place.
  6655. This function gets called just before an entry or tree gets cut from the
  6656. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6657. called immediately, to move the markers with the entries."
  6658. (setq org-markers-to-move nil)
  6659. (when (featurep 'org-clock)
  6660. (org-clock-save-markers-for-cut-and-paste beg end))
  6661. (when (featurep 'org-agenda)
  6662. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6663. (defun org-check-and-save-marker (marker beg end)
  6664. "Check if MARKER is between BEG and END.
  6665. If yes, remember the marker and the distance to BEG."
  6666. (when (and (marker-buffer marker)
  6667. (equal (marker-buffer marker) (current-buffer)))
  6668. (if (and (>= marker beg) (< marker end))
  6669. (push (cons marker (- marker beg)) org-markers-to-move))))
  6670. (defun org-reinstall-markers-in-region (beg)
  6671. "Move all remembered markers to their position relative to BEG."
  6672. (mapc (lambda (x)
  6673. (move-marker (car x) (+ beg (cdr x))))
  6674. org-markers-to-move)
  6675. (setq org-markers-to-move nil))
  6676. (defun org-narrow-to-subtree ()
  6677. "Narrow buffer to the current subtree."
  6678. (interactive)
  6679. (save-excursion
  6680. (save-match-data
  6681. (narrow-to-region
  6682. (progn (org-back-to-heading t) (point))
  6683. (progn (org-end-of-subtree t t)
  6684. (if (org-on-heading-p) (backward-char 1))
  6685. (point))))))
  6686. (eval-when-compile
  6687. (defvar org-property-drawer-re))
  6688. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6689. "Clone the task (subtree) at point N times.
  6690. The clones will be inserted as siblings.
  6691. In interactive use, the user will be prompted for the number of
  6692. clones to be produced, and for a time SHIFT, which may be a
  6693. repeater as used in time stamps, for example `+3d'.
  6694. When a valid repeater is given and the entry contains any time
  6695. stamps, the clones will become a sequence in time, with time
  6696. stamps in the subtree shifted for each clone produced. If SHIFT
  6697. is nil or the empty string, time stamps will be left alone. The
  6698. ID property of the original subtree is removed.
  6699. If the original subtree did contain time stamps with a repeater,
  6700. the following will happen:
  6701. - the repeater will be removed in each clone
  6702. - an additional clone will be produced, with the current, unshifted
  6703. date(s) in the entry.
  6704. - the original entry will be placed *after* all the clones, with
  6705. repeater intact.
  6706. - the start days in the repeater in the original entry will be shifted
  6707. to past the last clone.
  6708. I this way you can spell out a number of instances of a repeating task,
  6709. and still retain the repeater to cover future instances of the task."
  6710. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6711. (let (beg end template task idprop
  6712. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6713. (if (not (and (integerp n) (> n 0)))
  6714. (error "Invalid number of replications %s" n))
  6715. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6716. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6717. shift)))
  6718. (error "Invalid shift specification %s" shift))
  6719. (when doshift
  6720. (setq shift-n (string-to-number (match-string 1 shift))
  6721. shift-what (cdr (assoc (match-string 2 shift)
  6722. '(("d" . day) ("w" . week)
  6723. ("m" . month) ("y" . year))))))
  6724. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6725. (setq nmin 1 nmax n)
  6726. (org-back-to-heading t)
  6727. (setq beg (point))
  6728. (setq idprop (org-entry-get nil "ID"))
  6729. (org-end-of-subtree t t)
  6730. (or (bolp) (insert "\n"))
  6731. (setq end (point))
  6732. (setq template (buffer-substring beg end))
  6733. (when (and doshift
  6734. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6735. (delete-region beg end)
  6736. (setq end beg)
  6737. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6738. (goto-char end)
  6739. (loop for n from nmin to nmax do
  6740. ;; prepare clone
  6741. (with-temp-buffer
  6742. (insert template)
  6743. (org-mode)
  6744. (goto-char (point-min))
  6745. (and idprop (if org-clone-delete-id
  6746. (org-entry-delete nil "ID")
  6747. (org-id-get-create t)))
  6748. (while (re-search-forward org-property-drawer-re nil t)
  6749. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6750. (goto-char (point-min))
  6751. (when doshift
  6752. (while (re-search-forward org-ts-regexp-both nil t)
  6753. (org-timestamp-change (* n shift-n) shift-what))
  6754. (unless (= n n-no-remove)
  6755. (goto-char (point-min))
  6756. (while (re-search-forward org-ts-regexp nil t)
  6757. (save-excursion
  6758. (goto-char (match-beginning 0))
  6759. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6760. (delete-region (match-beginning 1) (match-end 1)))))))
  6761. (setq task (buffer-string)))
  6762. (insert task))
  6763. (goto-char beg)))
  6764. ;;; Outline Sorting
  6765. (defun org-sort (with-case)
  6766. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6767. Optional argument WITH-CASE means sort case-sensitively.
  6768. With a double prefix argument, also remove duplicate entries."
  6769. (interactive "P")
  6770. (if (org-at-table-p)
  6771. (org-call-with-arg 'org-table-sort-lines with-case)
  6772. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6773. (defun org-sort-remove-invisible (s)
  6774. (remove-text-properties 0 (length s) org-rm-props s)
  6775. (while (string-match org-bracket-link-regexp s)
  6776. (setq s (replace-match (if (match-end 2)
  6777. (match-string 3 s)
  6778. (match-string 1 s)) t t s)))
  6779. s)
  6780. (defvar org-priority-regexp) ; defined later in the file
  6781. (defvar org-after-sorting-entries-or-items-hook nil
  6782. "Hook that is run after a bunch of entries or items have been sorted.
  6783. When children are sorted, the cursor is in the parent line when this
  6784. hook gets called. When a region or a plain list is sorted, the cursor
  6785. will be in the first entry of the sorted region/list.")
  6786. (defun org-sort-entries-or-items
  6787. (&optional with-case sorting-type getkey-func compare-func property)
  6788. "Sort entries on a certain level of an outline tree, or plain list items.
  6789. If there is an active region, the entries in the region are sorted.
  6790. Else, if the cursor is before the first entry, sort the top-level items.
  6791. Else, the children of the entry at point are sorted.
  6792. If the cursor is at the first item in a plain list, the list items will be
  6793. sorted.
  6794. Sorting can be alphabetically, numerically, by date/time as given by
  6795. a time stamp, by a property or by priority.
  6796. The command prompts for the sorting type unless it has been given to the
  6797. function through the SORTING-TYPE argument, which needs to be a character,
  6798. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6799. precise meaning of each character:
  6800. n Numerically, by converting the beginning of the entry/item to a number.
  6801. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6802. t By date/time, either the first active time stamp in the entry, or, if
  6803. none exist, by the first inactive one.
  6804. In items, only the first line will be checked.
  6805. s By the scheduled date/time.
  6806. d By deadline date/time.
  6807. c By creation time, which is assumed to be the first inactive time stamp
  6808. at the beginning of a line.
  6809. p By priority according to the cookie.
  6810. r By the value of a property.
  6811. Capital letters will reverse the sort order.
  6812. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6813. called with point at the beginning of the record. It must return either
  6814. a string or a number that should serve as the sorting key for that record.
  6815. Comparing entries ignores case by default. However, with an optional argument
  6816. WITH-CASE, the sorting considers case as well."
  6817. (interactive "P")
  6818. (let ((case-func (if with-case 'identity 'downcase))
  6819. start beg end stars re re2
  6820. txt what tmp plain-list-p)
  6821. ;; Find beginning and end of region to sort
  6822. (cond
  6823. ((org-region-active-p)
  6824. ;; we will sort the region
  6825. (setq end (region-end)
  6826. what "region")
  6827. (goto-char (region-beginning))
  6828. (if (not (org-on-heading-p)) (outline-next-heading))
  6829. (setq start (point)))
  6830. ((org-at-item-p)
  6831. ;; we will sort this plain list
  6832. (org-beginning-of-item-list) (setq start (point))
  6833. (org-end-of-item-list)
  6834. (or (bolp) (insert "\n"))
  6835. (setq end (point))
  6836. (goto-char start)
  6837. (setq plain-list-p t
  6838. what "plain list"))
  6839. ((or (org-on-heading-p)
  6840. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6841. ;; we will sort the children of the current headline
  6842. (org-back-to-heading)
  6843. (setq start (point)
  6844. end (progn (org-end-of-subtree t t)
  6845. (or (bolp) (insert "\n"))
  6846. (org-back-over-empty-lines)
  6847. (point))
  6848. what "children")
  6849. (goto-char start)
  6850. (show-subtree)
  6851. (outline-next-heading))
  6852. (t
  6853. ;; we will sort the top-level entries in this file
  6854. (goto-char (point-min))
  6855. (or (org-on-heading-p) (outline-next-heading))
  6856. (setq start (point))
  6857. (goto-char (point-max))
  6858. (beginning-of-line 1)
  6859. (when (looking-at ".*?\\S-")
  6860. ;; File ends in a non-white line
  6861. (end-of-line 1)
  6862. (insert "\n"))
  6863. (setq end (point-max))
  6864. (setq what "top-level")
  6865. (goto-char start)
  6866. (show-all)))
  6867. (setq beg (point))
  6868. (if (>= beg end) (error "Nothing to sort"))
  6869. (unless plain-list-p
  6870. (looking-at "\\(\\*+\\)")
  6871. (setq stars (match-string 1)
  6872. re (concat "^" (regexp-quote stars) " +")
  6873. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6874. txt (buffer-substring beg end))
  6875. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6876. (if (and (not (equal stars "*")) (string-match re2 txt))
  6877. (error "Region to sort contains a level above the first entry")))
  6878. (unless sorting-type
  6879. (message
  6880. (if plain-list-p
  6881. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6882. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6883. [t]ime [s]cheduled [d]eadline [c]reated
  6884. A/N/T/S/D/C/P/O/F means reversed:")
  6885. what)
  6886. (setq sorting-type (read-char-exclusive))
  6887. (and (= (downcase sorting-type) ?f)
  6888. (setq getkey-func
  6889. (org-icompleting-read "Sort using function: "
  6890. obarray 'fboundp t nil nil))
  6891. (setq getkey-func (intern getkey-func)))
  6892. (and (= (downcase sorting-type) ?r)
  6893. (setq property
  6894. (org-icompleting-read "Property: "
  6895. (mapcar 'list (org-buffer-property-keys t))
  6896. nil t))))
  6897. (message "Sorting entries...")
  6898. (save-restriction
  6899. (narrow-to-region start end)
  6900. (let ((dcst (downcase sorting-type))
  6901. (case-fold-search nil)
  6902. (now (current-time)))
  6903. (sort-subr
  6904. (/= dcst sorting-type)
  6905. ;; This function moves to the beginning character of the "record" to
  6906. ;; be sorted.
  6907. (if plain-list-p
  6908. (lambda nil
  6909. (if (org-at-item-p) t (goto-char (point-max))))
  6910. (lambda nil
  6911. (if (re-search-forward re nil t)
  6912. (goto-char (match-beginning 0))
  6913. (goto-char (point-max)))))
  6914. ;; This function moves to the last character of the "record" being
  6915. ;; sorted.
  6916. (if plain-list-p
  6917. 'org-end-of-item
  6918. (lambda nil
  6919. (save-match-data
  6920. (condition-case nil
  6921. (outline-forward-same-level 1)
  6922. (error
  6923. (goto-char (point-max)))))))
  6924. ;; This function returns the value that gets sorted against.
  6925. (if plain-list-p
  6926. (lambda nil
  6927. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6928. (cond
  6929. ((= dcst ?n)
  6930. (string-to-number (buffer-substring (match-end 0)
  6931. (point-at-eol))))
  6932. ((= dcst ?a)
  6933. (buffer-substring (match-end 0) (point-at-eol)))
  6934. ((= dcst ?t)
  6935. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6936. (re-search-forward org-ts-regexp-both
  6937. (point-at-eol) t))
  6938. (org-time-string-to-seconds (match-string 0))
  6939. (org-float-time now)))
  6940. ((= dcst ?f)
  6941. (if getkey-func
  6942. (progn
  6943. (setq tmp (funcall getkey-func))
  6944. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6945. tmp)
  6946. (error "Invalid key function `%s'" getkey-func)))
  6947. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6948. (lambda nil
  6949. (cond
  6950. ((= dcst ?n)
  6951. (if (looking-at org-complex-heading-regexp)
  6952. (string-to-number (match-string 4))
  6953. nil))
  6954. ((= dcst ?a)
  6955. (if (looking-at org-complex-heading-regexp)
  6956. (funcall case-func (match-string 4))
  6957. nil))
  6958. ((= dcst ?t)
  6959. (let ((end (save-excursion (outline-next-heading) (point))))
  6960. (if (or (re-search-forward org-ts-regexp end t)
  6961. (re-search-forward org-ts-regexp-both end t))
  6962. (org-time-string-to-seconds (match-string 0))
  6963. (org-float-time now))))
  6964. ((= dcst ?c)
  6965. (let ((end (save-excursion (outline-next-heading) (point))))
  6966. (if (re-search-forward
  6967. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6968. end t)
  6969. (org-time-string-to-seconds (match-string 0))
  6970. (org-float-time now))))
  6971. ((= dcst ?s)
  6972. (let ((end (save-excursion (outline-next-heading) (point))))
  6973. (if (re-search-forward org-scheduled-time-regexp end t)
  6974. (org-time-string-to-seconds (match-string 1))
  6975. (org-float-time now))))
  6976. ((= dcst ?d)
  6977. (let ((end (save-excursion (outline-next-heading) (point))))
  6978. (if (re-search-forward org-deadline-time-regexp end t)
  6979. (org-time-string-to-seconds (match-string 1))
  6980. (org-float-time now))))
  6981. ((= dcst ?p)
  6982. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6983. (string-to-char (match-string 2))
  6984. org-default-priority))
  6985. ((= dcst ?r)
  6986. (or (org-entry-get nil property) ""))
  6987. ((= dcst ?o)
  6988. (if (looking-at org-complex-heading-regexp)
  6989. (- 9999 (length (member (match-string 2)
  6990. org-todo-keywords-1)))))
  6991. ((= dcst ?f)
  6992. (if getkey-func
  6993. (progn
  6994. (setq tmp (funcall getkey-func))
  6995. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6996. tmp)
  6997. (error "Invalid key function `%s'" getkey-func)))
  6998. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6999. nil
  7000. (cond
  7001. ((= dcst ?a) 'string<)
  7002. ((= dcst ?f) compare-func)
  7003. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7004. (t nil)))))
  7005. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7006. (message "Sorting entries...done")))
  7007. (defun org-do-sort (table what &optional with-case sorting-type)
  7008. "Sort TABLE of WHAT according to SORTING-TYPE.
  7009. The user will be prompted for the SORTING-TYPE if the call to this
  7010. function does not specify it. WHAT is only for the prompt, to indicate
  7011. what is being sorted. The sorting key will be extracted from
  7012. the car of the elements of the table.
  7013. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7014. (unless sorting-type
  7015. (message
  7016. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7017. what)
  7018. (setq sorting-type (read-char-exclusive)))
  7019. (let ((dcst (downcase sorting-type))
  7020. extractfun comparefun)
  7021. ;; Define the appropriate functions
  7022. (cond
  7023. ((= dcst ?n)
  7024. (setq extractfun 'string-to-number
  7025. comparefun (if (= dcst sorting-type) '< '>)))
  7026. ((= dcst ?a)
  7027. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7028. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7029. comparefun (if (= dcst sorting-type)
  7030. 'string<
  7031. (lambda (a b) (and (not (string< a b))
  7032. (not (string= a b)))))))
  7033. ((= dcst ?t)
  7034. (setq extractfun
  7035. (lambda (x)
  7036. (if (or (string-match org-ts-regexp x)
  7037. (string-match org-ts-regexp-both x))
  7038. (org-float-time
  7039. (org-time-string-to-time (match-string 0 x)))
  7040. 0))
  7041. comparefun (if (= dcst sorting-type) '< '>)))
  7042. (t (error "Invalid sorting type `%c'" sorting-type)))
  7043. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7044. table)
  7045. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7046. ;;; The orgstruct minor mode
  7047. ;; Define a minor mode which can be used in other modes in order to
  7048. ;; integrate the org-mode structure editing commands.
  7049. ;; This is really a hack, because the org-mode structure commands use
  7050. ;; keys which normally belong to the major mode. Here is how it
  7051. ;; works: The minor mode defines all the keys necessary to operate the
  7052. ;; structure commands, but wraps the commands into a function which
  7053. ;; tests if the cursor is currently at a headline or a plain list
  7054. ;; item. If that is the case, the structure command is used,
  7055. ;; temporarily setting many Org-mode variables like regular
  7056. ;; expressions for filling etc. However, when any of those keys is
  7057. ;; used at a different location, function uses `key-binding' to look
  7058. ;; up if the key has an associated command in another currently active
  7059. ;; keymap (minor modes, major mode, global), and executes that
  7060. ;; command. There might be problems if any of the keys is otherwise
  7061. ;; used as a prefix key.
  7062. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7063. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7064. ;; addresses this by checking explicitly for both bindings.
  7065. (defvar orgstruct-mode-map (make-sparse-keymap)
  7066. "Keymap for the minor `orgstruct-mode'.")
  7067. (defvar org-local-vars nil
  7068. "List of local variables, for use by `orgstruct-mode'.")
  7069. ;;;###autoload
  7070. (define-minor-mode orgstruct-mode
  7071. "Toggle the minor mode `orgstruct-mode'.
  7072. This mode is for using Org-mode structure commands in other
  7073. modes. The following keys behave as if Org-mode were active, if
  7074. the cursor is on a headline, or on a plain list item (both as
  7075. defined by Org-mode).
  7076. M-up Move entry/item up
  7077. M-down Move entry/item down
  7078. M-left Promote
  7079. M-right Demote
  7080. M-S-up Move entry/item up
  7081. M-S-down Move entry/item down
  7082. M-S-left Promote subtree
  7083. M-S-right Demote subtree
  7084. M-q Fill paragraph and items like in Org-mode
  7085. C-c ^ Sort entries
  7086. C-c - Cycle list bullet
  7087. TAB Cycle item visibility
  7088. M-RET Insert new heading/item
  7089. S-M-RET Insert new TODO heading / Checkbox item
  7090. C-c C-c Set tags / toggle checkbox"
  7091. nil " OrgStruct" nil
  7092. (org-load-modules-maybe)
  7093. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7094. ;;;###autoload
  7095. (defun turn-on-orgstruct ()
  7096. "Unconditionally turn on `orgstruct-mode'."
  7097. (orgstruct-mode 1))
  7098. (defun orgstruct++-mode (&optional arg)
  7099. "Toggle `orgstruct-mode', the enhanced version of it.
  7100. In addition to setting orgstruct-mode, this also exports all indentation
  7101. and autofilling variables from org-mode into the buffer. It will also
  7102. recognize item context in multiline items.
  7103. Note that turning off orgstruct-mode will *not* remove the
  7104. indentation/paragraph settings. This can only be done by refreshing the
  7105. major mode, for example with \\[normal-mode]."
  7106. (interactive "P")
  7107. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7108. (if (< arg 1)
  7109. (orgstruct-mode -1)
  7110. (orgstruct-mode 1)
  7111. (let (var val)
  7112. (mapc
  7113. (lambda (x)
  7114. (when (string-match
  7115. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7116. (symbol-name (car x)))
  7117. (setq var (car x) val (nth 1 x))
  7118. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7119. org-local-vars)
  7120. (org-set-local 'orgstruct-is-++ t))))
  7121. (defvar orgstruct-is-++ nil
  7122. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7123. (make-variable-buffer-local 'orgstruct-is-++)
  7124. ;;;###autoload
  7125. (defun turn-on-orgstruct++ ()
  7126. "Unconditionally turn on `orgstruct++-mode'."
  7127. (orgstruct++-mode 1))
  7128. (defun orgstruct-error ()
  7129. "Error when there is no default binding for a structure key."
  7130. (interactive)
  7131. (error "This key has no function outside structure elements"))
  7132. (defun orgstruct-setup ()
  7133. "Setup orgstruct keymaps."
  7134. (let ((nfunc 0)
  7135. (bindings
  7136. (list
  7137. '([(meta up)] org-metaup)
  7138. '([(meta down)] org-metadown)
  7139. '([(meta left)] org-metaleft)
  7140. '([(meta right)] org-metaright)
  7141. '([(meta shift up)] org-shiftmetaup)
  7142. '([(meta shift down)] org-shiftmetadown)
  7143. '([(meta shift left)] org-shiftmetaleft)
  7144. '([(meta shift right)] org-shiftmetaright)
  7145. '([?\e (up)] org-metaup)
  7146. '([?\e (down)] org-metadown)
  7147. '([?\e (left)] org-metaleft)
  7148. '([?\e (right)] org-metaright)
  7149. '([?\e (shift up)] org-shiftmetaup)
  7150. '([?\e (shift down)] org-shiftmetadown)
  7151. '([?\e (shift left)] org-shiftmetaleft)
  7152. '([?\e (shift right)] org-shiftmetaright)
  7153. '([(shift up)] org-shiftup)
  7154. '([(shift down)] org-shiftdown)
  7155. '([(shift left)] org-shiftleft)
  7156. '([(shift right)] org-shiftright)
  7157. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7158. '("\M-q" fill-paragraph)
  7159. '("\C-c^" org-sort)
  7160. '("\C-c-" org-cycle-list-bullet)))
  7161. elt key fun cmd)
  7162. (while (setq elt (pop bindings))
  7163. (setq nfunc (1+ nfunc))
  7164. (setq key (org-key (car elt))
  7165. fun (nth 1 elt)
  7166. cmd (orgstruct-make-binding fun nfunc key))
  7167. (org-defkey orgstruct-mode-map key cmd))
  7168. ;; Special treatment needed for TAB and RET
  7169. (org-defkey orgstruct-mode-map [(tab)]
  7170. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7171. (org-defkey orgstruct-mode-map "\C-i"
  7172. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7173. (org-defkey orgstruct-mode-map "\M-\C-m"
  7174. (orgstruct-make-binding 'org-insert-heading 105
  7175. "\M-\C-m" [(meta return)]))
  7176. (org-defkey orgstruct-mode-map [(meta return)]
  7177. (orgstruct-make-binding 'org-insert-heading 106
  7178. [(meta return)] "\M-\C-m"))
  7179. (org-defkey orgstruct-mode-map [(shift meta return)]
  7180. (orgstruct-make-binding 'org-insert-todo-heading 107
  7181. [(meta return)] "\M-\C-m"))
  7182. (org-defkey orgstruct-mode-map "\e\C-m"
  7183. (orgstruct-make-binding 'org-insert-heading 108
  7184. "\e\C-m" [?\e (return)]))
  7185. (org-defkey orgstruct-mode-map [?\e (return)]
  7186. (orgstruct-make-binding 'org-insert-heading 109
  7187. [?\e (return)] "\e\C-m"))
  7188. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7189. (orgstruct-make-binding 'org-insert-todo-heading 110
  7190. [?\e (return)] "\e\C-m"))
  7191. (unless org-local-vars
  7192. (setq org-local-vars (org-get-local-variables)))
  7193. t))
  7194. (defun orgstruct-make-binding (fun n &rest keys)
  7195. "Create a function for binding in the structure minor mode.
  7196. FUN is the command to call inside a table. N is used to create a unique
  7197. command name. KEYS are keys that should be checked in for a command
  7198. to execute outside of tables."
  7199. (eval
  7200. (list 'defun
  7201. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7202. '(arg)
  7203. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7204. "Outside of structure, run the binding of `"
  7205. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7206. "'.")
  7207. '(interactive "p")
  7208. (list 'if
  7209. `(org-context-p 'headline 'item
  7210. (and orgstruct-is-++
  7211. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7212. 'item-body))
  7213. (list 'org-run-like-in-org-mode (list 'quote fun))
  7214. (list 'let '(orgstruct-mode)
  7215. (list 'call-interactively
  7216. (append '(or)
  7217. (mapcar (lambda (k)
  7218. (list 'key-binding k))
  7219. keys)
  7220. '('orgstruct-error))))))))
  7221. (defun org-context-p (&rest contexts)
  7222. "Check if local context is any of CONTEXTS.
  7223. Possible values in the list of contexts are `table', `headline', and `item'."
  7224. (let ((pos (point)))
  7225. (goto-char (point-at-bol))
  7226. (prog1 (or (and (memq 'table contexts)
  7227. (looking-at "[ \t]*|"))
  7228. (and (memq 'headline contexts)
  7229. ;;????????? (looking-at "\\*+"))
  7230. (looking-at outline-regexp))
  7231. (and (memq 'item contexts)
  7232. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7233. (and (memq 'item-body contexts)
  7234. (org-in-item-p)))
  7235. (goto-char pos))))
  7236. (defun org-get-local-variables ()
  7237. "Return a list of all local variables in an org-mode buffer."
  7238. (let (varlist)
  7239. (with-current-buffer (get-buffer-create "*Org tmp*")
  7240. (erase-buffer)
  7241. (org-mode)
  7242. (setq varlist (buffer-local-variables)))
  7243. (kill-buffer "*Org tmp*")
  7244. (delq nil
  7245. (mapcar
  7246. (lambda (x)
  7247. (setq x
  7248. (if (symbolp x)
  7249. (list x)
  7250. (list (car x) (list 'quote (cdr x)))))
  7251. (if (string-match
  7252. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7253. (symbol-name (car x)))
  7254. x nil))
  7255. varlist))))
  7256. ;;;###autoload
  7257. (defun org-run-like-in-org-mode (cmd)
  7258. "Run a command, pretending that the current buffer is in Org-mode.
  7259. This will temporarily bind local variables that are typically bound in
  7260. Org-mode to the values they have in Org-mode, and then interactively
  7261. call CMD."
  7262. (org-load-modules-maybe)
  7263. (unless org-local-vars
  7264. (setq org-local-vars (org-get-local-variables)))
  7265. (eval (list 'let org-local-vars
  7266. (list 'call-interactively (list 'quote cmd)))))
  7267. ;;;; Archiving
  7268. (defun org-get-category (&optional pos)
  7269. "Get the category applying to position POS."
  7270. (get-text-property (or pos (point)) 'org-category))
  7271. (defun org-refresh-category-properties ()
  7272. "Refresh category text properties in the buffer."
  7273. (let ((def-cat (cond
  7274. ((null org-category)
  7275. (if buffer-file-name
  7276. (file-name-sans-extension
  7277. (file-name-nondirectory buffer-file-name))
  7278. "???"))
  7279. ((symbolp org-category) (symbol-name org-category))
  7280. (t org-category)))
  7281. beg end cat pos optionp)
  7282. (org-unmodified
  7283. (save-excursion
  7284. (save-restriction
  7285. (widen)
  7286. (goto-char (point-min))
  7287. (put-text-property (point) (point-max) 'org-category def-cat)
  7288. (while (re-search-forward
  7289. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7290. (setq pos (match-end 0)
  7291. optionp (equal (char-after (match-beginning 0)) ?#)
  7292. cat (org-trim (match-string 2)))
  7293. (if optionp
  7294. (setq beg (point-at-bol) end (point-max))
  7295. (org-back-to-heading t)
  7296. (setq beg (point) end (org-end-of-subtree t t)))
  7297. (put-text-property beg end 'org-category cat)
  7298. (goto-char pos)))))))
  7299. ;;;; Link Stuff
  7300. ;;; Link abbreviations
  7301. (defun org-link-expand-abbrev (link)
  7302. "Apply replacements as defined in `org-link-abbrev-alist."
  7303. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7304. (let* ((key (match-string 1 link))
  7305. (as (or (assoc key org-link-abbrev-alist-local)
  7306. (assoc key org-link-abbrev-alist)))
  7307. (tag (and (match-end 2) (match-string 3 link)))
  7308. rpl)
  7309. (if (not as)
  7310. link
  7311. (setq rpl (cdr as))
  7312. (cond
  7313. ((symbolp rpl) (funcall rpl tag))
  7314. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7315. ((string-match "%h" rpl)
  7316. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7317. (t (concat rpl tag)))))
  7318. link))
  7319. ;;; Storing and inserting links
  7320. (defvar org-insert-link-history nil
  7321. "Minibuffer history for links inserted with `org-insert-link'.")
  7322. (defvar org-stored-links nil
  7323. "Contains the links stored with `org-store-link'.")
  7324. (defvar org-store-link-plist nil
  7325. "Plist with info about the most recently link created with `org-store-link'.")
  7326. (defvar org-link-protocols nil
  7327. "Link protocols added to Org-mode using `org-add-link-type'.")
  7328. (defvar org-store-link-functions nil
  7329. "List of functions that are called to create and store a link.
  7330. Each function will be called in turn until one returns a non-nil
  7331. value. Each function should check if it is responsible for creating
  7332. this link (for example by looking at the major mode).
  7333. If not, it must exit and return nil.
  7334. If yes, it should return a non-nil value after a calling
  7335. `org-store-link-props' with a list of properties and values.
  7336. Special properties are:
  7337. :type The link prefix, like \"http\". This must be given.
  7338. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7339. This is obligatory as well.
  7340. :description Optional default description for the second pair
  7341. of brackets in an Org-mode link. The user can still change
  7342. this when inserting this link into an Org-mode buffer.
  7343. In addition to these, any additional properties can be specified
  7344. and then used in remember templates.")
  7345. (defun org-add-link-type (type &optional follow export)
  7346. "Add TYPE to the list of `org-link-types'.
  7347. Re-compute all regular expressions depending on `org-link-types'
  7348. FOLLOW and EXPORT are two functions.
  7349. FOLLOW should take the link path as the single argument and do whatever
  7350. is necessary to follow the link, for example find a file or display
  7351. a mail message.
  7352. EXPORT should format the link path for export to one of the export formats.
  7353. It should be a function accepting three arguments:
  7354. path the path of the link, the text after the prefix (like \"http:\")
  7355. desc the description of the link, if any, nil if there was no description
  7356. format the export format, a symbol like `html' or `latex'.
  7357. The function may use the FORMAT information to return different values
  7358. depending on the format. The return value will be put literally into
  7359. the exported file.
  7360. Org-mode has a built-in default for exporting links. If you are happy with
  7361. this default, there is no need to define an export function for the link
  7362. type. For a simple example of an export function, see `org-bbdb.el'."
  7363. (add-to-list 'org-link-types type t)
  7364. (org-make-link-regexps)
  7365. (if (assoc type org-link-protocols)
  7366. (setcdr (assoc type org-link-protocols) (list follow export))
  7367. (push (list type follow export) org-link-protocols)))
  7368. (defvar org-agenda-buffer-name)
  7369. ;;;###autoload
  7370. (defun org-store-link (arg)
  7371. "\\<org-mode-map>Store an org-link to the current location.
  7372. This link is added to `org-stored-links' and can later be inserted
  7373. into an org-buffer with \\[org-insert-link].
  7374. For some link types, a prefix arg is interpreted:
  7375. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7376. For file links, arg negates `org-context-in-file-links'."
  7377. (interactive "P")
  7378. (org-load-modules-maybe)
  7379. (setq org-store-link-plist nil) ; reset
  7380. (let ((outline-regexp (org-get-limited-outline-regexp))
  7381. link cpltxt desc description search txt custom-id agenda-link)
  7382. (cond
  7383. ((run-hook-with-args-until-success 'org-store-link-functions)
  7384. (setq link (plist-get org-store-link-plist :link)
  7385. desc (or (plist-get org-store-link-plist :description) link)))
  7386. ((equal (buffer-name) "*Org Edit Src Example*")
  7387. (let (label gc)
  7388. (while (or (not label)
  7389. (save-excursion
  7390. (save-restriction
  7391. (widen)
  7392. (goto-char (point-min))
  7393. (re-search-forward
  7394. (regexp-quote (format org-coderef-label-format label))
  7395. nil t))))
  7396. (when label (message "Label exists already") (sit-for 2))
  7397. (setq label (read-string "Code line label: " label)))
  7398. (end-of-line 1)
  7399. (setq link (format org-coderef-label-format label))
  7400. (setq gc (- 79 (length link)))
  7401. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7402. (insert link)
  7403. (setq link (concat "(" label ")") desc nil)))
  7404. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7405. ;; We are in the agenda, link to referenced location
  7406. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7407. (get-text-property (point) 'org-marker))))
  7408. (when m
  7409. (org-with-point-at m
  7410. (setq agenda-link
  7411. (if (interactive-p)
  7412. (call-interactively 'org-store-link)
  7413. (org-store-link nil)))))))
  7414. ((eq major-mode 'calendar-mode)
  7415. (let ((cd (calendar-cursor-to-date)))
  7416. (setq link
  7417. (format-time-string
  7418. (car org-time-stamp-formats)
  7419. (apply 'encode-time
  7420. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7421. nil nil nil))))
  7422. (org-store-link-props :type "calendar" :date cd)))
  7423. ((eq major-mode 'w3-mode)
  7424. (setq cpltxt (if (and (buffer-name)
  7425. (not (string-match "Untitled" (buffer-name))))
  7426. (buffer-name)
  7427. (url-view-url t))
  7428. link (org-make-link (url-view-url t)))
  7429. (org-store-link-props :type "w3" :url (url-view-url t)))
  7430. ((eq major-mode 'w3m-mode)
  7431. (setq cpltxt (or w3m-current-title w3m-current-url)
  7432. link (org-make-link w3m-current-url))
  7433. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7434. ((setq search (run-hook-with-args-until-success
  7435. 'org-create-file-search-functions))
  7436. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7437. "::" search))
  7438. (setq cpltxt (or description link)))
  7439. ((eq major-mode 'image-mode)
  7440. (setq cpltxt (concat "file:"
  7441. (abbreviate-file-name buffer-file-name))
  7442. link (org-make-link cpltxt))
  7443. (org-store-link-props :type "image" :file buffer-file-name))
  7444. ((eq major-mode 'dired-mode)
  7445. ;; link to the file in the current line
  7446. (let ((file (dired-get-filename nil t)))
  7447. (setq file (if file
  7448. (abbreviate-file-name
  7449. (expand-file-name (dired-get-filename nil t)))
  7450. ;; otherwise, no file so use current directory.
  7451. default-directory))
  7452. (setq cpltxt (concat "file:" file)
  7453. link (org-make-link cpltxt))))
  7454. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7455. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7456. (cond
  7457. ((org-in-regexp "<<\\(.*?\\)>>")
  7458. (setq cpltxt
  7459. (concat "file:"
  7460. (abbreviate-file-name
  7461. (buffer-file-name (buffer-base-buffer)))
  7462. "::" (match-string 1))
  7463. link (org-make-link cpltxt)))
  7464. ((and (featurep 'org-id)
  7465. (or (eq org-link-to-org-use-id t)
  7466. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7467. (interactive-p))
  7468. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7469. (interactive-p)
  7470. (not custom-id))
  7471. (and org-link-to-org-use-id
  7472. (condition-case nil
  7473. (org-entry-get nil "ID")
  7474. (error nil)))))
  7475. ;; We can make a link using the ID.
  7476. (setq link (condition-case nil
  7477. (prog1 (org-id-store-link)
  7478. (setq desc (plist-get org-store-link-plist
  7479. :description)))
  7480. (error
  7481. ;; probably before first headline, link to file only
  7482. (concat "file:"
  7483. (abbreviate-file-name
  7484. (buffer-file-name (buffer-base-buffer))))))))
  7485. (t
  7486. ;; Just link to current headline
  7487. (setq cpltxt (concat "file:"
  7488. (abbreviate-file-name
  7489. (buffer-file-name (buffer-base-buffer)))))
  7490. ;; Add a context search string
  7491. (when (org-xor org-context-in-file-links arg)
  7492. (setq txt (cond
  7493. ((org-on-heading-p) nil)
  7494. ((org-region-active-p)
  7495. (buffer-substring (region-beginning) (region-end)))
  7496. (t nil)))
  7497. (when (or (null txt) (string-match "\\S-" txt))
  7498. (setq cpltxt
  7499. (concat cpltxt "::"
  7500. (condition-case nil
  7501. (org-make-org-heading-search-string txt)
  7502. (error "")))
  7503. desc (or (nth 4 (ignore-errors
  7504. (org-heading-components))) "NONE"))))
  7505. (if (string-match "::\\'" cpltxt)
  7506. (setq cpltxt (substring cpltxt 0 -2)))
  7507. (setq link (org-make-link cpltxt)))))
  7508. ((buffer-file-name (buffer-base-buffer))
  7509. ;; Just link to this file here.
  7510. (setq cpltxt (concat "file:"
  7511. (abbreviate-file-name
  7512. (buffer-file-name (buffer-base-buffer)))))
  7513. ;; Add a context string
  7514. (when (org-xor org-context-in-file-links arg)
  7515. (setq txt (if (org-region-active-p)
  7516. (buffer-substring (region-beginning) (region-end))
  7517. (buffer-substring (point-at-bol) (point-at-eol))))
  7518. ;; Only use search option if there is some text.
  7519. (when (string-match "\\S-" txt)
  7520. (setq cpltxt
  7521. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7522. desc "NONE")))
  7523. (setq link (org-make-link cpltxt)))
  7524. ((interactive-p)
  7525. (error "Cannot link to a buffer which is not visiting a file"))
  7526. (t (setq link nil)))
  7527. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7528. (setq link (or link cpltxt)
  7529. desc (or desc cpltxt))
  7530. (if (equal desc "NONE") (setq desc nil))
  7531. (if (and (or (interactive-p) executing-kbd-macro) link)
  7532. (progn
  7533. (setq org-stored-links
  7534. (cons (list link desc) org-stored-links))
  7535. (message "Stored: %s" (or desc link))
  7536. (when custom-id
  7537. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7538. "::#" custom-id))
  7539. (setq org-stored-links
  7540. (cons (list link desc) org-stored-links))))
  7541. (or agenda-link (and link (org-make-link-string link desc))))))
  7542. (defun org-store-link-props (&rest plist)
  7543. "Store link properties, extract names and addresses."
  7544. (let (x adr)
  7545. (when (setq x (plist-get plist :from))
  7546. (setq adr (mail-extract-address-components x))
  7547. (setq plist (plist-put plist :fromname (car adr)))
  7548. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7549. (when (setq x (plist-get plist :to))
  7550. (setq adr (mail-extract-address-components x))
  7551. (setq plist (plist-put plist :toname (car adr)))
  7552. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7553. (let ((from (plist-get plist :from))
  7554. (to (plist-get plist :to)))
  7555. (when (and from to org-from-is-user-regexp)
  7556. (setq plist
  7557. (plist-put plist :fromto
  7558. (if (string-match org-from-is-user-regexp from)
  7559. (concat "to %t")
  7560. (concat "from %f"))))))
  7561. (setq org-store-link-plist plist))
  7562. (defun org-add-link-props (&rest plist)
  7563. "Add these properties to the link property list."
  7564. (let (key value)
  7565. (while plist
  7566. (setq key (pop plist) value (pop plist))
  7567. (setq org-store-link-plist
  7568. (plist-put org-store-link-plist key value)))))
  7569. (defun org-email-link-description (&optional fmt)
  7570. "Return the description part of an email link.
  7571. This takes information from `org-store-link-plist' and formats it
  7572. according to FMT (default from `org-email-link-description-format')."
  7573. (setq fmt (or fmt org-email-link-description-format))
  7574. (let* ((p org-store-link-plist)
  7575. (to (plist-get p :toaddress))
  7576. (from (plist-get p :fromaddress))
  7577. (table
  7578. (list
  7579. (cons "%c" (plist-get p :fromto))
  7580. (cons "%F" (plist-get p :from))
  7581. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7582. (cons "%T" (plist-get p :to))
  7583. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7584. (cons "%s" (plist-get p :subject))
  7585. (cons "%m" (plist-get p :message-id)))))
  7586. (when (string-match "%c" fmt)
  7587. ;; Check if the user wrote this message
  7588. (if (and org-from-is-user-regexp from to
  7589. (save-match-data (string-match org-from-is-user-regexp from)))
  7590. (setq fmt (replace-match "to %t" t t fmt))
  7591. (setq fmt (replace-match "from %f" t t fmt))))
  7592. (org-replace-escapes fmt table)))
  7593. (defun org-make-org-heading-search-string (&optional string heading)
  7594. "Make search string for STRING or current headline."
  7595. (interactive)
  7596. (let ((s (or string (org-get-heading))))
  7597. (unless (and string (not heading))
  7598. ;; We are using a headline, clean up garbage in there.
  7599. (if (string-match org-todo-regexp s)
  7600. (setq s (replace-match "" t t s)))
  7601. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7602. (setq s (replace-match "" t t s)))
  7603. (setq s (org-trim s))
  7604. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7605. org-comment-string "\\)") s)
  7606. (setq s (replace-match "" t t s)))
  7607. (while (string-match org-ts-regexp s)
  7608. (setq s (replace-match "" t t s))))
  7609. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7610. (setq s (replace-match " " t t s)))
  7611. (or string (setq s (concat "*" s))) ; Add * for headlines
  7612. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7613. (defun org-make-link (&rest strings)
  7614. "Concatenate STRINGS."
  7615. (apply 'concat strings))
  7616. (defun org-make-link-string (link &optional description)
  7617. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7618. (unless (string-match "\\S-" link)
  7619. (error "Empty link"))
  7620. (when (and description
  7621. (stringp description)
  7622. (not (string-match "\\S-" description)))
  7623. (setq description nil))
  7624. (when (stringp description)
  7625. ;; Remove brackets from the description, they are fatal.
  7626. (while (string-match "\\[" description)
  7627. (setq description (replace-match "{" t t description)))
  7628. (while (string-match "\\]" description)
  7629. (setq description (replace-match "}" t t description))))
  7630. (when (equal (org-link-escape link) description)
  7631. ;; No description needed, it is identical
  7632. (setq description nil))
  7633. (when (and (not description)
  7634. (not (equal link (org-link-escape link))))
  7635. (setq description (org-extract-attributes link)))
  7636. (concat "[[" (org-link-escape link) "]"
  7637. (if description (concat "[" description "]") "")
  7638. "]"))
  7639. (defconst org-link-escape-chars
  7640. '((?\ . "%20")
  7641. (?\[ . "%5B")
  7642. (?\] . "%5D")
  7643. (?\340 . "%E0") ; `a
  7644. (?\342 . "%E2") ; ^a
  7645. (?\347 . "%E7") ; ,c
  7646. (?\350 . "%E8") ; `e
  7647. (?\351 . "%E9") ; 'e
  7648. (?\352 . "%EA") ; ^e
  7649. (?\356 . "%EE") ; ^i
  7650. (?\364 . "%F4") ; ^o
  7651. (?\371 . "%F9") ; `u
  7652. (?\373 . "%FB") ; ^u
  7653. (?\; . "%3B")
  7654. ;; (?? . "%3F")
  7655. (?= . "%3D")
  7656. (?+ . "%2B")
  7657. )
  7658. "Association list of escapes for some characters problematic in links.
  7659. This is the list that is used for internal purposes.")
  7660. (defvar org-url-encoding-use-url-hexify nil)
  7661. (defconst org-link-escape-chars-browser
  7662. '((?\ . "%20")) ; 32 for the SPC char
  7663. "Association list of escapes for some characters problematic in links.
  7664. This is the list that is used before handing over to the browser.")
  7665. (defun org-link-escape (text &optional table)
  7666. "Escape characters in TEXT that are problematic for links."
  7667. (if (and org-url-encoding-use-url-hexify (not table))
  7668. (url-hexify-string text)
  7669. (setq table (or table org-link-escape-chars))
  7670. (when text
  7671. (let ((re (mapconcat (lambda (x) (regexp-quote
  7672. (char-to-string (car x))))
  7673. table "\\|")))
  7674. (while (string-match re text)
  7675. (setq text
  7676. (replace-match
  7677. (cdr (assoc (string-to-char (match-string 0 text))
  7678. table))
  7679. t t text)))
  7680. text))))
  7681. (defun org-link-unescape (text &optional table)
  7682. "Reverse the action of `org-link-escape'."
  7683. (if (and org-url-encoding-use-url-hexify (not table))
  7684. (url-unhex-string text)
  7685. (setq table (or table org-link-escape-chars))
  7686. (when text
  7687. (let ((case-fold-search t)
  7688. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7689. table "\\|")))
  7690. (while (string-match re text)
  7691. (setq text
  7692. (replace-match
  7693. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7694. table)))
  7695. t t text)))
  7696. text))))
  7697. (defun org-xor (a b)
  7698. "Exclusive or."
  7699. (if a (not b) b))
  7700. (defun org-fixup-message-id-for-http (s)
  7701. "Replace special characters in a message id, so it can be used in an http query."
  7702. (when (string-match "%" s)
  7703. (setq s (mapconcat (lambda (c)
  7704. (if (eq c ?%)
  7705. "%25"
  7706. (char-to-string c)))
  7707. s "")))
  7708. (while (string-match "<" s)
  7709. (setq s (replace-match "%3C" t t s)))
  7710. (while (string-match ">" s)
  7711. (setq s (replace-match "%3E" t t s)))
  7712. (while (string-match "@" s)
  7713. (setq s (replace-match "%40" t t s)))
  7714. s)
  7715. ;;;###autoload
  7716. (defun org-insert-link-global ()
  7717. "Insert a link like Org-mode does.
  7718. This command can be called in any mode to insert a link in Org-mode syntax."
  7719. (interactive)
  7720. (org-load-modules-maybe)
  7721. (org-run-like-in-org-mode 'org-insert-link))
  7722. (defun org-insert-link (&optional complete-file link-location)
  7723. "Insert a link. At the prompt, enter the link.
  7724. Completion can be used to insert any of the link protocol prefixes like
  7725. http or ftp in use.
  7726. The history can be used to select a link previously stored with
  7727. `org-store-link'. When the empty string is entered (i.e. if you just
  7728. press RET at the prompt), the link defaults to the most recently
  7729. stored link. As SPC triggers completion in the minibuffer, you need to
  7730. use M-SPC or C-q SPC to force the insertion of a space character.
  7731. You will also be prompted for a description, and if one is given, it will
  7732. be displayed in the buffer instead of the link.
  7733. If there is already a link at point, this command will allow you to edit link
  7734. and description parts.
  7735. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7736. be selected using completion. The path to the file will be relative to the
  7737. current directory if the file is in the current directory or a subdirectory.
  7738. Otherwise, the link will be the absolute path as completed in the minibuffer
  7739. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7740. option `org-link-file-path-type'.
  7741. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7742. the current directory or below.
  7743. With three \\[universal-argument] prefixes, negate the meaning of
  7744. `org-keep-stored-link-after-insertion'.
  7745. If `org-make-link-description-function' is non-nil, this function will be
  7746. called with the link target, and the result will be the default
  7747. link description.
  7748. If the LINK-LOCATION parameter is non-nil, this value will be
  7749. used as the link location instead of reading one interactively."
  7750. (interactive "P")
  7751. (let* ((wcf (current-window-configuration))
  7752. (region (if (org-region-active-p)
  7753. (buffer-substring (region-beginning) (region-end))))
  7754. (remove (and region (list (region-beginning) (region-end))))
  7755. (desc region)
  7756. tmphist ; byte-compile incorrectly complains about this
  7757. (link link-location)
  7758. entry file all-prefixes)
  7759. (cond
  7760. (link-location) ; specified by arg, just use it.
  7761. ((org-in-regexp org-bracket-link-regexp 1)
  7762. ;; We do have a link at point, and we are going to edit it.
  7763. (setq remove (list (match-beginning 0) (match-end 0)))
  7764. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7765. (setq link (read-string "Link: "
  7766. (org-link-unescape
  7767. (org-match-string-no-properties 1)))))
  7768. ((or (org-in-regexp org-angle-link-re)
  7769. (org-in-regexp org-plain-link-re))
  7770. ;; Convert to bracket link
  7771. (setq remove (list (match-beginning 0) (match-end 0))
  7772. link (read-string "Link: "
  7773. (org-remove-angle-brackets (match-string 0)))))
  7774. ((member complete-file '((4) (16)))
  7775. ;; Completing read for file names.
  7776. (setq link (org-file-complete-link complete-file)))
  7777. (t
  7778. ;; Read link, with completion for stored links.
  7779. (with-output-to-temp-buffer "*Org Links*"
  7780. (princ "Insert a link.
  7781. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7782. (when org-stored-links
  7783. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7784. (princ (mapconcat
  7785. (lambda (x)
  7786. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7787. (reverse org-stored-links) "\n"))))
  7788. (let ((cw (selected-window)))
  7789. (select-window (get-buffer-window "*Org Links*" 'visible))
  7790. (setq truncate-lines t)
  7791. (unless (pos-visible-in-window-p (point-max))
  7792. (org-fit-window-to-buffer))
  7793. (and (window-live-p cw) (select-window cw)))
  7794. ;; Fake a link history, containing the stored links.
  7795. (setq tmphist (append (mapcar 'car org-stored-links)
  7796. org-insert-link-history))
  7797. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7798. (mapcar 'car org-link-abbrev-alist)
  7799. org-link-types))
  7800. (unwind-protect
  7801. (progn
  7802. (setq link
  7803. (let ((org-completion-use-ido nil)
  7804. (org-completion-use-iswitchb nil))
  7805. (org-completing-read
  7806. "Link: "
  7807. (append
  7808. (mapcar (lambda (x) (list (concat x ":")))
  7809. all-prefixes)
  7810. (mapcar 'car org-stored-links))
  7811. nil nil nil
  7812. 'tmphist
  7813. (car (car org-stored-links)))))
  7814. (if (not (string-match "\\S-" link))
  7815. (error "No link selected"))
  7816. (if (or (member link all-prefixes)
  7817. (and (equal ":" (substring link -1))
  7818. (member (substring link 0 -1) all-prefixes)
  7819. (setq link (substring link 0 -1))))
  7820. (setq link (org-link-try-special-completion link))))
  7821. (set-window-configuration wcf)
  7822. (kill-buffer "*Org Links*"))
  7823. (setq entry (assoc link org-stored-links))
  7824. (or entry (push link org-insert-link-history))
  7825. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7826. (not org-keep-stored-link-after-insertion))
  7827. (setq org-stored-links (delq (assoc link org-stored-links)
  7828. org-stored-links)))
  7829. (setq desc (or desc (nth 1 entry)))))
  7830. (if (string-match org-plain-link-re link)
  7831. ;; URL-like link, normalize the use of angular brackets.
  7832. (setq link (org-make-link (org-remove-angle-brackets link))))
  7833. ;; Check if we are linking to the current file with a search option
  7834. ;; If yes, simplify the link by using only the search option.
  7835. (when (and buffer-file-name
  7836. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7837. (let* ((path (match-string 1 link))
  7838. (case-fold-search nil)
  7839. (search (match-string 2 link)))
  7840. (save-match-data
  7841. (if (equal (file-truename buffer-file-name) (file-truename path))
  7842. ;; We are linking to this same file, with a search option
  7843. (setq link search)))))
  7844. ;; Check if we can/should use a relative path. If yes, simplify the link
  7845. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7846. (let* ((type (match-string 1 link))
  7847. (path (match-string 2 link))
  7848. (origpath path)
  7849. (case-fold-search nil))
  7850. (cond
  7851. ((or (eq org-link-file-path-type 'absolute)
  7852. (equal complete-file '(16)))
  7853. (setq path (abbreviate-file-name (expand-file-name path))))
  7854. ((eq org-link-file-path-type 'noabbrev)
  7855. (setq path (expand-file-name path)))
  7856. ((eq org-link-file-path-type 'relative)
  7857. (setq path (file-relative-name path)))
  7858. (t
  7859. (save-match-data
  7860. (if (string-match (concat "^" (regexp-quote
  7861. (expand-file-name
  7862. (file-name-as-directory
  7863. default-directory))))
  7864. (expand-file-name path))
  7865. ;; We are linking a file with relative path name.
  7866. (setq path (substring (expand-file-name path)
  7867. (match-end 0)))
  7868. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7869. (setq link (concat type path))
  7870. (if (equal desc origpath)
  7871. (setq desc path))))
  7872. (if org-make-link-description-function
  7873. (setq desc (funcall org-make-link-description-function link desc)))
  7874. (setq desc (read-string "Description: " desc))
  7875. (unless (string-match "\\S-" desc) (setq desc nil))
  7876. (if remove (apply 'delete-region remove))
  7877. (insert (org-make-link-string link desc))))
  7878. (defun org-link-try-special-completion (type)
  7879. "If there is completion support for link type TYPE, offer it."
  7880. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7881. (if (functionp fun)
  7882. (funcall fun)
  7883. (read-string "Link (no completion support): " (concat type ":")))))
  7884. (defun org-file-complete-link (&optional arg)
  7885. "Create a file link using completion."
  7886. (let (file link)
  7887. (setq file (read-file-name "File: "))
  7888. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7889. (pwd1 (file-name-as-directory (abbreviate-file-name
  7890. (expand-file-name ".")))))
  7891. (cond
  7892. ((equal arg '(16))
  7893. (setq link (org-make-link
  7894. "file:"
  7895. (abbreviate-file-name (expand-file-name file)))))
  7896. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7897. (setq link (org-make-link "file:" (match-string 1 file))))
  7898. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7899. (expand-file-name file))
  7900. (setq link (org-make-link
  7901. "file:" (match-string 1 (expand-file-name file)))))
  7902. (t (setq link (org-make-link "file:" file)))))
  7903. link))
  7904. (defun org-completing-read (&rest args)
  7905. "Completing-read with SPACE being a normal character."
  7906. (let ((minibuffer-local-completion-map
  7907. (copy-keymap minibuffer-local-completion-map)))
  7908. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7909. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7910. (apply 'org-icompleting-read args)))
  7911. (defun org-completing-read-no-i (&rest args)
  7912. (let (org-completion-use-ido org-completion-use-iswitchb)
  7913. (apply 'org-completing-read args)))
  7914. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7915. "Use iswitch as a completing-read replacement to choose from choices.
  7916. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7917. from."
  7918. (let* ((iswitchb-use-virtual-buffers nil)
  7919. (iswitchb-make-buflist-hook
  7920. (lambda ()
  7921. (setq iswitchb-temp-buflist choices))))
  7922. (iswitchb-read-buffer prompt)))
  7923. (defun org-icompleting-read (&rest args)
  7924. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7925. (org-without-partial-completion
  7926. (if (and org-completion-use-ido
  7927. (fboundp 'ido-completing-read)
  7928. (boundp 'ido-mode) ido-mode
  7929. (listp (second args)))
  7930. (let ((ido-enter-matching-directory nil))
  7931. (apply 'ido-completing-read (concat (car args))
  7932. (if (consp (car (nth 1 args)))
  7933. (mapcar (lambda (x) (car x)) (nth 1 args))
  7934. (nth 1 args))
  7935. (cddr args)))
  7936. (if (and org-completion-use-iswitchb
  7937. (boundp 'iswitchb-mode) iswitchb-mode
  7938. (listp (second args)))
  7939. (apply 'org-iswitchb-completing-read (concat (car args))
  7940. (if (consp (car (nth 1 args)))
  7941. (mapcar (lambda (x) (car x)) (nth 1 args))
  7942. (nth 1 args))
  7943. (cddr args))
  7944. (apply 'completing-read args)))))
  7945. (defun org-extract-attributes (s)
  7946. "Extract the attributes cookie from a string and set as text property."
  7947. (let (a attr (start 0) key value)
  7948. (save-match-data
  7949. (when (string-match "{{\\([^}]+\\)}}$" s)
  7950. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7951. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7952. (setq key (match-string 1 a) value (match-string 2 a)
  7953. start (match-end 0)
  7954. attr (plist-put attr (intern key) value))))
  7955. (org-add-props s nil 'org-attr attr))
  7956. s))
  7957. (defun org-extract-attributes-from-string (tag)
  7958. (let (key value attr)
  7959. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7960. (setq key (match-string 1 tag) value (match-string 2 tag)
  7961. tag (replace-match "" t t tag)
  7962. attr (plist-put attr (intern key) value)))
  7963. (cons tag attr)))
  7964. (defun org-attributes-to-string (plist)
  7965. "Format a property list into an HTML attribute list."
  7966. (let ((s "") key value)
  7967. (while plist
  7968. (setq key (pop plist) value (pop plist))
  7969. (and value
  7970. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7971. s))
  7972. ;;; Opening/following a link
  7973. (defvar org-link-search-failed nil)
  7974. (defvar org-open-link-functions nil
  7975. "Hook for functions finding a plain text link.
  7976. These functions must take a single argument, the link content.
  7977. They will be called for links that look like [[link text][description]]
  7978. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7979. like a filename (e.g. \"./blue.png\").
  7980. These functions will be called *before* Org attempts to resolve the
  7981. link by doing text searches in the current buffer - so if you want a
  7982. link \"[[target]]\" to still find \"<<target>>\", your function should
  7983. handle this as a special case.
  7984. When the function does handle the link, it must return a non-nil value.
  7985. If it decides that it is not responsible for this link, it must return
  7986. nil to indicate that that Org-mode can continue with other options
  7987. like exact and fuzzy text search.")
  7988. (defun org-next-link ()
  7989. "Move forward to the next link.
  7990. If the link is in hidden text, expose it."
  7991. (interactive)
  7992. (when (and org-link-search-failed (eq this-command last-command))
  7993. (goto-char (point-min))
  7994. (message "Link search wrapped back to beginning of buffer"))
  7995. (setq org-link-search-failed nil)
  7996. (let* ((pos (point))
  7997. (ct (org-context))
  7998. (a (assoc :link ct)))
  7999. (if a (goto-char (nth 2 a)))
  8000. (if (re-search-forward org-any-link-re nil t)
  8001. (progn
  8002. (goto-char (match-beginning 0))
  8003. (if (org-invisible-p) (org-show-context)))
  8004. (goto-char pos)
  8005. (setq org-link-search-failed t)
  8006. (error "No further link found"))))
  8007. (defun org-previous-link ()
  8008. "Move backward to the previous link.
  8009. If the link is in hidden text, expose it."
  8010. (interactive)
  8011. (when (and org-link-search-failed (eq this-command last-command))
  8012. (goto-char (point-max))
  8013. (message "Link search wrapped back to end of buffer"))
  8014. (setq org-link-search-failed nil)
  8015. (let* ((pos (point))
  8016. (ct (org-context))
  8017. (a (assoc :link ct)))
  8018. (if a (goto-char (nth 1 a)))
  8019. (if (re-search-backward org-any-link-re nil t)
  8020. (progn
  8021. (goto-char (match-beginning 0))
  8022. (if (org-invisible-p) (org-show-context)))
  8023. (goto-char pos)
  8024. (setq org-link-search-failed t)
  8025. (error "No further link found"))))
  8026. (defun org-translate-link (s)
  8027. "Translate a link string if a translation function has been defined."
  8028. (if (and org-link-translation-function
  8029. (fboundp org-link-translation-function)
  8030. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8031. (progn
  8032. (setq s (funcall org-link-translation-function
  8033. (match-string 1) (match-string 2)))
  8034. (concat (car s) ":" (cdr s)))
  8035. s))
  8036. (defun org-translate-link-from-planner (type path)
  8037. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8038. This is still an experimental function, your mileage may vary."
  8039. (cond
  8040. ((member type '("http" "https" "news" "ftp"))
  8041. ;; standard Internet links are the same.
  8042. nil)
  8043. ((and (equal type "irc") (string-match "^//" path))
  8044. ;; Planner has two / at the beginning of an irc link, we have 1.
  8045. ;; We should have zero, actually....
  8046. (setq path (substring path 1)))
  8047. ((and (equal type "lisp") (string-match "^/" path))
  8048. ;; Planner has a slash, we do not.
  8049. (setq type "elisp" path (substring path 1)))
  8050. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8051. ;; A typical message link. Planner has the id after the final slash,
  8052. ;; we separate it with a hash mark
  8053. (setq path (concat (match-string 1 path) "#"
  8054. (org-remove-angle-brackets (match-string 2 path)))))
  8055. )
  8056. (cons type path))
  8057. (defun org-find-file-at-mouse (ev)
  8058. "Open file link or URL at mouse."
  8059. (interactive "e")
  8060. (mouse-set-point ev)
  8061. (org-open-at-point 'in-emacs))
  8062. (defun org-open-at-mouse (ev)
  8063. "Open file link or URL at mouse."
  8064. (interactive "e")
  8065. (mouse-set-point ev)
  8066. (if (eq major-mode 'org-agenda-mode)
  8067. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8068. (org-open-at-point))
  8069. (defvar org-window-config-before-follow-link nil
  8070. "The window configuration before following a link.
  8071. This is saved in case the need arises to restore it.")
  8072. (defvar org-open-link-marker (make-marker)
  8073. "Marker pointing to the location where `org-open-at-point; was called.")
  8074. ;;;###autoload
  8075. (defun org-open-at-point-global ()
  8076. "Follow a link like Org-mode does.
  8077. This command can be called in any mode to follow a link that has
  8078. Org-mode syntax."
  8079. (interactive)
  8080. (org-run-like-in-org-mode 'org-open-at-point))
  8081. ;;;###autoload
  8082. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8083. "Open a link in the string S, as if it was in Org-mode."
  8084. (interactive "sLink: \nP")
  8085. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8086. (with-temp-buffer
  8087. (let ((org-inhibit-startup t))
  8088. (org-mode)
  8089. (insert s)
  8090. (goto-char (point-min))
  8091. (when reference-buffer
  8092. (setq org-link-abbrev-alist-local
  8093. (with-current-buffer reference-buffer
  8094. org-link-abbrev-alist-local)))
  8095. (org-open-at-point arg reference-buffer)))))
  8096. (defvar org-open-at-point-functions nil
  8097. "Hook that is run when following a link at point.
  8098. Functions in this hook must return t if they identify and follow
  8099. a link at point. If they don't find anything interesting at point,
  8100. they must return nil.")
  8101. (defun org-open-at-point (&optional in-emacs reference-buffer)
  8102. "Open link at or after point.
  8103. If there is no link at point, this function will search forward up to
  8104. the end of the current line.
  8105. Normally, files will be opened by an appropriate application. If the
  8106. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  8107. With a double prefix argument, try to open outside of Emacs, in the
  8108. application the system uses for this file type."
  8109. (interactive "P")
  8110. ;; if in a code block, then open the block's results
  8111. (unless (call-interactively #'org-babel-open-src-block-result)
  8112. (org-load-modules-maybe)
  8113. (move-marker org-open-link-marker (point))
  8114. (setq org-window-config-before-follow-link (current-window-configuration))
  8115. (org-remove-occur-highlights nil nil t)
  8116. (cond
  8117. ((and (org-on-heading-p)
  8118. (not (org-in-regexp
  8119. (concat org-plain-link-re "\\|"
  8120. org-bracket-link-regexp "\\|"
  8121. org-angle-link-re "\\|"
  8122. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8123. (not (get-text-property (point) 'org-linked-text)))
  8124. (or (org-offer-links-in-entry in-emacs)
  8125. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8126. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8127. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8128. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8129. (org-footnote-action))
  8130. (t
  8131. (let (type path link line search (pos (point)))
  8132. (catch 'match
  8133. (save-excursion
  8134. (skip-chars-forward "^]\n\r")
  8135. (when (org-in-regexp org-bracket-link-regexp 1)
  8136. (setq link (org-extract-attributes
  8137. (org-link-unescape (org-match-string-no-properties 1))))
  8138. (while (string-match " *\n *" link)
  8139. (setq link (replace-match " " t t link)))
  8140. (setq link (org-link-expand-abbrev link))
  8141. (cond
  8142. ((or (file-name-absolute-p link)
  8143. (string-match "^\\.\\.?/" link))
  8144. (setq type "file" path link))
  8145. ((string-match org-link-re-with-space3 link)
  8146. (setq type (match-string 1 link) path (match-string 2 link)))
  8147. (t (setq type "thisfile" path link)))
  8148. (throw 'match t)))
  8149. (when (get-text-property (point) 'org-linked-text)
  8150. (setq type "thisfile"
  8151. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8152. (1+ (point)) (point))
  8153. path (buffer-substring
  8154. (previous-single-property-change pos 'org-linked-text)
  8155. (next-single-property-change pos 'org-linked-text)))
  8156. (throw 'match t))
  8157. (save-excursion
  8158. (when (or (org-in-regexp org-angle-link-re)
  8159. (org-in-regexp org-plain-link-re))
  8160. (setq type (match-string 1) path (match-string 2))
  8161. (throw 'match t)))
  8162. (save-excursion
  8163. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  8164. (setq type "tags"
  8165. path (match-string 1))
  8166. (while (string-match ":" path)
  8167. (setq path (replace-match "+" t t path)))
  8168. (throw 'match t)))
  8169. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8170. (setq type "tree-match"
  8171. path (match-string 1))
  8172. (throw 'match t)))
  8173. (unless path
  8174. (error "No link found"))
  8175. ;; switch back to reference buffer
  8176. ;; needed when if called in a temporary buffer through
  8177. ;; org-open-link-from-string
  8178. (with-current-buffer (or reference-buffer (current-buffer))
  8179. ;; Remove any trailing spaces in path
  8180. (if (string-match " +\\'" path)
  8181. (setq path (replace-match "" t t path)))
  8182. (if (and org-link-translation-function
  8183. (fboundp org-link-translation-function))
  8184. ;; Check if we need to translate the link
  8185. (let ((tmp (funcall org-link-translation-function type path)))
  8186. (setq type (car tmp) path (cdr tmp))))
  8187. (cond
  8188. ((assoc type org-link-protocols)
  8189. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8190. ((equal type "mailto")
  8191. (let ((cmd (car org-link-mailto-program))
  8192. (args (cdr org-link-mailto-program)) args1
  8193. (address path) (subject "") a)
  8194. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8195. (setq address (match-string 1 path)
  8196. subject (org-link-escape (match-string 2 path))))
  8197. (while args
  8198. (cond
  8199. ((not (stringp (car args))) (push (pop args) args1))
  8200. (t (setq a (pop args))
  8201. (if (string-match "%a" a)
  8202. (setq a (replace-match address t t a)))
  8203. (if (string-match "%s" a)
  8204. (setq a (replace-match subject t t a)))
  8205. (push a args1))))
  8206. (apply cmd (nreverse args1))))
  8207. ((member type '("http" "https" "ftp" "news"))
  8208. (browse-url (concat type ":" (org-link-escape
  8209. path org-link-escape-chars-browser))))
  8210. ((string= type "doi")
  8211. (browse-url (concat "http://dx.doi.org/"
  8212. (org-link-escape
  8213. path org-link-escape-chars-browser))))
  8214. ((member type '("message"))
  8215. (browse-url (concat type ":" path)))
  8216. ((string= type "tags")
  8217. (org-tags-view in-emacs path))
  8218. ((string= type "tree-match")
  8219. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8220. ((string= type "file")
  8221. (if (string-match "::\\([0-9]+\\)\\'" path)
  8222. (setq line (string-to-number (match-string 1 path))
  8223. path (substring path 0 (match-beginning 0)))
  8224. (if (string-match "::\\(.+\\)\\'" path)
  8225. (setq search (match-string 1 path)
  8226. path (substring path 0 (match-beginning 0)))))
  8227. (if (string-match "[*?{]" (file-name-nondirectory path))
  8228. (dired path)
  8229. (org-open-file path in-emacs line search)))
  8230. ((string= type "news")
  8231. (require 'org-gnus)
  8232. (org-gnus-follow-link path))
  8233. ((string= type "shell")
  8234. (let ((cmd path))
  8235. (if (or (not org-confirm-shell-link-function)
  8236. (funcall org-confirm-shell-link-function
  8237. (format "Execute \"%s\" in shell? "
  8238. (org-add-props cmd nil
  8239. 'face 'org-warning))))
  8240. (progn
  8241. (message "Executing %s" cmd)
  8242. (shell-command cmd))
  8243. (error "Abort"))))
  8244. ((string= type "elisp")
  8245. (let ((cmd path))
  8246. (if (or (not org-confirm-elisp-link-function)
  8247. (funcall org-confirm-elisp-link-function
  8248. (format "Execute \"%s\" as elisp? "
  8249. (org-add-props cmd nil
  8250. 'face 'org-warning))))
  8251. (message "%s => %s" cmd
  8252. (if (equal (string-to-char cmd) ?\()
  8253. (eval (read cmd))
  8254. (call-interactively (read cmd))))
  8255. (error "Abort"))))
  8256. ((and (string= type "thisfile")
  8257. (run-hook-with-args-until-success
  8258. 'org-open-link-functions path)))
  8259. ((string= type "thisfile")
  8260. (if in-emacs
  8261. (switch-to-buffer-other-window
  8262. (org-get-buffer-for-internal-link (current-buffer)))
  8263. (org-mark-ring-push))
  8264. (let ((cmd `(org-link-search
  8265. ,path
  8266. ,(cond ((equal in-emacs '(4)) 'occur)
  8267. ((equal in-emacs '(16)) 'org-occur)
  8268. (t nil))
  8269. ,pos)))
  8270. (condition-case nil (eval cmd)
  8271. (error (progn (widen) (eval cmd))))))
  8272. (t
  8273. (browse-url-at-point)))))))
  8274. (move-marker org-open-link-marker nil)
  8275. (run-hook-with-args 'org-follow-link-hook)))
  8276. (defun org-offer-links-in-entry (&optional nth zero)
  8277. "Offer links in the current entry and follow the selected link.
  8278. If there is only one link, follow it immediately as well.
  8279. If NTH is an integer, immediately pick the NTH link found.
  8280. If ZERO is a string, check also this string for a link, and if
  8281. there is one, offer it as link number zero."
  8282. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8283. "\\(" org-angle-link-re "\\)\\|"
  8284. "\\(" org-plain-link-re "\\)"))
  8285. (cnt ?0)
  8286. (in-emacs (if (integerp nth) nil nth))
  8287. have-zero end links link c)
  8288. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8289. (push (match-string 0 zero) links)
  8290. (setq cnt (1- cnt) have-zero t))
  8291. (save-excursion
  8292. (org-back-to-heading t)
  8293. (setq end (save-excursion (outline-next-heading) (point)))
  8294. (while (re-search-forward re end t)
  8295. (push (match-string 0) links))
  8296. (setq links (org-uniquify (reverse links))))
  8297. (cond
  8298. ((null links)
  8299. (message "No links"))
  8300. ((equal (length links) 1)
  8301. (setq link (list (car links))))
  8302. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8303. (setq link (nth (if have-zero nth (1- nth)) links)))
  8304. (t ; we have to select a link
  8305. (save-excursion
  8306. (save-window-excursion
  8307. (delete-other-windows)
  8308. (with-output-to-temp-buffer "*Select Link*"
  8309. (mapc (lambda (l)
  8310. (if (not (string-match org-bracket-link-regexp l))
  8311. (princ (format "[%c] %s\n" (incf cnt)
  8312. (org-remove-angle-brackets l)))
  8313. (if (match-end 3)
  8314. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8315. (match-string 3 l) (match-string 1 l)))
  8316. (princ (format "[%c] %s\n" (incf cnt)
  8317. (match-string 1 l))))))
  8318. links))
  8319. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8320. (message "Select link to open, RET to open all:")
  8321. (setq c (read-char-exclusive))
  8322. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8323. (when (equal c ?q) (error "Abort"))
  8324. (if (equal c ?\C-m)
  8325. (setq link links)
  8326. (setq nth (- c ?0))
  8327. (if have-zero (setq nth (1+ nth)))
  8328. (unless (and (integerp nth) (>= (length links) nth))
  8329. (error "Invalid link selection"))
  8330. (setq link (list (nth (1- nth) links))))))
  8331. (if link
  8332. (let ((buf (current-buffer)))
  8333. (dolist (l link)
  8334. (org-open-link-from-string l in-emacs buf))
  8335. t)
  8336. nil)))
  8337. ;; Add special file links that specify the way of opening
  8338. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8339. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8340. (defun org-open-file-with-system (path)
  8341. "Open file at PATH using the system way of opening it."
  8342. (org-open-file path 'system))
  8343. (defun org-open-file-with-emacs (path)
  8344. "Open file at PATH in Emacs."
  8345. (org-open-file path 'emacs))
  8346. (defun org-remove-file-link-modifiers ()
  8347. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8348. (goto-char (point-min))
  8349. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8350. (org-if-unprotected
  8351. (replace-match "file:" t t))))
  8352. (eval-after-load "org-exp"
  8353. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8354. 'org-remove-file-link-modifiers))
  8355. ;;;; Time estimates
  8356. (defun org-get-effort (&optional pom)
  8357. "Get the effort estimate for the current entry."
  8358. (org-entry-get pom org-effort-property))
  8359. ;;; File search
  8360. (defvar org-create-file-search-functions nil
  8361. "List of functions to construct the right search string for a file link.
  8362. These functions are called in turn with point at the location to
  8363. which the link should point.
  8364. A function in the hook should first test if it would like to
  8365. handle this file type, for example by checking the `major-mode'
  8366. or the file extension. If it decides not to handle this file, it
  8367. should just return nil to give other functions a chance. If it
  8368. does handle the file, it must return the search string to be used
  8369. when following the link. The search string will be part of the
  8370. file link, given after a double colon, and `org-open-at-point'
  8371. will automatically search for it. If special measures must be
  8372. taken to make the search successful, another function should be
  8373. added to the companion hook `org-execute-file-search-functions',
  8374. which see.
  8375. A function in this hook may also use `setq' to set the variable
  8376. `description' to provide a suggestion for the descriptive text to
  8377. be used for this link when it gets inserted into an Org-mode
  8378. buffer with \\[org-insert-link].")
  8379. (defvar org-execute-file-search-functions nil
  8380. "List of functions to execute a file search triggered by a link.
  8381. Functions added to this hook must accept a single argument, the
  8382. search string that was part of the file link, the part after the
  8383. double colon. The function must first check if it would like to
  8384. handle this search, for example by checking the `major-mode' or
  8385. the file extension. If it decides not to handle this search, it
  8386. should just return nil to give other functions a chance. If it
  8387. does handle the search, it must return a non-nil value to keep
  8388. other functions from trying.
  8389. Each function can access the current prefix argument through the
  8390. variable `current-prefix-argument'. Note that a single prefix is
  8391. used to force opening a link in Emacs, so it may be good to only
  8392. use a numeric or double prefix to guide the search function.
  8393. In case this is needed, a function in this hook can also restore
  8394. the window configuration before `org-open-at-point' was called using:
  8395. (set-window-configuration org-window-config-before-follow-link)")
  8396. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8397. (defun org-link-search (s &optional type avoid-pos)
  8398. "Search for a link search option.
  8399. If S is surrounded by forward slashes, it is interpreted as a
  8400. regular expression. In org-mode files, this will create an `org-occur'
  8401. sparse tree. In ordinary files, `occur' will be used to list matches.
  8402. If the current buffer is in `dired-mode', grep will be used to search
  8403. in all files. If AVOID-POS is given, ignore matches near that position."
  8404. (let ((case-fold-search t)
  8405. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8406. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8407. (append '(("") (" ") ("\t") ("\n"))
  8408. org-emphasis-alist)
  8409. "\\|") "\\)"))
  8410. (pos (point))
  8411. (pre nil) (post nil)
  8412. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8413. (cond
  8414. ;; First check if there are any special search functions
  8415. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8416. ;; Now try the builtin stuff
  8417. ((and (equal (string-to-char s0) ?#)
  8418. (> (length s0) 1)
  8419. (save-excursion
  8420. (goto-char (point-min))
  8421. (and
  8422. (re-search-forward
  8423. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8424. (setq type 'dedicated
  8425. pos (match-beginning 0))))
  8426. ;; There is an exact target for this
  8427. (goto-char pos)
  8428. (org-back-to-heading t)))
  8429. ((save-excursion
  8430. (goto-char (point-min))
  8431. (and
  8432. (re-search-forward
  8433. (concat "<<" (regexp-quote s0) ">>") nil t)
  8434. (setq type 'dedicated
  8435. pos (match-beginning 0))))
  8436. ;; There is an exact target for this
  8437. (goto-char pos))
  8438. ((and (string-match "^(\\(.*\\))$" s0)
  8439. (save-excursion
  8440. (goto-char (point-min))
  8441. (and
  8442. (re-search-forward
  8443. (concat "[^[]" (regexp-quote
  8444. (format org-coderef-label-format
  8445. (match-string 1 s0))))
  8446. nil t)
  8447. (setq type 'dedicated
  8448. pos (1+ (match-beginning 0))))))
  8449. ;; There is a coderef target for this
  8450. (goto-char pos))
  8451. ((string-match "^/\\(.*\\)/$" s)
  8452. ;; A regular expression
  8453. (cond
  8454. ((org-mode-p)
  8455. (org-occur (match-string 1 s)))
  8456. ;;((eq major-mode 'dired-mode)
  8457. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8458. (t (org-do-occur (match-string 1 s)))))
  8459. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8460. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8461. (goto-char (point-min))
  8462. (cond
  8463. ((let (case-fold-search)
  8464. (re-search-forward (format org-complex-heading-regexp-format
  8465. (regexp-quote s))
  8466. nil t))
  8467. ;; OK, found a match
  8468. (goto-char (match-beginning 0)))
  8469. ((and (not org-link-search-inhibit-query)
  8470. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8471. (y-or-n-p "No match - create this as a new heading? "))
  8472. (goto-char (point-max))
  8473. (or (bolp) (newline))
  8474. (insert "* " s "\n")
  8475. (beginning-of-line 0))
  8476. (t
  8477. (goto-char pos)
  8478. (error "No match"))))
  8479. (t
  8480. ;; A normal search string
  8481. (when (equal (string-to-char s) ?*)
  8482. ;; Anchor on headlines, post may include tags.
  8483. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8484. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8485. s (substring s 1)))
  8486. (remove-text-properties
  8487. 0 (length s)
  8488. '(face nil mouse-face nil keymap nil fontified nil) s)
  8489. ;; Make a series of regular expressions to find a match
  8490. (setq words (org-split-string s "[ \n\r\t]+")
  8491. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8492. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8493. "\\)" markers)
  8494. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8495. re2a (concat "[ \t\r\n]" re2a_)
  8496. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8497. re4 (concat "[^a-zA-Z_]" re4_)
  8498. re1 (concat pre re2 post)
  8499. re3 (concat pre (if pre re4_ re4) post)
  8500. re5 (concat pre ".*" re4)
  8501. re2 (concat pre re2)
  8502. re2a (concat pre (if pre re2a_ re2a))
  8503. re4 (concat pre (if pre re4_ re4))
  8504. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8505. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8506. re5 "\\)"
  8507. ))
  8508. (cond
  8509. ((eq type 'org-occur) (org-occur reall))
  8510. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8511. (t (goto-char (point-min))
  8512. (setq type 'fuzzy)
  8513. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8514. (org-search-not-self 1 re1 nil t)
  8515. (org-search-not-self 1 re2 nil t)
  8516. (org-search-not-self 1 re2a nil t)
  8517. (org-search-not-self 1 re3 nil t)
  8518. (org-search-not-self 1 re4 nil t)
  8519. (org-search-not-self 1 re5 nil t)
  8520. )
  8521. (goto-char (match-beginning 1))
  8522. (goto-char pos)
  8523. (error "No match"))))))
  8524. (and (org-mode-p) (org-show-context 'link-search))
  8525. type))
  8526. (defun org-search-not-self (group &rest args)
  8527. "Execute `re-search-forward', but only accept matches that do not
  8528. enclose the position of `org-open-link-marker'."
  8529. (let ((m org-open-link-marker))
  8530. (catch 'exit
  8531. (while (apply 're-search-forward args)
  8532. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8533. (goto-char (match-end group))
  8534. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8535. (> (match-beginning 0) (marker-position m))
  8536. (< (match-end 0) (marker-position m)))
  8537. (save-match-data
  8538. (or (not (org-in-regexp
  8539. org-bracket-link-analytic-regexp 1))
  8540. (not (match-end 4)) ; no description
  8541. (and (<= (match-beginning 4) (point))
  8542. (>= (match-end 4) (point))))))
  8543. (throw 'exit (point))))))))
  8544. (defun org-get-buffer-for-internal-link (buffer)
  8545. "Return a buffer to be used for displaying the link target of internal links."
  8546. (cond
  8547. ((not org-display-internal-link-with-indirect-buffer)
  8548. buffer)
  8549. ((string-match "(Clone)$" (buffer-name buffer))
  8550. (message "Buffer is already a clone, not making another one")
  8551. ;; we also do not modify visibility in this case
  8552. buffer)
  8553. (t ; make a new indirect buffer for displaying the link
  8554. (let* ((bn (buffer-name buffer))
  8555. (ibn (concat bn "(Clone)"))
  8556. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8557. (with-current-buffer ib (org-overview))
  8558. ib))))
  8559. (defun org-do-occur (regexp &optional cleanup)
  8560. "Call the Emacs command `occur'.
  8561. If CLEANUP is non-nil, remove the printout of the regular expression
  8562. in the *Occur* buffer. This is useful if the regex is long and not useful
  8563. to read."
  8564. (occur regexp)
  8565. (when cleanup
  8566. (let ((cwin (selected-window)) win beg end)
  8567. (when (setq win (get-buffer-window "*Occur*"))
  8568. (select-window win))
  8569. (goto-char (point-min))
  8570. (when (re-search-forward "match[a-z]+" nil t)
  8571. (setq beg (match-end 0))
  8572. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8573. (setq end (1- (match-beginning 0)))))
  8574. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8575. (goto-char (point-min))
  8576. (select-window cwin))))
  8577. ;;; The mark ring for links jumps
  8578. (defvar org-mark-ring nil
  8579. "Mark ring for positions before jumps in Org-mode.")
  8580. (defvar org-mark-ring-last-goto nil
  8581. "Last position in the mark ring used to go back.")
  8582. ;; Fill and close the ring
  8583. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8584. (loop for i from 1 to org-mark-ring-length do
  8585. (push (make-marker) org-mark-ring))
  8586. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8587. org-mark-ring)
  8588. (defun org-mark-ring-push (&optional pos buffer)
  8589. "Put the current position or POS into the mark ring and rotate it."
  8590. (interactive)
  8591. (setq pos (or pos (point)))
  8592. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8593. (move-marker (car org-mark-ring)
  8594. (or pos (point))
  8595. (or buffer (current-buffer)))
  8596. (message "%s"
  8597. (substitute-command-keys
  8598. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8599. (defun org-mark-ring-goto (&optional n)
  8600. "Jump to the previous position in the mark ring.
  8601. With prefix arg N, jump back that many stored positions. When
  8602. called several times in succession, walk through the entire ring.
  8603. Org-mode commands jumping to a different position in the current file,
  8604. or to another Org-mode file, automatically push the old position
  8605. onto the ring."
  8606. (interactive "p")
  8607. (let (p m)
  8608. (if (eq last-command this-command)
  8609. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8610. (setq p org-mark-ring))
  8611. (setq org-mark-ring-last-goto p)
  8612. (setq m (car p))
  8613. (switch-to-buffer (marker-buffer m))
  8614. (goto-char m)
  8615. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8616. (defun org-remove-angle-brackets (s)
  8617. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8618. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8619. s)
  8620. (defun org-add-angle-brackets (s)
  8621. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8622. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8623. s)
  8624. (defun org-remove-double-quotes (s)
  8625. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8626. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8627. s)
  8628. ;;; Following specific links
  8629. (defun org-follow-timestamp-link ()
  8630. (cond
  8631. ((org-at-date-range-p t)
  8632. (let ((org-agenda-start-on-weekday)
  8633. (t1 (match-string 1))
  8634. (t2 (match-string 2)))
  8635. (setq t1 (time-to-days (org-time-string-to-time t1))
  8636. t2 (time-to-days (org-time-string-to-time t2)))
  8637. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8638. ((org-at-timestamp-p t)
  8639. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8640. (substring (match-string 1) 0 10)))
  8641. 1))
  8642. (t (error "This should not happen"))))
  8643. ;;; Following file links
  8644. (defvar org-wait nil)
  8645. (defun org-open-file (path &optional in-emacs line search)
  8646. "Open the file at PATH.
  8647. First, this expands any special file name abbreviations. Then the
  8648. configuration variable `org-file-apps' is checked if it contains an
  8649. entry for this file type, and if yes, the corresponding command is launched.
  8650. If no application is found, Emacs simply visits the file.
  8651. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8652. With a double \\[universal-argument] \\[universal-argument] \
  8653. prefix arg, Org tries to avoid opening in Emacs
  8654. and to use an external application to visit the file.
  8655. Optional LINE specifies a line to go to, optional SEARCH a string
  8656. to search for. If LINE or SEARCH is given, the file will be
  8657. opened in Emacs, unless an entry from org-file-apps that makes
  8658. use of groups in a regexp matches.
  8659. If the file does not exist, an error is thrown."
  8660. (let* ((file (if (equal path "")
  8661. buffer-file-name
  8662. (substitute-in-file-name (expand-file-name path))))
  8663. (file-apps (append org-file-apps (org-default-apps)))
  8664. (apps (org-remove-if
  8665. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8666. (apps-dlink (org-remove-if-not
  8667. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8668. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8669. (dirp (if remp nil (file-directory-p file)))
  8670. (file (if (and dirp org-open-directory-means-index-dot-org)
  8671. (concat (file-name-as-directory file) "index.org")
  8672. file))
  8673. (a-m-a-p (assq 'auto-mode apps))
  8674. (dfile (downcase file))
  8675. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8676. (link (cond ((and (eq line nil)
  8677. (eq search nil))
  8678. file)
  8679. (line
  8680. (concat file "::" (number-to-string line)))
  8681. (search
  8682. (concat file "::" search))))
  8683. (dlink (downcase link))
  8684. (old-buffer (current-buffer))
  8685. (old-pos (point))
  8686. (old-mode major-mode)
  8687. ext cmd link-match-data)
  8688. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8689. (setq ext (match-string 1 dfile))
  8690. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8691. (setq ext (match-string 1 dfile))))
  8692. (cond
  8693. ((member in-emacs '((16) system))
  8694. (setq cmd (cdr (assoc 'system apps))))
  8695. (in-emacs (setq cmd 'emacs))
  8696. (t
  8697. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8698. (and dirp (cdr (assoc 'directory apps)))
  8699. ; first, try matching against apps-dlink
  8700. ; if we get a match here, store the match data for later
  8701. (let ((match (assoc-default dlink apps-dlink
  8702. 'string-match)))
  8703. (if match
  8704. (progn (setq link-match-data (match-data))
  8705. match)
  8706. (progn (setq in-emacs (or in-emacs line search))
  8707. nil))) ; if we have no match in apps-dlink,
  8708. ; always open the file in emacs if line or search
  8709. ; is given (for backwards compatibility)
  8710. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8711. 'string-match)
  8712. (cdr (assoc ext apps))
  8713. (cdr (assoc t apps))))))
  8714. (when (eq cmd 'system)
  8715. (setq cmd (cdr (assoc 'system apps))))
  8716. (when (eq cmd 'default)
  8717. (setq cmd (cdr (assoc t apps))))
  8718. (when (eq cmd 'mailcap)
  8719. (require 'mailcap)
  8720. (mailcap-parse-mailcaps)
  8721. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8722. (command (mailcap-mime-info mime-type)))
  8723. (if (stringp command)
  8724. (setq cmd command)
  8725. (setq cmd 'emacs))))
  8726. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8727. (not (file-exists-p file))
  8728. (not org-open-non-existing-files))
  8729. (error "No such file: %s" file))
  8730. (cond
  8731. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8732. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8733. (while (string-match "['\"]%s['\"]" cmd)
  8734. (setq cmd (replace-match "%s" t t cmd)))
  8735. (while (string-match "%s" cmd)
  8736. (setq cmd (replace-match
  8737. (save-match-data
  8738. (shell-quote-argument
  8739. (convert-standard-filename file)))
  8740. t t cmd)))
  8741. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8742. (save-match-data
  8743. (let ((match-index 1)
  8744. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8745. (set-match-data link-match-data)
  8746. (while (<= match-index number-of-groups)
  8747. (let ((regex (concat "%" (number-to-string match-index)))
  8748. (replace-with (match-string match-index dlink)))
  8749. (while (string-match regex cmd)
  8750. (setq cmd (replace-match replace-with t t cmd))))
  8751. (setq match-index (+ match-index 1)))))
  8752. (save-window-excursion
  8753. (start-process-shell-command cmd nil cmd)
  8754. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8755. ))
  8756. ((or (stringp cmd)
  8757. (eq cmd 'emacs))
  8758. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8759. (widen)
  8760. (if line (org-goto-line line)
  8761. (if search (org-link-search search))))
  8762. ((consp cmd)
  8763. (let ((file (convert-standard-filename file)))
  8764. (save-match-data
  8765. (set-match-data link-match-data)
  8766. (eval cmd))))
  8767. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8768. (and (org-mode-p) (eq old-mode 'org-mode)
  8769. (or (not (equal old-buffer (current-buffer)))
  8770. (not (equal old-pos (point))))
  8771. (org-mark-ring-push old-pos old-buffer))))
  8772. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8773. "This function returns non-nil if `entry' uses a regular
  8774. expression which should be matched against the whole link by
  8775. org-open-file.
  8776. It assumes that is the case when the entry uses a regular
  8777. expression which has at least one grouping construct and the
  8778. action is either a lisp form or a command string containing
  8779. '%1', i.e. using at least one subexpression match as a
  8780. parameter."
  8781. (let ((selector (car entry))
  8782. (action (cdr entry)))
  8783. (if (stringp selector)
  8784. (and (> (regexp-opt-depth selector) 0)
  8785. (or (and (stringp action)
  8786. (string-match "%[0-9]" action))
  8787. (consp action)))
  8788. nil)))
  8789. (defun org-default-apps ()
  8790. "Return the default applications for this operating system."
  8791. (cond
  8792. ((eq system-type 'darwin)
  8793. org-file-apps-defaults-macosx)
  8794. ((eq system-type 'windows-nt)
  8795. org-file-apps-defaults-windowsnt)
  8796. (t org-file-apps-defaults-gnu)))
  8797. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8798. "Convert extensions to regular expressions in the cars of LIST.
  8799. Also, weed out any non-string entries, because the return value is used
  8800. only for regexp matching.
  8801. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8802. point to the symbol `emacs', indicating that the file should
  8803. be opened in Emacs."
  8804. (append
  8805. (delq nil
  8806. (mapcar (lambda (x)
  8807. (if (not (stringp (car x)))
  8808. nil
  8809. (if (string-match "\\W" (car x))
  8810. x
  8811. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8812. list))
  8813. (if add-auto-mode
  8814. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8815. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8816. (defun org-file-remote-p (file)
  8817. "Test whether FILE specifies a location on a remote system.
  8818. Return non-nil if the location is indeed remote.
  8819. For example, the filename \"/user@host:/foo\" specifies a location
  8820. on the system \"/user@host:\"."
  8821. (cond ((fboundp 'file-remote-p)
  8822. (file-remote-p file))
  8823. ((fboundp 'tramp-handle-file-remote-p)
  8824. (tramp-handle-file-remote-p file))
  8825. ((and (boundp 'ange-ftp-name-format)
  8826. (string-match (car ange-ftp-name-format) file))
  8827. t)
  8828. (t nil)))
  8829. ;;;; Refiling
  8830. (defun org-get-org-file ()
  8831. "Read a filename, with default directory `org-directory'."
  8832. (let ((default (or org-default-notes-file remember-data-file)))
  8833. (read-file-name (format "File name [%s]: " default)
  8834. (file-name-as-directory org-directory)
  8835. default)))
  8836. (defun org-notes-order-reversed-p ()
  8837. "Check if the current file should receive notes in reversed order."
  8838. (cond
  8839. ((not org-reverse-note-order) nil)
  8840. ((eq t org-reverse-note-order) t)
  8841. ((not (listp org-reverse-note-order)) nil)
  8842. (t (catch 'exit
  8843. (let ((all org-reverse-note-order)
  8844. entry)
  8845. (while (setq entry (pop all))
  8846. (if (string-match (car entry) buffer-file-name)
  8847. (throw 'exit (cdr entry))))
  8848. nil)))))
  8849. (defvar org-refile-target-table nil
  8850. "The list of refile targets, created by `org-refile'.")
  8851. (defvar org-agenda-new-buffers nil
  8852. "Buffers created to visit agenda files.")
  8853. (defvar org-refile-cache nil
  8854. "Cache for refile targets.")
  8855. (defvar org-refile-markers nil
  8856. "All the markers used for caching refile locations.")
  8857. (defun org-refile-marker (pos)
  8858. "Get a new refile marker, but only if caching is in use."
  8859. (if (not org-refile-use-cache)
  8860. pos
  8861. (let ((m (make-marker)))
  8862. (move-marker m pos)
  8863. (push m org-refile-markers)
  8864. m)))
  8865. (defun org-refile-cache-clear ()
  8866. "Clear the refile cache and disable all the markers."
  8867. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8868. (setq org-refile-markers nil)
  8869. (setq org-refile-cache nil)
  8870. (message "Refile cache has been cleared"))
  8871. (defun org-refile-cache-check-set (set)
  8872. "Check if all the markers in the cache still have live buffers."
  8873. (let (marker)
  8874. (catch 'exit
  8875. (while (and set (setq marker (nth 3 (pop set))))
  8876. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8877. (when (and marker (null (marker-buffer marker)))
  8878. (message "not found") (sit-for 3)
  8879. (throw 'exit nil)))
  8880. t)))
  8881. (defun org-refile-cache-put (set &rest identifiers)
  8882. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8883. (let* ((key (sha1 (prin1-to-string identifiers)))
  8884. (entry (assoc key org-refile-cache)))
  8885. (if entry
  8886. (setcdr entry set)
  8887. (push (cons key set) org-refile-cache))))
  8888. (defun org-refile-cache-get (&rest identifiers)
  8889. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8890. (cond
  8891. ((not org-refile-cache) nil)
  8892. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8893. (t
  8894. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8895. org-refile-cache))))
  8896. (and set (org-refile-cache-check-set set) set)))))
  8897. (defun org-get-refile-targets (&optional default-buffer)
  8898. "Produce a table with refile targets."
  8899. (let ((case-fold-search nil)
  8900. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8901. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8902. targets tgs txt re files f desc descre fast-path-p level pos0)
  8903. (message "Getting targets...")
  8904. (with-current-buffer (or default-buffer (current-buffer))
  8905. (while (setq entry (pop entries))
  8906. (setq files (car entry) desc (cdr entry))
  8907. (setq fast-path-p nil)
  8908. (cond
  8909. ((null files) (setq files (list (current-buffer))))
  8910. ((eq files 'org-agenda-files)
  8911. (setq files (org-agenda-files 'unrestricted)))
  8912. ((and (symbolp files) (fboundp files))
  8913. (setq files (funcall files)))
  8914. ((and (symbolp files) (boundp files))
  8915. (setq files (symbol-value files))))
  8916. (if (stringp files) (setq files (list files)))
  8917. (cond
  8918. ((eq (car desc) :tag)
  8919. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8920. ((eq (car desc) :todo)
  8921. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8922. ((eq (car desc) :regexp)
  8923. (setq descre (cdr desc)))
  8924. ((eq (car desc) :level)
  8925. (setq descre (concat "^\\*\\{" (number-to-string
  8926. (if org-odd-levels-only
  8927. (1- (* 2 (cdr desc)))
  8928. (cdr desc)))
  8929. "\\}[ \t]")))
  8930. ((eq (car desc) :maxlevel)
  8931. (setq fast-path-p t)
  8932. (setq descre (concat "^\\*\\{1," (number-to-string
  8933. (if org-odd-levels-only
  8934. (1- (* 2 (cdr desc)))
  8935. (cdr desc)))
  8936. "\\}[ \t]")))
  8937. (t (error "Bad refiling target description %s" desc)))
  8938. (while (setq f (pop files))
  8939. (with-current-buffer
  8940. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8941. (or
  8942. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8943. (progn
  8944. (if (bufferp f) (setq f (buffer-file-name
  8945. (buffer-base-buffer f))))
  8946. (setq f (and f (expand-file-name f)))
  8947. (if (eq org-refile-use-outline-path 'file)
  8948. (push (list (file-name-nondirectory f) f nil nil) tgs))
  8949. (save-excursion
  8950. (save-restriction
  8951. (widen)
  8952. (goto-char (point-min))
  8953. (while (re-search-forward descre nil t)
  8954. (goto-char (setq pos0 (point-at-bol)))
  8955. (catch 'next
  8956. (when org-refile-target-verify-function
  8957. (save-match-data
  8958. (or (funcall org-refile-target-verify-function)
  8959. (throw 'next t))))
  8960. (when (looking-at org-complex-heading-regexp)
  8961. (setq level (org-reduced-level
  8962. (- (match-end 1) (match-beginning 1)))
  8963. txt (org-link-display-format (match-string 4))
  8964. re (format org-complex-heading-regexp-format
  8965. (regexp-quote (match-string 4))))
  8966. (when org-refile-use-outline-path
  8967. (setq txt (mapconcat
  8968. 'org-protect-slash
  8969. (append
  8970. (if (eq org-refile-use-outline-path
  8971. 'file)
  8972. (list (file-name-nondirectory
  8973. (buffer-file-name
  8974. (buffer-base-buffer))))
  8975. (if (eq org-refile-use-outline-path
  8976. 'full-file-path)
  8977. (list (buffer-file-name
  8978. (buffer-base-buffer)))))
  8979. (org-get-outline-path fast-path-p
  8980. level txt)
  8981. (list txt))
  8982. "/")))
  8983. (push (list txt f re (org-refile-marker (point)))
  8984. tgs)))
  8985. (when (= (point) pos0)
  8986. ;; verification function has not moved point
  8987. (goto-char (point-at-eol))))))))
  8988. (when org-refile-use-cache
  8989. (org-refile-cache-put tgs (buffer-file-name) descre))
  8990. (setq targets (append tgs targets))
  8991. ))))
  8992. (message "Getting targets...done")
  8993. (nreverse targets)))
  8994. (defun org-protect-slash (s)
  8995. (while (string-match "/" s)
  8996. (setq s (replace-match "\\" t t s)))
  8997. s)
  8998. (defvar org-olpa (make-vector 20 nil))
  8999. (defun org-get-outline-path (&optional fastp level heading)
  9000. "Return the outline path to the current entry, as a list.
  9001. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9002. routine which makes outline path derivations for an entire file,
  9003. avoiding backtracing. Refile target collection makes use of that."
  9004. (if fastp
  9005. (progn
  9006. (if (> level 19)
  9007. (error "Outline path failure, more than 19 levels"))
  9008. (loop for i from level upto 19 do
  9009. (aset org-olpa i nil))
  9010. (prog1
  9011. (delq nil (append org-olpa nil))
  9012. (aset org-olpa level heading)))
  9013. (let (rtn case-fold-search)
  9014. (save-excursion
  9015. (save-restriction
  9016. (widen)
  9017. (while (org-up-heading-safe)
  9018. (when (looking-at org-complex-heading-regexp)
  9019. (push (org-match-string-no-properties 4) rtn)))
  9020. rtn)))))
  9021. (defun org-format-outline-path (path &optional width prefix)
  9022. "Format the outline path PATH for display.
  9023. Width is the maximum number of characters that is available.
  9024. Prefix is a prefix to be included in the returned string,
  9025. such as the file name."
  9026. (setq width (or width 79))
  9027. (if prefix (setq width (- width (length prefix))))
  9028. (if (not path)
  9029. (or prefix "")
  9030. (let* ((nsteps (length path))
  9031. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9032. (maxwidth (if (<= total-width width)
  9033. 10000 ;; everything fits
  9034. ;; we need to shorten the level headings
  9035. (/ (- width nsteps) nsteps)))
  9036. (org-odd-levels-only nil)
  9037. (n 0)
  9038. (total (1+ (length prefix))))
  9039. (setq maxwidth (max maxwidth 10))
  9040. (concat prefix
  9041. (mapconcat
  9042. (lambda (h)
  9043. (setq n (1+ n))
  9044. (if (and (= n nsteps) (< maxwidth 10000))
  9045. (setq maxwidth (- total-width total)))
  9046. (if (< (length h) maxwidth)
  9047. (progn (setq total (+ total (length h) 1)) h)
  9048. (setq h (substring h 0 (- maxwidth 2))
  9049. total (+ total maxwidth 1))
  9050. (if (string-match "[ \t]+\\'" h)
  9051. (setq h (substring h 0 (match-beginning 0))))
  9052. (setq h (concat h "..")))
  9053. (org-add-props h nil 'face
  9054. (nth (% (1- n) org-n-level-faces)
  9055. org-level-faces))
  9056. h)
  9057. path "/")))))
  9058. (defun org-display-outline-path (&optional file current)
  9059. "Display the current outline path in the echo area."
  9060. (interactive "P")
  9061. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9062. (case-fold-search nil)
  9063. (path (and (org-mode-p) (org-get-outline-path))))
  9064. (if current (setq path (append path
  9065. (save-excursion
  9066. (org-back-to-heading t)
  9067. (if (looking-at org-complex-heading-regexp)
  9068. (list (match-string 4)))))))
  9069. (message "%s"
  9070. (org-format-outline-path
  9071. path
  9072. (1- (frame-width))
  9073. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9074. (defvar org-refile-history nil
  9075. "History for refiling operations.")
  9076. (defvar org-after-refile-insert-hook nil
  9077. "Hook run after `org-refile' has inserted its stuff at the new location.
  9078. Note that this is still *before* the stuff will be removed from
  9079. the *old* location.")
  9080. (defvar org-capture-last-stored-marker)
  9081. (defun org-refile (&optional goto default-buffer rfloc)
  9082. "Move the entry at point to another heading.
  9083. The list of target headings is compiled using the information in
  9084. `org-refile-targets', which see. This list is created before each use
  9085. and will therefore always be up-to-date.
  9086. At the target location, the entry is filed as a subitem of the target heading.
  9087. Depending on `org-reverse-note-order', the new subitem will either be the
  9088. first or the last subitem.
  9089. If there is an active region, all entries in that region will be moved.
  9090. However, the region must fulfill the requirement that the first heading
  9091. is the first one sets the top-level of the moved text - at most siblings
  9092. below it are allowed.
  9093. With prefix arg GOTO, the command will only visit the target location,
  9094. not actually move anything.
  9095. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9096. go to the location where the last refiling
  9097. operation has put the subtree.
  9098. With a prefix argument of `2', refile to the running clock.
  9099. RFLOC can be a refile location obtained in a different way.
  9100. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9101. If you are using target caching (see `org-refile-use-cache'),
  9102. You have to clear the target cache in order to find new targets.
  9103. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9104. (interactive "P")
  9105. (if (member goto '(0 (64)))
  9106. (org-refile-cache-clear)
  9107. (let* ((cbuf (current-buffer))
  9108. (regionp (org-region-active-p))
  9109. (region-start (and regionp (region-beginning)))
  9110. (region-end (and regionp (region-end)))
  9111. (region-length (and regionp (- region-end region-start)))
  9112. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9113. pos it nbuf file re level reversed)
  9114. (setq last-command nil)
  9115. (when regionp
  9116. (goto-char region-start)
  9117. (or (bolp) (goto-char (point-at-bol)))
  9118. (setq region-start (point))
  9119. (unless (org-kill-is-subtree-p
  9120. (buffer-substring region-start region-end))
  9121. (error "The region is not a (sequence of) subtree(s)")))
  9122. (if (equal goto '(16))
  9123. (org-refile-goto-last-stored)
  9124. (when (or
  9125. (and (equal goto 2)
  9126. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9127. (prog1
  9128. (setq it (list (or org-clock-heading "running clock")
  9129. (buffer-file-name
  9130. (marker-buffer org-clock-hd-marker))
  9131. ""
  9132. (marker-position org-clock-hd-marker)))
  9133. (setq goto nil)))
  9134. (setq it (or rfloc
  9135. (save-excursion
  9136. (org-refile-get-location
  9137. (if goto "Goto: " "Refile to: ") default-buffer
  9138. org-refile-allow-creating-parent-nodes)))))
  9139. (setq file (nth 1 it)
  9140. re (nth 2 it)
  9141. pos (nth 3 it))
  9142. (if (and (not goto)
  9143. pos
  9144. (equal (buffer-file-name) file)
  9145. (if regionp
  9146. (and (>= pos region-start)
  9147. (<= pos region-end))
  9148. (and (>= pos (point))
  9149. (< pos (save-excursion
  9150. (org-end-of-subtree t t))))))
  9151. (error "Cannot refile to position inside the tree or region"))
  9152. (setq nbuf (or (find-buffer-visiting file)
  9153. (find-file-noselect file)))
  9154. (if goto
  9155. (progn
  9156. (switch-to-buffer nbuf)
  9157. (goto-char pos)
  9158. (org-show-context 'org-goto))
  9159. (if regionp
  9160. (progn
  9161. (org-kill-new (buffer-substring region-start region-end))
  9162. (org-save-markers-in-region region-start region-end))
  9163. (org-copy-subtree 1 nil t))
  9164. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9165. (find-file-noselect file)))
  9166. (setq reversed (org-notes-order-reversed-p))
  9167. (save-excursion
  9168. (save-restriction
  9169. (widen)
  9170. (if pos
  9171. (progn
  9172. (goto-char pos)
  9173. (looking-at outline-regexp)
  9174. (setq level (org-get-valid-level (funcall outline-level) 1))
  9175. (goto-char
  9176. (if reversed
  9177. (or (outline-next-heading) (point-max))
  9178. (or (save-excursion (org-get-next-sibling))
  9179. (org-end-of-subtree t t)
  9180. (point-max)))))
  9181. (setq level 1)
  9182. (if (not reversed)
  9183. (goto-char (point-max))
  9184. (goto-char (point-min))
  9185. (or (outline-next-heading) (goto-char (point-max)))))
  9186. (if (not (bolp)) (newline))
  9187. (org-paste-subtree level)
  9188. (when org-log-refile
  9189. (org-add-log-setup 'refile nil nil 'findpos
  9190. org-log-refile)
  9191. (unless (eq org-log-refile 'note)
  9192. (save-excursion (org-add-log-note))))
  9193. (and org-auto-align-tags (org-set-tags nil t))
  9194. (bookmark-set "org-refile-last-stored")
  9195. ;; If we are refiling for capture, make sure that the
  9196. ;; last-capture pointers point here
  9197. (when (org-bound-and-true-p org-refile-for-capture)
  9198. (bookmark-set "org-capture-last-stored-marker")
  9199. (move-marker org-capture-last-stored-marker (point)))
  9200. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9201. (run-hooks 'org-after-refile-insert-hook))))
  9202. (if regionp
  9203. (delete-region (point) (+ (point) region-length))
  9204. (org-cut-subtree))
  9205. (when (featurep 'org-inlinetask)
  9206. (org-inlinetask-remove-END-maybe))
  9207. (setq org-markers-to-move nil)
  9208. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9209. (defun org-refile-goto-last-stored ()
  9210. "Go to the location where the last refile was stored."
  9211. (interactive)
  9212. (bookmark-jump "org-refile-last-stored")
  9213. (message "This is the location of the last refile"))
  9214. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9215. "Prompt the user for a refile location, using PROMPT."
  9216. (let ((org-refile-targets org-refile-targets)
  9217. (org-refile-use-outline-path org-refile-use-outline-path))
  9218. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  9219. (unless org-refile-target-table
  9220. (error "No refile targets"))
  9221. (let* ((cbuf (current-buffer))
  9222. (partial-completion-mode nil)
  9223. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9224. (cfunc (if (and org-refile-use-outline-path
  9225. org-outline-path-complete-in-steps)
  9226. 'org-olpath-completing-read
  9227. 'org-icompleting-read))
  9228. (extra (if org-refile-use-outline-path "/" ""))
  9229. (filename (and cfn (expand-file-name cfn)))
  9230. (tbl (mapcar
  9231. (lambda (x)
  9232. (if (and (not (member org-refile-use-outline-path
  9233. '(file full-file-path)))
  9234. (not (equal filename (nth 1 x))))
  9235. (cons (concat (car x) extra " ("
  9236. (file-name-nondirectory (nth 1 x)) ")")
  9237. (cdr x))
  9238. (cons (concat (car x) extra) (cdr x))))
  9239. org-refile-target-table))
  9240. (completion-ignore-case t)
  9241. pa answ parent-target child parent old-hist)
  9242. (setq old-hist org-refile-history)
  9243. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9244. nil 'org-refile-history))
  9245. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9246. (org-refile-check-position pa)
  9247. (if pa
  9248. (progn
  9249. (when (or (not org-refile-history)
  9250. (not (eq old-hist org-refile-history))
  9251. (not (equal (car pa) (car org-refile-history))))
  9252. (setq org-refile-history
  9253. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9254. org-refile-history
  9255. (cdr org-refile-history))))
  9256. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9257. (pop org-refile-history)))
  9258. pa)
  9259. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9260. (progn
  9261. (setq parent (match-string 1 answ)
  9262. child (match-string 2 answ))
  9263. (setq parent-target (or (assoc parent tbl)
  9264. (assoc (concat parent "/") tbl)))
  9265. (when (and parent-target
  9266. (or (eq new-nodes t)
  9267. (and (eq new-nodes 'confirm)
  9268. (y-or-n-p (format "Create new node \"%s\"? "
  9269. child)))))
  9270. (org-refile-new-child parent-target child)))
  9271. (error "Invalid target location")))))
  9272. (defun org-refile-check-position (refile-pointer)
  9273. "Check if the refile pointer matches the readline to which it points."
  9274. (let* ((file (nth 1 refile-pointer))
  9275. (re (nth 2 refile-pointer))
  9276. (pos (nth 3 refile-pointer))
  9277. buffer)
  9278. (when (org-string-nw-p re)
  9279. (setq buffer (if (markerp pos)
  9280. (marker-buffer pos)
  9281. (or (find-buffer-visiting file)
  9282. (find-file-noselect file))))
  9283. (with-current-buffer buffer
  9284. (save-excursion
  9285. (save-restriction
  9286. (widen)
  9287. (goto-char pos)
  9288. (beginning-of-line 1)
  9289. (unless (org-looking-at-p re)
  9290. (error "Invalid refile position, please rebuild the cache"))))))))
  9291. (defun org-refile-new-child (parent-target child)
  9292. "Use refile target PARENT-TARGET to add new CHILD below it."
  9293. (unless parent-target
  9294. (error "Cannot find parent for new node"))
  9295. (let ((file (nth 1 parent-target))
  9296. (pos (nth 3 parent-target))
  9297. level)
  9298. (with-current-buffer (or (find-buffer-visiting file)
  9299. (find-file-noselect file))
  9300. (save-excursion
  9301. (save-restriction
  9302. (widen)
  9303. (if pos
  9304. (goto-char pos)
  9305. (goto-char (point-max))
  9306. (if (not (bolp)) (newline)))
  9307. (when (looking-at outline-regexp)
  9308. (setq level (funcall outline-level))
  9309. (org-end-of-subtree t t))
  9310. (org-back-over-empty-lines)
  9311. (insert "\n" (make-string
  9312. (if pos (org-get-valid-level level 1) 1) ?*)
  9313. " " child "\n")
  9314. (beginning-of-line 0)
  9315. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9316. (defun org-olpath-completing-read (prompt collection &rest args)
  9317. "Read an outline path like a file name."
  9318. (let ((thetable collection)
  9319. (org-completion-use-ido nil) ; does not work with ido.
  9320. (org-completion-use-iswitchb nil)) ; or iswitchb
  9321. (apply
  9322. 'org-icompleting-read prompt
  9323. (lambda (string predicate &optional flag)
  9324. (let (rtn r f (l (length string)))
  9325. (cond
  9326. ((eq flag nil)
  9327. ;; try completion
  9328. (try-completion string thetable))
  9329. ((eq flag t)
  9330. ;; all-completions
  9331. (setq rtn (all-completions string thetable predicate))
  9332. (mapcar
  9333. (lambda (x)
  9334. (setq r (substring x l))
  9335. (if (string-match " ([^)]*)$" x)
  9336. (setq f (match-string 0 x))
  9337. (setq f ""))
  9338. (if (string-match "/" r)
  9339. (concat string (substring r 0 (match-end 0)) f)
  9340. x))
  9341. rtn))
  9342. ((eq flag 'lambda)
  9343. ;; exact match?
  9344. (assoc string thetable)))
  9345. ))
  9346. args)))
  9347. ;;;; Dynamic blocks
  9348. (defun org-find-dblock (name)
  9349. "Find the first dynamic block with name NAME in the buffer.
  9350. If not found, stay at current position and return nil."
  9351. (let (pos)
  9352. (save-excursion
  9353. (goto-char (point-min))
  9354. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9355. nil t)
  9356. (match-beginning 0))))
  9357. (if pos (goto-char pos))
  9358. pos))
  9359. (defconst org-dblock-start-re
  9360. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9361. "Matches the start line of a dynamic block, with parameters.")
  9362. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9363. "Matches the end of a dynamic block.")
  9364. (defun org-create-dblock (plist)
  9365. "Create a dynamic block section, with parameters taken from PLIST.
  9366. PLIST must contain a :name entry which is used as name of the block."
  9367. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9368. (end-of-line 1)
  9369. (newline))
  9370. (let ((col (current-column))
  9371. (name (plist-get plist :name)))
  9372. (insert "#+BEGIN: " name)
  9373. (while plist
  9374. (if (eq (car plist) :name)
  9375. (setq plist (cddr plist))
  9376. (insert " " (prin1-to-string (pop plist)))))
  9377. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9378. (beginning-of-line -2)))
  9379. (defun org-prepare-dblock ()
  9380. "Prepare dynamic block for refresh.
  9381. This empties the block, puts the cursor at the insert position and returns
  9382. the property list including an extra property :name with the block name."
  9383. (unless (looking-at org-dblock-start-re)
  9384. (error "Not at a dynamic block"))
  9385. (let* ((begdel (1+ (match-end 0)))
  9386. (name (org-no-properties (match-string 1)))
  9387. (params (append (list :name name)
  9388. (read (concat "(" (match-string 3) ")")))))
  9389. (save-excursion
  9390. (beginning-of-line 1)
  9391. (skip-chars-forward " \t")
  9392. (setq params (plist-put params :indentation-column (current-column))))
  9393. (unless (re-search-forward org-dblock-end-re nil t)
  9394. (error "Dynamic block not terminated"))
  9395. (setq params
  9396. (append params
  9397. (list :content (buffer-substring
  9398. begdel (match-beginning 0)))))
  9399. (delete-region begdel (match-beginning 0))
  9400. (goto-char begdel)
  9401. (open-line 1)
  9402. params))
  9403. (defun org-map-dblocks (&optional command)
  9404. "Apply COMMAND to all dynamic blocks in the current buffer.
  9405. If COMMAND is not given, use `org-update-dblock'."
  9406. (let ((cmd (or command 'org-update-dblock)))
  9407. (save-excursion
  9408. (goto-char (point-min))
  9409. (while (re-search-forward org-dblock-start-re nil t)
  9410. (goto-char (match-beginning 0))
  9411. (save-excursion
  9412. (condition-case nil
  9413. (funcall cmd)
  9414. (error (message "Error during update of dynamic block"))))
  9415. (unless (re-search-forward org-dblock-end-re nil t)
  9416. (error "Dynamic block not terminated"))))))
  9417. (defun org-dblock-update (&optional arg)
  9418. "User command for updating dynamic blocks.
  9419. Update the dynamic block at point. With prefix ARG, update all dynamic
  9420. blocks in the buffer."
  9421. (interactive "P")
  9422. (if arg
  9423. (org-update-all-dblocks)
  9424. (or (looking-at org-dblock-start-re)
  9425. (org-beginning-of-dblock))
  9426. (org-update-dblock)))
  9427. (defun org-update-dblock ()
  9428. "Update the dynamic block at point.
  9429. This means to empty the block, parse for parameters and then call
  9430. the correct writing function."
  9431. (save-window-excursion
  9432. (let* ((pos (point))
  9433. (line (org-current-line))
  9434. (params (org-prepare-dblock))
  9435. (name (plist-get params :name))
  9436. (indent (plist-get params :indentation-column))
  9437. (cmd (intern (concat "org-dblock-write:" name))))
  9438. (message "Updating dynamic block `%s' at line %d..." name line)
  9439. (funcall cmd params)
  9440. (message "Updating dynamic block `%s' at line %d...done" name line)
  9441. (goto-char pos)
  9442. (when (and indent (> indent 0))
  9443. (setq indent (make-string indent ?\ ))
  9444. (save-excursion
  9445. (org-beginning-of-dblock)
  9446. (forward-line 1)
  9447. (while (not (looking-at org-dblock-end-re))
  9448. (insert indent)
  9449. (beginning-of-line 2))
  9450. (when (looking-at org-dblock-end-re)
  9451. (and (looking-at "[ \t]+")
  9452. (replace-match ""))
  9453. (insert indent)))))))
  9454. (defun org-beginning-of-dblock ()
  9455. "Find the beginning of the dynamic block at point.
  9456. Error if there is no such block at point."
  9457. (let ((pos (point))
  9458. beg)
  9459. (end-of-line 1)
  9460. (if (and (re-search-backward org-dblock-start-re nil t)
  9461. (setq beg (match-beginning 0))
  9462. (re-search-forward org-dblock-end-re nil t)
  9463. (> (match-end 0) pos))
  9464. (goto-char beg)
  9465. (goto-char pos)
  9466. (error "Not in a dynamic block"))))
  9467. (defun org-update-all-dblocks ()
  9468. "Update all dynamic blocks in the buffer.
  9469. This function can be used in a hook."
  9470. (when (org-mode-p)
  9471. (org-map-dblocks 'org-update-dblock)))
  9472. ;;;; Completion
  9473. (defconst org-additional-option-like-keywords
  9474. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  9475. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  9476. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9477. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  9478. "BEGIN:" "END:"
  9479. "ORGTBL" "TBLFM:" "TBLNAME:"
  9480. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9481. "BEGIN_QUOTE" "END_QUOTE"
  9482. "BEGIN_VERSE" "END_VERSE"
  9483. "BEGIN_CENTER" "END_CENTER"
  9484. "BEGIN_SRC" "END_SRC"
  9485. "CATEGORY" "COLUMNS"
  9486. "CAPTION" "LABEL"
  9487. "SETUPFILE"
  9488. "BIND"
  9489. "MACRO"))
  9490. (defcustom org-structure-template-alist
  9491. '(
  9492. ("s" "#+begin_src ?\n\n#+end_src"
  9493. "<src lang=\"?\">\n\n</src>")
  9494. ("e" "#+begin_example\n?\n#+end_example"
  9495. "<example>\n?\n</example>")
  9496. ("q" "#+begin_quote\n?\n#+end_quote"
  9497. "<quote>\n?\n</quote>")
  9498. ("v" "#+begin_verse\n?\n#+end_verse"
  9499. "<verse>\n?\n/verse>")
  9500. ("c" "#+begin_center\n?\n#+end_center"
  9501. "<center>\n?\n/center>")
  9502. ("l" "#+begin_latex\n?\n#+end_latex"
  9503. "<literal style=\"latex\">\n?\n</literal>")
  9504. ("L" "#+latex: "
  9505. "<literal style=\"latex\">?</literal>")
  9506. ("h" "#+begin_html\n?\n#+end_html"
  9507. "<literal style=\"html\">\n?\n</literal>")
  9508. ("H" "#+html: "
  9509. "<literal style=\"html\">?</literal>")
  9510. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9511. ("A" "#+ascii: ")
  9512. ("i" "#+include %file ?"
  9513. "<include file=%file markup=\"?\">")
  9514. )
  9515. "Structure completion elements.
  9516. This is a list of abbreviation keys and values. The value gets inserted
  9517. if you type `<' followed by the key and then press the completion key,
  9518. usually `M-TAB'. %file will be replaced by a file name after prompting
  9519. for the file using completion.
  9520. There are two templates for each key, the first uses the original Org syntax,
  9521. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9522. the default when the /org-mtags.el/ module has been loaded. See also the
  9523. variable `org-mtags-prefer-muse-templates'.
  9524. This is an experimental feature, it is undecided if it is going to stay in."
  9525. :group 'org-completion
  9526. :type '(repeat
  9527. (string :tag "Key")
  9528. (string :tag "Template")
  9529. (string :tag "Muse Template")))
  9530. (defun org-try-structure-completion ()
  9531. "Try to complete a structure template before point.
  9532. This looks for strings like \"<e\" on an otherwise empty line and
  9533. expands them."
  9534. (let ((l (buffer-substring (point-at-bol) (point)))
  9535. a)
  9536. (when (and (looking-at "[ \t]*$")
  9537. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9538. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9539. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9540. (match-beginning 1)) a)
  9541. t)))
  9542. (defun org-complete-expand-structure-template (start cell)
  9543. "Expand a structure template."
  9544. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9545. (rpl (nth (if musep 2 1) cell))
  9546. (ind ""))
  9547. (delete-region start (point))
  9548. (when (string-match "\\`#\\+" rpl)
  9549. (cond
  9550. ((bolp))
  9551. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9552. (setq ind (buffer-substring (point-at-bol) (point))))
  9553. (t (newline))))
  9554. (setq start (point))
  9555. (if (string-match "%file" rpl)
  9556. (setq rpl (replace-match
  9557. (concat
  9558. "\""
  9559. (save-match-data
  9560. (abbreviate-file-name (read-file-name "Include file: ")))
  9561. "\"")
  9562. t t rpl)))
  9563. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9564. (concat "\n" ind)))
  9565. (insert rpl)
  9566. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9567. (defun org-complete (&optional arg)
  9568. "Perform completion on word at point.
  9569. At the beginning of a headline, this completes TODO keywords as given in
  9570. `org-todo-keywords'.
  9571. If the current word is preceded by a backslash, completes the TeX symbols
  9572. that are supported for HTML support.
  9573. If the current word is preceded by \"#+\", completes special words for
  9574. setting file options.
  9575. In the line after \"#+STARTUP:, complete valid keywords.\"
  9576. At all other locations, this simply calls the value of
  9577. `org-completion-fallback-command'."
  9578. (interactive "P")
  9579. (org-without-partial-completion
  9580. (catch 'exit
  9581. (let* ((a nil)
  9582. (end (point))
  9583. (beg1 (save-excursion
  9584. (skip-chars-backward (org-re "[:alnum:]_@"))
  9585. (point)))
  9586. (beg (save-excursion
  9587. (skip-chars-backward "a-zA-Z0-9_:$")
  9588. (point)))
  9589. (confirm (lambda (x) (stringp (car x))))
  9590. (searchhead (equal (char-before beg) ?*))
  9591. (struct
  9592. (when (and (member (char-before beg1) '(?. ?<))
  9593. (setq a (assoc (buffer-substring beg1 (point))
  9594. org-structure-template-alist)))
  9595. (org-complete-expand-structure-template (1- beg1) a)
  9596. (throw 'exit t)))
  9597. (tag (and (equal (char-before beg1) ?:)
  9598. (equal (char-after (point-at-bol)) ?*)))
  9599. (prop (and (equal (char-before beg1) ?:)
  9600. (not (equal (char-after (point-at-bol)) ?*))))
  9601. (texp (equal (char-before beg) ?\\))
  9602. (link (equal (char-before beg) ?\[))
  9603. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9604. beg)
  9605. "#+"))
  9606. (startup (string-match "^#\\+STARTUP:.*"
  9607. (buffer-substring (point-at-bol) (point))))
  9608. (completion-ignore-case opt)
  9609. (type nil)
  9610. (tbl nil)
  9611. (table (cond
  9612. (opt
  9613. (setq type :opt)
  9614. (require 'org-exp)
  9615. (append
  9616. (delq nil
  9617. (mapcar
  9618. (lambda (x)
  9619. (if (string-match
  9620. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9621. (cons (match-string 2 x)
  9622. (match-string 1 x))))
  9623. (org-split-string (org-get-current-options) "\n")))
  9624. (mapcar 'list org-additional-option-like-keywords)))
  9625. (startup
  9626. (setq type :startup)
  9627. org-startup-options)
  9628. (link (append org-link-abbrev-alist-local
  9629. org-link-abbrev-alist))
  9630. (texp
  9631. (setq type :tex)
  9632. (append org-entities-user org-entities))
  9633. ((string-match "\\`\\*+[ \t]+\\'"
  9634. (buffer-substring (point-at-bol) beg))
  9635. (setq type :todo)
  9636. (mapcar 'list org-todo-keywords-1))
  9637. (searchhead
  9638. (setq type :searchhead)
  9639. (save-excursion
  9640. (goto-char (point-min))
  9641. (while (re-search-forward org-todo-line-regexp nil t)
  9642. (push (list
  9643. (org-make-org-heading-search-string
  9644. (match-string 3) t))
  9645. tbl)))
  9646. tbl)
  9647. (tag (setq type :tag beg beg1)
  9648. (or org-tag-alist (org-get-buffer-tags)))
  9649. (prop (setq type :prop beg beg1)
  9650. (mapcar 'list (org-buffer-property-keys nil t t)))
  9651. (t (progn
  9652. (call-interactively org-completion-fallback-command)
  9653. (throw 'exit nil)))))
  9654. (pattern (buffer-substring-no-properties beg end))
  9655. (completion (try-completion pattern table confirm)))
  9656. (cond ((eq completion t)
  9657. (if (not (assoc (upcase pattern) table))
  9658. (message "Already complete")
  9659. (if (and (equal type :opt)
  9660. (not (member (car (assoc (upcase pattern) table))
  9661. org-additional-option-like-keywords)))
  9662. (insert (substring (cdr (assoc (upcase pattern) table))
  9663. (length pattern)))
  9664. (if (memq type '(:tag :prop)) (insert ":")))))
  9665. ((null completion)
  9666. (message "Can't find completion for \"%s\"" pattern)
  9667. (ding))
  9668. ((not (string= pattern completion))
  9669. (delete-region beg end)
  9670. (if (string-match " +$" completion)
  9671. (setq completion (replace-match "" t t completion)))
  9672. (insert completion)
  9673. (if (get-buffer-window "*Completions*")
  9674. (delete-window (get-buffer-window "*Completions*")))
  9675. (if (assoc completion table)
  9676. (if (eq type :todo) (insert " ")
  9677. (if (memq type '(:tag :prop)) (insert ":"))))
  9678. (if (and (equal type :opt) (assoc completion table))
  9679. (message "%s" (substitute-command-keys
  9680. "Press \\[org-complete] again to insert example settings"))))
  9681. (t
  9682. (message "Making completion list...")
  9683. (let ((list (sort (all-completions pattern table confirm)
  9684. 'string<)))
  9685. (with-output-to-temp-buffer "*Completions*"
  9686. (condition-case nil
  9687. ;; Protection needed for XEmacs and emacs 21
  9688. (display-completion-list list pattern)
  9689. (error (display-completion-list list)))))
  9690. (message "Making completion list...%s" "done")))))))
  9691. ;;;; TODO, DEADLINE, Comments
  9692. (defun org-toggle-comment ()
  9693. "Change the COMMENT state of an entry."
  9694. (interactive)
  9695. (save-excursion
  9696. (org-back-to-heading)
  9697. (let (case-fold-search)
  9698. (if (looking-at (concat outline-regexp
  9699. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9700. (replace-match "" t t nil 1)
  9701. (if (looking-at outline-regexp)
  9702. (progn
  9703. (goto-char (match-end 0))
  9704. (insert org-comment-string " ")))))))
  9705. (defvar org-last-todo-state-is-todo nil
  9706. "This is non-nil when the last TODO state change led to a TODO state.
  9707. If the last change removed the TODO tag or switched to DONE, then
  9708. this is nil.")
  9709. (defvar org-setting-tags nil) ; dynamically skipped
  9710. (defun org-parse-local-options (string var)
  9711. "Parse STRING for startup setting relevant for variable VAR."
  9712. (let ((rtn (symbol-value var))
  9713. e opts)
  9714. (save-match-data
  9715. (if (or (not string) (not (string-match "\\S-" string)))
  9716. rtn
  9717. (setq opts (delq nil (mapcar (lambda (x)
  9718. (setq e (assoc x org-startup-options))
  9719. (if (eq (nth 1 e) var) e nil))
  9720. (org-split-string string "[ \t]+"))))
  9721. (if (not opts)
  9722. rtn
  9723. (setq rtn nil)
  9724. (while (setq e (pop opts))
  9725. (if (not (nth 3 e))
  9726. (setq rtn (nth 2 e))
  9727. (if (not (listp rtn)) (setq rtn nil))
  9728. (push (nth 2 e) rtn)))
  9729. rtn)))))
  9730. (defvar org-todo-setup-filter-hook nil
  9731. "Hook for functions that pre-filter todo specs.
  9732. Each function takes a todo spec and returns either nil or the spec
  9733. transformed into canonical form." )
  9734. (defvar org-todo-get-default-hook nil
  9735. "Hook for functions that get a default item for todo.
  9736. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9737. nil or a string to be used for the todo mark." )
  9738. (defvar org-agenda-headline-snapshot-before-repeat)
  9739. (defun org-todo (&optional arg)
  9740. "Change the TODO state of an item.
  9741. The state of an item is given by a keyword at the start of the heading,
  9742. like
  9743. *** TODO Write paper
  9744. *** DONE Call mom
  9745. The different keywords are specified in the variable `org-todo-keywords'.
  9746. By default the available states are \"TODO\" and \"DONE\".
  9747. So for this example: when the item starts with TODO, it is changed to DONE.
  9748. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9749. DONE are present, add TODO at the beginning of the heading.
  9750. With \\[universal-argument] prefix arg, use completion to determine the new \
  9751. state.
  9752. With numeric prefix arg, switch to that state.
  9753. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9754. keywords (nextset).
  9755. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9756. For calling through lisp, arg is also interpreted in the following way:
  9757. 'none -> empty state
  9758. \"\"(empty string) -> switch to empty state
  9759. 'done -> switch to DONE
  9760. 'nextset -> switch to the next set of keywords
  9761. 'previousset -> switch to the previous set of keywords
  9762. \"WAITING\" -> switch to the specified keyword, but only if it
  9763. really is a member of `org-todo-keywords'."
  9764. (interactive "P")
  9765. (if (equal arg '(16)) (setq arg 'nextset))
  9766. (let ((org-blocker-hook org-blocker-hook)
  9767. (case-fold-search nil))
  9768. (when (equal arg '(64))
  9769. (setq arg nil org-blocker-hook nil))
  9770. (when (and org-blocker-hook
  9771. (or org-inhibit-blocking
  9772. (org-entry-get nil "NOBLOCKING")))
  9773. (setq org-blocker-hook nil))
  9774. (save-excursion
  9775. (catch 'exit
  9776. (org-back-to-heading t)
  9777. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9778. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9779. (looking-at " *"))
  9780. (let* ((match-data (match-data))
  9781. (startpos (point-at-bol))
  9782. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9783. (org-log-done org-log-done)
  9784. (org-log-repeat org-log-repeat)
  9785. (org-todo-log-states org-todo-log-states)
  9786. (this (match-string 1))
  9787. (hl-pos (match-beginning 0))
  9788. (head (org-get-todo-sequence-head this))
  9789. (ass (assoc head org-todo-kwd-alist))
  9790. (interpret (nth 1 ass))
  9791. (done-word (nth 3 ass))
  9792. (final-done-word (nth 4 ass))
  9793. (last-state (or this ""))
  9794. (completion-ignore-case t)
  9795. (member (member this org-todo-keywords-1))
  9796. (tail (cdr member))
  9797. (state (cond
  9798. ((and org-todo-key-trigger
  9799. (or (and (equal arg '(4))
  9800. (eq org-use-fast-todo-selection 'prefix))
  9801. (and (not arg) org-use-fast-todo-selection
  9802. (not (eq org-use-fast-todo-selection
  9803. 'prefix)))))
  9804. ;; Use fast selection
  9805. (org-fast-todo-selection))
  9806. ((and (equal arg '(4))
  9807. (or (not org-use-fast-todo-selection)
  9808. (not org-todo-key-trigger)))
  9809. ;; Read a state with completion
  9810. (org-icompleting-read
  9811. "State: " (mapcar (lambda(x) (list x))
  9812. org-todo-keywords-1)
  9813. nil t))
  9814. ((eq arg 'right)
  9815. (if this
  9816. (if tail (car tail) nil)
  9817. (car org-todo-keywords-1)))
  9818. ((eq arg 'left)
  9819. (if (equal member org-todo-keywords-1)
  9820. nil
  9821. (if this
  9822. (nth (- (length org-todo-keywords-1)
  9823. (length tail) 2)
  9824. org-todo-keywords-1)
  9825. (org-last org-todo-keywords-1))))
  9826. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9827. (setq arg nil))) ; hack to fall back to cycling
  9828. (arg
  9829. ;; user or caller requests a specific state
  9830. (cond
  9831. ((equal arg "") nil)
  9832. ((eq arg 'none) nil)
  9833. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9834. ((eq arg 'nextset)
  9835. (or (car (cdr (member head org-todo-heads)))
  9836. (car org-todo-heads)))
  9837. ((eq arg 'previousset)
  9838. (let ((org-todo-heads (reverse org-todo-heads)))
  9839. (or (car (cdr (member head org-todo-heads)))
  9840. (car org-todo-heads))))
  9841. ((car (member arg org-todo-keywords-1)))
  9842. ((stringp arg)
  9843. (error "State `%s' not valid in this file" arg))
  9844. ((nth (1- (prefix-numeric-value arg))
  9845. org-todo-keywords-1))))
  9846. ((null member) (or head (car org-todo-keywords-1)))
  9847. ((equal this final-done-word) nil) ;; -> make empty
  9848. ((null tail) nil) ;; -> first entry
  9849. ((memq interpret '(type priority))
  9850. (if (eq this-command last-command)
  9851. (car tail)
  9852. (if (> (length tail) 0)
  9853. (or done-word (car org-done-keywords))
  9854. nil)))
  9855. (t
  9856. (car tail))))
  9857. (state (or
  9858. (run-hook-with-args-until-success
  9859. 'org-todo-get-default-hook state last-state)
  9860. state))
  9861. (next (if state (concat " " state " ") " "))
  9862. (change-plist (list :type 'todo-state-change :from this :to state
  9863. :position startpos))
  9864. dolog now-done-p)
  9865. (when org-blocker-hook
  9866. (setq org-last-todo-state-is-todo
  9867. (not (member this org-done-keywords)))
  9868. (unless (save-excursion
  9869. (save-match-data
  9870. (run-hook-with-args-until-failure
  9871. 'org-blocker-hook change-plist)))
  9872. (if (interactive-p)
  9873. (error "TODO state change from %s to %s blocked" this state)
  9874. ;; fail silently
  9875. (message "TODO state change from %s to %s blocked" this state)
  9876. (throw 'exit nil))))
  9877. (store-match-data match-data)
  9878. (replace-match next t t)
  9879. (unless (pos-visible-in-window-p hl-pos)
  9880. (message "TODO state changed to %s" (org-trim next)))
  9881. (unless head
  9882. (setq head (org-get-todo-sequence-head state)
  9883. ass (assoc head org-todo-kwd-alist)
  9884. interpret (nth 1 ass)
  9885. done-word (nth 3 ass)
  9886. final-done-word (nth 4 ass)))
  9887. (when (memq arg '(nextset previousset))
  9888. (message "Keyword-Set %d/%d: %s"
  9889. (- (length org-todo-sets) -1
  9890. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9891. (length org-todo-sets)
  9892. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9893. (setq org-last-todo-state-is-todo
  9894. (not (member state org-done-keywords)))
  9895. (setq now-done-p (and (member state org-done-keywords)
  9896. (not (member this org-done-keywords))))
  9897. (and logging (org-local-logging logging))
  9898. (when (and (or org-todo-log-states org-log-done)
  9899. (not (eq org-inhibit-logging t))
  9900. (not (memq arg '(nextset previousset))))
  9901. ;; we need to look at recording a time and note
  9902. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9903. (nth 2 (assoc this org-todo-log-states))))
  9904. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9905. (setq dolog 'time))
  9906. (when (and state
  9907. (member state org-not-done-keywords)
  9908. (not (member this org-not-done-keywords)))
  9909. ;; This is now a todo state and was not one before
  9910. ;; If there was a CLOSED time stamp, get rid of it.
  9911. (org-add-planning-info nil nil 'closed))
  9912. (when (and now-done-p org-log-done)
  9913. ;; It is now done, and it was not done before
  9914. (org-add-planning-info 'closed (org-current-time))
  9915. (if (and (not dolog) (eq 'note org-log-done))
  9916. (org-add-log-setup 'done state this 'findpos 'note)))
  9917. (when (and state dolog)
  9918. ;; This is a non-nil state, and we need to log it
  9919. (org-add-log-setup 'state state this 'findpos dolog)))
  9920. ;; Fixup tag positioning
  9921. (org-todo-trigger-tag-changes state)
  9922. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9923. (when org-provide-todo-statistics
  9924. (org-update-parent-todo-statistics))
  9925. (run-hooks 'org-after-todo-state-change-hook)
  9926. (if (and arg (not (member state org-done-keywords)))
  9927. (setq head (org-get-todo-sequence-head state)))
  9928. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9929. ;; Do we need to trigger a repeat?
  9930. (when now-done-p
  9931. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9932. ;; This is for the agenda, take a snapshot of the headline.
  9933. (save-match-data
  9934. (setq org-agenda-headline-snapshot-before-repeat
  9935. (org-get-heading))))
  9936. (org-auto-repeat-maybe state))
  9937. ;; Fixup cursor location if close to the keyword
  9938. (if (and (outline-on-heading-p)
  9939. (not (bolp))
  9940. (save-excursion (beginning-of-line 1)
  9941. (looking-at org-todo-line-regexp))
  9942. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9943. (progn
  9944. (goto-char (or (match-end 2) (match-end 1)))
  9945. (and (looking-at " ") (just-one-space))))
  9946. (when org-trigger-hook
  9947. (save-excursion
  9948. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9949. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9950. "Block turning an entry into a TODO, using the hierarchy.
  9951. This checks whether the current task should be blocked from state
  9952. changes. Such blocking occurs when:
  9953. 1. The task has children which are not all in a completed state.
  9954. 2. A task has a parent with the property :ORDERED:, and there
  9955. are siblings prior to the current task with incomplete
  9956. status.
  9957. 3. The parent of the task is blocked because it has siblings that should
  9958. be done first, or is child of a block grandparent TODO entry."
  9959. (if (not org-enforce-todo-dependencies)
  9960. t ; if locally turned off don't block
  9961. (catch 'dont-block
  9962. ;; If this is not a todo state change, or if this entry is already DONE,
  9963. ;; do not block
  9964. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9965. (member (plist-get change-plist :from)
  9966. (cons 'done org-done-keywords))
  9967. (member (plist-get change-plist :to)
  9968. (cons 'todo org-not-done-keywords))
  9969. (not (plist-get change-plist :to)))
  9970. (throw 'dont-block t))
  9971. ;; If this task has children, and any are undone, it's blocked
  9972. (save-excursion
  9973. (org-back-to-heading t)
  9974. (let ((this-level (funcall outline-level)))
  9975. (outline-next-heading)
  9976. (let ((child-level (funcall outline-level)))
  9977. (while (and (not (eobp))
  9978. (> child-level this-level))
  9979. ;; this todo has children, check whether they are all
  9980. ;; completed
  9981. (if (and (not (org-entry-is-done-p))
  9982. (org-entry-is-todo-p))
  9983. (throw 'dont-block nil))
  9984. (outline-next-heading)
  9985. (setq child-level (funcall outline-level))))))
  9986. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9987. ;; any previous siblings are undone, it's blocked
  9988. (save-excursion
  9989. (org-back-to-heading t)
  9990. (let* ((pos (point))
  9991. (parent-pos (and (org-up-heading-safe) (point))))
  9992. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9993. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9994. (forward-line 1)
  9995. (re-search-forward org-not-done-heading-regexp pos t))
  9996. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9997. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9998. (while t
  9999. (goto-char parent-pos)
  10000. (if (not (looking-at org-not-done-heading-regexp))
  10001. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10002. (setq pos (point))
  10003. (setq parent-pos (and (org-up-heading-safe) (point)))
  10004. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10005. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10006. (forward-line 1)
  10007. (re-search-forward org-not-done-heading-regexp pos t))
  10008. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10009. (defcustom org-track-ordered-property-with-tag nil
  10010. "Should the ORDERED property also be shown as a tag?
  10011. The ORDERED property decides if an entry should require subtasks to be
  10012. completed in sequence. Since a property is not very visible, setting
  10013. this option means that toggling the ORDERED property with the command
  10014. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10015. not relevant for the behavior, but it makes things more visible.
  10016. Note that toggling the tag with tags commands will not change the property
  10017. and therefore not influence behavior!
  10018. This can be t, meaning the tag ORDERED should be used, It can also be a
  10019. string to select a different tag for this task."
  10020. :group 'org-todo
  10021. :type '(choice
  10022. (const :tag "No tracking" nil)
  10023. (const :tag "Track with ORDERED tag" t)
  10024. (string :tag "Use other tag")))
  10025. (defun org-toggle-ordered-property ()
  10026. "Toggle the ORDERED property of the current entry.
  10027. For better visibility, you can track the value of this property with a tag.
  10028. See variable `org-track-ordered-property-with-tag'."
  10029. (interactive)
  10030. (let* ((t1 org-track-ordered-property-with-tag)
  10031. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10032. (save-excursion
  10033. (org-back-to-heading)
  10034. (if (org-entry-get nil "ORDERED")
  10035. (progn
  10036. (org-delete-property "ORDERED")
  10037. (and tag (org-toggle-tag tag 'off))
  10038. (message "Subtasks can be completed in arbitrary order"))
  10039. (org-entry-put nil "ORDERED" "t")
  10040. (and tag (org-toggle-tag tag 'on))
  10041. (message "Subtasks must be completed in sequence")))))
  10042. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10043. (defun org-block-todo-from-checkboxes (change-plist)
  10044. "Block turning an entry into a TODO, using checkboxes.
  10045. This checks whether the current task should be blocked from state
  10046. changes because there are unchecked boxes in this entry."
  10047. (if (not org-enforce-todo-checkbox-dependencies)
  10048. t ; if locally turned off don't block
  10049. (catch 'dont-block
  10050. ;; If this is not a todo state change, or if this entry is already DONE,
  10051. ;; do not block
  10052. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10053. (member (plist-get change-plist :from)
  10054. (cons 'done org-done-keywords))
  10055. (member (plist-get change-plist :to)
  10056. (cons 'todo org-not-done-keywords))
  10057. (not (plist-get change-plist :to)))
  10058. (throw 'dont-block t))
  10059. ;; If this task has checkboxes that are not checked, it's blocked
  10060. (save-excursion
  10061. (org-back-to-heading t)
  10062. (let ((beg (point)) end)
  10063. (outline-next-heading)
  10064. (setq end (point))
  10065. (goto-char beg)
  10066. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10067. end t)
  10068. (progn
  10069. (if (boundp 'org-blocked-by-checkboxes)
  10070. (setq org-blocked-by-checkboxes t))
  10071. (throw 'dont-block nil)))))
  10072. t))) ; do not block
  10073. (defun org-entry-blocked-p ()
  10074. "Is the current entry blocked?"
  10075. (if (org-entry-get nil "NOBLOCKING")
  10076. nil ;; Never block this entry
  10077. (not
  10078. (run-hook-with-args-until-failure
  10079. 'org-blocker-hook
  10080. (list :type 'todo-state-change
  10081. :position (point)
  10082. :from 'todo
  10083. :to 'done)))))
  10084. (defun org-update-statistics-cookies (all)
  10085. "Update the statistics cookie, either from TODO or from checkboxes.
  10086. This should be called with the cursor in a line with a statistics cookie."
  10087. (interactive "P")
  10088. (if all
  10089. (progn
  10090. (org-update-checkbox-count 'all)
  10091. (org-map-entries 'org-update-parent-todo-statistics))
  10092. (if (not (org-on-heading-p))
  10093. (org-update-checkbox-count)
  10094. (let ((pos (move-marker (make-marker) (point)))
  10095. end l1 l2)
  10096. (ignore-errors (org-back-to-heading t))
  10097. (if (not (org-on-heading-p))
  10098. (org-update-checkbox-count)
  10099. (setq l1 (org-outline-level))
  10100. (setq end (save-excursion
  10101. (outline-next-heading)
  10102. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10103. (point)))
  10104. (if (and (save-excursion
  10105. (re-search-forward
  10106. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10107. (not (save-excursion (re-search-forward
  10108. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10109. (org-update-checkbox-count)
  10110. (if (and l2 (> l2 l1))
  10111. (progn
  10112. (goto-char end)
  10113. (org-update-parent-todo-statistics))
  10114. (goto-char pos)
  10115. (beginning-of-line 1)
  10116. (while (re-search-forward
  10117. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10118. (point-at-eol) t)
  10119. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10120. (goto-char pos)
  10121. (move-marker pos nil)))))
  10122. (defvar org-entry-property-inherited-from) ;; defined below
  10123. (defun org-update-parent-todo-statistics ()
  10124. "Update any statistics cookie in the parent of the current headline.
  10125. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10126. the entire subtree and this will travel up the hierarchy and update
  10127. statistics everywhere."
  10128. (interactive)
  10129. (let* ((lim 0) prop
  10130. (recursive (or (not org-hierarchical-todo-statistics)
  10131. (string-match
  10132. "\\<recursive\\>"
  10133. (or (setq prop (org-entry-get
  10134. nil "COOKIE_DATA" 'inherit)) ""))))
  10135. (lim (or (and prop (marker-position
  10136. org-entry-property-inherited-from))
  10137. lim))
  10138. (first t)
  10139. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10140. level ltoggle l1 new ndel
  10141. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  10142. (catch 'exit
  10143. (save-excursion
  10144. (beginning-of-line 1)
  10145. (if (org-at-heading-p)
  10146. (setq ltoggle (funcall outline-level))
  10147. (error "This should not happen"))
  10148. (while (and (setq level (org-up-heading-safe))
  10149. (or recursive first)
  10150. (>= (point) lim))
  10151. (setq first nil cookie-present nil)
  10152. (unless (and level
  10153. (not (string-match
  10154. "\\<checkbox\\>"
  10155. (downcase
  10156. (or (org-entry-get
  10157. nil "COOKIE_DATA")
  10158. "")))))
  10159. (throw 'exit nil))
  10160. (while (re-search-forward box-re (point-at-eol) t)
  10161. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10162. (setq is-percent (match-end 2))
  10163. (save-match-data
  10164. (unless (outline-next-heading) (throw 'exit nil))
  10165. (while (and (looking-at org-complex-heading-regexp)
  10166. (> (setq l1 (length (match-string 1))) level))
  10167. (setq kwd (and (or recursive (= l1 ltoggle))
  10168. (match-string 2)))
  10169. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10170. (and (listp org-provide-todo-statistics)
  10171. (or (member kwd org-provide-todo-statistics)
  10172. (member kwd org-done-keywords))))
  10173. (setq cnt-all (1+ cnt-all))
  10174. (if (eq org-provide-todo-statistics t)
  10175. (and kwd (setq cnt-all (1+ cnt-all)))))
  10176. (and (member kwd org-done-keywords)
  10177. (setq cnt-done (1+ cnt-done)))
  10178. (outline-next-heading)))
  10179. (setq new
  10180. (if is-percent
  10181. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10182. (format "[%d/%d]" cnt-done cnt-all))
  10183. ndel (- (match-end 0) (match-beginning 0)))
  10184. (goto-char (match-beginning 0))
  10185. (insert new)
  10186. (delete-region (point) (+ (point) ndel)))
  10187. (when cookie-present
  10188. (run-hook-with-args 'org-after-todo-statistics-hook
  10189. cnt-done (- cnt-all cnt-done))))))
  10190. (run-hooks 'org-todo-statistics-hook)))
  10191. (defvar org-after-todo-statistics-hook nil
  10192. "Hook that is called after a TODO statistics cookie has been updated.
  10193. Each function is called with two arguments: the number of not-done entries
  10194. and the number of done entries.
  10195. For example, the following function, when added to this hook, will switch
  10196. an entry to DONE when all children are done, and back to TODO when new
  10197. entries are set to a TODO status. Note that this hook is only called
  10198. when there is a statistics cookie in the headline!
  10199. (defun org-summary-todo (n-done n-not-done)
  10200. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10201. (let (org-log-done org-log-states) ; turn off logging
  10202. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10203. ")
  10204. (defvar org-todo-statistics-hook nil
  10205. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10206. This hook runs even if there is no statistics cookie present, in which case
  10207. `org-after-todo-statistics-hook' would not run.")
  10208. (defun org-todo-trigger-tag-changes (state)
  10209. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10210. (let ((l org-todo-state-tags-triggers)
  10211. changes)
  10212. (when (or (not state) (equal state ""))
  10213. (setq changes (append changes (cdr (assoc "" l)))))
  10214. (when (and (stringp state) (> (length state) 0))
  10215. (setq changes (append changes (cdr (assoc state l)))))
  10216. (when (member state org-not-done-keywords)
  10217. (setq changes (append changes (cdr (assoc 'todo l)))))
  10218. (when (member state org-done-keywords)
  10219. (setq changes (append changes (cdr (assoc 'done l)))))
  10220. (dolist (c changes)
  10221. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10222. (defun org-local-logging (value)
  10223. "Get logging settings from a property VALUE."
  10224. (let* (words w a)
  10225. ;; directly set the variables, they are already local.
  10226. (setq org-log-done nil
  10227. org-log-repeat nil
  10228. org-todo-log-states nil)
  10229. (setq words (org-split-string value))
  10230. (while (setq w (pop words))
  10231. (cond
  10232. ((setq a (assoc w org-startup-options))
  10233. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10234. (set (nth 1 a) (nth 2 a))))
  10235. ((setq a (org-extract-log-state-settings w))
  10236. (and (member (car a) org-todo-keywords-1)
  10237. (push a org-todo-log-states)))))))
  10238. (defun org-get-todo-sequence-head (kwd)
  10239. "Return the head of the TODO sequence to which KWD belongs.
  10240. If KWD is not set, check if there is a text property remembering the
  10241. right sequence."
  10242. (let (p)
  10243. (cond
  10244. ((not kwd)
  10245. (or (get-text-property (point-at-bol) 'org-todo-head)
  10246. (progn
  10247. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10248. nil (point-at-eol)))
  10249. (get-text-property p 'org-todo-head))))
  10250. ((not (member kwd org-todo-keywords-1))
  10251. (car org-todo-keywords-1))
  10252. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10253. (defun org-fast-todo-selection ()
  10254. "Fast TODO keyword selection with single keys.
  10255. Returns the new TODO keyword, or nil if no state change should occur."
  10256. (let* ((fulltable org-todo-key-alist)
  10257. (done-keywords org-done-keywords) ;; needed for the faces.
  10258. (maxlen (apply 'max (mapcar
  10259. (lambda (x)
  10260. (if (stringp (car x)) (string-width (car x)) 0))
  10261. fulltable)))
  10262. (expert nil)
  10263. (fwidth (+ maxlen 3 1 3))
  10264. (ncol (/ (- (window-width) 4) fwidth))
  10265. tg cnt e c tbl
  10266. groups ingroup)
  10267. (save-excursion
  10268. (save-window-excursion
  10269. (if expert
  10270. (set-buffer (get-buffer-create " *Org todo*"))
  10271. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10272. (erase-buffer)
  10273. (org-set-local 'org-done-keywords done-keywords)
  10274. (setq tbl fulltable cnt 0)
  10275. (while (setq e (pop tbl))
  10276. (cond
  10277. ((equal e '(:startgroup))
  10278. (push '() groups) (setq ingroup t)
  10279. (when (not (= cnt 0))
  10280. (setq cnt 0)
  10281. (insert "\n"))
  10282. (insert "{ "))
  10283. ((equal e '(:endgroup))
  10284. (setq ingroup nil cnt 0)
  10285. (insert "}\n"))
  10286. ((equal e '(:newline))
  10287. (when (not (= cnt 0))
  10288. (setq cnt 0)
  10289. (insert "\n")
  10290. (setq e (car tbl))
  10291. (while (equal (car tbl) '(:newline))
  10292. (insert "\n")
  10293. (setq tbl (cdr tbl)))))
  10294. (t
  10295. (setq tg (car e) c (cdr e))
  10296. (if ingroup (push tg (car groups)))
  10297. (setq tg (org-add-props tg nil 'face
  10298. (org-get-todo-face tg)))
  10299. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10300. (insert "[" c "] " tg (make-string
  10301. (- fwidth 4 (length tg)) ?\ ))
  10302. (when (= (setq cnt (1+ cnt)) ncol)
  10303. (insert "\n")
  10304. (if ingroup (insert " "))
  10305. (setq cnt 0)))))
  10306. (insert "\n")
  10307. (goto-char (point-min))
  10308. (if (not expert) (org-fit-window-to-buffer))
  10309. (message "[a-z..]:Set [SPC]:clear")
  10310. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10311. (cond
  10312. ((or (= c ?\C-g)
  10313. (and (= c ?q) (not (rassoc c fulltable))))
  10314. (setq quit-flag t))
  10315. ((= c ?\ ) nil)
  10316. ((setq e (rassoc c fulltable) tg (car e))
  10317. tg)
  10318. (t (setq quit-flag t)))))))
  10319. (defun org-entry-is-todo-p ()
  10320. (member (org-get-todo-state) org-not-done-keywords))
  10321. (defun org-entry-is-done-p ()
  10322. (member (org-get-todo-state) org-done-keywords))
  10323. (defun org-get-todo-state ()
  10324. (save-excursion
  10325. (org-back-to-heading t)
  10326. (and (looking-at org-todo-line-regexp)
  10327. (match-end 2)
  10328. (match-string 2))))
  10329. (defun org-at-date-range-p (&optional inactive-ok)
  10330. "Is the cursor inside a date range?"
  10331. (interactive)
  10332. (save-excursion
  10333. (catch 'exit
  10334. (let ((pos (point)))
  10335. (skip-chars-backward "^[<\r\n")
  10336. (skip-chars-backward "<[")
  10337. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10338. (>= (match-end 0) pos)
  10339. (throw 'exit t))
  10340. (skip-chars-backward "^<[\r\n")
  10341. (skip-chars-backward "<[")
  10342. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10343. (>= (match-end 0) pos)
  10344. (throw 'exit t)))
  10345. nil)))
  10346. (defun org-get-repeat (&optional tagline)
  10347. "Check if there is a deadline/schedule with repeater in this entry."
  10348. (save-match-data
  10349. (save-excursion
  10350. (org-back-to-heading t)
  10351. (and (re-search-forward (if tagline
  10352. (concat tagline "\\s-*" org-repeat-re)
  10353. org-repeat-re)
  10354. (org-entry-end-position) t)
  10355. (match-string-no-properties 1)))))
  10356. (defvar org-last-changed-timestamp)
  10357. (defvar org-last-inserted-timestamp)
  10358. (defvar org-log-post-message)
  10359. (defvar org-log-note-purpose)
  10360. (defvar org-log-note-how)
  10361. (defvar org-log-note-extra)
  10362. (defun org-auto-repeat-maybe (done-word)
  10363. "Check if the current headline contains a repeated deadline/schedule.
  10364. If yes, set TODO state back to what it was and change the base date
  10365. of repeating deadline/scheduled time stamps to new date.
  10366. This function is run automatically after each state change to a DONE state."
  10367. ;; last-state is dynamically scoped into this function
  10368. (let* ((repeat (org-get-repeat))
  10369. (aa (assoc last-state org-todo-kwd-alist))
  10370. (interpret (nth 1 aa))
  10371. (head (nth 2 aa))
  10372. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10373. (msg "Entry repeats: ")
  10374. (org-log-done nil)
  10375. (org-todo-log-states nil)
  10376. (nshiftmax 10) (nshift 0)
  10377. re type n what ts time to-state)
  10378. (when repeat
  10379. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10380. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10381. org-todo-repeat-to-state))
  10382. (unless (and to-state (member to-state org-todo-keywords-1))
  10383. (setq to-state (if (eq interpret 'type) last-state head)))
  10384. (org-todo to-state)
  10385. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10386. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10387. (org-time-stamp-format t t))))
  10388. (when org-log-repeat
  10389. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10390. (memq 'org-add-log-note post-command-hook))
  10391. ;; OK, we are already setup for some record
  10392. (if (eq org-log-repeat 'note)
  10393. ;; make sure we take a note, not only a time stamp
  10394. (setq org-log-note-how 'note))
  10395. ;; Set up for taking a record
  10396. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10397. last-state
  10398. 'findpos org-log-repeat)))
  10399. (org-back-to-heading t)
  10400. (org-add-planning-info nil nil 'closed)
  10401. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10402. org-deadline-time-regexp "\\)\\|\\("
  10403. org-ts-regexp "\\)"))
  10404. (while (re-search-forward
  10405. re (save-excursion (outline-next-heading) (point)) t)
  10406. (setq type (if (match-end 1) org-scheduled-string
  10407. (if (match-end 3) org-deadline-string "Plain:"))
  10408. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10409. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10410. (setq n (string-to-number (match-string 2 ts))
  10411. what (match-string 3 ts))
  10412. (if (equal what "w") (setq n (* n 7) what "d"))
  10413. ;; Preparation, see if we need to modify the start date for the change
  10414. (when (match-end 1)
  10415. (setq time (save-match-data (org-time-string-to-time ts)))
  10416. (cond
  10417. ((equal (match-string 1 ts) ".")
  10418. ;; Shift starting date to today
  10419. (org-timestamp-change
  10420. (- (time-to-days (current-time)) (time-to-days time))
  10421. 'day))
  10422. ((equal (match-string 1 ts) "+")
  10423. (while (or (= nshift 0)
  10424. (<= (time-to-days time) (time-to-days (current-time))))
  10425. (when (= (incf nshift) nshiftmax)
  10426. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10427. (error "Abort")))
  10428. (org-timestamp-change n (cdr (assoc what whata)))
  10429. (org-at-timestamp-p t)
  10430. (setq ts (match-string 1))
  10431. (setq time (save-match-data (org-time-string-to-time ts))))
  10432. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10433. ;; rematch, so that we have everything in place for the real shift
  10434. (org-at-timestamp-p t)
  10435. (setq ts (match-string 1))
  10436. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10437. (org-timestamp-change n (cdr (assoc what whata)))
  10438. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10439. (setq org-log-post-message msg)
  10440. (message "%s" msg))))
  10441. (defun org-show-todo-tree (arg)
  10442. "Make a compact tree which shows all headlines marked with TODO.
  10443. The tree will show the lines where the regexp matches, and all higher
  10444. headlines above the match.
  10445. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10446. With a numeric prefix N, construct a sparse tree for the Nth element
  10447. of `org-todo-keywords-1'."
  10448. (interactive "P")
  10449. (let ((case-fold-search nil)
  10450. (kwd-re
  10451. (cond ((null arg) org-not-done-regexp)
  10452. ((equal arg '(4))
  10453. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10454. (mapcar 'list org-todo-keywords-1))))
  10455. (concat "\\("
  10456. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10457. "\\)\\>")))
  10458. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10459. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10460. org-todo-keywords-1)))
  10461. (t (error "Invalid prefix argument: %s" arg)))))
  10462. (message "%d TODO entries found"
  10463. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10464. (defun org-deadline (&optional remove time)
  10465. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10466. With argument REMOVE, remove any deadline from the item.
  10467. When TIME is set, it should be an internal time specification, and the
  10468. scheduling will use the corresponding date."
  10469. (interactive "P")
  10470. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10471. (repeater (and old-date
  10472. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10473. (match-string 1 old-date))))
  10474. (if remove
  10475. (progn
  10476. (when (and old-date org-log-redeadline)
  10477. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10478. org-log-redeadline))
  10479. (org-remove-timestamp-with-keyword org-deadline-string)
  10480. (message "Item no longer has a deadline."))
  10481. (org-add-planning-info 'deadline time 'closed)
  10482. (when (and old-date org-log-redeadline
  10483. (not (equal old-date
  10484. (substring org-last-inserted-timestamp 1 -1))))
  10485. (org-add-log-setup 'redeadline nil old-date 'findpos
  10486. org-log-redeadline))
  10487. (when repeater
  10488. (save-excursion
  10489. (org-back-to-heading t)
  10490. (when (re-search-forward (concat org-deadline-string " "
  10491. org-last-inserted-timestamp)
  10492. (save-excursion
  10493. (outline-next-heading) (point)) t)
  10494. (goto-char (1- (match-end 0)))
  10495. (insert " " repeater)
  10496. (setq org-last-inserted-timestamp
  10497. (concat (substring org-last-inserted-timestamp 0 -1)
  10498. " " repeater
  10499. (substring org-last-inserted-timestamp -1))))))
  10500. (message "Deadline on %s" org-last-inserted-timestamp))))
  10501. (defun org-schedule (&optional remove time)
  10502. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10503. With argument REMOVE, remove any scheduling date from the item.
  10504. When TIME is set, it should be an internal time specification, and the
  10505. scheduling will use the corresponding date."
  10506. (interactive "P")
  10507. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10508. (repeater (and old-date
  10509. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10510. (match-string 1 old-date))))
  10511. (if remove
  10512. (progn
  10513. (when (and old-date org-log-reschedule)
  10514. (org-add-log-setup 'delschedule nil old-date 'findpos
  10515. org-log-reschedule))
  10516. (org-remove-timestamp-with-keyword org-scheduled-string)
  10517. (message "Item is no longer scheduled."))
  10518. (org-add-planning-info 'scheduled time 'closed)
  10519. (when (and old-date org-log-reschedule
  10520. (not (equal old-date
  10521. (substring org-last-inserted-timestamp 1 -1))))
  10522. (org-add-log-setup 'reschedule nil old-date 'findpos
  10523. org-log-reschedule))
  10524. (when repeater
  10525. (save-excursion
  10526. (org-back-to-heading t)
  10527. (when (re-search-forward (concat org-scheduled-string " "
  10528. org-last-inserted-timestamp)
  10529. (save-excursion
  10530. (outline-next-heading) (point)) t)
  10531. (goto-char (1- (match-end 0)))
  10532. (insert " " repeater)
  10533. (setq org-last-inserted-timestamp
  10534. (concat (substring org-last-inserted-timestamp 0 -1)
  10535. " " repeater
  10536. (substring org-last-inserted-timestamp -1))))))
  10537. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10538. (defun org-get-scheduled-time (pom &optional inherit)
  10539. "Get the scheduled time as a time tuple, of a format suitable
  10540. for calling org-schedule with, or if there is no scheduling,
  10541. returns nil."
  10542. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10543. (when time
  10544. (apply 'encode-time (org-parse-time-string time)))))
  10545. (defun org-get-deadline-time (pom &optional inherit)
  10546. "Get the deadline as a time tuple, of a format suitable for
  10547. calling org-deadline with, or if there is no scheduling, returns
  10548. nil."
  10549. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10550. (when time
  10551. (apply 'encode-time (org-parse-time-string time)))))
  10552. (defun org-remove-timestamp-with-keyword (keyword)
  10553. "Remove all time stamps with KEYWORD in the current entry."
  10554. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10555. beg)
  10556. (save-excursion
  10557. (org-back-to-heading t)
  10558. (setq beg (point))
  10559. (outline-next-heading)
  10560. (while (re-search-backward re beg t)
  10561. (replace-match "")
  10562. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10563. (equal (char-before) ?\ ))
  10564. (backward-delete-char 1)
  10565. (if (string-match "^[ \t]*$" (buffer-substring
  10566. (point-at-bol) (point-at-eol)))
  10567. (delete-region (point-at-bol)
  10568. (min (point-max) (1+ (point-at-eol))))))))))
  10569. (defun org-add-planning-info (what &optional time &rest remove)
  10570. "Insert new timestamp with keyword in the line directly after the headline.
  10571. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10572. If non is given, the user is prompted for a date.
  10573. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10574. be removed."
  10575. (interactive)
  10576. (let (org-time-was-given org-end-time-was-given ts
  10577. end default-time default-input)
  10578. (catch 'exit
  10579. (when (and (not time) (memq what '(scheduled deadline)))
  10580. ;; Try to get a default date/time from existing timestamp
  10581. (save-excursion
  10582. (org-back-to-heading t)
  10583. (setq end (save-excursion (outline-next-heading) (point)))
  10584. (when (re-search-forward (if (eq what 'scheduled)
  10585. org-scheduled-time-regexp
  10586. org-deadline-time-regexp)
  10587. end t)
  10588. (setq ts (match-string 1)
  10589. default-time
  10590. (apply 'encode-time (org-parse-time-string ts))
  10591. default-input (and ts (org-get-compact-tod ts))))))
  10592. (when what
  10593. ;; If necessary, get the time from the user
  10594. (setq time (or time (org-read-date nil 'to-time nil nil
  10595. default-time default-input))))
  10596. (when (and org-insert-labeled-timestamps-at-point
  10597. (member what '(scheduled deadline)))
  10598. (insert
  10599. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10600. (org-insert-time-stamp time org-time-was-given
  10601. nil nil nil (list org-end-time-was-given))
  10602. (setq what nil))
  10603. (save-excursion
  10604. (save-restriction
  10605. (let (col list elt ts buffer-invisibility-spec)
  10606. (org-back-to-heading t)
  10607. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10608. (goto-char (match-end 1))
  10609. (setq col (current-column))
  10610. (goto-char (match-end 0))
  10611. (if (eobp) (insert "\n") (forward-char 1))
  10612. (when (and (not what)
  10613. (not (looking-at
  10614. (concat "[ \t]*"
  10615. org-keyword-time-not-clock-regexp))))
  10616. ;; Nothing to add, nothing to remove...... :-)
  10617. (throw 'exit nil))
  10618. (if (and (not (looking-at outline-regexp))
  10619. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10620. "[^\r\n]*"))
  10621. (not (equal (match-string 1) org-clock-string)))
  10622. (narrow-to-region (match-beginning 0) (match-end 0))
  10623. (insert-before-markers "\n")
  10624. (backward-char 1)
  10625. (narrow-to-region (point) (point))
  10626. (and org-adapt-indentation (org-indent-to-column col)))
  10627. ;; Check if we have to remove something.
  10628. (setq list (cons what remove))
  10629. (while list
  10630. (setq elt (pop list))
  10631. (goto-char (point-min))
  10632. (when (or (and (eq elt 'scheduled)
  10633. (re-search-forward org-scheduled-time-regexp nil t))
  10634. (and (eq elt 'deadline)
  10635. (re-search-forward org-deadline-time-regexp nil t))
  10636. (and (eq elt 'closed)
  10637. (re-search-forward org-closed-time-regexp nil t)))
  10638. (replace-match "")
  10639. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10640. (skip-chars-backward " ")
  10641. (if (looking-at " +") (replace-match ""))))
  10642. (goto-char (point-max))
  10643. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10644. (when what
  10645. (insert
  10646. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10647. (cond ((eq what 'scheduled) org-scheduled-string)
  10648. ((eq what 'deadline) org-deadline-string)
  10649. ((eq what 'closed) org-closed-string))
  10650. " ")
  10651. (setq ts (org-insert-time-stamp
  10652. time
  10653. (or org-time-was-given
  10654. (and (eq what 'closed) org-log-done-with-time))
  10655. (eq what 'closed)
  10656. nil nil (list org-end-time-was-given)))
  10657. (end-of-line 1))
  10658. (goto-char (point-min))
  10659. (widen)
  10660. (if (and (looking-at "[ \t]*\n")
  10661. (equal (char-before) ?\n))
  10662. (delete-region (1- (point)) (point-at-eol)))
  10663. ts))))))
  10664. (defvar org-log-note-marker (make-marker))
  10665. (defvar org-log-note-purpose nil)
  10666. (defvar org-log-note-state nil)
  10667. (defvar org-log-note-previous-state nil)
  10668. (defvar org-log-note-how nil)
  10669. (defvar org-log-note-extra nil)
  10670. (defvar org-log-note-window-configuration nil)
  10671. (defvar org-log-note-return-to (make-marker))
  10672. (defvar org-log-post-message nil
  10673. "Message to be displayed after a log note has been stored.
  10674. The auto-repeater uses this.")
  10675. (defun org-add-note ()
  10676. "Add a note to the current entry.
  10677. This is done in the same way as adding a state change note."
  10678. (interactive)
  10679. (org-add-log-setup 'note nil nil 'findpos nil))
  10680. (defvar org-property-end-re)
  10681. (defun org-add-log-setup (&optional purpose state prev-state
  10682. findpos how &optional extra)
  10683. "Set up the post command hook to take a note.
  10684. If this is about to TODO state change, the new state is expected in STATE.
  10685. When FINDPOS is non-nil, find the correct position for the note in
  10686. the current entry. If not, assume that it can be inserted at point.
  10687. HOW is an indicator what kind of note should be created.
  10688. EXTRA is additional text that will be inserted into the notes buffer."
  10689. (let* ((org-log-into-drawer (org-log-into-drawer))
  10690. (drawer (cond ((stringp org-log-into-drawer)
  10691. org-log-into-drawer)
  10692. (org-log-into-drawer "LOGBOOK")
  10693. (t nil))))
  10694. (save-restriction
  10695. (save-excursion
  10696. (when findpos
  10697. (org-back-to-heading t)
  10698. (narrow-to-region (point) (save-excursion
  10699. (outline-next-heading) (point)))
  10700. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10701. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10702. "[^\r\n]*\\)?"))
  10703. (goto-char (match-end 0))
  10704. (cond
  10705. (drawer
  10706. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10707. nil t)
  10708. (progn
  10709. (goto-char (match-end 0))
  10710. (or org-log-states-order-reversed
  10711. (and (re-search-forward org-property-end-re nil t)
  10712. (goto-char (1- (match-beginning 0))))))
  10713. (insert "\n:" drawer ":\n:END:")
  10714. (beginning-of-line 0)
  10715. (org-indent-line-function)
  10716. (beginning-of-line 2)
  10717. (org-indent-line-function)
  10718. (end-of-line 0)))
  10719. ((and org-log-state-notes-insert-after-drawers
  10720. (save-excursion
  10721. (forward-line) (looking-at org-drawer-regexp)))
  10722. (forward-line)
  10723. (while (looking-at org-drawer-regexp)
  10724. (goto-char (match-end 0))
  10725. (re-search-forward org-property-end-re (point-max) t)
  10726. (forward-line))
  10727. (forward-line -1)))
  10728. (unless org-log-states-order-reversed
  10729. (and (= (char-after) ?\n) (forward-char 1))
  10730. (org-skip-over-state-notes)
  10731. (skip-chars-backward " \t\n\r")))
  10732. (move-marker org-log-note-marker (point))
  10733. (setq org-log-note-purpose purpose
  10734. org-log-note-state state
  10735. org-log-note-previous-state prev-state
  10736. org-log-note-how how
  10737. org-log-note-extra extra)
  10738. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10739. (defun org-skip-over-state-notes ()
  10740. "Skip past the list of State notes in an entry."
  10741. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10742. (while (looking-at "[ \t]*- State")
  10743. (condition-case nil
  10744. (org-next-item)
  10745. (error (org-end-of-item)))))
  10746. (defun org-add-log-note (&optional purpose)
  10747. "Pop up a window for taking a note, and add this note later at point."
  10748. (remove-hook 'post-command-hook 'org-add-log-note)
  10749. (setq org-log-note-window-configuration (current-window-configuration))
  10750. (delete-other-windows)
  10751. (move-marker org-log-note-return-to (point))
  10752. (switch-to-buffer (marker-buffer org-log-note-marker))
  10753. (goto-char org-log-note-marker)
  10754. (org-switch-to-buffer-other-window "*Org Note*")
  10755. (erase-buffer)
  10756. (if (memq org-log-note-how '(time state))
  10757. (let (current-prefix-arg) (org-store-log-note))
  10758. (let ((org-inhibit-startup t)) (org-mode))
  10759. (insert (format "# Insert note for %s.
  10760. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10761. (cond
  10762. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10763. ((eq org-log-note-purpose 'done) "closed todo item")
  10764. ((eq org-log-note-purpose 'state)
  10765. (format "state change from \"%s\" to \"%s\""
  10766. (or org-log-note-previous-state "")
  10767. (or org-log-note-state "")))
  10768. ((eq org-log-note-purpose 'reschedule)
  10769. "rescheduling")
  10770. ((eq org-log-note-purpose 'delschedule)
  10771. "no longer scheduled")
  10772. ((eq org-log-note-purpose 'redeadline)
  10773. "changing deadline")
  10774. ((eq org-log-note-purpose 'deldeadline)
  10775. "removing deadline")
  10776. ((eq org-log-note-purpose 'refile)
  10777. "refiling")
  10778. ((eq org-log-note-purpose 'note)
  10779. "this entry")
  10780. (t (error "This should not happen")))))
  10781. (if org-log-note-extra (insert org-log-note-extra))
  10782. (org-set-local 'org-finish-function 'org-store-log-note)))
  10783. (defvar org-note-abort nil) ; dynamically scoped
  10784. (defun org-store-log-note ()
  10785. "Finish taking a log note, and insert it to where it belongs."
  10786. (let ((txt (buffer-string))
  10787. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10788. lines ind)
  10789. (kill-buffer (current-buffer))
  10790. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10791. (setq txt (replace-match "" t t txt)))
  10792. (if (string-match "\\s-+\\'" txt)
  10793. (setq txt (replace-match "" t t txt)))
  10794. (setq lines (org-split-string txt "\n"))
  10795. (when (and note (string-match "\\S-" note))
  10796. (setq note
  10797. (org-replace-escapes
  10798. note
  10799. (list (cons "%u" (user-login-name))
  10800. (cons "%U" user-full-name)
  10801. (cons "%t" (format-time-string
  10802. (org-time-stamp-format 'long 'inactive)
  10803. (current-time)))
  10804. (cons "%T" (format-time-string
  10805. (org-time-stamp-format 'long nil)
  10806. (current-time)))
  10807. (cons "%s" (if org-log-note-state
  10808. (concat "\"" org-log-note-state "\"")
  10809. ""))
  10810. (cons "%S" (if org-log-note-previous-state
  10811. (concat "\"" org-log-note-previous-state "\"")
  10812. "\"\"")))))
  10813. (if lines (setq note (concat note " \\\\")))
  10814. (push note lines))
  10815. (when (or current-prefix-arg org-note-abort)
  10816. (when org-log-into-drawer
  10817. (org-remove-empty-drawer-at
  10818. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10819. org-log-note-marker))
  10820. (setq lines nil))
  10821. (when lines
  10822. (with-current-buffer (marker-buffer org-log-note-marker)
  10823. (save-excursion
  10824. (goto-char org-log-note-marker)
  10825. (move-marker org-log-note-marker nil)
  10826. (end-of-line 1)
  10827. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10828. (insert "- " (pop lines))
  10829. (org-indent-line-function)
  10830. (beginning-of-line 1)
  10831. (looking-at "[ \t]*")
  10832. (setq ind (concat (match-string 0) " "))
  10833. (end-of-line 1)
  10834. (while lines (insert "\n" ind (pop lines)))
  10835. (message "Note stored")
  10836. (org-back-to-heading t)
  10837. (org-cycle-hide-drawers 'children)))))
  10838. (set-window-configuration org-log-note-window-configuration)
  10839. (with-current-buffer (marker-buffer org-log-note-return-to)
  10840. (goto-char org-log-note-return-to))
  10841. (move-marker org-log-note-return-to nil)
  10842. (and org-log-post-message (message "%s" org-log-post-message)))
  10843. (defun org-remove-empty-drawer-at (drawer pos)
  10844. "Remove an empty drawer DRAWER at position POS.
  10845. POS may also be a marker."
  10846. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10847. (save-excursion
  10848. (save-restriction
  10849. (widen)
  10850. (goto-char pos)
  10851. (if (org-in-regexp
  10852. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10853. (replace-match ""))))))
  10854. (defun org-sparse-tree (&optional arg)
  10855. "Create a sparse tree, prompt for the details.
  10856. This command can create sparse trees. You first need to select the type
  10857. of match used to create the tree:
  10858. t Show all TODO entries.
  10859. T Show entries with a specific TODO keyword.
  10860. m Show entries selected by a tags/property match.
  10861. p Enter a property name and its value (both with completion on existing
  10862. names/values) and show entries with that property.
  10863. / Show entries matching a regular expression (`r' can be used as well)
  10864. d Show deadlines due within `org-deadline-warning-days'.
  10865. b Show deadlines and scheduled items before a date.
  10866. a Show deadlines and scheduled items after a date."
  10867. (interactive "P")
  10868. (let (ans kwd value)
  10869. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
  10870. (setq ans (read-char-exclusive))
  10871. (cond
  10872. ((equal ans ?d)
  10873. (call-interactively 'org-check-deadlines))
  10874. ((equal ans ?b)
  10875. (call-interactively 'org-check-before-date))
  10876. ((equal ans ?a)
  10877. (call-interactively 'org-check-after-date))
  10878. ((equal ans ?t)
  10879. (org-show-todo-tree nil))
  10880. ((equal ans ?T)
  10881. (org-show-todo-tree '(4)))
  10882. ((member ans '(?T ?m))
  10883. (call-interactively 'org-match-sparse-tree))
  10884. ((member ans '(?p ?P))
  10885. (setq kwd (org-icompleting-read "Property: "
  10886. (mapcar 'list (org-buffer-property-keys))))
  10887. (setq value (org-icompleting-read "Value: "
  10888. (mapcar 'list (org-property-values kwd))))
  10889. (unless (string-match "\\`{.*}\\'" value)
  10890. (setq value (concat "\"" value "\"")))
  10891. (org-match-sparse-tree arg (concat kwd "=" value)))
  10892. ((member ans '(?r ?R ?/))
  10893. (call-interactively 'org-occur))
  10894. (t (error "No such sparse tree command \"%c\"" ans)))))
  10895. (defvar org-occur-highlights nil
  10896. "List of overlays used for occur matches.")
  10897. (make-variable-buffer-local 'org-occur-highlights)
  10898. (defvar org-occur-parameters nil
  10899. "Parameters of the active org-occur calls.
  10900. This is a list, each call to org-occur pushes as cons cell,
  10901. containing the regular expression and the callback, onto the list.
  10902. The list can contain several entries if `org-occur' has been called
  10903. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10904. will only contain one set of parameters. When the highlights are
  10905. removed (for example with `C-c C-c', or with the next edit (depending
  10906. on `org-remove-highlights-with-change'), this variable is emptied
  10907. as well.")
  10908. (make-variable-buffer-local 'org-occur-parameters)
  10909. (defun org-occur (regexp &optional keep-previous callback)
  10910. "Make a compact tree which shows all matches of REGEXP.
  10911. The tree will show the lines where the regexp matches, and all higher
  10912. headlines above the match. It will also show the heading after the match,
  10913. to make sure editing the matching entry is easy.
  10914. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10915. call to `org-occur' will be kept, to allow stacking of calls to this
  10916. command.
  10917. If CALLBACK is non-nil, it is a function which is called to confirm
  10918. that the match should indeed be shown."
  10919. (interactive "sRegexp: \nP")
  10920. (when (equal regexp "")
  10921. (error "Regexp cannot be empty"))
  10922. (unless keep-previous
  10923. (org-remove-occur-highlights nil nil t))
  10924. (push (cons regexp callback) org-occur-parameters)
  10925. (let ((cnt 0))
  10926. (save-excursion
  10927. (goto-char (point-min))
  10928. (if (or (not keep-previous) ; do not want to keep
  10929. (not org-occur-highlights)) ; no previous matches
  10930. ;; hide everything
  10931. (org-overview))
  10932. (while (re-search-forward regexp nil t)
  10933. (when (or (not callback)
  10934. (save-match-data (funcall callback)))
  10935. (setq cnt (1+ cnt))
  10936. (when org-highlight-sparse-tree-matches
  10937. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10938. (org-show-context 'occur-tree))))
  10939. (when org-remove-highlights-with-change
  10940. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10941. nil 'local))
  10942. (unless org-sparse-tree-open-archived-trees
  10943. (org-hide-archived-subtrees (point-min) (point-max)))
  10944. (run-hooks 'org-occur-hook)
  10945. (if (interactive-p)
  10946. (message "%d match(es) for regexp %s" cnt regexp))
  10947. cnt))
  10948. (defun org-show-context (&optional key)
  10949. "Make sure point and context are visible.
  10950. How much context is shown depends upon the variables
  10951. `org-show-hierarchy-above', `org-show-following-heading'. and
  10952. `org-show-siblings'."
  10953. (let ((heading-p (org-on-heading-p t))
  10954. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10955. (following-p (org-get-alist-option org-show-following-heading key))
  10956. (entry-p (org-get-alist-option org-show-entry-below key))
  10957. (siblings-p (org-get-alist-option org-show-siblings key)))
  10958. (catch 'exit
  10959. ;; Show heading or entry text
  10960. (if (and heading-p (not entry-p))
  10961. (org-flag-heading nil) ; only show the heading
  10962. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10963. (org-show-hidden-entry))) ; show entire entry
  10964. (when following-p
  10965. ;; Show next sibling, or heading below text
  10966. (save-excursion
  10967. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10968. (org-flag-heading nil))))
  10969. (when siblings-p (org-show-siblings))
  10970. (when hierarchy-p
  10971. ;; show all higher headings, possibly with siblings
  10972. (save-excursion
  10973. (while (and (condition-case nil
  10974. (progn (org-up-heading-all 1) t)
  10975. (error nil))
  10976. (not (bobp)))
  10977. (org-flag-heading nil)
  10978. (when siblings-p (org-show-siblings))))))))
  10979. (defvar org-reveal-start-hook nil
  10980. "Hook run before revealing a location.")
  10981. (defun org-reveal (&optional siblings)
  10982. "Show current entry, hierarchy above it, and the following headline.
  10983. This can be used to show a consistent set of context around locations
  10984. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10985. not t for the search context.
  10986. With optional argument SIBLINGS, on each level of the hierarchy all
  10987. siblings are shown. This repairs the tree structure to what it would
  10988. look like when opened with hierarchical calls to `org-cycle'.
  10989. With double optional argument \\[universal-argument] \\[universal-argument], \
  10990. go to the parent and show the
  10991. entire tree."
  10992. (interactive "P")
  10993. (run-hooks 'org-reveal-start-hook)
  10994. (let ((org-show-hierarchy-above t)
  10995. (org-show-following-heading t)
  10996. (org-show-siblings (if siblings t org-show-siblings)))
  10997. (org-show-context nil))
  10998. (when (equal siblings '(16))
  10999. (save-excursion
  11000. (when (org-up-heading-safe)
  11001. (org-show-subtree)
  11002. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11003. (defun org-highlight-new-match (beg end)
  11004. "Highlight from BEG to END and mark the highlight is an occur headline."
  11005. (let ((ov (make-overlay beg end)))
  11006. (overlay-put ov 'face 'secondary-selection)
  11007. (push ov org-occur-highlights)))
  11008. (defun org-remove-occur-highlights (&optional beg end noremove)
  11009. "Remove the occur highlights from the buffer.
  11010. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11011. from the `before-change-functions' in the current buffer."
  11012. (interactive)
  11013. (unless org-inhibit-highlight-removal
  11014. (mapc 'delete-overlay org-occur-highlights)
  11015. (setq org-occur-highlights nil)
  11016. (setq org-occur-parameters nil)
  11017. (unless noremove
  11018. (remove-hook 'before-change-functions
  11019. 'org-remove-occur-highlights 'local))))
  11020. ;;;; Priorities
  11021. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11022. "Regular expression matching the priority indicator.")
  11023. (defvar org-remove-priority-next-time nil)
  11024. (defun org-priority-up ()
  11025. "Increase the priority of the current item."
  11026. (interactive)
  11027. (org-priority 'up))
  11028. (defun org-priority-down ()
  11029. "Decrease the priority of the current item."
  11030. (interactive)
  11031. (org-priority 'down))
  11032. (defun org-priority (&optional action)
  11033. "Change the priority of an item by ARG.
  11034. ACTION can be `set', `up', `down', or a character."
  11035. (interactive)
  11036. (unless org-enable-priority-commands
  11037. (error "Priority commands are disabled"))
  11038. (setq action (or action 'set))
  11039. (let (current new news have remove)
  11040. (save-excursion
  11041. (org-back-to-heading t)
  11042. (if (looking-at org-priority-regexp)
  11043. (setq current (string-to-char (match-string 2))
  11044. have t)
  11045. (setq current org-default-priority))
  11046. (cond
  11047. ((eq action 'remove)
  11048. (setq remove t new ?\ ))
  11049. ((or (eq action 'set)
  11050. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11051. (if (not (eq action 'set))
  11052. (setq new action)
  11053. (message "Priority %c-%c, SPC to remove: "
  11054. org-highest-priority org-lowest-priority)
  11055. (setq new (read-char-exclusive)))
  11056. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11057. (= (upcase org-lowest-priority) org-lowest-priority))
  11058. (setq new (upcase new)))
  11059. (cond ((equal new ?\ ) (setq remove t))
  11060. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11061. (error "Priority must be between `%c' and `%c'"
  11062. org-highest-priority org-lowest-priority))))
  11063. ((eq action 'up)
  11064. (if (and (not have) (eq last-command this-command))
  11065. (setq new org-lowest-priority)
  11066. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11067. org-default-priority (1- current)))))
  11068. ((eq action 'down)
  11069. (if (and (not have) (eq last-command this-command))
  11070. (setq new org-highest-priority)
  11071. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11072. org-default-priority (1+ current)))))
  11073. (t (error "Invalid action")))
  11074. (if (or (< (upcase new) org-highest-priority)
  11075. (> (upcase new) org-lowest-priority))
  11076. (setq remove t))
  11077. (setq news (format "%c" new))
  11078. (if have
  11079. (if remove
  11080. (replace-match "" t t nil 1)
  11081. (replace-match news t t nil 2))
  11082. (if remove
  11083. (error "No priority cookie found in line")
  11084. (let ((case-fold-search nil))
  11085. (looking-at org-todo-line-regexp))
  11086. (if (match-end 2)
  11087. (progn
  11088. (goto-char (match-end 2))
  11089. (insert " [#" news "]"))
  11090. (goto-char (match-beginning 3))
  11091. (insert "[#" news "] "))))
  11092. (org-preserve-lc (org-set-tags nil 'align)))
  11093. (if remove
  11094. (message "Priority removed")
  11095. (message "Priority of current item set to %s" news))))
  11096. (defun org-get-priority (s)
  11097. "Find priority cookie and return priority."
  11098. (save-match-data
  11099. (if (not (string-match org-priority-regexp s))
  11100. (* 1000 (- org-lowest-priority org-default-priority))
  11101. (* 1000 (- org-lowest-priority
  11102. (string-to-char (match-string 2 s)))))))
  11103. ;;;; Tags
  11104. (defvar org-agenda-archives-mode)
  11105. (defvar org-map-continue-from nil
  11106. "Position from where mapping should continue.
  11107. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11108. (defvar org-scanner-tags nil
  11109. "The current tag list while the tags scanner is running.")
  11110. (defvar org-trust-scanner-tags nil
  11111. "Should `org-get-tags-at' use the tags fro the scanner.
  11112. This is for internal dynamical scoping only.
  11113. When this is non-nil, the function `org-get-tags-at' will return the value
  11114. of `org-scanner-tags' instead of building the list by itself. This
  11115. can lead to large speed-ups when the tags scanner is used in a file with
  11116. many entries, and when the list of tags is retrieved, for example to
  11117. obtain a list of properties. Building the tags list for each entry in such
  11118. a file becomes an N^2 operation - but with this variable set, it scales
  11119. as N.")
  11120. (defun org-scan-tags (action matcher &optional todo-only)
  11121. "Scan headline tags with inheritance and produce output ACTION.
  11122. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11123. or `agenda' to produce an entry list for an agenda view. It can also be
  11124. a Lisp form or a function that should be called at each matched headline, in
  11125. this case the return value is a list of all return values from these calls.
  11126. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11127. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11128. only lines with a TODO keyword are included in the output."
  11129. (require 'org-agenda)
  11130. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11131. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11132. (org-re
  11133. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  11134. (props (list 'face 'default
  11135. 'done-face 'org-agenda-done
  11136. 'undone-face 'default
  11137. 'mouse-face 'highlight
  11138. 'org-not-done-regexp org-not-done-regexp
  11139. 'org-todo-regexp org-todo-regexp
  11140. 'help-echo
  11141. (format "mouse-2 or RET jump to org file %s"
  11142. (abbreviate-file-name
  11143. (or (buffer-file-name (buffer-base-buffer))
  11144. (buffer-name (buffer-base-buffer)))))))
  11145. (case-fold-search nil)
  11146. (org-map-continue-from nil)
  11147. lspos tags tags-list
  11148. (tags-alist (list (cons 0 org-file-tags)))
  11149. (llast 0) rtn rtn1 level category i txt
  11150. todo marker entry priority)
  11151. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11152. (setq action (list 'lambda nil action)))
  11153. (save-excursion
  11154. (goto-char (point-min))
  11155. (when (eq action 'sparse-tree)
  11156. (org-overview)
  11157. (org-remove-occur-highlights))
  11158. (while (re-search-forward re nil t)
  11159. (catch :skip
  11160. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11161. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11162. (goto-char (setq lspos (match-beginning 0)))
  11163. (setq level (org-reduced-level (funcall outline-level))
  11164. category (org-get-category))
  11165. (setq i llast llast level)
  11166. ;; remove tag lists from same and sublevels
  11167. (while (>= i level)
  11168. (when (setq entry (assoc i tags-alist))
  11169. (setq tags-alist (delete entry tags-alist)))
  11170. (setq i (1- i)))
  11171. ;; add the next tags
  11172. (when tags
  11173. (setq tags (org-split-string tags ":")
  11174. tags-alist
  11175. (cons (cons level tags) tags-alist)))
  11176. ;; compile tags for current headline
  11177. (setq tags-list
  11178. (if org-use-tag-inheritance
  11179. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11180. tags)
  11181. org-scanner-tags tags-list)
  11182. (when org-use-tag-inheritance
  11183. (setcdr (car tags-alist)
  11184. (mapcar (lambda (x)
  11185. (setq x (copy-sequence x))
  11186. (org-add-prop-inherited x))
  11187. (cdar tags-alist))))
  11188. (when (and tags org-use-tag-inheritance
  11189. (or (not (eq t org-use-tag-inheritance))
  11190. org-tags-exclude-from-inheritance))
  11191. ;; selective inheritance, remove uninherited ones
  11192. (setcdr (car tags-alist)
  11193. (org-remove-uniherited-tags (cdar tags-alist))))
  11194. (when (and (or (not todo-only)
  11195. (and (member todo org-not-done-keywords)
  11196. (or (not org-agenda-tags-todo-honor-ignore-options)
  11197. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11198. (let ((case-fold-search t)) (eval matcher))
  11199. (or
  11200. (not (member org-archive-tag tags-list))
  11201. ;; we have an archive tag, should we use this anyway?
  11202. (or (not org-agenda-skip-archived-trees)
  11203. (and (eq action 'agenda) org-agenda-archives-mode))))
  11204. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11205. ;; select this headline
  11206. (cond
  11207. ((eq action 'sparse-tree)
  11208. (and org-highlight-sparse-tree-matches
  11209. (org-get-heading) (match-end 0)
  11210. (org-highlight-new-match
  11211. (match-beginning 0) (match-beginning 1)))
  11212. (org-show-context 'tags-tree))
  11213. ((eq action 'agenda)
  11214. (setq txt (org-format-agenda-item
  11215. ""
  11216. (concat
  11217. (if (eq org-tags-match-list-sublevels 'indented)
  11218. (make-string (1- level) ?.) "")
  11219. (org-get-heading))
  11220. category
  11221. tags-list
  11222. )
  11223. priority (org-get-priority txt))
  11224. (goto-char lspos)
  11225. (setq marker (org-agenda-new-marker))
  11226. (org-add-props txt props
  11227. 'org-marker marker 'org-hd-marker marker 'org-category category
  11228. 'todo-state todo
  11229. 'priority priority 'type "tagsmatch")
  11230. (push txt rtn))
  11231. ((functionp action)
  11232. (setq org-map-continue-from nil)
  11233. (save-excursion
  11234. (setq rtn1 (funcall action))
  11235. (push rtn1 rtn)))
  11236. (t (error "Invalid action")))
  11237. ;; if we are to skip sublevels, jump to end of subtree
  11238. (unless org-tags-match-list-sublevels
  11239. (org-end-of-subtree t)
  11240. (backward-char 1))))
  11241. ;; Get the correct position from where to continue
  11242. (if org-map-continue-from
  11243. (goto-char org-map-continue-from)
  11244. (and (= (point) lspos) (end-of-line 1)))))
  11245. (when (and (eq action 'sparse-tree)
  11246. (not org-sparse-tree-open-archived-trees))
  11247. (org-hide-archived-subtrees (point-min) (point-max)))
  11248. (nreverse rtn)))
  11249. (defun org-remove-uniherited-tags (tags)
  11250. "Remove all tags that are not inherited from the list TAGS."
  11251. (cond
  11252. ((eq org-use-tag-inheritance t)
  11253. (if org-tags-exclude-from-inheritance
  11254. (org-delete-all org-tags-exclude-from-inheritance tags)
  11255. tags))
  11256. ((not org-use-tag-inheritance) nil)
  11257. ((stringp org-use-tag-inheritance)
  11258. (delq nil (mapcar
  11259. (lambda (x)
  11260. (if (and (string-match org-use-tag-inheritance x)
  11261. (not (member x org-tags-exclude-from-inheritance)))
  11262. x nil))
  11263. tags)))
  11264. ((listp org-use-tag-inheritance)
  11265. (delq nil (mapcar
  11266. (lambda (x)
  11267. (if (member x org-use-tag-inheritance) x nil))
  11268. tags)))))
  11269. (defvar todo-only) ;; dynamically scoped
  11270. (defun org-match-sparse-tree (&optional todo-only match)
  11271. "Create a sparse tree according to tags string MATCH.
  11272. MATCH can contain positive and negative selection of tags, like
  11273. \"+WORK+URGENT-WITHBOSS\".
  11274. If optional argument TODO-ONLY is non-nil, only select lines that are
  11275. also TODO lines."
  11276. (interactive "P")
  11277. (org-prepare-agenda-buffers (list (current-buffer)))
  11278. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11279. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11280. (defvar org-cached-props nil)
  11281. (defun org-cached-entry-get (pom property)
  11282. (if (or (eq t org-use-property-inheritance)
  11283. (and (stringp org-use-property-inheritance)
  11284. (string-match org-use-property-inheritance property))
  11285. (and (listp org-use-property-inheritance)
  11286. (member property org-use-property-inheritance)))
  11287. ;; Caching is not possible, check it directly
  11288. (org-entry-get pom property 'inherit)
  11289. ;; Get all properties, so that we can do complicated checks easily
  11290. (cdr (assoc property (or org-cached-props
  11291. (setq org-cached-props
  11292. (org-entry-properties pom)))))))
  11293. (defun org-global-tags-completion-table (&optional files)
  11294. "Return the list of all tags in all agenda buffer/files."
  11295. (save-excursion
  11296. (org-uniquify
  11297. (delq nil
  11298. (apply 'append
  11299. (mapcar
  11300. (lambda (file)
  11301. (set-buffer (find-file-noselect file))
  11302. (append (org-get-buffer-tags)
  11303. (mapcar (lambda (x) (if (stringp (car-safe x))
  11304. (list (car-safe x)) nil))
  11305. org-tag-alist)))
  11306. (if (and files (car files))
  11307. files
  11308. (org-agenda-files))))))))
  11309. (defun org-make-tags-matcher (match)
  11310. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11311. ;; todo-only is scoped dynamically into this function, and the function
  11312. ;; may change it if the matcher asks for it.
  11313. (unless match
  11314. ;; Get a new match request, with completion
  11315. (let ((org-last-tags-completion-table
  11316. (org-global-tags-completion-table)))
  11317. (setq match (org-completing-read-no-i
  11318. "Match: " 'org-tags-completion-function nil nil nil
  11319. 'org-tags-history))))
  11320. ;; Parse the string and create a lisp form
  11321. (let ((match0 match)
  11322. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  11323. minus tag mm
  11324. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11325. orterms term orlist re-p str-p level-p level-op time-p
  11326. prop-p pn pv po cat-p gv rest)
  11327. (if (string-match "/+" match)
  11328. ;; match contains also a todo-matching request
  11329. (progn
  11330. (setq tagsmatch (substring match 0 (match-beginning 0))
  11331. todomatch (substring match (match-end 0)))
  11332. (if (string-match "^!" todomatch)
  11333. (setq todo-only t todomatch (substring todomatch 1)))
  11334. (if (string-match "^\\s-*$" todomatch)
  11335. (setq todomatch nil)))
  11336. ;; only matching tags
  11337. (setq tagsmatch match todomatch nil))
  11338. ;; Make the tags matcher
  11339. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11340. (setq tagsmatcher t)
  11341. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11342. (while (setq term (pop orterms))
  11343. (while (and (equal (substring term -1) "\\") orterms)
  11344. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11345. (while (string-match re term)
  11346. (setq rest (substring term (match-end 0))
  11347. minus (and (match-end 1)
  11348. (equal (match-string 1 term) "-"))
  11349. tag (save-match-data (replace-regexp-in-string
  11350. "\\\\-" "-"
  11351. (match-string 2 term)))
  11352. re-p (equal (string-to-char tag) ?{)
  11353. level-p (match-end 4)
  11354. prop-p (match-end 5)
  11355. mm (cond
  11356. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11357. (level-p
  11358. (setq level-op (org-op-to-function (match-string 3 term)))
  11359. `(,level-op level ,(string-to-number
  11360. (match-string 4 term))))
  11361. (prop-p
  11362. (setq pn (match-string 5 term)
  11363. po (match-string 6 term)
  11364. pv (match-string 7 term)
  11365. cat-p (equal pn "CATEGORY")
  11366. re-p (equal (string-to-char pv) ?{)
  11367. str-p (equal (string-to-char pv) ?\")
  11368. time-p (save-match-data
  11369. (string-match "^\"[[<].*[]>]\"$" pv))
  11370. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11371. (if time-p (setq pv (org-matcher-time pv)))
  11372. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11373. (cond
  11374. ((equal pn "CATEGORY")
  11375. (setq gv '(get-text-property (point) 'org-category)))
  11376. ((equal pn "TODO")
  11377. (setq gv 'todo))
  11378. (t
  11379. (setq gv `(org-cached-entry-get nil ,pn))))
  11380. (if re-p
  11381. (if (eq po 'org<>)
  11382. `(not (string-match ,pv (or ,gv "")))
  11383. `(string-match ,pv (or ,gv "")))
  11384. (if str-p
  11385. `(,po (or ,gv "") ,pv)
  11386. `(,po (string-to-number (or ,gv ""))
  11387. ,(string-to-number pv) ))))
  11388. (t `(member ,tag tags-list)))
  11389. mm (if minus (list 'not mm) mm)
  11390. term rest)
  11391. (push mm tagsmatcher))
  11392. (push (if (> (length tagsmatcher) 1)
  11393. (cons 'and tagsmatcher)
  11394. (car tagsmatcher))
  11395. orlist)
  11396. (setq tagsmatcher nil))
  11397. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11398. (setq tagsmatcher
  11399. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11400. ;; Make the todo matcher
  11401. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11402. (setq todomatcher t)
  11403. (setq orterms (org-split-string todomatch "|") orlist nil)
  11404. (while (setq term (pop orterms))
  11405. (while (string-match re term)
  11406. (setq minus (and (match-end 1)
  11407. (equal (match-string 1 term) "-"))
  11408. kwd (match-string 2 term)
  11409. re-p (equal (string-to-char kwd) ?{)
  11410. term (substring term (match-end 0))
  11411. mm (if re-p
  11412. `(string-match ,(substring kwd 1 -1) todo)
  11413. (list 'equal 'todo kwd))
  11414. mm (if minus (list 'not mm) mm))
  11415. (push mm todomatcher))
  11416. (push (if (> (length todomatcher) 1)
  11417. (cons 'and todomatcher)
  11418. (car todomatcher))
  11419. orlist)
  11420. (setq todomatcher nil))
  11421. (setq todomatcher (if (> (length orlist) 1)
  11422. (cons 'or orlist) (car orlist))))
  11423. ;; Return the string and lisp forms of the matcher
  11424. (setq matcher (if todomatcher
  11425. (list 'and tagsmatcher todomatcher)
  11426. tagsmatcher))
  11427. (cons match0 matcher)))
  11428. (defun org-op-to-function (op &optional stringp)
  11429. "Turn an operator into the appropriate function."
  11430. (setq op
  11431. (cond
  11432. ((equal op "<" ) '(< string< org-time<))
  11433. ((equal op ">" ) '(> org-string> org-time>))
  11434. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11435. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11436. ((member op '("=" "==")) '(= string= org-time=))
  11437. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11438. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11439. (defun org<> (a b) (not (= a b)))
  11440. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11441. (defun org-string>= (a b) (not (string< a b)))
  11442. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11443. (defun org-string<> (a b) (not (string= a b)))
  11444. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11445. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11446. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11447. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11448. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11449. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11450. (defun org-2ft (s)
  11451. "Convert S to a floating point time.
  11452. If S is already a number, just return it. If it is a string, parse
  11453. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11454. (cond
  11455. ((numberp s) s)
  11456. ((stringp s)
  11457. (condition-case nil
  11458. (float-time (apply 'encode-time (org-parse-time-string s)))
  11459. (error 0.)))
  11460. (t 0.)))
  11461. (defun org-time-today ()
  11462. "Time in seconds today at 0:00.
  11463. Returns the float number of seconds since the beginning of the
  11464. epoch to the beginning of today (00:00)."
  11465. (float-time (apply 'encode-time
  11466. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11467. (defun org-matcher-time (s)
  11468. "Interpret a time comparison value."
  11469. (save-match-data
  11470. (cond
  11471. ((string= s "<now>") (float-time))
  11472. ((string= s "<today>") (org-time-today))
  11473. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11474. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11475. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11476. (+ (org-time-today)
  11477. (* (string-to-number (match-string 1 s))
  11478. (cdr (assoc (match-string 2 s)
  11479. '(("d" . 86400.0) ("w" . 604800.0)
  11480. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11481. (t (org-2ft s)))))
  11482. (defun org-match-any-p (re list)
  11483. "Does re match any element of list?"
  11484. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11485. (delq nil list))
  11486. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11487. (defvar org-tags-overlay (make-overlay 1 1))
  11488. (org-detach-overlay org-tags-overlay)
  11489. (defun org-get-local-tags-at (&optional pos)
  11490. "Get a list of tags defined in the current headline."
  11491. (org-get-tags-at pos 'local))
  11492. (defun org-get-local-tags ()
  11493. "Get a list of tags defined in the current headline."
  11494. (org-get-tags-at nil 'local))
  11495. (defun org-get-tags-at (&optional pos local)
  11496. "Get a list of all headline tags applicable at POS.
  11497. POS defaults to point. If tags are inherited, the list contains
  11498. the targets in the same sequence as the headlines appear, i.e.
  11499. the tags of the current headline come last.
  11500. When LOCAL is non-nil, only return tags from the current headline,
  11501. ignore inherited ones."
  11502. (interactive)
  11503. (if (and org-trust-scanner-tags
  11504. (or (not pos) (equal pos (point)))
  11505. (not local))
  11506. org-scanner-tags
  11507. (let (tags ltags lastpos parent)
  11508. (save-excursion
  11509. (save-restriction
  11510. (widen)
  11511. (goto-char (or pos (point)))
  11512. (save-match-data
  11513. (catch 'done
  11514. (condition-case nil
  11515. (progn
  11516. (org-back-to-heading t)
  11517. (while (not (equal lastpos (point)))
  11518. (setq lastpos (point))
  11519. (when (looking-at
  11520. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  11521. (setq ltags (org-split-string
  11522. (org-match-string-no-properties 1) ":"))
  11523. (when parent
  11524. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11525. (setq tags (append
  11526. (if parent
  11527. (org-remove-uniherited-tags ltags)
  11528. ltags)
  11529. tags)))
  11530. (or org-use-tag-inheritance (throw 'done t))
  11531. (if local (throw 'done t))
  11532. (or (org-up-heading-safe) (error nil))
  11533. (setq parent t)))
  11534. (error nil)))))
  11535. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11536. (defun org-add-prop-inherited (s)
  11537. (add-text-properties 0 (length s) '(inherited t) s)
  11538. s)
  11539. (defun org-toggle-tag (tag &optional onoff)
  11540. "Toggle the tag TAG for the current line.
  11541. If ONOFF is `on' or `off', don't toggle but set to this state."
  11542. (let (res current)
  11543. (save-excursion
  11544. (org-back-to-heading t)
  11545. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11546. (point-at-eol) t)
  11547. (progn
  11548. (setq current (match-string 1))
  11549. (replace-match ""))
  11550. (setq current ""))
  11551. (setq current (nreverse (org-split-string current ":")))
  11552. (cond
  11553. ((eq onoff 'on)
  11554. (setq res t)
  11555. (or (member tag current) (push tag current)))
  11556. ((eq onoff 'off)
  11557. (or (not (member tag current)) (setq current (delete tag current))))
  11558. (t (if (member tag current)
  11559. (setq current (delete tag current))
  11560. (setq res t)
  11561. (push tag current))))
  11562. (end-of-line 1)
  11563. (if current
  11564. (progn
  11565. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11566. (org-set-tags nil t))
  11567. (delete-horizontal-space))
  11568. (run-hooks 'org-after-tags-change-hook))
  11569. res))
  11570. (defun org-align-tags-here (to-col)
  11571. ;; Assumes that this is a headline
  11572. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11573. (beginning-of-line 1)
  11574. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11575. (< pos (match-beginning 2)))
  11576. (progn
  11577. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11578. (goto-char (match-beginning 1))
  11579. (insert " ")
  11580. (delete-region (point) (1+ (match-beginning 2)))
  11581. (setq ncol (max (1+ (current-column))
  11582. (1+ col)
  11583. (if (> to-col 0)
  11584. to-col
  11585. (- (abs to-col) tags-l))))
  11586. (setq p (point))
  11587. (insert (make-string (- ncol (current-column)) ?\ ))
  11588. (setq ncol (current-column))
  11589. (when indent-tabs-mode (tabify p (point-at-eol)))
  11590. (org-move-to-column (min ncol col) t))
  11591. (goto-char pos))))
  11592. (defun org-set-tags-command (&optional arg just-align)
  11593. "Call the set-tags command for the current entry."
  11594. (interactive "P")
  11595. (if (org-on-heading-p)
  11596. (org-set-tags arg just-align)
  11597. (save-excursion
  11598. (org-back-to-heading t)
  11599. (org-set-tags arg just-align))))
  11600. (defun org-set-tags-to (data)
  11601. "Set the tags of the current entry to DATA, replacing the current tags.
  11602. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11603. If DATA is nil or the empty string, any tags will be removed."
  11604. (interactive "sTags: ")
  11605. (setq data
  11606. (cond
  11607. ((eq data nil) "")
  11608. ((equal data "") "")
  11609. ((stringp data)
  11610. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11611. ":"))
  11612. ((listp data)
  11613. (concat ":" (mapconcat 'identity data ":") ":"))
  11614. (t nil)))
  11615. (when data
  11616. (save-excursion
  11617. (org-back-to-heading t)
  11618. (when (looking-at org-complex-heading-regexp)
  11619. (if (match-end 5)
  11620. (progn
  11621. (goto-char (match-beginning 5))
  11622. (insert data)
  11623. (delete-region (point) (point-at-eol))
  11624. (org-set-tags nil 'align))
  11625. (goto-char (point-at-eol))
  11626. (insert " " data)
  11627. (org-set-tags nil 'align)))
  11628. (beginning-of-line 1)
  11629. (if (looking-at ".*?\\([ \t]+\\)$")
  11630. (delete-region (match-beginning 1) (match-end 1))))))
  11631. (defun org-align-all-tags ()
  11632. "Align the tags i all headings."
  11633. (interactive)
  11634. (save-excursion
  11635. (or (ignore-errors (org-back-to-heading t))
  11636. (outline-next-heading))
  11637. (if (org-on-heading-p)
  11638. (org-set-tags t)
  11639. (message "No headings"))))
  11640. (defvar org-indent-indentation-per-level)
  11641. (defun org-set-tags (&optional arg just-align)
  11642. "Set the tags for the current headline.
  11643. With prefix ARG, realign all tags in headings in the current buffer."
  11644. (interactive "P")
  11645. (let* ((re (concat "^" outline-regexp))
  11646. (current (org-get-tags-string))
  11647. (col (current-column))
  11648. (org-setting-tags t)
  11649. table current-tags inherited-tags ; computed below when needed
  11650. tags p0 c0 c1 rpl di tc level)
  11651. (if arg
  11652. (save-excursion
  11653. (goto-char (point-min))
  11654. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11655. (while (re-search-forward re nil t)
  11656. (org-set-tags nil t)
  11657. (end-of-line 1)))
  11658. (message "All tags realigned to column %d" org-tags-column))
  11659. (if just-align
  11660. (setq tags current)
  11661. ;; Get a new set of tags from the user
  11662. (save-excursion
  11663. (setq table (append org-tag-persistent-alist
  11664. (or org-tag-alist (org-get-buffer-tags))
  11665. (and org-complete-tags-always-offer-all-agenda-tags
  11666. (org-global-tags-completion-table (org-agenda-files))))
  11667. org-last-tags-completion-table table
  11668. current-tags (org-split-string current ":")
  11669. inherited-tags (nreverse
  11670. (nthcdr (length current-tags)
  11671. (nreverse (org-get-tags-at))))
  11672. tags
  11673. (if (or (eq t org-use-fast-tag-selection)
  11674. (and org-use-fast-tag-selection
  11675. (delq nil (mapcar 'cdr table))))
  11676. (org-fast-tag-selection
  11677. current-tags inherited-tags table
  11678. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11679. (let ((org-add-colon-after-tag-completion t))
  11680. (org-trim
  11681. (org-without-partial-completion
  11682. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11683. nil nil current 'org-tags-history)))))))
  11684. (while (string-match "[-+&]+" tags)
  11685. ;; No boolean logic, just a list
  11686. (setq tags (replace-match ":" t t tags))))
  11687. (if org-tags-sort-function
  11688. (setq tags (mapconcat 'identity
  11689. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11690. org-tags-sort-function) ":")))
  11691. (if (string-match "\\`[\t ]*\\'" tags)
  11692. (setq tags "")
  11693. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11694. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11695. ;; Insert new tags at the correct column
  11696. (beginning-of-line 1)
  11697. (setq level (or (and (looking-at org-outline-regexp)
  11698. (- (match-end 0) (point) 1))
  11699. 1))
  11700. (cond
  11701. ((and (equal current "") (equal tags "")))
  11702. ((re-search-forward
  11703. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11704. (point-at-eol) t)
  11705. (if (equal tags "")
  11706. (setq rpl "")
  11707. (goto-char (match-beginning 0))
  11708. (setq c0 (current-column)
  11709. ;; compute offset for the case of org-indent-mode active
  11710. di (if org-indent-mode
  11711. (* (1- org-indent-indentation-per-level) (1- level))
  11712. 0)
  11713. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11714. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11715. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11716. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11717. (replace-match rpl t t)
  11718. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11719. tags)
  11720. (t (error "Tags alignment failed")))
  11721. (org-move-to-column col)
  11722. (unless just-align
  11723. (run-hooks 'org-after-tags-change-hook)))))
  11724. (defun org-change-tag-in-region (beg end tag off)
  11725. "Add or remove TAG for each entry in the region.
  11726. This works in the agenda, and also in an org-mode buffer."
  11727. (interactive
  11728. (list (region-beginning) (region-end)
  11729. (let ((org-last-tags-completion-table
  11730. (if (org-mode-p)
  11731. (org-get-buffer-tags)
  11732. (org-global-tags-completion-table))))
  11733. (org-icompleting-read
  11734. "Tag: " 'org-tags-completion-function nil nil nil
  11735. 'org-tags-history))
  11736. (progn
  11737. (message "[s]et or [r]emove? ")
  11738. (equal (read-char-exclusive) ?r))))
  11739. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11740. (let ((agendap (equal major-mode 'org-agenda-mode))
  11741. l1 l2 m buf pos newhead (cnt 0))
  11742. (goto-char end)
  11743. (setq l2 (1- (org-current-line)))
  11744. (goto-char beg)
  11745. (setq l1 (org-current-line))
  11746. (loop for l from l1 to l2 do
  11747. (org-goto-line l)
  11748. (setq m (get-text-property (point) 'org-hd-marker))
  11749. (when (or (and (org-mode-p) (org-on-heading-p))
  11750. (and agendap m))
  11751. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11752. pos (if agendap m (point)))
  11753. (with-current-buffer buf
  11754. (save-excursion
  11755. (save-restriction
  11756. (goto-char pos)
  11757. (setq cnt (1+ cnt))
  11758. (org-toggle-tag tag (if off 'off 'on))
  11759. (setq newhead (org-get-heading)))))
  11760. (and agendap (org-agenda-change-all-lines newhead m))))
  11761. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11762. (defun org-tags-completion-function (string predicate &optional flag)
  11763. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11764. (confirm (lambda (x) (stringp (car x)))))
  11765. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11766. (setq s1 (match-string 1 string)
  11767. s2 (match-string 2 string))
  11768. (setq s1 "" s2 string))
  11769. (cond
  11770. ((eq flag nil)
  11771. ;; try completion
  11772. (setq rtn (try-completion s2 ctable confirm))
  11773. (if (stringp rtn)
  11774. (setq rtn
  11775. (concat s1 s2 (substring rtn (length s2))
  11776. (if (and org-add-colon-after-tag-completion
  11777. (assoc rtn ctable))
  11778. ":" ""))))
  11779. rtn)
  11780. ((eq flag t)
  11781. ;; all-completions
  11782. (all-completions s2 ctable confirm)
  11783. )
  11784. ((eq flag 'lambda)
  11785. ;; exact match?
  11786. (assoc s2 ctable)))
  11787. ))
  11788. (defun org-fast-tag-insert (kwd tags face &optional end)
  11789. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11790. (insert (format "%-12s" (concat kwd ":"))
  11791. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11792. (or end "")))
  11793. (defun org-fast-tag-show-exit (flag)
  11794. (save-excursion
  11795. (org-goto-line 3)
  11796. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11797. (replace-match ""))
  11798. (when flag
  11799. (end-of-line 1)
  11800. (org-move-to-column (- (window-width) 19) t)
  11801. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11802. (defun org-set-current-tags-overlay (current prefix)
  11803. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11804. (if (featurep 'xemacs)
  11805. (org-overlay-display org-tags-overlay (concat prefix s)
  11806. 'secondary-selection)
  11807. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11808. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11809. (defvar org-last-tag-selection-key nil)
  11810. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11811. "Fast tag selection with single keys.
  11812. CURRENT is the current list of tags in the headline, INHERITED is the
  11813. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11814. possibly with grouping information. TODO-TABLE is a similar table with
  11815. TODO keywords, should these have keys assigned to them.
  11816. If the keys are nil, a-z are automatically assigned.
  11817. Returns the new tags string, or nil to not change the current settings."
  11818. (let* ((fulltable (append table todo-table))
  11819. (maxlen (apply 'max (mapcar
  11820. (lambda (x)
  11821. (if (stringp (car x)) (string-width (car x)) 0))
  11822. fulltable)))
  11823. (buf (current-buffer))
  11824. (expert (eq org-fast-tag-selection-single-key 'expert))
  11825. (buffer-tags nil)
  11826. (fwidth (+ maxlen 3 1 3))
  11827. (ncol (/ (- (window-width) 4) fwidth))
  11828. (i-face 'org-done)
  11829. (c-face 'org-todo)
  11830. tg cnt e c char c1 c2 ntable tbl rtn
  11831. ov-start ov-end ov-prefix
  11832. (exit-after-next org-fast-tag-selection-single-key)
  11833. (done-keywords org-done-keywords)
  11834. groups ingroup)
  11835. (save-excursion
  11836. (beginning-of-line 1)
  11837. (if (looking-at
  11838. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11839. (setq ov-start (match-beginning 1)
  11840. ov-end (match-end 1)
  11841. ov-prefix "")
  11842. (setq ov-start (1- (point-at-eol))
  11843. ov-end (1+ ov-start))
  11844. (skip-chars-forward "^\n\r")
  11845. (setq ov-prefix
  11846. (concat
  11847. (buffer-substring (1- (point)) (point))
  11848. (if (> (current-column) org-tags-column)
  11849. " "
  11850. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11851. (move-overlay org-tags-overlay ov-start ov-end)
  11852. (save-window-excursion
  11853. (if expert
  11854. (set-buffer (get-buffer-create " *Org tags*"))
  11855. (delete-other-windows)
  11856. (split-window-vertically)
  11857. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11858. (erase-buffer)
  11859. (org-set-local 'org-done-keywords done-keywords)
  11860. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11861. (org-fast-tag-insert "Current" current c-face "\n\n")
  11862. (org-fast-tag-show-exit exit-after-next)
  11863. (org-set-current-tags-overlay current ov-prefix)
  11864. (setq tbl fulltable char ?a cnt 0)
  11865. (while (setq e (pop tbl))
  11866. (cond
  11867. ((equal (car e) :startgroup)
  11868. (push '() groups) (setq ingroup t)
  11869. (when (not (= cnt 0))
  11870. (setq cnt 0)
  11871. (insert "\n"))
  11872. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11873. ((equal (car e) :endgroup)
  11874. (setq ingroup nil cnt 0)
  11875. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11876. ((equal e '(:newline))
  11877. (when (not (= cnt 0))
  11878. (setq cnt 0)
  11879. (insert "\n")
  11880. (setq e (car tbl))
  11881. (while (equal (car tbl) '(:newline))
  11882. (insert "\n")
  11883. (setq tbl (cdr tbl)))))
  11884. (t
  11885. (setq tg (copy-sequence (car e)) c2 nil)
  11886. (if (cdr e)
  11887. (setq c (cdr e))
  11888. ;; automatically assign a character.
  11889. (setq c1 (string-to-char
  11890. (downcase (substring
  11891. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11892. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11893. (while (or (rassoc char ntable) (rassoc char table))
  11894. (setq char (1+ char)))
  11895. (setq c2 c1))
  11896. (setq c (or c2 char)))
  11897. (if ingroup (push tg (car groups)))
  11898. (setq tg (org-add-props tg nil 'face
  11899. (cond
  11900. ((not (assoc tg table))
  11901. (org-get-todo-face tg))
  11902. ((member tg current) c-face)
  11903. ((member tg inherited) i-face)
  11904. (t nil))))
  11905. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11906. (insert "[" c "] " tg (make-string
  11907. (- fwidth 4 (length tg)) ?\ ))
  11908. (push (cons tg c) ntable)
  11909. (when (= (setq cnt (1+ cnt)) ncol)
  11910. (insert "\n")
  11911. (if ingroup (insert " "))
  11912. (setq cnt 0)))))
  11913. (setq ntable (nreverse ntable))
  11914. (insert "\n")
  11915. (goto-char (point-min))
  11916. (if (not expert) (org-fit-window-to-buffer))
  11917. (setq rtn
  11918. (catch 'exit
  11919. (while t
  11920. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11921. (if (not groups) "no " "")
  11922. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11923. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11924. (setq org-last-tag-selection-key c)
  11925. (cond
  11926. ((= c ?\r) (throw 'exit t))
  11927. ((= c ?!)
  11928. (setq groups (not groups))
  11929. (goto-char (point-min))
  11930. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11931. ((= c ?\C-c)
  11932. (if (not expert)
  11933. (org-fast-tag-show-exit
  11934. (setq exit-after-next (not exit-after-next)))
  11935. (setq expert nil)
  11936. (delete-other-windows)
  11937. (split-window-vertically)
  11938. (org-switch-to-buffer-other-window " *Org tags*")
  11939. (org-fit-window-to-buffer)))
  11940. ((or (= c ?\C-g)
  11941. (and (= c ?q) (not (rassoc c ntable))))
  11942. (org-detach-overlay org-tags-overlay)
  11943. (setq quit-flag t))
  11944. ((= c ?\ )
  11945. (setq current nil)
  11946. (if exit-after-next (setq exit-after-next 'now)))
  11947. ((= c ?\t)
  11948. (condition-case nil
  11949. (setq tg (org-icompleting-read
  11950. "Tag: "
  11951. (or buffer-tags
  11952. (with-current-buffer buf
  11953. (org-get-buffer-tags)))))
  11954. (quit (setq tg "")))
  11955. (when (string-match "\\S-" tg)
  11956. (add-to-list 'buffer-tags (list tg))
  11957. (if (member tg current)
  11958. (setq current (delete tg current))
  11959. (push tg current)))
  11960. (if exit-after-next (setq exit-after-next 'now)))
  11961. ((setq e (rassoc c todo-table) tg (car e))
  11962. (with-current-buffer buf
  11963. (save-excursion (org-todo tg)))
  11964. (if exit-after-next (setq exit-after-next 'now)))
  11965. ((setq e (rassoc c ntable) tg (car e))
  11966. (if (member tg current)
  11967. (setq current (delete tg current))
  11968. (loop for g in groups do
  11969. (if (member tg g)
  11970. (mapc (lambda (x)
  11971. (setq current (delete x current)))
  11972. g)))
  11973. (push tg current))
  11974. (if exit-after-next (setq exit-after-next 'now))))
  11975. ;; Create a sorted list
  11976. (setq current
  11977. (sort current
  11978. (lambda (a b)
  11979. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11980. (if (eq exit-after-next 'now) (throw 'exit t))
  11981. (goto-char (point-min))
  11982. (beginning-of-line 2)
  11983. (delete-region (point) (point-at-eol))
  11984. (org-fast-tag-insert "Current" current c-face)
  11985. (org-set-current-tags-overlay current ov-prefix)
  11986. (while (re-search-forward
  11987. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11988. (setq tg (match-string 1))
  11989. (add-text-properties
  11990. (match-beginning 1) (match-end 1)
  11991. (list 'face
  11992. (cond
  11993. ((member tg current) c-face)
  11994. ((member tg inherited) i-face)
  11995. (t (get-text-property (match-beginning 1) 'face))))))
  11996. (goto-char (point-min)))))
  11997. (org-detach-overlay org-tags-overlay)
  11998. (if rtn
  11999. (mapconcat 'identity current ":")
  12000. nil))))
  12001. (defun org-get-tags-string ()
  12002. "Get the TAGS string in the current headline."
  12003. (unless (org-on-heading-p t)
  12004. (error "Not on a heading"))
  12005. (save-excursion
  12006. (beginning-of-line 1)
  12007. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12008. (org-match-string-no-properties 1)
  12009. "")))
  12010. (defun org-get-tags ()
  12011. "Get the list of tags specified in the current headline."
  12012. (org-split-string (org-get-tags-string) ":"))
  12013. (defun org-get-buffer-tags ()
  12014. "Get a table of all tags used in the buffer, for completion."
  12015. (let (tags)
  12016. (save-excursion
  12017. (goto-char (point-min))
  12018. (while (re-search-forward
  12019. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  12020. (when (equal (char-after (point-at-bol 0)) ?*)
  12021. (mapc (lambda (x) (add-to-list 'tags x))
  12022. (org-split-string (org-match-string-no-properties 1) ":")))))
  12023. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12024. (mapcar 'list tags)))
  12025. ;;;; The mapping API
  12026. ;;;###autoload
  12027. (defun org-map-entries (func &optional match scope &rest skip)
  12028. "Call FUNC at each headline selected by MATCH in SCOPE.
  12029. FUNC is a function or a lisp form. The function will be called without
  12030. arguments, with the cursor positioned at the beginning of the headline.
  12031. The return values of all calls to the function will be collected and
  12032. returned as a list.
  12033. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12034. does not need to preserve point. After evaluation, the cursor will be
  12035. moved to the end of the line (presumably of the headline of the
  12036. processed entry) and search continues from there. Under some
  12037. circumstances, this may not produce the wanted results. For example,
  12038. if you have removed (e.g. archived) the current (sub)tree it could
  12039. mean that the next entry will be skipped entirely. In such cases, you
  12040. can specify the position from where search should continue by making
  12041. FUNC set the variable `org-map-continue-from' to the desired buffer
  12042. position.
  12043. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12044. Only headlines that are matched by this query will be considered during
  12045. the iteration. When MATCH is nil or t, all headlines will be
  12046. visited by the iteration.
  12047. SCOPE determines the scope of this command. It can be any of:
  12048. nil The current buffer, respecting the restriction if any
  12049. tree The subtree started with the entry at point
  12050. file The current buffer, without restriction
  12051. file-with-archives
  12052. The current buffer, and any archives associated with it
  12053. agenda All agenda files
  12054. agenda-with-archives
  12055. All agenda files with any archive files associated with them
  12056. \(file1 file2 ...)
  12057. If this is a list, all files in the list will be scanned
  12058. The remaining args are treated as settings for the skipping facilities of
  12059. the scanner. The following items can be given here:
  12060. archive skip trees with the archive tag.
  12061. comment skip trees with the COMMENT keyword
  12062. function or Emacs Lisp form:
  12063. will be used as value for `org-agenda-skip-function', so whenever
  12064. the function returns t, FUNC will not be called for that
  12065. entry and search will continue from the point where the
  12066. function leaves it.
  12067. If your function needs to retrieve the tags including inherited tags
  12068. at the *current* entry, you can use the value of the variable
  12069. `org-scanner-tags' which will be much faster than getting the value
  12070. with `org-get-tags-at'. If your function gets properties with
  12071. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12072. to t around the call to `org-entry-properties' to get the same speedup.
  12073. Note that if your function moves around to retrieve tags and properties at
  12074. a *different* entry, you cannot use these techniques."
  12075. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12076. (org-agenda-skip-archived-trees (memq 'archive skip))
  12077. (org-agenda-skip-comment-trees (memq 'comment skip))
  12078. (org-agenda-skip-function
  12079. (car (org-delete-all '(comment archive) skip)))
  12080. (org-tags-match-list-sublevels t)
  12081. matcher file res
  12082. org-todo-keywords-for-agenda
  12083. org-done-keywords-for-agenda
  12084. org-todo-keyword-alist-for-agenda
  12085. org-drawers-for-agenda
  12086. org-tag-alist-for-agenda)
  12087. (cond
  12088. ((eq match t) (setq matcher t))
  12089. ((eq match nil) (setq matcher t))
  12090. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12091. (save-excursion
  12092. (save-restriction
  12093. (when (eq scope 'tree)
  12094. (org-back-to-heading t)
  12095. (org-narrow-to-subtree)
  12096. (setq scope nil))
  12097. (if (not scope)
  12098. (progn
  12099. (org-prepare-agenda-buffers
  12100. (list (buffer-file-name (current-buffer))))
  12101. (setq res (org-scan-tags func matcher)))
  12102. ;; Get the right scope
  12103. (cond
  12104. ((and scope (listp scope) (symbolp (car scope)))
  12105. (setq scope (eval scope)))
  12106. ((eq scope 'agenda)
  12107. (setq scope (org-agenda-files t)))
  12108. ((eq scope 'agenda-with-archives)
  12109. (setq scope (org-agenda-files t))
  12110. (setq scope (org-add-archive-files scope)))
  12111. ((eq scope 'file)
  12112. (setq scope (list (buffer-file-name))))
  12113. ((eq scope 'file-with-archives)
  12114. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12115. (org-prepare-agenda-buffers scope)
  12116. (while (setq file (pop scope))
  12117. (with-current-buffer (org-find-base-buffer-visiting file)
  12118. (save-excursion
  12119. (save-restriction
  12120. (widen)
  12121. (goto-char (point-min))
  12122. (setq res (append res (org-scan-tags func matcher))))))))))
  12123. res))
  12124. ;;;; Properties
  12125. ;;; Setting and retrieving properties
  12126. (defconst org-special-properties
  12127. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12128. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  12129. "The special properties valid in Org-mode.
  12130. These are properties that are not defined in the property drawer,
  12131. but in some other way.")
  12132. (defconst org-default-properties
  12133. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12134. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12135. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12136. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12137. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12138. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12139. "Some properties that are used by Org-mode for various purposes.
  12140. Being in this list makes sure that they are offered for completion.")
  12141. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12142. "Regular expression matching the first line of a property drawer.")
  12143. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12144. "Regular expression matching the last line of a property drawer.")
  12145. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12146. "Regular expression matching the first line of a property drawer.")
  12147. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12148. "Regular expression matching the first line of a property drawer.")
  12149. (defconst org-property-drawer-re
  12150. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12151. org-property-end-re "\\)\n?")
  12152. "Matches an entire property drawer.")
  12153. (defconst org-clock-drawer-re
  12154. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12155. org-property-end-re "\\)\n?")
  12156. "Matches an entire clock drawer.")
  12157. (defun org-property-action ()
  12158. "Do an action on properties."
  12159. (interactive)
  12160. (let (c)
  12161. (org-at-property-p)
  12162. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12163. (setq c (read-char-exclusive))
  12164. (cond
  12165. ((equal c ?s)
  12166. (call-interactively 'org-set-property))
  12167. ((equal c ?d)
  12168. (call-interactively 'org-delete-property))
  12169. ((equal c ?D)
  12170. (call-interactively 'org-delete-property-globally))
  12171. ((equal c ?c)
  12172. (call-interactively 'org-compute-property-at-point))
  12173. (t (error "No such property action %c" c)))))
  12174. (defun org-set-effort (&optional value)
  12175. "Set the effort property of the current entry.
  12176. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12177. allowed value."
  12178. (interactive "P")
  12179. (if (equal value 0) (setq value 10))
  12180. (let* ((completion-ignore-case t)
  12181. (prop org-effort-property)
  12182. (cur (org-entry-get nil prop))
  12183. (allowed (org-property-get-allowed-values nil prop 'table))
  12184. (existing (mapcar 'list (org-property-values prop)))
  12185. rpl
  12186. (val (cond
  12187. ((stringp value) value)
  12188. ((and allowed (integerp value))
  12189. (or (car (nth (1- value) allowed))
  12190. (car (org-last allowed))))
  12191. (allowed
  12192. (message "Select 1-9,0, [RET%s]: %s"
  12193. (if cur (concat "=" cur) "")
  12194. (mapconcat 'car allowed " "))
  12195. (setq rpl (read-char-exclusive))
  12196. (if (equal rpl ?\r)
  12197. cur
  12198. (setq rpl (- rpl ?0))
  12199. (if (equal rpl 0) (setq rpl 10))
  12200. (if (and (> rpl 0) (<= rpl (length allowed)))
  12201. (car (nth (1- rpl) allowed))
  12202. (org-completing-read "Effort: " allowed nil))))
  12203. (t
  12204. (let (org-completion-use-ido org-completion-use-iswitchb)
  12205. (org-completing-read
  12206. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12207. (concat "[" cur "]") "")
  12208. ": ")
  12209. existing nil nil "" nil cur))))))
  12210. (unless (equal (org-entry-get nil prop) val)
  12211. (org-entry-put nil prop val))
  12212. (message "%s is now %s" prop val)))
  12213. (defun org-at-property-p ()
  12214. "Is cursor inside a property drawer?"
  12215. (save-excursion
  12216. (beginning-of-line 1)
  12217. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12218. (save-match-data ;; Used by calling procedures
  12219. (let ((p (point))
  12220. (range (unless (org-before-first-heading-p)
  12221. (org-get-property-block))))
  12222. (and range (<= (car range) p) (< p (cdr range))))))))
  12223. (defun org-get-property-block (&optional beg end force)
  12224. "Return the (beg . end) range of the body of the property drawer.
  12225. BEG and END can be beginning and end of subtree, if not given
  12226. they will be found.
  12227. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12228. (catch 'exit
  12229. (save-excursion
  12230. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12231. (end (or end (progn (outline-next-heading) (point)))))
  12232. (goto-char beg)
  12233. (if (re-search-forward org-property-start-re end t)
  12234. (setq beg (1+ (match-end 0)))
  12235. (if force
  12236. (save-excursion
  12237. (org-insert-property-drawer)
  12238. (setq end (progn (outline-next-heading) (point))))
  12239. (throw 'exit nil))
  12240. (goto-char beg)
  12241. (if (re-search-forward org-property-start-re end t)
  12242. (setq beg (1+ (match-end 0)))))
  12243. (if (re-search-forward org-property-end-re end t)
  12244. (setq end (match-beginning 0))
  12245. (or force (throw 'exit nil))
  12246. (goto-char beg)
  12247. (setq end beg)
  12248. (org-indent-line-function)
  12249. (insert ":END:\n"))
  12250. (cons beg end)))))
  12251. (defun org-entry-properties (&optional pom which specific)
  12252. "Get all properties of the entry at point-or-marker POM.
  12253. This includes the TODO keyword, the tags, time strings for deadline,
  12254. scheduled, and clocking, and any additional properties defined in the
  12255. entry. The return value is an alist, keys may occur multiple times
  12256. if the property key was used several times.
  12257. POM may also be nil, in which case the current entry is used.
  12258. If WHICH is nil or `all', get all properties. If WHICH is
  12259. `special' or `standard', only get that subclass. If WHICH
  12260. is a string only get exactly this property. Specific can be a string, the
  12261. specific property we are interested in. Specifying it can speed
  12262. things up because then unnecessary parsing is avoided."
  12263. (setq which (or which 'all))
  12264. (org-with-point-at pom
  12265. (let ((clockstr (substring org-clock-string 0 -1))
  12266. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12267. (case-fold-search nil)
  12268. beg end range props sum-props key key1 value string clocksum)
  12269. (save-excursion
  12270. (when (condition-case nil
  12271. (and (org-mode-p) (org-back-to-heading t))
  12272. (error nil))
  12273. (setq beg (point))
  12274. (setq sum-props (get-text-property (point) 'org-summaries))
  12275. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12276. (outline-next-heading)
  12277. (setq end (point))
  12278. (when (memq which '(all special))
  12279. ;; Get the special properties, like TODO and tags
  12280. (goto-char beg)
  12281. (when (and (or (not specific) (string= specific "TODO"))
  12282. (looking-at org-todo-line-regexp) (match-end 2))
  12283. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12284. (when (and (or (not specific) (string= specific "PRIORITY"))
  12285. (looking-at org-priority-regexp))
  12286. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12287. (when (and (or (not specific) (string= specific "TAGS"))
  12288. (setq value (org-get-tags-string))
  12289. (string-match "\\S-" value))
  12290. (push (cons "TAGS" value) props))
  12291. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12292. (setq value (org-get-tags-at)))
  12293. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12294. ":"))
  12295. props))
  12296. (when (or (not specific) (string= specific "BLOCKED"))
  12297. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12298. (when (or (not specific)
  12299. (member specific
  12300. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12301. "TIMESTAMP" "TIMESTAMP_IA")))
  12302. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12303. (setq key (if (match-end 1)
  12304. (substring (org-match-string-no-properties 1)
  12305. 0 -1))
  12306. string (if (equal key clockstr)
  12307. (org-no-properties
  12308. (org-trim
  12309. (buffer-substring
  12310. (match-beginning 3) (goto-char
  12311. (point-at-eol)))))
  12312. (substring (org-match-string-no-properties 3)
  12313. 1 -1)))
  12314. ;; Get the correct property name from the key. This is
  12315. ;; necessary if the user has configured time keywords.
  12316. (setq key1 (concat key ":"))
  12317. (cond
  12318. ((not key)
  12319. (setq key
  12320. (if (= (char-after (match-beginning 3)) ?\[)
  12321. "TIMESTAMP_IA" "TIMESTAMP")))
  12322. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12323. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12324. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12325. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12326. (when (or (equal key "CLOCK") (not (assoc key props)))
  12327. (push (cons key string) props))))
  12328. )
  12329. (when (memq which '(all standard))
  12330. ;; Get the standard properties, like :PROP: ...
  12331. (setq range (org-get-property-block beg end))
  12332. (when range
  12333. (goto-char (car range))
  12334. (while (re-search-forward
  12335. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12336. (cdr range) t)
  12337. (setq key (org-match-string-no-properties 1)
  12338. value (org-trim (or (org-match-string-no-properties 2) "")))
  12339. (unless (member key excluded)
  12340. (push (cons key (or value "")) props)))))
  12341. (if clocksum
  12342. (push (cons "CLOCKSUM"
  12343. (org-columns-number-to-string (/ (float clocksum) 60.)
  12344. 'add_times))
  12345. props))
  12346. (unless (assoc "CATEGORY" props)
  12347. (setq value (or (org-get-category)
  12348. (progn (org-refresh-category-properties)
  12349. (org-get-category))))
  12350. (push (cons "CATEGORY" value) props))
  12351. (append sum-props (nreverse props)))))))
  12352. (defun org-entry-get (pom property &optional inherit literal-nil)
  12353. "Get value of PROPERTY for entry at point-or-marker POM.
  12354. If INHERIT is non-nil and the entry does not have the property,
  12355. then also check higher levels of the hierarchy.
  12356. If INHERIT is the symbol `selective', use inheritance only if the setting
  12357. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12358. If the property is present but empty, the return value is the empty string.
  12359. If the property is not present at all, nil is returned.
  12360. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12361. do not interpret it as the list atom nil. This is used for inheritance
  12362. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12363. (org-with-point-at pom
  12364. (if (and inherit (if (eq inherit 'selective)
  12365. (org-property-inherit-p property)
  12366. t))
  12367. (org-entry-get-with-inheritance property literal-nil)
  12368. (if (member property org-special-properties)
  12369. ;; We need a special property. Use `org-entry-properties' to
  12370. ;; retrieve it, but specify the wanted property
  12371. (cdr (assoc property (org-entry-properties nil 'special property)))
  12372. (let ((range (org-get-property-block)))
  12373. (if (and range
  12374. (goto-char (car range))
  12375. (re-search-forward
  12376. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12377. (cdr range) t))
  12378. ;; Found the property, return it.
  12379. (if (match-end 1)
  12380. (if literal-nil
  12381. (org-match-string-no-properties 1)
  12382. (org-not-nil (org-match-string-no-properties 1)))
  12383. "")))))))
  12384. (defun org-property-or-variable-value (var &optional inherit)
  12385. "Check if there is a property fixing the value of VAR.
  12386. If yes, return this value. If not, return the current value of the variable."
  12387. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12388. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12389. (read prop)
  12390. (symbol-value var))))
  12391. (defun org-entry-delete (pom property)
  12392. "Delete the property PROPERTY from entry at point-or-marker POM."
  12393. (org-with-point-at pom
  12394. (if (member property org-special-properties)
  12395. nil ; cannot delete these properties.
  12396. (let ((range (org-get-property-block)))
  12397. (if (and range
  12398. (goto-char (car range))
  12399. (re-search-forward
  12400. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12401. (cdr range) t))
  12402. (progn
  12403. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12404. t)
  12405. nil)))))
  12406. ;; Multi-values properties are properties that contain multiple values
  12407. ;; These values are assumed to be single words, separated by whitespace.
  12408. (defun org-entry-add-to-multivalued-property (pom property value)
  12409. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12410. (let* ((old (org-entry-get pom property))
  12411. (values (and old (org-split-string old "[ \t]"))))
  12412. (setq value (org-entry-protect-space value))
  12413. (unless (member value values)
  12414. (setq values (cons value values))
  12415. (org-entry-put pom property
  12416. (mapconcat 'identity values " ")))))
  12417. (defun org-entry-remove-from-multivalued-property (pom property value)
  12418. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12419. (let* ((old (org-entry-get pom property))
  12420. (values (and old (org-split-string old "[ \t]"))))
  12421. (setq value (org-entry-protect-space value))
  12422. (when (member value values)
  12423. (setq values (delete value values))
  12424. (org-entry-put pom property
  12425. (mapconcat 'identity values " ")))))
  12426. (defun org-entry-member-in-multivalued-property (pom property value)
  12427. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12428. (let* ((old (org-entry-get pom property))
  12429. (values (and old (org-split-string old "[ \t]"))))
  12430. (setq value (org-entry-protect-space value))
  12431. (member value values)))
  12432. (defun org-entry-get-multivalued-property (pom property)
  12433. "Return a list of values in a multivalued property."
  12434. (let* ((value (org-entry-get pom property))
  12435. (values (and value (org-split-string value "[ \t]"))))
  12436. (mapcar 'org-entry-restore-space values)))
  12437. (defun org-entry-put-multivalued-property (pom property &rest values)
  12438. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12439. VALUES should be a list of strings. Spaces will be protected."
  12440. (org-entry-put pom property
  12441. (mapconcat 'org-entry-protect-space values " "))
  12442. (let* ((value (org-entry-get pom property))
  12443. (values (and value (org-split-string value "[ \t]"))))
  12444. (mapcar 'org-entry-restore-space values)))
  12445. (defun org-entry-protect-space (s)
  12446. "Protect spaces and newline in string S."
  12447. (while (string-match " " s)
  12448. (setq s (replace-match "%20" t t s)))
  12449. (while (string-match "\n" s)
  12450. (setq s (replace-match "%0A" t t s)))
  12451. s)
  12452. (defun org-entry-restore-space (s)
  12453. "Restore spaces and newline in string S."
  12454. (while (string-match "%20" s)
  12455. (setq s (replace-match " " t t s)))
  12456. (while (string-match "%0A" s)
  12457. (setq s (replace-match "\n" t t s)))
  12458. s)
  12459. (defvar org-entry-property-inherited-from (make-marker)
  12460. "Marker pointing to the entry from where a property was inherited.
  12461. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12462. location of the entry where the inheritance search matched. If there was
  12463. no match, the marker will point nowhere.
  12464. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12465. is set.")
  12466. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12467. "Get entry property, and search higher levels if not present.
  12468. The search will stop at the first ancestor which has the property defined.
  12469. If the value found is \"nil\", return nil to show that the property
  12470. should be considered as undefined (this is the meaning of nil here).
  12471. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12472. (move-marker org-entry-property-inherited-from nil)
  12473. (let (tmp)
  12474. (save-excursion
  12475. (save-restriction
  12476. (widen)
  12477. (catch 'ex
  12478. (while t
  12479. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12480. (org-back-to-heading t)
  12481. (move-marker org-entry-property-inherited-from (point))
  12482. (throw 'ex tmp))
  12483. (or (org-up-heading-safe) (throw 'ex nil)))))
  12484. (setq tmp (or tmp
  12485. (cdr (assoc property org-file-properties))
  12486. (cdr (assoc property org-global-properties))
  12487. (cdr (assoc property org-global-properties-fixed))))
  12488. (if literal-nil tmp (org-not-nil tmp)))))
  12489. (defvar org-property-changed-functions nil
  12490. "Hook called when the value of a property has changed.
  12491. Each hook function should accept two arguments, the name of the property
  12492. and the new value.")
  12493. (defun org-entry-put (pom property value)
  12494. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12495. (org-with-point-at pom
  12496. (org-back-to-heading t)
  12497. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12498. range)
  12499. (cond
  12500. ((equal property "TODO")
  12501. (when (and (stringp value) (string-match "\\S-" value)
  12502. (not (member value org-todo-keywords-1)))
  12503. (error "\"%s\" is not a valid TODO state" value))
  12504. (if (or (not value)
  12505. (not (string-match "\\S-" value)))
  12506. (setq value 'none))
  12507. (org-todo value)
  12508. (org-set-tags nil 'align))
  12509. ((equal property "PRIORITY")
  12510. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12511. (string-to-char value) ?\ ))
  12512. (org-set-tags nil 'align))
  12513. ((equal property "SCHEDULED")
  12514. (if (re-search-forward org-scheduled-time-regexp end t)
  12515. (cond
  12516. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12517. ((eq value 'later) (org-timestamp-change 1 'day))
  12518. (t (call-interactively 'org-schedule)))
  12519. (call-interactively 'org-schedule)))
  12520. ((equal property "DEADLINE")
  12521. (if (re-search-forward org-deadline-time-regexp end t)
  12522. (cond
  12523. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12524. ((eq value 'later) (org-timestamp-change 1 'day))
  12525. (t (call-interactively 'org-deadline)))
  12526. (call-interactively 'org-deadline)))
  12527. ((member property org-special-properties)
  12528. (error "The %s property can not yet be set with `org-entry-put'"
  12529. property))
  12530. (t ; a non-special property
  12531. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12532. (setq range (org-get-property-block beg end 'force))
  12533. (goto-char (car range))
  12534. (if (re-search-forward
  12535. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12536. (progn
  12537. (delete-region (match-beginning 1) (match-end 1))
  12538. (goto-char (match-beginning 1)))
  12539. (goto-char (cdr range))
  12540. (insert "\n")
  12541. (backward-char 1)
  12542. (org-indent-line-function)
  12543. (insert ":" property ":"))
  12544. (and value (insert " " value))
  12545. (org-indent-line-function)))))
  12546. (run-hook-with-args 'org-property-changed-functions property value)))
  12547. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12548. "Get all property keys in the current buffer.
  12549. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12550. like tags and TODO state.
  12551. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12552. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12553. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12554. formats in the current buffer."
  12555. (let (rtn range cfmt s p)
  12556. (save-excursion
  12557. (save-restriction
  12558. (widen)
  12559. (goto-char (point-min))
  12560. (while (re-search-forward org-property-start-re nil t)
  12561. (setq range (org-get-property-block))
  12562. (goto-char (car range))
  12563. (while (re-search-forward
  12564. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12565. (cdr range) t)
  12566. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12567. (outline-next-heading))))
  12568. (when include-specials
  12569. (setq rtn (append org-special-properties rtn)))
  12570. (when include-defaults
  12571. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12572. (add-to-list 'rtn org-effort-property))
  12573. (when include-columns
  12574. (save-excursion
  12575. (save-restriction
  12576. (widen)
  12577. (goto-char (point-min))
  12578. (while (re-search-forward
  12579. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12580. nil t)
  12581. (setq cfmt (match-string 2) s 0)
  12582. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12583. cfmt s)
  12584. (setq s (match-end 0)
  12585. p (match-string 1 cfmt))
  12586. (unless (or (equal p "ITEM")
  12587. (member p org-special-properties))
  12588. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12589. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12590. (defun org-property-values (key)
  12591. "Return a list of all values of property KEY."
  12592. (save-excursion
  12593. (save-restriction
  12594. (widen)
  12595. (goto-char (point-min))
  12596. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12597. values)
  12598. (while (re-search-forward re nil t)
  12599. (add-to-list 'values (org-trim (match-string 1))))
  12600. (delete "" values)))))
  12601. (defun org-insert-property-drawer ()
  12602. "Insert a property drawer into the current entry."
  12603. (interactive)
  12604. (org-back-to-heading t)
  12605. (looking-at outline-regexp)
  12606. (let ((indent (if org-adapt-indentation
  12607. (- (match-end 0)(match-beginning 0))
  12608. 0))
  12609. (beg (point))
  12610. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12611. end hiddenp)
  12612. (outline-next-heading)
  12613. (setq end (point))
  12614. (goto-char beg)
  12615. (while (re-search-forward re end t))
  12616. (setq hiddenp (org-invisible-p))
  12617. (end-of-line 1)
  12618. (and (equal (char-after) ?\n) (forward-char 1))
  12619. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12620. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12621. ;; just skip this line
  12622. (beginning-of-line 2)
  12623. ;; Drawer start, find the end
  12624. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12625. (beginning-of-line 1)))
  12626. (org-skip-over-state-notes)
  12627. (skip-chars-backward " \t\n\r")
  12628. (if (eq (char-before) ?*) (forward-char 1))
  12629. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12630. (beginning-of-line 0)
  12631. (org-indent-to-column indent)
  12632. (beginning-of-line 2)
  12633. (org-indent-to-column indent)
  12634. (beginning-of-line 0)
  12635. (if hiddenp
  12636. (save-excursion
  12637. (org-back-to-heading t)
  12638. (hide-entry))
  12639. (org-flag-drawer t))))
  12640. (defun org-set-property (property value)
  12641. "In the current entry, set PROPERTY to VALUE.
  12642. When called interactively, this will prompt for a property name, offering
  12643. completion on existing and default properties. And then it will prompt
  12644. for a value, offering completion either on allowed values (via an inherited
  12645. xxx_ALL property) or on existing values in other instances of this property
  12646. in the current file."
  12647. (interactive
  12648. (let* ((completion-ignore-case t)
  12649. (keys (org-buffer-property-keys nil t t))
  12650. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12651. (prop (if (member prop0 keys)
  12652. prop0
  12653. (or (cdr (assoc (downcase prop0)
  12654. (mapcar (lambda (x) (cons (downcase x) x))
  12655. keys)))
  12656. prop0)))
  12657. (cur (org-entry-get nil prop))
  12658. (prompt (concat prop " value"
  12659. (if (and cur (string-match "\\S-" cur))
  12660. (concat " [" cur "]") "") ": "))
  12661. (allowed (org-property-get-allowed-values nil prop 'table))
  12662. (existing (mapcar 'list (org-property-values prop)))
  12663. (val (if allowed
  12664. (org-completing-read prompt allowed nil
  12665. (not (get-text-property 0 'org-unrestricted
  12666. (caar allowed))))
  12667. (let (org-completion-use-ido org-completion-use-iswitchb)
  12668. (org-completing-read prompt existing nil nil "" nil cur)))))
  12669. (list prop (if (equal val "") cur val))))
  12670. (unless (equal (org-entry-get nil property) value)
  12671. (org-entry-put nil property value)))
  12672. (defun org-delete-property (property)
  12673. "In the current entry, delete PROPERTY."
  12674. (interactive
  12675. (let* ((completion-ignore-case t)
  12676. (prop (org-icompleting-read "Property: "
  12677. (org-entry-properties nil 'standard))))
  12678. (list prop)))
  12679. (message "Property %s %s" property
  12680. (if (org-entry-delete nil property)
  12681. "deleted"
  12682. "was not present in the entry")))
  12683. (defun org-delete-property-globally (property)
  12684. "Remove PROPERTY globally, from all entries."
  12685. (interactive
  12686. (let* ((completion-ignore-case t)
  12687. (prop (org-icompleting-read
  12688. "Globally remove property: "
  12689. (mapcar 'list (org-buffer-property-keys)))))
  12690. (list prop)))
  12691. (save-excursion
  12692. (save-restriction
  12693. (widen)
  12694. (goto-char (point-min))
  12695. (let ((cnt 0))
  12696. (while (re-search-forward
  12697. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12698. nil t)
  12699. (setq cnt (1+ cnt))
  12700. (replace-match ""))
  12701. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12702. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12703. (defun org-compute-property-at-point ()
  12704. "Compute the property at point.
  12705. This looks for an enclosing column format, extracts the operator and
  12706. then applies it to the property in the column format's scope."
  12707. (interactive)
  12708. (unless (org-at-property-p)
  12709. (error "Not at a property"))
  12710. (let ((prop (org-match-string-no-properties 2)))
  12711. (org-columns-get-format-and-top-level)
  12712. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12713. (error "No operator defined for property %s" prop))
  12714. (org-columns-compute prop)))
  12715. (defvar org-property-allowed-value-functions nil
  12716. "Hook for functions supplying allowed values for a specific property.
  12717. The functions must take a single argument, the name of the property, and
  12718. return a flat list of allowed values. If \":ETC\" is one of
  12719. the values, this means that these values are intended as defaults for
  12720. completion, but that other values should be allowed too.
  12721. The functions must return nil if they are not responsible for this
  12722. property.")
  12723. (defun org-property-get-allowed-values (pom property &optional table)
  12724. "Get allowed values for the property PROPERTY.
  12725. When TABLE is non-nil, return an alist that can directly be used for
  12726. completion."
  12727. (let (vals)
  12728. (cond
  12729. ((equal property "TODO")
  12730. (setq vals (org-with-point-at pom
  12731. (append org-todo-keywords-1 '("")))))
  12732. ((equal property "PRIORITY")
  12733. (let ((n org-lowest-priority))
  12734. (while (>= n org-highest-priority)
  12735. (push (char-to-string n) vals)
  12736. (setq n (1- n)))))
  12737. ((member property org-special-properties))
  12738. ((setq vals (run-hook-with-args-until-success
  12739. 'org-property-allowed-value-functions property)))
  12740. (t
  12741. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12742. (when (and vals (string-match "\\S-" vals))
  12743. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12744. (setq vals (mapcar (lambda (x)
  12745. (cond ((stringp x) x)
  12746. ((numberp x) (number-to-string x))
  12747. ((symbolp x) (symbol-name x))
  12748. (t "???")))
  12749. vals)))))
  12750. (when (member ":ETC" vals)
  12751. (setq vals (remove ":ETC" vals))
  12752. (org-add-props (car vals) '(org-unrestricted t)))
  12753. (if table (mapcar 'list vals) vals)))
  12754. (defun org-property-previous-allowed-value (&optional previous)
  12755. "Switch to the next allowed value for this property."
  12756. (interactive)
  12757. (org-property-next-allowed-value t))
  12758. (defun org-property-next-allowed-value (&optional previous)
  12759. "Switch to the next allowed value for this property."
  12760. (interactive)
  12761. (unless (org-at-property-p)
  12762. (error "Not at a property"))
  12763. (let* ((key (match-string 2))
  12764. (value (match-string 3))
  12765. (allowed (or (org-property-get-allowed-values (point) key)
  12766. (and (member value '("[ ]" "[-]" "[X]"))
  12767. '("[ ]" "[X]"))))
  12768. nval)
  12769. (unless allowed
  12770. (error "Allowed values for this property have not been defined"))
  12771. (if previous (setq allowed (reverse allowed)))
  12772. (if (member value allowed)
  12773. (setq nval (car (cdr (member value allowed)))))
  12774. (setq nval (or nval (car allowed)))
  12775. (if (equal nval value)
  12776. (error "Only one allowed value for this property"))
  12777. (org-at-property-p)
  12778. (replace-match (concat " :" key ": " nval) t t)
  12779. (org-indent-line-function)
  12780. (beginning-of-line 1)
  12781. (skip-chars-forward " \t")
  12782. (run-hook-with-args 'org-property-changed-functions key nval)))
  12783. (defun org-find-olp (path &optional this-buffer)
  12784. "Return a marker pointing to the entry at outline path OLP.
  12785. If anything goes wrong, throw an error.
  12786. You can wrap this call to catch the error like this:
  12787. (condition-case msg
  12788. (org-mobile-locate-entry (match-string 4))
  12789. (error (nth 1 msg)))
  12790. The return value will then be either a string with the error message,
  12791. or a marker if everything is OK.
  12792. If THIS-BUFFER is set, the outline path does not contain a file,
  12793. only headings."
  12794. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12795. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12796. (level 1)
  12797. (lmin 1)
  12798. (lmax 1)
  12799. limit re end found pos heading cnt)
  12800. (unless buffer (error "File not found :%s" file))
  12801. (with-current-buffer buffer
  12802. (save-excursion
  12803. (save-restriction
  12804. (widen)
  12805. (setq limit (point-max))
  12806. (goto-char (point-min))
  12807. (while (setq heading (pop path))
  12808. (setq re (format org-complex-heading-regexp-format
  12809. (regexp-quote heading)))
  12810. (setq cnt 0 pos (point))
  12811. (while (re-search-forward re end t)
  12812. (setq level (- (match-end 1) (match-beginning 1)))
  12813. (if (and (>= level lmin) (<= level lmax))
  12814. (setq found (match-beginning 0) cnt (1+ cnt))))
  12815. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12816. lmax heading))
  12817. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12818. lmax heading))
  12819. (goto-char found)
  12820. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12821. (setq end (save-excursion (org-end-of-subtree t t))))
  12822. (when (org-on-heading-p)
  12823. (move-marker (make-marker) (point))))))))
  12824. (defun org-find-exact-headling-in-buffer (heading &optional buffer pos-only)
  12825. "Find node HEADING in BUFFER.
  12826. Return a marker to the heading if it was found, or nil if not.
  12827. If POS-ONLY is set, return just the position instead of a marker.
  12828. The heading text must match exact, but it may have a TODO keyword,
  12829. a priority cookie and tags in the standard locations."
  12830. (with-current-buffer (or buffer (current-buffer))
  12831. (save-excursion
  12832. (save-restriction
  12833. (widen)
  12834. (goto-char (point-min))
  12835. (let (case-fold-search)
  12836. (if (re-search-forward
  12837. (format org-complex-heading-regexp-format
  12838. (regexp-quote heading)) nil t)
  12839. (if pos-only
  12840. (match-beginning 0)
  12841. (move-marker (make-marker) (match-beginning 0)))))))))
  12842. (defun org-find-exact-heading-in-directory (heading &optional dir)
  12843. "Find Org node headline HEADING in all .org files in directory DIR.
  12844. When the target headline is found, return a marker to this location."
  12845. (let ((files (directory-files (or dir default-directory)
  12846. nil "\\`[^.#].*\\.org\\'"))
  12847. file visiting m buffer)
  12848. (catch 'found
  12849. (while (setq file (pop files))
  12850. (message "trying %s" file)
  12851. (setq visiting (org-find-base-buffer-visiting file))
  12852. (setq buffer (or visiting (find-file-noselect file)))
  12853. (setq m (org-find-exact-headling-in-buffer
  12854. heading buffer))
  12855. (when (and (not m) (not visiting)) (kill-buffer buffer))
  12856. (and m (throw 'found m))))))
  12857. (defun org-find-entry-with-id (ident)
  12858. "Locate the entry that contains the ID property with exact value IDENT.
  12859. IDENT can be a string, a symbol or a number, this function will search for
  12860. the string representation of it.
  12861. Return the position where this entry starts, or nil if there is no such entry."
  12862. (interactive "sID: ")
  12863. (let ((id (cond
  12864. ((stringp ident) ident)
  12865. ((symbol-name ident) (symbol-name ident))
  12866. ((numberp ident) (number-to-string ident))
  12867. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12868. (case-fold-search nil))
  12869. (save-excursion
  12870. (save-restriction
  12871. (widen)
  12872. (goto-char (point-min))
  12873. (when (re-search-forward
  12874. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12875. nil t)
  12876. (org-back-to-heading t)
  12877. (point))))))
  12878. ;;;; Timestamps
  12879. (defvar org-last-changed-timestamp nil)
  12880. (defvar org-last-inserted-timestamp nil
  12881. "The last time stamp inserted with `org-insert-time-stamp'.")
  12882. (defvar org-time-was-given) ; dynamically scoped parameter
  12883. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12884. (defvar org-ts-what) ; dynamically scoped parameter
  12885. (defun org-time-stamp (arg &optional inactive)
  12886. "Prompt for a date/time and insert a time stamp.
  12887. If the user specifies a time like HH:MM, or if this command is called
  12888. with a prefix argument, the time stamp will contain date and time.
  12889. Otherwise, only the date will be included. All parts of a date not
  12890. specified by the user will be filled in from the current date/time.
  12891. So if you press just return without typing anything, the time stamp
  12892. will represent the current date/time. If there is already a timestamp
  12893. at the cursor, it will be modified."
  12894. (interactive "P")
  12895. (let* ((ts nil)
  12896. (default-time
  12897. ;; Default time is either today, or, when entering a range,
  12898. ;; the range start.
  12899. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12900. (save-excursion
  12901. (re-search-backward
  12902. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12903. (- (point) 20) t)))
  12904. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12905. (current-time)))
  12906. (default-input (and ts (org-get-compact-tod ts)))
  12907. org-time-was-given org-end-time-was-given time)
  12908. (cond
  12909. ((and (org-at-timestamp-p t)
  12910. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12911. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12912. (insert "--")
  12913. (setq time (let ((this-command this-command))
  12914. (org-read-date arg 'totime nil nil
  12915. default-time default-input)))
  12916. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12917. ((org-at-timestamp-p t)
  12918. (setq time (let ((this-command this-command))
  12919. (org-read-date arg 'totime nil nil default-time default-input)))
  12920. (when (org-at-timestamp-p t) ; just to get the match data
  12921. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12922. (replace-match "")
  12923. (setq org-last-changed-timestamp
  12924. (org-insert-time-stamp
  12925. time (or org-time-was-given arg)
  12926. inactive nil nil (list org-end-time-was-given))))
  12927. (message "Timestamp updated"))
  12928. (t
  12929. (setq time (let ((this-command this-command))
  12930. (org-read-date arg 'totime nil nil default-time default-input)))
  12931. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12932. nil nil (list org-end-time-was-given))))))
  12933. ;; FIXME: can we use this for something else, like computing time differences?
  12934. (defun org-get-compact-tod (s)
  12935. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12936. (let* ((t1 (match-string 1 s))
  12937. (h1 (string-to-number (match-string 2 s)))
  12938. (m1 (string-to-number (match-string 3 s)))
  12939. (t2 (and (match-end 4) (match-string 5 s)))
  12940. (h2 (and t2 (string-to-number (match-string 6 s))))
  12941. (m2 (and t2 (string-to-number (match-string 7 s))))
  12942. dh dm)
  12943. (if (not t2)
  12944. t1
  12945. (setq dh (- h2 h1) dm (- m2 m1))
  12946. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12947. (concat t1 "+" (number-to-string dh)
  12948. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12949. (defun org-time-stamp-inactive (&optional arg)
  12950. "Insert an inactive time stamp.
  12951. An inactive time stamp is enclosed in square brackets instead of angle
  12952. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12953. does not link to the calendar and cannot be changed with the S-cursor keys.
  12954. So these are more for recording a certain time/date."
  12955. (interactive "P")
  12956. (org-time-stamp arg 'inactive))
  12957. (defvar org-date-ovl (make-overlay 1 1))
  12958. (overlay-put org-date-ovl 'face 'org-warning)
  12959. (org-detach-overlay org-date-ovl)
  12960. (defvar org-ans1) ; dynamically scoped parameter
  12961. (defvar org-ans2) ; dynamically scoped parameter
  12962. (defvar org-plain-time-of-day-regexp) ; defined below
  12963. (defvar org-overriding-default-time nil) ; dynamically scoped
  12964. (defvar org-read-date-overlay nil)
  12965. (defvar org-dcst nil) ; dynamically scoped
  12966. (defvar org-read-date-history nil)
  12967. (defvar org-read-date-final-answer nil)
  12968. (defun org-read-date (&optional with-time to-time from-string prompt
  12969. default-time default-input)
  12970. "Read a date, possibly a time, and make things smooth for the user.
  12971. The prompt will suggest to enter an ISO date, but you can also enter anything
  12972. which will at least partially be understood by `parse-time-string'.
  12973. Unrecognized parts of the date will default to the current day, month, year,
  12974. hour and minute. If this command is called to replace a timestamp at point,
  12975. of to enter the second timestamp of a range, the default time is taken
  12976. from the existing stamp. Furthermore, the command prefers the future,
  12977. so if you are giving a date where the year is not given, and the day-month
  12978. combination is already past in the current year, it will assume you
  12979. mean next year. For details, see the manual. A few examples:
  12980. 3-2-5 --> 2003-02-05
  12981. feb 15 --> currentyear-02-15
  12982. 2/15 --> currentyear-02-15
  12983. sep 12 9 --> 2009-09-12
  12984. 12:45 --> today 12:45
  12985. 22 sept 0:34 --> currentyear-09-22 0:34
  12986. 12 --> currentyear-currentmonth-12
  12987. Fri --> nearest Friday (today or later)
  12988. etc.
  12989. Furthermore you can specify a relative date by giving, as the *first* thing
  12990. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12991. change in days weeks, months, years.
  12992. With a single plus or minus, the date is relative to today. With a double
  12993. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12994. +4d --> four days from today
  12995. +4 --> same as above
  12996. +2w --> two weeks from today
  12997. ++5 --> five days from default date
  12998. The function understands only English month and weekday abbreviations,
  12999. but this can be configured with the variables `parse-time-months' and
  13000. `parse-time-weekdays'.
  13001. While prompting, a calendar is popped up - you can also select the
  13002. date with the mouse (button 1). The calendar shows a period of three
  13003. months. To scroll it to other months, use the keys `>' and `<'.
  13004. If you don't like the calendar, turn it off with
  13005. \(setq org-read-date-popup-calendar nil)
  13006. With optional argument TO-TIME, the date will immediately be converted
  13007. to an internal time.
  13008. With an optional argument WITH-TIME, the prompt will suggest to also
  13009. insert a time. Note that when WITH-TIME is not set, you can still
  13010. enter a time, and this function will inform the calling routine about
  13011. this change. The calling routine may then choose to change the format
  13012. used to insert the time stamp into the buffer to include the time.
  13013. With optional argument FROM-STRING, read from this string instead from
  13014. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13015. the time/date that is used for everything that is not specified by the
  13016. user."
  13017. (require 'parse-time)
  13018. (let* ((org-time-stamp-rounding-minutes
  13019. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13020. (org-dcst org-display-custom-times)
  13021. (ct (org-current-time))
  13022. (def (or org-overriding-default-time default-time ct))
  13023. (defdecode (decode-time def))
  13024. (dummy (progn
  13025. (when (< (nth 2 defdecode) org-extend-today-until)
  13026. (setcar (nthcdr 2 defdecode) -1)
  13027. (setcar (nthcdr 1 defdecode) 59)
  13028. (setq def (apply 'encode-time defdecode)
  13029. defdecode (decode-time def)))))
  13030. (calendar-frame-setup nil)
  13031. (calendar-setup nil)
  13032. (calendar-move-hook nil)
  13033. (calendar-view-diary-initially-flag nil)
  13034. (calendar-view-holidays-initially-flag nil)
  13035. (timestr (format-time-string
  13036. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13037. (prompt (concat (if prompt (concat prompt " ") "")
  13038. (format "Date+time [%s]: " timestr)))
  13039. ans (org-ans0 "") org-ans1 org-ans2 final)
  13040. (cond
  13041. (from-string (setq ans from-string))
  13042. (org-read-date-popup-calendar
  13043. (save-excursion
  13044. (save-window-excursion
  13045. (calendar)
  13046. (calendar-forward-day (- (time-to-days def)
  13047. (calendar-absolute-from-gregorian
  13048. (calendar-current-date))))
  13049. (org-eval-in-calendar nil t)
  13050. (let* ((old-map (current-local-map))
  13051. (map (copy-keymap calendar-mode-map))
  13052. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13053. (org-defkey map (kbd "RET") 'org-calendar-select)
  13054. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13055. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13056. (org-defkey minibuffer-local-map [(meta shift left)]
  13057. (lambda () (interactive)
  13058. (org-eval-in-calendar '(calendar-backward-month 1))))
  13059. (org-defkey minibuffer-local-map [(meta shift right)]
  13060. (lambda () (interactive)
  13061. (org-eval-in-calendar '(calendar-forward-month 1))))
  13062. (org-defkey minibuffer-local-map [(meta shift up)]
  13063. (lambda () (interactive)
  13064. (org-eval-in-calendar '(calendar-backward-year 1))))
  13065. (org-defkey minibuffer-local-map [(meta shift down)]
  13066. (lambda () (interactive)
  13067. (org-eval-in-calendar '(calendar-forward-year 1))))
  13068. (org-defkey minibuffer-local-map [?\e (shift left)]
  13069. (lambda () (interactive)
  13070. (org-eval-in-calendar '(calendar-backward-month 1))))
  13071. (org-defkey minibuffer-local-map [?\e (shift right)]
  13072. (lambda () (interactive)
  13073. (org-eval-in-calendar '(calendar-forward-month 1))))
  13074. (org-defkey minibuffer-local-map [?\e (shift up)]
  13075. (lambda () (interactive)
  13076. (org-eval-in-calendar '(calendar-backward-year 1))))
  13077. (org-defkey minibuffer-local-map [?\e (shift down)]
  13078. (lambda () (interactive)
  13079. (org-eval-in-calendar '(calendar-forward-year 1))))
  13080. (org-defkey minibuffer-local-map [(shift up)]
  13081. (lambda () (interactive)
  13082. (org-eval-in-calendar '(calendar-backward-week 1))))
  13083. (org-defkey minibuffer-local-map [(shift down)]
  13084. (lambda () (interactive)
  13085. (org-eval-in-calendar '(calendar-forward-week 1))))
  13086. (org-defkey minibuffer-local-map [(shift left)]
  13087. (lambda () (interactive)
  13088. (org-eval-in-calendar '(calendar-backward-day 1))))
  13089. (org-defkey minibuffer-local-map [(shift right)]
  13090. (lambda () (interactive)
  13091. (org-eval-in-calendar '(calendar-forward-day 1))))
  13092. (org-defkey minibuffer-local-map ">"
  13093. (lambda () (interactive)
  13094. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13095. (org-defkey minibuffer-local-map "<"
  13096. (lambda () (interactive)
  13097. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13098. (org-defkey minibuffer-local-map "\C-v"
  13099. (lambda () (interactive)
  13100. (org-eval-in-calendar
  13101. '(calendar-scroll-left-three-months 1))))
  13102. (org-defkey minibuffer-local-map "\M-v"
  13103. (lambda () (interactive)
  13104. (org-eval-in-calendar
  13105. '(calendar-scroll-right-three-months 1))))
  13106. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13107. (unwind-protect
  13108. (progn
  13109. (use-local-map map)
  13110. (add-hook 'post-command-hook 'org-read-date-display)
  13111. (setq org-ans0 (read-string prompt default-input
  13112. 'org-read-date-history nil))
  13113. ;; org-ans0: from prompt
  13114. ;; org-ans1: from mouse click
  13115. ;; org-ans2: from calendar motion
  13116. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13117. (remove-hook 'post-command-hook 'org-read-date-display)
  13118. (use-local-map old-map)
  13119. (when org-read-date-overlay
  13120. (delete-overlay org-read-date-overlay)
  13121. (setq org-read-date-overlay nil)))))))
  13122. (t ; Naked prompt only
  13123. (unwind-protect
  13124. (setq ans (read-string prompt default-input
  13125. 'org-read-date-history timestr))
  13126. (when org-read-date-overlay
  13127. (delete-overlay org-read-date-overlay)
  13128. (setq org-read-date-overlay nil)))))
  13129. (setq final (org-read-date-analyze ans def defdecode))
  13130. (setq org-read-date-final-answer ans)
  13131. (if to-time
  13132. (apply 'encode-time final)
  13133. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13134. (format "%04d-%02d-%02d %02d:%02d"
  13135. (nth 5 final) (nth 4 final) (nth 3 final)
  13136. (nth 2 final) (nth 1 final))
  13137. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13138. (defvar def)
  13139. (defvar defdecode)
  13140. (defvar with-time)
  13141. (defvar org-read-date-analyze-futurep nil)
  13142. (defun org-read-date-display ()
  13143. "Display the current date prompt interpretation in the minibuffer."
  13144. (when org-read-date-display-live
  13145. (when org-read-date-overlay
  13146. (delete-overlay org-read-date-overlay))
  13147. (let ((p (point)))
  13148. (end-of-line 1)
  13149. (while (not (equal (buffer-substring
  13150. (max (point-min) (- (point) 4)) (point))
  13151. " "))
  13152. (insert " "))
  13153. (goto-char p))
  13154. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13155. " " (or org-ans1 org-ans2)))
  13156. (org-end-time-was-given nil)
  13157. (f (org-read-date-analyze ans def defdecode))
  13158. (fmts (if org-dcst
  13159. org-time-stamp-custom-formats
  13160. org-time-stamp-formats))
  13161. (fmt (if (or with-time
  13162. (and (boundp 'org-time-was-given) org-time-was-given))
  13163. (cdr fmts)
  13164. (car fmts)))
  13165. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13166. (when (and org-end-time-was-given
  13167. (string-match org-plain-time-of-day-regexp txt))
  13168. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13169. org-end-time-was-given
  13170. (substring txt (match-end 0)))))
  13171. (when org-read-date-analyze-futurep
  13172. (setq txt (concat txt " (=>F)")))
  13173. (setq org-read-date-overlay
  13174. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13175. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13176. (defun org-read-date-analyze (ans def defdecode)
  13177. "Analyze the combined answer of the date prompt."
  13178. ;; FIXME: cleanup and comment
  13179. (let ((nowdecode (decode-time (current-time)))
  13180. delta deltan deltaw deltadef year month day
  13181. hour minute second wday pm h2 m2 tl wday1
  13182. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13183. (setq org-read-date-analyze-futurep nil)
  13184. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13185. (setq ans "+0"))
  13186. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13187. (setq ans (replace-match "" t t ans)
  13188. deltan (car delta)
  13189. deltaw (nth 1 delta)
  13190. deltadef (nth 2 delta)))
  13191. ;; Check if there is an iso week date in there
  13192. ;; If yes, store the info and postpone interpreting it until the rest
  13193. ;; of the parsing is done
  13194. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13195. (setq iso-year (if (match-end 1)
  13196. (org-small-year-to-year
  13197. (string-to-number (match-string 1 ans))))
  13198. iso-weekday (if (match-end 3)
  13199. (string-to-number (match-string 3 ans)))
  13200. iso-week (string-to-number (match-string 2 ans)))
  13201. (setq ans (replace-match "" t t ans)))
  13202. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13203. (when (string-match
  13204. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13205. (setq year (if (match-end 2)
  13206. (string-to-number (match-string 2 ans))
  13207. (progn (setq kill-year t)
  13208. (string-to-number (format-time-string "%Y"))))
  13209. month (string-to-number (match-string 3 ans))
  13210. day (string-to-number (match-string 4 ans)))
  13211. (if (< year 100) (setq year (+ 2000 year)))
  13212. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13213. t nil ans)))
  13214. ;; Help matching american dates, like 5/30 or 5/30/7
  13215. (when (string-match
  13216. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13217. (setq year (if (match-end 4)
  13218. (string-to-number (match-string 4 ans))
  13219. (progn (setq kill-year t)
  13220. (string-to-number (format-time-string "%Y"))))
  13221. month (string-to-number (match-string 1 ans))
  13222. day (string-to-number (match-string 2 ans)))
  13223. (if (< year 100) (setq year (+ 2000 year)))
  13224. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13225. t nil ans)))
  13226. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13227. ;; If there is a time with am/pm, and *no* time without it, we convert
  13228. ;; so that matching will be successful.
  13229. (loop for i from 1 to 2 do ; twice, for end time as well
  13230. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13231. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13232. (setq hour (string-to-number (match-string 1 ans))
  13233. minute (if (match-end 3)
  13234. (string-to-number (match-string 3 ans))
  13235. 0)
  13236. pm (equal ?p
  13237. (string-to-char (downcase (match-string 4 ans)))))
  13238. (if (and (= hour 12) (not pm))
  13239. (setq hour 0)
  13240. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13241. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13242. t t ans))))
  13243. ;; Check if a time range is given as a duration
  13244. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13245. (setq hour (string-to-number (match-string 1 ans))
  13246. h2 (+ hour (string-to-number (match-string 3 ans)))
  13247. minute (string-to-number (match-string 2 ans))
  13248. m2 (+ minute (if (match-end 5) (string-to-number
  13249. (match-string 5 ans))0)))
  13250. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13251. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13252. t t ans)))
  13253. ;; Check if there is a time range
  13254. (when (boundp 'org-end-time-was-given)
  13255. (setq org-time-was-given nil)
  13256. (when (and (string-match org-plain-time-of-day-regexp ans)
  13257. (match-end 8))
  13258. (setq org-end-time-was-given (match-string 8 ans))
  13259. (setq ans (concat (substring ans 0 (match-beginning 7))
  13260. (substring ans (match-end 7))))))
  13261. (setq tl (parse-time-string ans)
  13262. day (or (nth 3 tl) (nth 3 defdecode))
  13263. month (or (nth 4 tl)
  13264. (if (and org-read-date-prefer-future
  13265. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13266. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13267. (nth 4 defdecode)))
  13268. year (or (and (not kill-year) (nth 5 tl))
  13269. (if (and org-read-date-prefer-future
  13270. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13271. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13272. (nth 5 defdecode)))
  13273. hour (or (nth 2 tl) (nth 2 defdecode))
  13274. minute (or (nth 1 tl) (nth 1 defdecode))
  13275. second (or (nth 0 tl) 0)
  13276. wday (nth 6 tl))
  13277. (when (and (eq org-read-date-prefer-future 'time)
  13278. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13279. (equal day (nth 3 nowdecode))
  13280. (equal month (nth 4 nowdecode))
  13281. (equal year (nth 5 nowdecode))
  13282. (nth 2 tl)
  13283. (or (< (nth 2 tl) (nth 2 nowdecode))
  13284. (and (= (nth 2 tl) (nth 2 nowdecode))
  13285. (nth 1 tl)
  13286. (< (nth 1 tl) (nth 1 nowdecode)))))
  13287. (setq day (1+ day)
  13288. futurep t))
  13289. ;; Special date definitions below
  13290. (cond
  13291. (iso-week
  13292. ;; There was an iso week
  13293. (require 'cal-iso)
  13294. (setq futurep nil)
  13295. (setq year (or iso-year year)
  13296. day (or iso-weekday wday 1)
  13297. wday nil ; to make sure that the trigger below does not match
  13298. iso-date (calendar-gregorian-from-absolute
  13299. (calendar-absolute-from-iso
  13300. (list iso-week day year))))
  13301. ; FIXME: Should we also push ISO weeks into the future?
  13302. ; (when (and org-read-date-prefer-future
  13303. ; (not iso-year)
  13304. ; (< (calendar-absolute-from-gregorian iso-date)
  13305. ; (time-to-days (current-time))))
  13306. ; (setq year (1+ year)
  13307. ; iso-date (calendar-gregorian-from-absolute
  13308. ; (calendar-absolute-from-iso
  13309. ; (list iso-week day year)))))
  13310. (setq month (car iso-date)
  13311. year (nth 2 iso-date)
  13312. day (nth 1 iso-date)))
  13313. (deltan
  13314. (setq futurep nil)
  13315. (unless deltadef
  13316. (let ((now (decode-time (current-time))))
  13317. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13318. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13319. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13320. ((equal deltaw "m") (setq month (+ month deltan)))
  13321. ((equal deltaw "y") (setq year (+ year deltan)))))
  13322. ((and wday (not (nth 3 tl)))
  13323. (setq futurep nil)
  13324. ;; Weekday was given, but no day, so pick that day in the week
  13325. ;; on or after the derived date.
  13326. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13327. (unless (equal wday wday1)
  13328. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13329. (if (and (boundp 'org-time-was-given)
  13330. (nth 2 tl))
  13331. (setq org-time-was-given t))
  13332. (if (< year 100) (setq year (+ 2000 year)))
  13333. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  13334. (setq org-read-date-analyze-futurep futurep)
  13335. (list second minute hour day month year)))
  13336. (defvar parse-time-weekdays)
  13337. (defun org-read-date-get-relative (s today default)
  13338. "Check string S for special relative date string.
  13339. TODAY and DEFAULT are internal times, for today and for a default.
  13340. Return shift list (N what def-flag)
  13341. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13342. N is the number of WHATs to shift.
  13343. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13344. the DEFAULT date rather than TODAY."
  13345. (when (and
  13346. (string-match
  13347. (concat
  13348. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13349. "\\([0-9]+\\)?"
  13350. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13351. "\\([ \t]\\|$\\)") s)
  13352. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13353. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13354. (string-to-char (substring (match-string 1 s) -1))
  13355. ?+))
  13356. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13357. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13358. (what (if (match-end 3) (match-string 3 s) "d"))
  13359. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13360. (date (if rel default today))
  13361. (wday (nth 6 (decode-time date)))
  13362. delta)
  13363. (if wday1
  13364. (progn
  13365. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13366. (if (= dir ?-) (setq delta (- delta 7)))
  13367. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13368. (list delta "d" rel))
  13369. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13370. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13371. "Turn a user-specified date into the internal representation.
  13372. The internal representation needed by the calendar is (month day year).
  13373. This is a wrapper to handle the brain-dead convention in calendar that
  13374. user function argument order change dependent on argument order."
  13375. (if (boundp 'calendar-date-style)
  13376. (cond
  13377. ((eq calendar-date-style 'american)
  13378. (list arg1 arg2 arg3))
  13379. ((eq calendar-date-style 'european)
  13380. (list arg2 arg1 arg3))
  13381. ((eq calendar-date-style 'iso)
  13382. (list arg2 arg3 arg1)))
  13383. (if (org-bound-and-true-p european-calendar-style)
  13384. (list arg2 arg1 arg3)
  13385. (list arg1 arg2 arg3))))
  13386. (defun org-eval-in-calendar (form &optional keepdate)
  13387. "Eval FORM in the calendar window and return to current window.
  13388. Also, store the cursor date in variable org-ans2."
  13389. (let ((sf (selected-frame))
  13390. (sw (selected-window)))
  13391. (select-window (get-buffer-window "*Calendar*" t))
  13392. (eval form)
  13393. (when (and (not keepdate) (calendar-cursor-to-date))
  13394. (let* ((date (calendar-cursor-to-date))
  13395. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13396. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13397. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13398. (select-window sw)
  13399. (org-select-frame-set-input-focus sf)))
  13400. (defun org-calendar-select ()
  13401. "Return to `org-read-date' with the date currently selected.
  13402. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13403. (interactive)
  13404. (when (calendar-cursor-to-date)
  13405. (let* ((date (calendar-cursor-to-date))
  13406. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13407. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13408. (if (active-minibuffer-window) (exit-minibuffer))))
  13409. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13410. "Insert a date stamp for the date given by the internal TIME.
  13411. WITH-HM means use the stamp format that includes the time of the day.
  13412. INACTIVE means use square brackets instead of angular ones, so that the
  13413. stamp will not contribute to the agenda.
  13414. PRE and POST are optional strings to be inserted before and after the
  13415. stamp.
  13416. The command returns the inserted time stamp."
  13417. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13418. stamp)
  13419. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13420. (insert-before-markers (or pre ""))
  13421. (when (listp extra)
  13422. (setq extra (car extra))
  13423. (if (and (stringp extra)
  13424. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13425. (setq extra (format "-%02d:%02d"
  13426. (string-to-number (match-string 1 extra))
  13427. (string-to-number (match-string 2 extra))))
  13428. (setq extra nil)))
  13429. (when extra
  13430. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13431. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13432. (insert-before-markers (or post ""))
  13433. (setq org-last-inserted-timestamp stamp)))
  13434. (defun org-toggle-time-stamp-overlays ()
  13435. "Toggle the use of custom time stamp formats."
  13436. (interactive)
  13437. (setq org-display-custom-times (not org-display-custom-times))
  13438. (unless org-display-custom-times
  13439. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13440. (while (setq p (next-single-property-change p 'display))
  13441. (if (and (get-text-property p 'display)
  13442. (eq (get-text-property p 'face) 'org-date))
  13443. (remove-text-properties
  13444. p (setq p (next-single-property-change p 'display))
  13445. '(display t))))
  13446. (set-buffer-modified-p bmp)))
  13447. (if (featurep 'xemacs)
  13448. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13449. (org-restart-font-lock)
  13450. (setq org-table-may-need-update t)
  13451. (if org-display-custom-times
  13452. (message "Time stamps are overlayed with custom format")
  13453. (message "Time stamp overlays removed")))
  13454. (defun org-display-custom-time (beg end)
  13455. "Overlay modified time stamp format over timestamp between BEG and END."
  13456. (let* ((ts (buffer-substring beg end))
  13457. t1 w1 with-hm tf time str w2 (off 0))
  13458. (save-match-data
  13459. (setq t1 (org-parse-time-string ts t))
  13460. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13461. (setq off (- (match-end 0) (match-beginning 0)))))
  13462. (setq end (- end off))
  13463. (setq w1 (- end beg)
  13464. with-hm (and (nth 1 t1) (nth 2 t1))
  13465. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13466. time (org-fix-decoded-time t1)
  13467. str (org-add-props
  13468. (format-time-string
  13469. (substring tf 1 -1) (apply 'encode-time time))
  13470. nil 'mouse-face 'highlight)
  13471. w2 (length str))
  13472. (if (not (= w2 w1))
  13473. (add-text-properties (1+ beg) (+ 2 beg)
  13474. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13475. (if (featurep 'xemacs)
  13476. (progn
  13477. (put-text-property beg end 'invisible t)
  13478. (put-text-property beg end 'end-glyph (make-glyph str)))
  13479. (put-text-property beg end 'display str))))
  13480. (defun org-translate-time (string)
  13481. "Translate all timestamps in STRING to custom format.
  13482. But do this only if the variable `org-display-custom-times' is set."
  13483. (when org-display-custom-times
  13484. (save-match-data
  13485. (let* ((start 0)
  13486. (re org-ts-regexp-both)
  13487. t1 with-hm inactive tf time str beg end)
  13488. (while (setq start (string-match re string start))
  13489. (setq beg (match-beginning 0)
  13490. end (match-end 0)
  13491. t1 (save-match-data
  13492. (org-parse-time-string (substring string beg end) t))
  13493. with-hm (and (nth 1 t1) (nth 2 t1))
  13494. inactive (equal (substring string beg (1+ beg)) "[")
  13495. tf (funcall (if with-hm 'cdr 'car)
  13496. org-time-stamp-custom-formats)
  13497. time (org-fix-decoded-time t1)
  13498. str (format-time-string
  13499. (concat
  13500. (if inactive "[" "<") (substring tf 1 -1)
  13501. (if inactive "]" ">"))
  13502. (apply 'encode-time time))
  13503. string (replace-match str t t string)
  13504. start (+ start (length str)))))))
  13505. string)
  13506. (defun org-fix-decoded-time (time)
  13507. "Set 0 instead of nil for the first 6 elements of time.
  13508. Don't touch the rest."
  13509. (let ((n 0))
  13510. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13511. (defun org-days-to-time (timestamp-string)
  13512. "Difference between TIMESTAMP-STRING and now in days."
  13513. (- (time-to-days (org-time-string-to-time timestamp-string))
  13514. (time-to-days (current-time))))
  13515. (defun org-deadline-close (timestamp-string &optional ndays)
  13516. "Is the time in TIMESTAMP-STRING close to the current date?"
  13517. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13518. (and (< (org-days-to-time timestamp-string) ndays)
  13519. (not (org-entry-is-done-p))))
  13520. (defun org-get-wdays (ts)
  13521. "Get the deadline lead time appropriate for timestring TS."
  13522. (cond
  13523. ((<= org-deadline-warning-days 0)
  13524. ;; 0 or negative, enforce this value no matter what
  13525. (- org-deadline-warning-days))
  13526. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13527. ;; lead time is specified.
  13528. (floor (* (string-to-number (match-string 1 ts))
  13529. (cdr (assoc (match-string 2 ts)
  13530. '(("d" . 1) ("w" . 7)
  13531. ("m" . 30.4) ("y" . 365.25)))))))
  13532. ;; go for the default.
  13533. (t org-deadline-warning-days)))
  13534. (defun org-calendar-select-mouse (ev)
  13535. "Return to `org-read-date' with the date currently selected.
  13536. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13537. (interactive "e")
  13538. (mouse-set-point ev)
  13539. (when (calendar-cursor-to-date)
  13540. (let* ((date (calendar-cursor-to-date))
  13541. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13542. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13543. (if (active-minibuffer-window) (exit-minibuffer))))
  13544. (defun org-check-deadlines (ndays)
  13545. "Check if there are any deadlines due or past due.
  13546. A deadline is considered due if it happens within `org-deadline-warning-days'
  13547. days from today's date. If the deadline appears in an entry marked DONE,
  13548. it is not shown. The prefix arg NDAYS can be used to test that many
  13549. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13550. (interactive "P")
  13551. (let* ((org-warn-days
  13552. (cond
  13553. ((equal ndays '(4)) 100000)
  13554. (ndays (prefix-numeric-value ndays))
  13555. (t (abs org-deadline-warning-days))))
  13556. (case-fold-search nil)
  13557. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13558. (callback
  13559. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13560. (message "%d deadlines past-due or due within %d days"
  13561. (org-occur regexp nil callback)
  13562. org-warn-days)))
  13563. (defun org-check-before-date (date)
  13564. "Check if there are deadlines or scheduled entries before DATE."
  13565. (interactive (list (org-read-date)))
  13566. (let ((case-fold-search nil)
  13567. (regexp (concat "\\<\\(" org-deadline-string
  13568. "\\|" org-scheduled-string
  13569. "\\) *<\\([^>]+\\)>"))
  13570. (callback
  13571. (lambda () (time-less-p
  13572. (org-time-string-to-time (match-string 2))
  13573. (org-time-string-to-time date)))))
  13574. (message "%d entries before %s"
  13575. (org-occur regexp nil callback) date)))
  13576. (defun org-check-after-date (date)
  13577. "Check if there are deadlines or scheduled entries after DATE."
  13578. (interactive (list (org-read-date)))
  13579. (let ((case-fold-search nil)
  13580. (regexp (concat "\\<\\(" org-deadline-string
  13581. "\\|" org-scheduled-string
  13582. "\\) *<\\([^>]+\\)>"))
  13583. (callback
  13584. (lambda () (not
  13585. (time-less-p
  13586. (org-time-string-to-time (match-string 2))
  13587. (org-time-string-to-time date))))))
  13588. (message "%d entries after %s"
  13589. (org-occur regexp nil callback) date)))
  13590. (defun org-evaluate-time-range (&optional to-buffer)
  13591. "Evaluate a time range by computing the difference between start and end.
  13592. Normally the result is just printed in the echo area, but with prefix arg
  13593. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13594. If the time range is actually in a table, the result is inserted into the
  13595. next column.
  13596. For time difference computation, a year is assumed to be exactly 365
  13597. days in order to avoid rounding problems."
  13598. (interactive "P")
  13599. (or
  13600. (org-clock-update-time-maybe)
  13601. (save-excursion
  13602. (unless (org-at-date-range-p t)
  13603. (goto-char (point-at-bol))
  13604. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13605. (if (not (org-at-date-range-p t))
  13606. (error "Not at a time-stamp range, and none found in current line")))
  13607. (let* ((ts1 (match-string 1))
  13608. (ts2 (match-string 2))
  13609. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13610. (match-end (match-end 0))
  13611. (time1 (org-time-string-to-time ts1))
  13612. (time2 (org-time-string-to-time ts2))
  13613. (t1 (org-float-time time1))
  13614. (t2 (org-float-time time2))
  13615. (diff (abs (- t2 t1)))
  13616. (negative (< (- t2 t1) 0))
  13617. ;; (ys (floor (* 365 24 60 60)))
  13618. (ds (* 24 60 60))
  13619. (hs (* 60 60))
  13620. (fy "%dy %dd %02d:%02d")
  13621. (fy1 "%dy %dd")
  13622. (fd "%dd %02d:%02d")
  13623. (fd1 "%dd")
  13624. (fh "%02d:%02d")
  13625. y d h m align)
  13626. (if havetime
  13627. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13628. y 0
  13629. d (floor (/ diff ds)) diff (mod diff ds)
  13630. h (floor (/ diff hs)) diff (mod diff hs)
  13631. m (floor (/ diff 60)))
  13632. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13633. y 0
  13634. d (floor (+ (/ diff ds) 0.5))
  13635. h 0 m 0))
  13636. (if (not to-buffer)
  13637. (message "%s" (org-make-tdiff-string y d h m))
  13638. (if (org-at-table-p)
  13639. (progn
  13640. (goto-char match-end)
  13641. (setq align t)
  13642. (and (looking-at " *|") (goto-char (match-end 0))))
  13643. (goto-char match-end))
  13644. (if (looking-at
  13645. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13646. (replace-match ""))
  13647. (if negative (insert " -"))
  13648. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13649. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13650. (insert " " (format fh h m))))
  13651. (if align (org-table-align))
  13652. (message "Time difference inserted")))))
  13653. (defun org-make-tdiff-string (y d h m)
  13654. (let ((fmt "")
  13655. (l nil))
  13656. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13657. l (push y l)))
  13658. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13659. l (push d l)))
  13660. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13661. l (push h l)))
  13662. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13663. l (push m l)))
  13664. (apply 'format fmt (nreverse l))))
  13665. (defun org-time-string-to-time (s)
  13666. (apply 'encode-time (org-parse-time-string s)))
  13667. (defun org-time-string-to-seconds (s)
  13668. (org-float-time (org-time-string-to-time s)))
  13669. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13670. "Convert a time stamp to an absolute day number.
  13671. If there is a specifier for a cyclic time stamp, get the closest date to
  13672. DAYNR.
  13673. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13674. the variable date is bound by the calendar when this is called."
  13675. (cond
  13676. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13677. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13678. daynr
  13679. (+ daynr 1000)))
  13680. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13681. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13682. (time-to-days (current-time))) (match-string 0 s)
  13683. prefer show-all))
  13684. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13685. (defun org-days-to-iso-week (days)
  13686. "Return the iso week number."
  13687. (require 'cal-iso)
  13688. (car (calendar-iso-from-absolute days)))
  13689. (defun org-small-year-to-year (year)
  13690. "Convert 2-digit years into 4-digit years.
  13691. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13692. The year 2000 cannot be abbreviated. Any year larger than 99
  13693. is returned unchanged."
  13694. (if (< year 38)
  13695. (setq year (+ 2000 year))
  13696. (if (< year 100)
  13697. (setq year (+ 1900 year))))
  13698. year)
  13699. (defun org-time-from-absolute (d)
  13700. "Return the time corresponding to date D.
  13701. D may be an absolute day number, or a calendar-type list (month day year)."
  13702. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13703. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13704. (defun org-calendar-holiday ()
  13705. "List of holidays, for Diary display in Org-mode."
  13706. (require 'holidays)
  13707. (let ((hl (funcall
  13708. (if (fboundp 'calendar-check-holidays)
  13709. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13710. (if hl (mapconcat 'identity hl "; "))))
  13711. (defun org-diary-sexp-entry (sexp entry date)
  13712. "Process a SEXP diary ENTRY for DATE."
  13713. (require 'diary-lib)
  13714. (let ((result (if calendar-debug-sexp
  13715. (let ((stack-trace-on-error t))
  13716. (eval (car (read-from-string sexp))))
  13717. (condition-case nil
  13718. (eval (car (read-from-string sexp)))
  13719. (error
  13720. (beep)
  13721. (message "Bad sexp at line %d in %s: %s"
  13722. (org-current-line)
  13723. (buffer-file-name) sexp)
  13724. (sleep-for 2))))))
  13725. (cond ((stringp result) result)
  13726. ((and (consp result)
  13727. (stringp (cdr result))) (cdr result))
  13728. (result entry)
  13729. (t nil))))
  13730. (defun org-diary-to-ical-string (frombuf)
  13731. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13732. This uses the icalendar.el library."
  13733. (let* ((tmpdir (if (featurep 'xemacs)
  13734. (temp-directory)
  13735. temporary-file-directory))
  13736. (tmpfile (make-temp-name
  13737. (expand-file-name "orgics" tmpdir)))
  13738. buf rtn b e)
  13739. (with-current-buffer frombuf
  13740. (icalendar-export-region (point-min) (point-max) tmpfile)
  13741. (setq buf (find-buffer-visiting tmpfile))
  13742. (set-buffer buf)
  13743. (goto-char (point-min))
  13744. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13745. (setq b (match-beginning 0)))
  13746. (goto-char (point-max))
  13747. (if (re-search-backward "^END:VEVENT" nil t)
  13748. (setq e (match-end 0)))
  13749. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13750. (kill-buffer buf)
  13751. (delete-file tmpfile)
  13752. rtn))
  13753. (defun org-closest-date (start current change prefer show-all)
  13754. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13755. When PREFER is `past' return a date that is either CURRENT or past.
  13756. When PREFER is `future', return a date that is either CURRENT or future.
  13757. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13758. ;; Make the proper lists from the dates
  13759. (catch 'exit
  13760. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13761. dn dw sday cday n1 n2 n0
  13762. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13763. (setq start (org-date-to-gregorian start)
  13764. current (org-date-to-gregorian
  13765. (if show-all
  13766. current
  13767. (time-to-days (current-time))))
  13768. sday (calendar-absolute-from-gregorian start)
  13769. cday (calendar-absolute-from-gregorian current))
  13770. (if (<= cday sday) (throw 'exit sday))
  13771. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13772. (setq dn (string-to-number (match-string 1 change))
  13773. dw (cdr (assoc (match-string 2 change) a1)))
  13774. (error "Invalid change specifier: %s" change))
  13775. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13776. (cond
  13777. ((eq dw 'day)
  13778. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13779. n2 (+ n1 dn)))
  13780. ((eq dw 'year)
  13781. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13782. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13783. (setq date1 (list m d y1)
  13784. n1 (calendar-absolute-from-gregorian date1)
  13785. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13786. n2 (calendar-absolute-from-gregorian date2)))
  13787. ((eq dw 'month)
  13788. ;; approx number of month between the two dates
  13789. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13790. ;; How often does dn fit in there?
  13791. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13792. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13793. m (+ m nm)
  13794. ny (floor (/ m 12))
  13795. y (+ y ny)
  13796. m (- m (* ny 12)))
  13797. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13798. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13799. (setq m2 (+ m dn) y2 y)
  13800. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13801. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13802. (while (<= n2 cday)
  13803. (setq n1 n2 m m2 y y2)
  13804. (setq m2 (+ m dn) y2 y)
  13805. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13806. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13807. ;; Make sure n1 is the earlier date
  13808. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13809. (if show-all
  13810. (cond
  13811. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13812. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13813. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13814. (cond
  13815. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13816. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13817. (t (if (= cday n1) n1 n2)))))))
  13818. (defun org-date-to-gregorian (date)
  13819. "Turn any specification of DATE into a Gregorian date for the calendar."
  13820. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13821. ((and (listp date) (= (length date) 3)) date)
  13822. ((stringp date)
  13823. (setq date (org-parse-time-string date))
  13824. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13825. ((listp date)
  13826. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13827. (defun org-parse-time-string (s &optional nodefault)
  13828. "Parse the standard Org-mode time string.
  13829. This should be a lot faster than the normal `parse-time-string'.
  13830. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13831. hour and minute fields will be nil if not given."
  13832. (if (string-match org-ts-regexp0 s)
  13833. (list 0
  13834. (if (or (match-beginning 8) (not nodefault))
  13835. (string-to-number (or (match-string 8 s) "0")))
  13836. (if (or (match-beginning 7) (not nodefault))
  13837. (string-to-number (or (match-string 7 s) "0")))
  13838. (string-to-number (match-string 4 s))
  13839. (string-to-number (match-string 3 s))
  13840. (string-to-number (match-string 2 s))
  13841. nil nil nil)
  13842. (error "Not a standard Org-mode time string: %s" s)))
  13843. (defun org-timestamp-up (&optional arg)
  13844. "Increase the date item at the cursor by one.
  13845. If the cursor is on the year, change the year. If it is on the month or
  13846. the day, change that.
  13847. With prefix ARG, change by that many units."
  13848. (interactive "p")
  13849. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  13850. (defun org-timestamp-down (&optional arg)
  13851. "Decrease the date item at the cursor by one.
  13852. If the cursor is on the year, change the year. If it is on the month or
  13853. the day, change that.
  13854. With prefix ARG, change by that many units."
  13855. (interactive "p")
  13856. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  13857. (defun org-timestamp-up-day (&optional arg)
  13858. "Increase the date in the time stamp by one day.
  13859. With prefix ARG, change that many days."
  13860. (interactive "p")
  13861. (if (and (not (org-at-timestamp-p t))
  13862. (org-on-heading-p))
  13863. (org-todo 'up)
  13864. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  13865. (defun org-timestamp-down-day (&optional arg)
  13866. "Decrease the date in the time stamp by one day.
  13867. With prefix ARG, change that many days."
  13868. (interactive "p")
  13869. (if (and (not (org-at-timestamp-p t))
  13870. (org-on-heading-p))
  13871. (org-todo 'down)
  13872. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  13873. (defun org-at-timestamp-p (&optional inactive-ok)
  13874. "Determine if the cursor is in or at a timestamp."
  13875. (interactive)
  13876. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13877. (pos (point))
  13878. (ans (or (looking-at tsr)
  13879. (save-excursion
  13880. (skip-chars-backward "^[<\n\r\t")
  13881. (if (> (point) (point-min)) (backward-char 1))
  13882. (and (looking-at tsr)
  13883. (> (- (match-end 0) pos) -1))))))
  13884. (and ans
  13885. (boundp 'org-ts-what)
  13886. (setq org-ts-what
  13887. (cond
  13888. ((= pos (match-beginning 0)) 'bracket)
  13889. ((= pos (1- (match-end 0))) 'bracket)
  13890. ((org-pos-in-match-range pos 2) 'year)
  13891. ((org-pos-in-match-range pos 3) 'month)
  13892. ((org-pos-in-match-range pos 7) 'hour)
  13893. ((org-pos-in-match-range pos 8) 'minute)
  13894. ((or (org-pos-in-match-range pos 4)
  13895. (org-pos-in-match-range pos 5)) 'day)
  13896. ((and (> pos (or (match-end 8) (match-end 5)))
  13897. (< pos (match-end 0)))
  13898. (- pos (or (match-end 8) (match-end 5))))
  13899. (t 'day))))
  13900. ans))
  13901. (defun org-toggle-timestamp-type ()
  13902. "Toggle the type (<active> or [inactive]) of a time stamp."
  13903. (interactive)
  13904. (when (org-at-timestamp-p t)
  13905. (let ((beg (match-beginning 0)) (end (match-end 0))
  13906. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13907. (save-excursion
  13908. (goto-char beg)
  13909. (while (re-search-forward "[][<>]" end t)
  13910. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13911. t t)))
  13912. (message "Timestamp is now %sactive"
  13913. (if (equal (char-after beg) ?<) "" "in")))))
  13914. (defun org-timestamp-change (n &optional what updown)
  13915. "Change the date in the time stamp at point.
  13916. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13917. `year', `minute', `second'. If WHAT is not given, the cursor position
  13918. in the timestamp determines what will be changed."
  13919. (let ((pos (point))
  13920. with-hm inactive
  13921. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13922. org-ts-what
  13923. extra rem
  13924. ts time time0)
  13925. (if (not (org-at-timestamp-p t))
  13926. (error "Not at a timestamp"))
  13927. (if (and (not what) (eq org-ts-what 'bracket))
  13928. (org-toggle-timestamp-type)
  13929. (if (and (not what) (not (eq org-ts-what 'day))
  13930. org-display-custom-times
  13931. (get-text-property (point) 'display)
  13932. (not (get-text-property (1- (point)) 'display)))
  13933. (setq org-ts-what 'day))
  13934. (setq org-ts-what (or what org-ts-what)
  13935. inactive (= (char-after (match-beginning 0)) ?\[)
  13936. ts (match-string 0))
  13937. (replace-match "")
  13938. (if (string-match
  13939. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13940. ts)
  13941. (setq extra (match-string 1 ts)))
  13942. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13943. (setq with-hm t))
  13944. (setq time0 (org-parse-time-string ts))
  13945. (when (and updown
  13946. (eq org-ts-what 'minute)
  13947. (not current-prefix-arg))
  13948. ;; This looks like s-up and s-down. Change by one rounding step.
  13949. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13950. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13951. (setcar (cdr time0) (+ (nth 1 time0)
  13952. (if (> n 0) (- rem) (- dm rem))))))
  13953. (setq time
  13954. (encode-time (or (car time0) 0)
  13955. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13956. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13957. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13958. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13959. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13960. (nthcdr 6 time0)))
  13961. (when (and (member org-ts-what '(hour minute))
  13962. extra
  13963. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13964. (setq extra (org-modify-ts-extra
  13965. extra
  13966. (if (eq org-ts-what 'hour) 2 5)
  13967. n dm)))
  13968. (when (integerp org-ts-what)
  13969. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13970. (if (eq what 'calendar)
  13971. (let ((cal-date (org-get-date-from-calendar)))
  13972. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13973. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13974. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13975. (setcar time0 (or (car time0) 0))
  13976. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13977. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13978. (setq time (apply 'encode-time time0))))
  13979. (setq org-last-changed-timestamp
  13980. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13981. (org-clock-update-time-maybe)
  13982. (goto-char pos)
  13983. ;; Try to recenter the calendar window, if any
  13984. (if (and org-calendar-follow-timestamp-change
  13985. (get-buffer-window "*Calendar*" t)
  13986. (memq org-ts-what '(day month year)))
  13987. (org-recenter-calendar (time-to-days time))))))
  13988. (defun org-modify-ts-extra (s pos n dm)
  13989. "Change the different parts of the lead-time and repeat fields in timestamp."
  13990. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13991. ng h m new rem)
  13992. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13993. (cond
  13994. ((or (org-pos-in-match-range pos 2)
  13995. (org-pos-in-match-range pos 3))
  13996. (setq m (string-to-number (match-string 3 s))
  13997. h (string-to-number (match-string 2 s)))
  13998. (if (org-pos-in-match-range pos 2)
  13999. (setq h (+ h n))
  14000. (setq n (* dm (org-no-warnings (signum n))))
  14001. (when (not (= 0 (setq rem (% m dm))))
  14002. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14003. (setq m (+ m n)))
  14004. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14005. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14006. (setq h (min 24 (max 0 h)))
  14007. (setq ng 1 new (format "-%02d:%02d" h m)))
  14008. ((org-pos-in-match-range pos 6)
  14009. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14010. ((org-pos-in-match-range pos 5)
  14011. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14012. ((org-pos-in-match-range pos 9)
  14013. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14014. ((org-pos-in-match-range pos 8)
  14015. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14016. (when ng
  14017. (setq s (concat
  14018. (substring s 0 (match-beginning ng))
  14019. new
  14020. (substring s (match-end ng))))))
  14021. s))
  14022. (defun org-recenter-calendar (date)
  14023. "If the calendar is visible, recenter it to DATE."
  14024. (let* ((win (selected-window))
  14025. (cwin (get-buffer-window "*Calendar*" t))
  14026. (calendar-move-hook nil))
  14027. (when cwin
  14028. (select-window cwin)
  14029. (calendar-goto-date (if (listp date) date
  14030. (calendar-gregorian-from-absolute date)))
  14031. (select-window win))))
  14032. (defun org-goto-calendar (&optional arg)
  14033. "Go to the Emacs calendar at the current date.
  14034. If there is a time stamp in the current line, go to that date.
  14035. A prefix ARG can be used to force the current date."
  14036. (interactive "P")
  14037. (let ((tsr org-ts-regexp) diff
  14038. (calendar-move-hook nil)
  14039. (calendar-view-holidays-initially-flag nil)
  14040. (calendar-view-diary-initially-flag nil))
  14041. (if (or (org-at-timestamp-p)
  14042. (save-excursion
  14043. (beginning-of-line 1)
  14044. (looking-at (concat ".*" tsr))))
  14045. (let ((d1 (time-to-days (current-time)))
  14046. (d2 (time-to-days
  14047. (org-time-string-to-time (match-string 1)))))
  14048. (setq diff (- d2 d1))))
  14049. (calendar)
  14050. (calendar-goto-today)
  14051. (if (and diff (not arg)) (calendar-forward-day diff))))
  14052. (defun org-get-date-from-calendar ()
  14053. "Return a list (month day year) of date at point in calendar."
  14054. (with-current-buffer "*Calendar*"
  14055. (save-match-data
  14056. (calendar-cursor-to-date))))
  14057. (defun org-date-from-calendar ()
  14058. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14059. If there is already a time stamp at the cursor position, update it."
  14060. (interactive)
  14061. (if (org-at-timestamp-p t)
  14062. (org-timestamp-change 0 'calendar)
  14063. (let ((cal-date (org-get-date-from-calendar)))
  14064. (org-insert-time-stamp
  14065. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14066. (defun org-minutes-to-hh:mm-string (m)
  14067. "Compute H:MM from a number of minutes."
  14068. (let ((h (/ m 60)))
  14069. (setq m (- m (* 60 h)))
  14070. (format org-time-clocksum-format h m)))
  14071. (defun org-hh:mm-string-to-minutes (s)
  14072. "Convert a string H:MM to a number of minutes.
  14073. If the string is just a number, interpret it as minutes.
  14074. In fact, the first hh:mm or number in the string will be taken,
  14075. there can be extra stuff in the string.
  14076. If no number is found, the return value is 0."
  14077. (cond
  14078. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14079. (+ (* (string-to-number (match-string 1 s)) 60)
  14080. (string-to-number (match-string 2 s))))
  14081. ((string-match "\\([0-9]+\\)" s)
  14082. (string-to-number (match-string 1 s)))
  14083. (t 0)))
  14084. ;;;; Files
  14085. (defun org-save-all-org-buffers ()
  14086. "Save all Org-mode buffers without user confirmation."
  14087. (interactive)
  14088. (message "Saving all Org-mode buffers...")
  14089. (save-some-buffers t 'org-mode-p)
  14090. (when (featurep 'org-id) (org-id-locations-save))
  14091. (message "Saving all Org-mode buffers... done"))
  14092. (defun org-revert-all-org-buffers ()
  14093. "Revert all Org-mode buffers.
  14094. Prompt for confirmation when there are unsaved changes.
  14095. Be sure you know what you are doing before letting this function
  14096. overwrite your changes.
  14097. This function is useful in a setup where one tracks org files
  14098. with a version control system, to revert on one machine after pulling
  14099. changes from another. I believe the procedure must be like this:
  14100. 1. M-x org-save-all-org-buffers
  14101. 2. Pull changes from the other machine, resolve conflicts
  14102. 3. M-x org-revert-all-org-buffers"
  14103. (interactive)
  14104. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14105. (error "Abort"))
  14106. (save-excursion
  14107. (save-window-excursion
  14108. (mapc
  14109. (lambda (b)
  14110. (when (and (with-current-buffer b (org-mode-p))
  14111. (with-current-buffer b buffer-file-name))
  14112. (switch-to-buffer b)
  14113. (revert-buffer t 'no-confirm)))
  14114. (buffer-list))
  14115. (when (and (featurep 'org-id) org-id-track-globally)
  14116. (org-id-locations-load)))))
  14117. ;;;; Agenda files
  14118. ;;;###autoload
  14119. (defun org-switchb (&optional arg)
  14120. "Switch between Org buffers.
  14121. With a prefix argument, restrict available to files.
  14122. With two prefix arguments, restrict available buffers to agenda files.
  14123. Defaults to `iswitchb' for buffer name completion.
  14124. Set `org-completion-use-ido' to make it use ido instead."
  14125. (interactive "P")
  14126. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14127. ((equal arg '(16)) (org-buffer-list 'agenda))
  14128. (t (org-buffer-list))))
  14129. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14130. (org-completion-use-ido org-completion-use-ido))
  14131. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14132. (setq org-completion-use-iswitchb t))
  14133. (switch-to-buffer
  14134. (org-icompleting-read "Org buffer: "
  14135. (mapcar 'list (mapcar 'buffer-name blist))
  14136. nil t))))
  14137. ;;; Define some older names previously used for this functionality
  14138. ;;;###autoload
  14139. (defalias 'org-ido-switchb 'org-switchb)
  14140. ;;;###autoload
  14141. (defalias 'org-iswitchb 'org-switchb)
  14142. (defun org-buffer-list (&optional predicate exclude-tmp)
  14143. "Return a list of Org buffers.
  14144. PREDICATE can be `export', `files' or `agenda'.
  14145. export restrict the list to Export buffers.
  14146. files restrict the list to buffers visiting Org files.
  14147. agenda restrict the list to buffers visiting agenda files.
  14148. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14149. (let* ((bfn nil)
  14150. (agenda-files (and (eq predicate 'agenda)
  14151. (mapcar 'file-truename (org-agenda-files t))))
  14152. (filter
  14153. (cond
  14154. ((eq predicate 'files)
  14155. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14156. ((eq predicate 'export)
  14157. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14158. ((eq predicate 'agenda)
  14159. (lambda (b)
  14160. (with-current-buffer b
  14161. (and (eq major-mode 'org-mode)
  14162. (setq bfn (buffer-file-name b))
  14163. (member (file-truename bfn) agenda-files)))))
  14164. (t (lambda (b) (with-current-buffer b
  14165. (or (eq major-mode 'org-mode)
  14166. (string-match "\*Org .*Export"
  14167. (buffer-name b)))))))))
  14168. (delq nil
  14169. (mapcar
  14170. (lambda(b)
  14171. (if (and (funcall filter b)
  14172. (or (not exclude-tmp)
  14173. (not (string-match "tmp" (buffer-name b)))))
  14174. b
  14175. nil))
  14176. (buffer-list)))))
  14177. (defun org-agenda-files (&optional unrestricted archives)
  14178. "Get the list of agenda files.
  14179. Optional UNRESTRICTED means return the full list even if a restriction
  14180. is currently in place.
  14181. When ARCHIVES is t, include all archive files that are really being
  14182. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14183. `org-agenda-archives-mode' is t."
  14184. (let ((files
  14185. (cond
  14186. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14187. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14188. ((listp org-agenda-files) org-agenda-files)
  14189. (t (error "Invalid value of `org-agenda-files'")))))
  14190. (setq files (apply 'append
  14191. (mapcar (lambda (f)
  14192. (if (file-directory-p f)
  14193. (directory-files
  14194. f t org-agenda-file-regexp)
  14195. (list f)))
  14196. files)))
  14197. (when org-agenda-skip-unavailable-files
  14198. (setq files (delq nil
  14199. (mapcar (function
  14200. (lambda (file)
  14201. (and (file-readable-p file) file)))
  14202. files))))
  14203. (when (or (eq archives t)
  14204. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14205. (setq files (org-add-archive-files files)))
  14206. files))
  14207. (defun org-agenda-file-p (&optional file)
  14208. "Return non-nil, if FILE is an agenda file.
  14209. If FILE is omitted, use the file associated with the current
  14210. buffer."
  14211. (member (or file (buffer-file-name))
  14212. (org-agenda-files t)))
  14213. (defun org-edit-agenda-file-list ()
  14214. "Edit the list of agenda files.
  14215. Depending on setup, this either uses customize to edit the variable
  14216. `org-agenda-files', or it visits the file that is holding the list. In the
  14217. latter case, the buffer is set up in a way that saving it automatically kills
  14218. the buffer and restores the previous window configuration."
  14219. (interactive)
  14220. (if (stringp org-agenda-files)
  14221. (let ((cw (current-window-configuration)))
  14222. (find-file org-agenda-files)
  14223. (org-set-local 'org-window-configuration cw)
  14224. (org-add-hook 'after-save-hook
  14225. (lambda ()
  14226. (set-window-configuration
  14227. (prog1 org-window-configuration
  14228. (kill-buffer (current-buffer))))
  14229. (org-install-agenda-files-menu)
  14230. (message "New agenda file list installed"))
  14231. nil 'local)
  14232. (message "%s" (substitute-command-keys
  14233. "Edit list and finish with \\[save-buffer]")))
  14234. (customize-variable 'org-agenda-files)))
  14235. (defun org-store-new-agenda-file-list (list)
  14236. "Set new value for the agenda file list and save it correctly."
  14237. (if (stringp org-agenda-files)
  14238. (let ((fe (org-read-agenda-file-list t)) b u)
  14239. (while (setq b (find-buffer-visiting org-agenda-files))
  14240. (kill-buffer b))
  14241. (with-temp-file org-agenda-files
  14242. (insert
  14243. (mapconcat
  14244. (lambda (f) ;; Keep un-expanded entries.
  14245. (if (setq u (assoc f fe))
  14246. (cdr u)
  14247. f))
  14248. list "\n")
  14249. "\n")))
  14250. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14251. (org-insert-mode-line-in-empty-file nil))
  14252. (setq org-agenda-files list)
  14253. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14254. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14255. "Read the list of agenda files from a file.
  14256. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14257. filenames, used by `org-store-new-agenda-file-list' to write back
  14258. un-expanded file names."
  14259. (when (file-directory-p org-agenda-files)
  14260. (error "`org-agenda-files' cannot be a single directory"))
  14261. (when (stringp org-agenda-files)
  14262. (with-temp-buffer
  14263. (insert-file-contents org-agenda-files)
  14264. (mapcar
  14265. (lambda (f)
  14266. (let ((e (expand-file-name (substitute-in-file-name f)
  14267. org-directory)))
  14268. (if pair-with-expansion
  14269. (cons e f)
  14270. e)))
  14271. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14272. ;;;###autoload
  14273. (defun org-cycle-agenda-files ()
  14274. "Cycle through the files in `org-agenda-files'.
  14275. If the current buffer visits an agenda file, find the next one in the list.
  14276. If the current buffer does not, find the first agenda file."
  14277. (interactive)
  14278. (let* ((fs (org-agenda-files t))
  14279. (files (append fs (list (car fs))))
  14280. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14281. file)
  14282. (unless files (error "No agenda files"))
  14283. (catch 'exit
  14284. (while (setq file (pop files))
  14285. (if (equal (file-truename file) tcf)
  14286. (when (car files)
  14287. (find-file (car files))
  14288. (throw 'exit t))))
  14289. (find-file (car fs)))
  14290. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14291. (defun org-agenda-file-to-front (&optional to-end)
  14292. "Move/add the current file to the top of the agenda file list.
  14293. If the file is not present in the list, it is added to the front. If it is
  14294. present, it is moved there. With optional argument TO-END, add/move to the
  14295. end of the list."
  14296. (interactive "P")
  14297. (let ((org-agenda-skip-unavailable-files nil)
  14298. (file-alist (mapcar (lambda (x)
  14299. (cons (file-truename x) x))
  14300. (org-agenda-files t)))
  14301. (ctf (file-truename buffer-file-name))
  14302. x had)
  14303. (setq x (assoc ctf file-alist) had x)
  14304. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14305. (if to-end
  14306. (setq file-alist (append (delq x file-alist) (list x)))
  14307. (setq file-alist (cons x (delq x file-alist))))
  14308. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14309. (org-install-agenda-files-menu)
  14310. (message "File %s to %s of agenda file list"
  14311. (if had "moved" "added") (if to-end "end" "front"))))
  14312. (defun org-remove-file (&optional file)
  14313. "Remove current file from the list of files in variable `org-agenda-files'.
  14314. These are the files which are being checked for agenda entries.
  14315. Optional argument FILE means use this file instead of the current."
  14316. (interactive)
  14317. (let* ((org-agenda-skip-unavailable-files nil)
  14318. (file (or file buffer-file-name))
  14319. (true-file (file-truename file))
  14320. (afile (abbreviate-file-name file))
  14321. (files (delq nil (mapcar
  14322. (lambda (x)
  14323. (if (equal true-file
  14324. (file-truename x))
  14325. nil x))
  14326. (org-agenda-files t)))))
  14327. (if (not (= (length files) (length (org-agenda-files t))))
  14328. (progn
  14329. (org-store-new-agenda-file-list files)
  14330. (org-install-agenda-files-menu)
  14331. (message "Removed file: %s" afile))
  14332. (message "File was not in list: %s (not removed)" afile))))
  14333. (defun org-file-menu-entry (file)
  14334. (vector file (list 'find-file file) t))
  14335. (defun org-check-agenda-file (file)
  14336. "Make sure FILE exists. If not, ask user what to do."
  14337. (when (not (file-exists-p file))
  14338. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14339. (abbreviate-file-name file))
  14340. (let ((r (downcase (read-char-exclusive))))
  14341. (cond
  14342. ((equal r ?r)
  14343. (org-remove-file file)
  14344. (throw 'nextfile t))
  14345. (t (error "Abort"))))))
  14346. (defun org-get-agenda-file-buffer (file)
  14347. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14348. it to the list of buffers which might be released later."
  14349. (let ((buf (org-find-base-buffer-visiting file)))
  14350. (if buf
  14351. buf ; just return it
  14352. ;; Make a new buffer and remember it
  14353. (setq buf (find-file-noselect file))
  14354. (if buf (push buf org-agenda-new-buffers))
  14355. buf)))
  14356. (defun org-release-buffers (blist)
  14357. "Release all buffers in list, asking the user for confirmation when needed.
  14358. When a buffer is unmodified, it is just killed. When modified, it is saved
  14359. \(if the user agrees) and then killed."
  14360. (let (buf file)
  14361. (while (setq buf (pop blist))
  14362. (setq file (buffer-file-name buf))
  14363. (when (and (buffer-modified-p buf)
  14364. file
  14365. (y-or-n-p (format "Save file %s? " file)))
  14366. (with-current-buffer buf (save-buffer)))
  14367. (kill-buffer buf))))
  14368. (defun org-prepare-agenda-buffers (files)
  14369. "Create buffers for all agenda files, protect archived trees and comments."
  14370. (interactive)
  14371. (let ((pa '(:org-archived t))
  14372. (pc '(:org-comment t))
  14373. (pall '(:org-archived t :org-comment t))
  14374. (inhibit-read-only t)
  14375. (rea (concat ":" org-archive-tag ":"))
  14376. bmp file re)
  14377. (save-excursion
  14378. (save-restriction
  14379. (while (setq file (pop files))
  14380. (catch 'nextfile
  14381. (if (bufferp file)
  14382. (set-buffer file)
  14383. (org-check-agenda-file file)
  14384. (set-buffer (org-get-agenda-file-buffer file)))
  14385. (widen)
  14386. (setq bmp (buffer-modified-p))
  14387. (org-refresh-category-properties)
  14388. (setq org-todo-keywords-for-agenda
  14389. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14390. (setq org-done-keywords-for-agenda
  14391. (append org-done-keywords-for-agenda org-done-keywords))
  14392. (setq org-todo-keyword-alist-for-agenda
  14393. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14394. (setq org-drawers-for-agenda
  14395. (append org-drawers-for-agenda org-drawers))
  14396. (setq org-tag-alist-for-agenda
  14397. (append org-tag-alist-for-agenda org-tag-alist))
  14398. (save-excursion
  14399. (remove-text-properties (point-min) (point-max) pall)
  14400. (when org-agenda-skip-archived-trees
  14401. (goto-char (point-min))
  14402. (while (re-search-forward rea nil t)
  14403. (if (org-on-heading-p t)
  14404. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14405. (goto-char (point-min))
  14406. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14407. (while (re-search-forward re nil t)
  14408. (add-text-properties
  14409. (match-beginning 0) (org-end-of-subtree t) pc)))
  14410. (set-buffer-modified-p bmp)))))
  14411. (setq org-todo-keywords-for-agenda
  14412. (org-uniquify org-todo-keywords-for-agenda))
  14413. (setq org-todo-keyword-alist-for-agenda
  14414. (org-uniquify org-todo-keyword-alist-for-agenda)
  14415. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14416. ;;;; Embedded LaTeX
  14417. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14418. "Keymap for the minor `org-cdlatex-mode'.")
  14419. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14420. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14421. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14422. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14423. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14424. (defvar org-cdlatex-texmathp-advice-is-done nil
  14425. "Flag remembering if we have applied the advice to texmathp already.")
  14426. (define-minor-mode org-cdlatex-mode
  14427. "Toggle the minor `org-cdlatex-mode'.
  14428. This mode supports entering LaTeX environment and math in LaTeX fragments
  14429. in Org-mode.
  14430. \\{org-cdlatex-mode-map}"
  14431. nil " OCDL" nil
  14432. (when org-cdlatex-mode (require 'cdlatex))
  14433. (unless org-cdlatex-texmathp-advice-is-done
  14434. (setq org-cdlatex-texmathp-advice-is-done t)
  14435. (defadvice texmathp (around org-math-always-on activate)
  14436. "Always return t in org-mode buffers.
  14437. This is because we want to insert math symbols without dollars even outside
  14438. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14439. an embedded LaTeX fragment, let texmathp do its job.
  14440. \\[org-cdlatex-mode-map]"
  14441. (interactive)
  14442. (let (p)
  14443. (cond
  14444. ((not (org-mode-p)) ad-do-it)
  14445. ((eq this-command 'cdlatex-math-symbol)
  14446. (setq ad-return-value t
  14447. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14448. (t
  14449. (let ((p (org-inside-LaTeX-fragment-p)))
  14450. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14451. (setq ad-return-value t
  14452. texmathp-why '("Org-mode embedded math" . 0))
  14453. (if p ad-do-it)))))))))
  14454. (defun turn-on-org-cdlatex ()
  14455. "Unconditionally turn on `org-cdlatex-mode'."
  14456. (org-cdlatex-mode 1))
  14457. (defun org-inside-LaTeX-fragment-p ()
  14458. "Test if point is inside a LaTeX fragment.
  14459. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14460. sequence appearing also before point.
  14461. Even though the matchers for math are configurable, this function assumes
  14462. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14463. delimiters are skipped when they have been removed by customization.
  14464. The return value is nil, or a cons cell with the delimiter and
  14465. and the position of this delimiter.
  14466. This function does a reasonably good job, but can locally be fooled by
  14467. for example currency specifications. For example it will assume being in
  14468. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14469. fragments that are properly closed, but during editing, we have to live
  14470. with the uncertainty caused by missing closing delimiters. This function
  14471. looks only before point, not after."
  14472. (catch 'exit
  14473. (let ((pos (point))
  14474. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14475. (lim (progn
  14476. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14477. (point)))
  14478. dd-on str (start 0) m re)
  14479. (goto-char pos)
  14480. (when dodollar
  14481. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14482. re (nth 1 (assoc "$" org-latex-regexps)))
  14483. (while (string-match re str start)
  14484. (cond
  14485. ((= (match-end 0) (length str))
  14486. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14487. ((= (match-end 0) (- (length str) 5))
  14488. (throw 'exit nil))
  14489. (t (setq start (match-end 0))))))
  14490. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14491. (goto-char pos)
  14492. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14493. (and (match-beginning 2) (throw 'exit nil))
  14494. ;; count $$
  14495. (while (re-search-backward "\\$\\$" lim t)
  14496. (setq dd-on (not dd-on)))
  14497. (goto-char pos)
  14498. (if dd-on (cons "$$" m))))))
  14499. (defun org-inside-latex-macro-p ()
  14500. "Is point inside a LaTeX macro or its arguments?"
  14501. (save-match-data
  14502. (org-in-regexp
  14503. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14504. (defun org-try-cdlatex-tab ()
  14505. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14506. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14507. - inside a LaTeX fragment, or
  14508. - after the first word in a line, where an abbreviation expansion could
  14509. insert a LaTeX environment."
  14510. (when org-cdlatex-mode
  14511. (cond
  14512. ((save-excursion
  14513. (skip-chars-backward "a-zA-Z0-9*")
  14514. (skip-chars-backward " \t")
  14515. (bolp))
  14516. (cdlatex-tab) t)
  14517. ((org-inside-LaTeX-fragment-p)
  14518. (cdlatex-tab) t)
  14519. (t nil))))
  14520. (defun org-cdlatex-underscore-caret (&optional arg)
  14521. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14522. Revert to the normal definition outside of these fragments."
  14523. (interactive "P")
  14524. (if (org-inside-LaTeX-fragment-p)
  14525. (call-interactively 'cdlatex-sub-superscript)
  14526. (let (org-cdlatex-mode)
  14527. (call-interactively (key-binding (vector last-input-event))))))
  14528. (defun org-cdlatex-math-modify (&optional arg)
  14529. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14530. Revert to the normal definition outside of these fragments."
  14531. (interactive "P")
  14532. (if (org-inside-LaTeX-fragment-p)
  14533. (call-interactively 'cdlatex-math-modify)
  14534. (let (org-cdlatex-mode)
  14535. (call-interactively (key-binding (vector last-input-event))))))
  14536. (defvar org-latex-fragment-image-overlays nil
  14537. "List of overlays carrying the images of latex fragments.")
  14538. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14539. (defun org-remove-latex-fragment-image-overlays ()
  14540. "Remove all overlays with LaTeX fragment images in current buffer."
  14541. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14542. (setq org-latex-fragment-image-overlays nil))
  14543. (defun org-preview-latex-fragment (&optional subtree)
  14544. "Preview the LaTeX fragment at point, or all locally or globally.
  14545. If the cursor is in a LaTeX fragment, create the image and overlay
  14546. it over the source code. If there is no fragment at point, display
  14547. all fragments in the current text, from one headline to the next. With
  14548. prefix SUBTREE, display all fragments in the current subtree. With a
  14549. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14550. the cursor is before the first headline,
  14551. display all fragments in the buffer.
  14552. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14553. (interactive "P")
  14554. (org-remove-latex-fragment-image-overlays)
  14555. (save-excursion
  14556. (save-restriction
  14557. (let (beg end at msg)
  14558. (cond
  14559. ((or (equal subtree '(16))
  14560. (not (save-excursion
  14561. (re-search-backward (concat "^" outline-regexp) nil t))))
  14562. (setq beg (point-min) end (point-max)
  14563. msg "Creating images for buffer...%s"))
  14564. ((equal subtree '(4))
  14565. (org-back-to-heading)
  14566. (setq beg (point) end (org-end-of-subtree t)
  14567. msg "Creating images for subtree...%s"))
  14568. (t
  14569. (if (setq at (org-inside-LaTeX-fragment-p))
  14570. (goto-char (max (point-min) (- (cdr at) 2)))
  14571. (org-back-to-heading))
  14572. (setq beg (point) end (progn (outline-next-heading) (point))
  14573. msg (if at "Creating image...%s"
  14574. "Creating images for entry...%s"))))
  14575. (message msg "")
  14576. (narrow-to-region beg end)
  14577. (goto-char beg)
  14578. (org-format-latex
  14579. (concat "ltxpng/" (file-name-sans-extension
  14580. (file-name-nondirectory
  14581. buffer-file-name)))
  14582. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14583. (message msg "done. Use `C-c C-c' to remove images.")))))
  14584. (defvar org-latex-regexps
  14585. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14586. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14587. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14588. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14589. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14590. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14591. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14592. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14593. "Regular expressions for matching embedded LaTeX.")
  14594. (defvar org-export-have-math nil) ;; dynamic scoping
  14595. (defun org-format-latex (prefix &optional dir overlays msg at
  14596. forbuffer processing-type)
  14597. "Replace LaTeX fragments with links to an image, and produce images.
  14598. Some of the options can be changed using the variable
  14599. `org-format-latex-options'."
  14600. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14601. (let* ((prefixnodir (file-name-nondirectory prefix))
  14602. (absprefix (expand-file-name prefix dir))
  14603. (todir (file-name-directory absprefix))
  14604. (opt org-format-latex-options)
  14605. (matchers (plist-get opt :matchers))
  14606. (re-list org-latex-regexps)
  14607. (org-format-latex-header-extra
  14608. (plist-get (org-infile-export-plist) :latex-header-extra))
  14609. (cnt 0) txt hash link beg end re e checkdir
  14610. executables-checked string
  14611. m n block linkfile movefile ov)
  14612. ;; Check the different regular expressions
  14613. (while (setq e (pop re-list))
  14614. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14615. block (if (nth 3 e) "\n\n" ""))
  14616. (when (member m matchers)
  14617. (goto-char (point-min))
  14618. (while (re-search-forward re nil t)
  14619. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14620. (not (get-text-property (match-beginning n)
  14621. 'org-protected))
  14622. (or (not overlays)
  14623. (not (eq (get-char-property (match-beginning n)
  14624. 'org-overlay-type)
  14625. 'org-latex-overlay))))
  14626. (setq org-export-have-math t)
  14627. (cond
  14628. ((eq processing-type 'verbatim)
  14629. ;; Leave the text verbatim, just protect it
  14630. (add-text-properties (match-beginning n) (match-end n)
  14631. '(org-protected t)))
  14632. ((eq processing-type 'mathjax)
  14633. ;; Prepare for MathJax processing
  14634. (setq string (match-string n))
  14635. (if (equal m "$")
  14636. (save-excursion
  14637. (delete-region (match-beginning n) (match-end n))
  14638. (goto-char (match-beginning n))
  14639. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14640. "\\)")
  14641. '(org-protected t))))
  14642. (add-text-properties (match-beginning n) (match-end n)
  14643. '(org-protected t))))
  14644. ((or (eq processing-type 'dvipng) t)
  14645. ;; Process to an image
  14646. (setq txt (match-string n)
  14647. beg (match-beginning n) end (match-end n)
  14648. cnt (1+ cnt))
  14649. (let (print-length print-level) ; make sure full list is printed
  14650. (setq hash (sha1 (prin1-to-string
  14651. (list org-format-latex-header
  14652. org-format-latex-header-extra
  14653. org-export-latex-default-packages-alist
  14654. org-export-latex-packages-alist
  14655. org-format-latex-options
  14656. forbuffer txt)))
  14657. linkfile (format "%s_%s.png" prefix hash)
  14658. movefile (format "%s_%s.png" absprefix hash)))
  14659. (setq link (concat block "[[file:" linkfile "]]" block))
  14660. (if msg (message msg cnt))
  14661. (goto-char beg)
  14662. (unless checkdir ; make sure the directory exists
  14663. (setq checkdir t)
  14664. (or (file-directory-p todir) (make-directory todir t)))
  14665. (unless executables-checked
  14666. (org-check-external-command
  14667. "latex" "needed to convert LaTeX fragments to images")
  14668. (org-check-external-command
  14669. "dvipng" "needed to convert LaTeX fragments to images")
  14670. (setq executables-checked t))
  14671. (unless (file-exists-p movefile)
  14672. (org-create-formula-image
  14673. txt movefile opt forbuffer))
  14674. (if overlays
  14675. (progn
  14676. (mapc (lambda (o)
  14677. (if (eq (overlay-get o 'org-overlay-type)
  14678. 'org-latex-overlay)
  14679. (delete-overlay o)))
  14680. (overlays-in beg end))
  14681. (setq ov (make-overlay beg end))
  14682. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14683. (if (featurep 'xemacs)
  14684. (progn
  14685. (overlay-put ov 'invisible t)
  14686. (overlay-put
  14687. ov 'end-glyph
  14688. (make-glyph (vector 'png :file movefile))))
  14689. (overlay-put
  14690. ov 'display
  14691. (list 'image :type 'png :file movefile :ascent 'center)))
  14692. (push ov org-latex-fragment-image-overlays)
  14693. (goto-char end))
  14694. (delete-region beg end)
  14695. (insert (org-add-props link
  14696. (list 'org-latex-src
  14697. (replace-regexp-in-string
  14698. "\"" "" txt)))))))))))))
  14699. ;; This function borrows from Ganesh Swami's latex2png.el
  14700. (defun org-create-formula-image (string tofile options buffer)
  14701. "This calls dvipng."
  14702. (require 'org-latex)
  14703. (let* ((tmpdir (if (featurep 'xemacs)
  14704. (temp-directory)
  14705. temporary-file-directory))
  14706. (texfilebase (make-temp-name
  14707. (expand-file-name "orgtex" tmpdir)))
  14708. (texfile (concat texfilebase ".tex"))
  14709. (dvifile (concat texfilebase ".dvi"))
  14710. (pngfile (concat texfilebase ".png"))
  14711. (fnh (if (featurep 'xemacs)
  14712. (font-height (get-face-font 'default))
  14713. (face-attribute 'default :height nil)))
  14714. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14715. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14716. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14717. "Black"))
  14718. (bg (or (plist-get options (if buffer :background :html-background))
  14719. "Transparent")))
  14720. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14721. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14722. (with-temp-file texfile
  14723. (insert (org-splice-latex-header
  14724. org-format-latex-header
  14725. org-export-latex-default-packages-alist
  14726. org-export-latex-packages-alist t
  14727. org-format-latex-header-extra))
  14728. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14729. (require 'org-latex)
  14730. (org-export-latex-fix-inputenc))
  14731. (let ((dir default-directory))
  14732. (condition-case nil
  14733. (progn
  14734. (cd tmpdir)
  14735. (call-process "latex" nil nil nil texfile))
  14736. (error nil))
  14737. (cd dir))
  14738. (if (not (file-exists-p dvifile))
  14739. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14740. (condition-case nil
  14741. (call-process "dvipng" nil nil nil
  14742. "-fg" fg "-bg" bg
  14743. "-D" dpi
  14744. ;;"-x" scale "-y" scale
  14745. "-T" "tight"
  14746. "-o" pngfile
  14747. dvifile)
  14748. (error nil))
  14749. (if (not (file-exists-p pngfile))
  14750. (if org-format-latex-signal-error
  14751. (error "Failed to create png file from %s" texfile)
  14752. (message "Failed to create png file from %s" texfile)
  14753. nil)
  14754. ;; Use the requested file name and clean up
  14755. (copy-file pngfile tofile 'replace)
  14756. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14757. (delete-file (concat texfilebase e)))
  14758. pngfile))))
  14759. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14760. "Fill a LaTeX header template TPL.
  14761. In the template, the following place holders will be recognized:
  14762. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14763. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14764. [PACKAGES] \\usepackage statements for PKG
  14765. [NO-PACKAGES] do not include PKG
  14766. [EXTRA] the string EXTRA
  14767. [NO-EXTRA] do not include EXTRA
  14768. For backward compatibility, if both the positive and the negative place
  14769. holder is missing, the positive one (without the \"NO-\") will be
  14770. assumed to be present at the end of the template.
  14771. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14772. EXTRA is a string.
  14773. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14774. (let (rpl (end ""))
  14775. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14776. (setq rpl (if (or (match-end 1) (not def-pkg))
  14777. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14778. tpl (replace-match rpl t t tpl))
  14779. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14780. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14781. (setq rpl (if (or (match-end 1) (not pkg))
  14782. "" (org-latex-packages-to-string pkg snippets-p t))
  14783. tpl (replace-match rpl t t tpl))
  14784. (if pkg (setq end
  14785. (concat end "\n"
  14786. (org-latex-packages-to-string pkg snippets-p)))))
  14787. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14788. (setq rpl (if (or (match-end 1) (not extra))
  14789. "" (concat extra "\n"))
  14790. tpl (replace-match rpl t t tpl))
  14791. (if (and extra (string-match "\\S-" extra))
  14792. (setq end (concat end "\n" extra))))
  14793. (if (string-match "\\S-" end)
  14794. (concat tpl "\n" end)
  14795. tpl)))
  14796. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14797. "Turn an alist of packages into a string with the \\usepackage macros."
  14798. (setq pkg (mapconcat (lambda(p)
  14799. (cond
  14800. ((stringp p) p)
  14801. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14802. (format "%% Package %s omitted" (cadr p)))
  14803. ((equal "" (car p))
  14804. (format "\\usepackage{%s}" (cadr p)))
  14805. (t
  14806. (format "\\usepackage[%s]{%s}"
  14807. (car p) (cadr p)))))
  14808. pkg
  14809. "\n"))
  14810. (if newline (concat pkg "\n") pkg))
  14811. (defun org-dvipng-color (attr)
  14812. "Return an rgb color specification for dvipng."
  14813. (apply 'format "rgb %s %s %s"
  14814. (mapcar 'org-normalize-color
  14815. (color-values (face-attribute 'default attr nil)))))
  14816. (defun org-normalize-color (value)
  14817. "Return string to be used as color value for an RGB component."
  14818. (format "%g" (/ value 65535.0)))
  14819. ;; Image display
  14820. (defvar org-inline-image-overlays nil)
  14821. (make-variable-buffer-local 'org-inline-image-overlays)
  14822. (defun org-toggle-inline-images (&optional include-linked)
  14823. "Toggle the display of inline images.
  14824. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14825. (interactive "P")
  14826. (if org-inline-image-overlays
  14827. (progn
  14828. (org-remove-inline-images)
  14829. (message "Inline image display turned off"))
  14830. (org-display-inline-images include-linked)
  14831. (if org-inline-image-overlays
  14832. (message "%d images displayed inline"
  14833. (length org-inline-image-overlays))
  14834. (message "No images to display inline"))))
  14835. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14836. "Display inline images.
  14837. Normally only links without a description part are inlined, because this
  14838. is how it will work for export. When INCLUDE-LINKED is set, also links
  14839. with a description part will be inlined. This can be nice for a quick
  14840. look at those images, but it does not reflect what exported files will look
  14841. like.
  14842. When REFRESH is set, refresh existing images between BEG and END.
  14843. This will create new image displays only if necessary.
  14844. BEG and END default to the buffer boundaries."
  14845. (interactive "P")
  14846. (unless refresh
  14847. (org-remove-inline-images)
  14848. (clear-image-cache))
  14849. (save-excursion
  14850. (save-restriction
  14851. (widen)
  14852. (setq beg (or beg (point-min)) end (or end (point-max)))
  14853. (goto-char (point-min))
  14854. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~.:/\\_0-9a-zA-Z ]+"
  14855. (substring (org-image-file-name-regexp) 0 -2)
  14856. "\\)\\]" (if include-linked "" "\\]")))
  14857. old file ov img)
  14858. (while (re-search-forward re end t)
  14859. (setq old (get-char-property-and-overlay (match-beginning 1)
  14860. 'org-image-overlay))
  14861. (setq file (expand-file-name
  14862. (concat (or (match-string 3) "") (match-string 4))))
  14863. (when (file-exists-p file)
  14864. (if (and (car-safe old) refresh)
  14865. (image-refresh (overlay-get (cdr old) 'display))
  14866. (setq img (create-image file))
  14867. (when img
  14868. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14869. (overlay-put ov 'display img)
  14870. (overlay-put ov 'face 'default)
  14871. (overlay-put ov 'org-image-overlay t)
  14872. (overlay-put ov 'modification-hooks
  14873. (list 'org-display-inline-modification-hook))
  14874. (push ov org-inline-image-overlays)))))))))
  14875. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  14876. "Remove inline-display overlay if a corresponding region is modified."
  14877. (let ((inhibit-modification-hooks t))
  14878. (when (and ov after)
  14879. (delete ov org-inline-image-overlays)
  14880. (delete-overlay ov))))
  14881. (defun org-remove-inline-images ()
  14882. "Remove inline display of images."
  14883. (interactive)
  14884. (mapc 'delete-overlay org-inline-image-overlays)
  14885. (setq org-inline-image-overlays nil))
  14886. ;;;; Key bindings
  14887. ;; Make `C-c C-x' a prefix key
  14888. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14889. ;; TAB key with modifiers
  14890. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14891. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14892. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14893. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14894. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14895. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14896. ;; The following line is necessary under Suse GNU/Linux
  14897. (unless (featurep 'xemacs)
  14898. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14899. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14900. (define-key org-mode-map [backtab] 'org-shifttab)
  14901. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14902. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14903. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14904. ;; Cursor keys with modifiers
  14905. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14906. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14907. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14908. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14909. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14910. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14911. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14912. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14913. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14914. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14915. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14916. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14917. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14918. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14919. ;; Babel keys
  14920. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  14921. (mapc (lambda (pair)
  14922. (define-key org-babel-map (car pair) (cdr pair)))
  14923. org-babel-key-bindings)
  14924. ;;; Extra keys for tty access.
  14925. ;; We only set them when really needed because otherwise the
  14926. ;; menus don't show the simple keys
  14927. (when (or org-use-extra-keys
  14928. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14929. (not window-system))
  14930. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14931. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14932. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14933. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14934. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14935. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14936. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14937. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14938. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14939. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14940. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14941. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14942. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14943. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14944. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14945. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14946. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14947. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14948. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14949. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14950. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14951. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14952. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14953. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14954. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14955. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14956. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14957. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14958. ;; All the other keys
  14959. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14960. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14961. (if (boundp 'narrow-map)
  14962. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14963. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14964. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14965. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14966. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14967. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14968. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14969. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14970. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14971. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14972. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14973. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14974. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14975. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14976. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14977. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14978. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14979. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14980. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14981. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14982. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14983. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14984. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14985. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14986. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14987. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14988. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14989. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14990. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14991. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14992. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14993. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14994. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14995. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14996. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14997. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14998. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14999. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15000. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15001. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15002. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15003. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15004. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15005. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15006. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15007. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15008. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15009. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15010. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15011. (org-defkey org-mode-map "\C-m" 'org-return)
  15012. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15013. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15014. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15015. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15016. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15017. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15018. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15019. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15020. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15021. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15022. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15023. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15024. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15025. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15026. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15027. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15028. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15029. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15030. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15031. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15032. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15033. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15034. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15035. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15036. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15037. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15038. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15039. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15040. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15041. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15042. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15043. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15044. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15045. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15046. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15047. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15048. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15049. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15050. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15051. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15052. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15053. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15054. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15055. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15056. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15057. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15058. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15059. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15060. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15061. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15062. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15063. (when (featurep 'xemacs)
  15064. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15065. (defconst org-speed-commands-default
  15066. '(
  15067. ("Outline Navigation")
  15068. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15069. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15070. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15071. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15072. ("u" . (org-speed-move-safe 'outline-up-heading))
  15073. ("j" . org-goto)
  15074. ("g" . (org-refile t))
  15075. ("Outline Visibility")
  15076. ("c" . org-cycle)
  15077. ("C" . org-shifttab)
  15078. (" " . org-display-outline-path)
  15079. ("Outline Structure Editing")
  15080. ("U" . org-shiftmetaup)
  15081. ("D" . org-shiftmetadown)
  15082. ("r" . org-metaright)
  15083. ("l" . org-metaleft)
  15084. ("R" . org-shiftmetaright)
  15085. ("L" . org-shiftmetaleft)
  15086. ("i" . (progn (forward-char 1) (call-interactively
  15087. 'org-insert-heading-respect-content)))
  15088. ("^" . org-sort)
  15089. ("w" . org-refile)
  15090. ("a" . org-archive-subtree-default-with-confirmation)
  15091. ("." . outline-mark-subtree)
  15092. ("Clock Commands")
  15093. ("I" . org-clock-in)
  15094. ("O" . org-clock-out)
  15095. ("Meta Data Editing")
  15096. ("t" . org-todo)
  15097. ("0" . (org-priority ?\ ))
  15098. ("1" . (org-priority ?A))
  15099. ("2" . (org-priority ?B))
  15100. ("3" . (org-priority ?C))
  15101. (";" . org-set-tags-command)
  15102. ("e" . org-set-effort)
  15103. ("Agenda Views etc")
  15104. ("v" . org-agenda)
  15105. ("/" . org-sparse-tree)
  15106. ("Misc")
  15107. ("o" . org-open-at-point)
  15108. ("?" . org-speed-command-help)
  15109. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15110. (">" . (org-agenda-remove-restriction-lock))
  15111. )
  15112. "The default speed commands.")
  15113. (defun org-print-speed-command (e)
  15114. (if (> (length (car e)) 1)
  15115. (progn
  15116. (princ "\n")
  15117. (princ (car e))
  15118. (princ "\n")
  15119. (princ (make-string (length (car e)) ?-))
  15120. (princ "\n"))
  15121. (princ (car e))
  15122. (princ " ")
  15123. (if (symbolp (cdr e))
  15124. (princ (symbol-name (cdr e)))
  15125. (prin1 (cdr e)))
  15126. (princ "\n")))
  15127. (defun org-speed-command-help ()
  15128. "Show the available speed commands."
  15129. (interactive)
  15130. (if (not org-use-speed-commands)
  15131. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15132. (with-output-to-temp-buffer "*Help*"
  15133. (princ "User-defined Speed commands\n===========================\n")
  15134. (mapc 'org-print-speed-command org-speed-commands-user)
  15135. (princ "\n")
  15136. (princ "Built-in Speed commands\n=======================\n")
  15137. (mapc 'org-print-speed-command org-speed-commands-default))
  15138. (with-current-buffer "*Help*"
  15139. (setq truncate-lines t))))
  15140. (defun org-speed-move-safe (cmd)
  15141. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15142. If not, return to the original position and throw an error."
  15143. (interactive)
  15144. (let ((pos (point)))
  15145. (call-interactively cmd)
  15146. (unless (and (bolp) (org-on-heading-p))
  15147. (goto-char pos)
  15148. (error "Boundary reached while executing %s" cmd))))
  15149. (defvar org-self-insert-command-undo-counter 0)
  15150. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15151. (defvar org-speed-command nil)
  15152. (defun org-self-insert-command (N)
  15153. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15154. If the cursor is in a table looking at whitespace, the whitespace is
  15155. overwritten, and the table is not marked as requiring realignment."
  15156. (interactive "p")
  15157. (cond
  15158. ((and org-use-speed-commands
  15159. (or (and (bolp) (looking-at outline-regexp))
  15160. (and (functionp org-use-speed-commands)
  15161. (funcall org-use-speed-commands)))
  15162. (setq
  15163. org-speed-command
  15164. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  15165. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  15166. (cond
  15167. ((commandp org-speed-command)
  15168. (setq this-command org-speed-command)
  15169. (call-interactively org-speed-command))
  15170. ((functionp org-speed-command)
  15171. (funcall org-speed-command))
  15172. ((and org-speed-command (listp org-speed-command))
  15173. (eval org-speed-command))
  15174. (t (let (org-use-speed-commands)
  15175. (call-interactively 'org-self-insert-command)))))
  15176. ((and
  15177. (org-table-p)
  15178. (progn
  15179. ;; check if we blank the field, and if that triggers align
  15180. (and (featurep 'org-table) org-table-auto-blank-field
  15181. (member last-command
  15182. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15183. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15184. ;; got extra space, this field does not determine column width
  15185. (let (org-table-may-need-update) (org-table-blank-field))
  15186. ;; no extra space, this field may determine column width
  15187. (org-table-blank-field)))
  15188. t)
  15189. (eq N 1)
  15190. (looking-at "[^|\n]* |"))
  15191. (let (org-table-may-need-update)
  15192. (goto-char (1- (match-end 0)))
  15193. (delete-backward-char 1)
  15194. (goto-char (match-beginning 0))
  15195. (self-insert-command N)))
  15196. (t
  15197. (setq org-table-may-need-update t)
  15198. (self-insert-command N)
  15199. (org-fix-tags-on-the-fly)
  15200. (if org-self-insert-cluster-for-undo
  15201. (if (not (eq last-command 'org-self-insert-command))
  15202. (setq org-self-insert-command-undo-counter 1)
  15203. (if (>= org-self-insert-command-undo-counter 20)
  15204. (setq org-self-insert-command-undo-counter 1)
  15205. (and (> org-self-insert-command-undo-counter 0)
  15206. buffer-undo-list
  15207. (not (cadr buffer-undo-list)) ; remove nil entry
  15208. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15209. (setq org-self-insert-command-undo-counter
  15210. (1+ org-self-insert-command-undo-counter))))))))
  15211. (defun org-fix-tags-on-the-fly ()
  15212. (when (and (equal (char-after (point-at-bol)) ?*)
  15213. (org-on-heading-p))
  15214. (org-align-tags-here org-tags-column)))
  15215. (defun org-delete-backward-char (N)
  15216. "Like `delete-backward-char', insert whitespace at field end in tables.
  15217. When deleting backwards, in tables this function will insert whitespace in
  15218. front of the next \"|\" separator, to keep the table aligned. The table will
  15219. still be marked for re-alignment if the field did fill the entire column,
  15220. because, in this case the deletion might narrow the column."
  15221. (interactive "p")
  15222. (if (and (org-table-p)
  15223. (eq N 1)
  15224. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15225. (looking-at ".*?|"))
  15226. (let ((pos (point))
  15227. (noalign (looking-at "[^|\n\r]* |"))
  15228. (c org-table-may-need-update))
  15229. (backward-delete-char N)
  15230. (skip-chars-forward "^|")
  15231. (insert " ")
  15232. (goto-char (1- pos))
  15233. ;; noalign: if there were two spaces at the end, this field
  15234. ;; does not determine the width of the column.
  15235. (if noalign (setq org-table-may-need-update c)))
  15236. (backward-delete-char N)
  15237. (org-fix-tags-on-the-fly)))
  15238. (defun org-delete-char (N)
  15239. "Like `delete-char', but insert whitespace at field end in tables.
  15240. When deleting characters, in tables this function will insert whitespace in
  15241. front of the next \"|\" separator, to keep the table aligned. The table will
  15242. still be marked for re-alignment if the field did fill the entire column,
  15243. because, in this case the deletion might narrow the column."
  15244. (interactive "p")
  15245. (if (and (org-table-p)
  15246. (not (bolp))
  15247. (not (= (char-after) ?|))
  15248. (eq N 1))
  15249. (if (looking-at ".*?|")
  15250. (let ((pos (point))
  15251. (noalign (looking-at "[^|\n\r]* |"))
  15252. (c org-table-may-need-update))
  15253. (replace-match (concat
  15254. (substring (match-string 0) 1 -1)
  15255. " |"))
  15256. (goto-char pos)
  15257. ;; noalign: if there were two spaces at the end, this field
  15258. ;; does not determine the width of the column.
  15259. (if noalign (setq org-table-may-need-update c)))
  15260. (delete-char N))
  15261. (delete-char N)
  15262. (org-fix-tags-on-the-fly)))
  15263. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15264. (put 'org-self-insert-command 'delete-selection t)
  15265. (put 'orgtbl-self-insert-command 'delete-selection t)
  15266. (put 'org-delete-char 'delete-selection 'supersede)
  15267. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15268. (put 'org-yank 'delete-selection 'yank)
  15269. ;; Make `flyspell-mode' delay after some commands
  15270. (put 'org-self-insert-command 'flyspell-delayed t)
  15271. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15272. (put 'org-delete-char 'flyspell-delayed t)
  15273. (put 'org-delete-backward-char 'flyspell-delayed t)
  15274. ;; Make pabbrev-mode expand after org-mode commands
  15275. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15276. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15277. ;; How to do this: Measure non-white length of current string
  15278. ;; If equal to column width, we should realign.
  15279. (defun org-remap (map &rest commands)
  15280. "In MAP, remap the functions given in COMMANDS.
  15281. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15282. (let (new old)
  15283. (while commands
  15284. (setq old (pop commands) new (pop commands))
  15285. (if (fboundp 'command-remapping)
  15286. (org-defkey map (vector 'remap old) new)
  15287. (substitute-key-definition old new map global-map)))))
  15288. (when (eq org-enable-table-editor 'optimized)
  15289. ;; If the user wants maximum table support, we need to hijack
  15290. ;; some standard editing functions
  15291. (org-remap org-mode-map
  15292. 'self-insert-command 'org-self-insert-command
  15293. 'delete-char 'org-delete-char
  15294. 'delete-backward-char 'org-delete-backward-char)
  15295. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15296. (defvar org-ctrl-c-ctrl-c-hook nil
  15297. "Hook for functions attaching themselves to `C-c C-c'.
  15298. This can be used to add additional functionality to the C-c C-c key which
  15299. executes context-dependent commands.
  15300. Each function will be called with no arguments. The function must check
  15301. if the context is appropriate for it to act. If yes, it should do its
  15302. thing and then return a non-nil value. If the context is wrong,
  15303. just do nothing and return nil.")
  15304. (defvar org-tab-first-hook nil
  15305. "Hook for functions to attach themselves to TAB.
  15306. See `org-ctrl-c-ctrl-c-hook' for more information.
  15307. This hook runs as the first action when TAB is pressed, even before
  15308. `org-cycle' messes around with the `outline-regexp' to cater for
  15309. inline tasks and plain list item folding.
  15310. If any function in this hook returns t, any other actions that
  15311. would have been caused by TAB (such as table field motion or visibility
  15312. cycling) will not occur.")
  15313. (defvar org-tab-after-check-for-table-hook nil
  15314. "Hook for functions to attach themselves to TAB.
  15315. See `org-ctrl-c-ctrl-c-hook' for more information.
  15316. This hook runs after it has been established that the cursor is not in a
  15317. table, but before checking if the cursor is in a headline or if global cycling
  15318. should be done.
  15319. If any function in this hook returns t, not other actions like visibility
  15320. cycling will be done.")
  15321. (defvar org-tab-after-check-for-cycling-hook nil
  15322. "Hook for functions to attach themselves to TAB.
  15323. See `org-ctrl-c-ctrl-c-hook' for more information.
  15324. This hook runs after it has been established that not table field motion and
  15325. not visibility should be done because of current context. This is probably
  15326. the place where a package like yasnippets can hook in.")
  15327. (defvar org-tab-before-tab-emulation-hook nil
  15328. "Hook for functions to attach themselves to TAB.
  15329. See `org-ctrl-c-ctrl-c-hook' for more information.
  15330. This hook runs after every other options for TAB have been exhausted, but
  15331. before indentation and \t insertion takes place.")
  15332. (defvar org-metaleft-hook nil
  15333. "Hook for functions attaching themselves to `M-left'.
  15334. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15335. (defvar org-metaright-hook nil
  15336. "Hook for functions attaching themselves to `M-right'.
  15337. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15338. (defvar org-metaup-hook nil
  15339. "Hook for functions attaching themselves to `M-up'.
  15340. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15341. (defvar org-metadown-hook nil
  15342. "Hook for functions attaching themselves to `M-down'.
  15343. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15344. (defvar org-shiftmetaleft-hook nil
  15345. "Hook for functions attaching themselves to `M-S-left'.
  15346. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15347. (defvar org-shiftmetaright-hook nil
  15348. "Hook for functions attaching themselves to `M-S-right'.
  15349. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15350. (defvar org-shiftmetaup-hook nil
  15351. "Hook for functions attaching themselves to `M-S-up'.
  15352. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15353. (defvar org-shiftmetadown-hook nil
  15354. "Hook for functions attaching themselves to `M-S-down'.
  15355. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15356. (defvar org-metareturn-hook nil
  15357. "Hook for functions attaching themselves to `M-RET'.
  15358. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15359. (defvar org-shiftup-hook nil
  15360. "Hook for functions attaching themselves to `S-up'.
  15361. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15362. (defvar org-shiftup-final-hook nil
  15363. "Hook for functions attaching themselves to `S-up'.
  15364. This one runs after all other options except shift-select have been excluded.
  15365. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15366. (defvar org-shiftdown-hook nil
  15367. "Hook for functions attaching themselves to `S-down'.
  15368. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15369. (defvar org-shiftdown-final-hook nil
  15370. "Hook for functions attaching themselves to `S-down'.
  15371. This one runs after all other options except shift-select have been excluded.
  15372. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15373. (defvar org-shiftleft-hook nil
  15374. "Hook for functions attaching themselves to `S-left'.
  15375. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15376. (defvar org-shiftleft-final-hook nil
  15377. "Hook for functions attaching themselves to `S-left'.
  15378. This one runs after all other options except shift-select have been excluded.
  15379. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15380. (defvar org-shiftright-hook nil
  15381. "Hook for functions attaching themselves to `S-right'.
  15382. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15383. (defvar org-shiftright-final-hook nil
  15384. "Hook for functions attaching themselves to `S-right'.
  15385. This one runs after all other options except shift-select have been excluded.
  15386. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15387. (defun org-modifier-cursor-error ()
  15388. "Throw an error, a modified cursor command was applied in wrong context."
  15389. (error "This command is active in special context like tables, headlines or items"))
  15390. (defun org-shiftselect-error ()
  15391. "Throw an error because Shift-Cursor command was applied in wrong context."
  15392. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15393. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15394. (error "This command works only in special context like headlines or timestamps")))
  15395. (defun org-call-for-shift-select (cmd)
  15396. (let ((this-command-keys-shift-translated t))
  15397. (call-interactively cmd)))
  15398. (defun org-shifttab (&optional arg)
  15399. "Global visibility cycling or move to previous table field.
  15400. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15401. on context.
  15402. See the individual commands for more information."
  15403. (interactive "P")
  15404. (cond
  15405. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15406. ((integerp arg)
  15407. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15408. (message "Content view to level: %d" arg)
  15409. (org-content (prefix-numeric-value arg2))
  15410. (setq org-cycle-global-status 'overview)))
  15411. (t (call-interactively 'org-global-cycle))))
  15412. (defun org-shiftmetaleft ()
  15413. "Promote subtree or delete table column.
  15414. Calls `org-promote-subtree', `org-outdent-item',
  15415. or `org-table-delete-column', depending on context.
  15416. See the individual commands for more information."
  15417. (interactive)
  15418. (cond
  15419. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15420. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15421. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15422. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15423. (t (org-modifier-cursor-error))))
  15424. (defun org-shiftmetaright ()
  15425. "Demote subtree or insert table column.
  15426. Calls `org-demote-subtree', `org-indent-item',
  15427. or `org-table-insert-column', depending on context.
  15428. See the individual commands for more information."
  15429. (interactive)
  15430. (cond
  15431. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15432. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15433. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15434. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15435. (t (org-modifier-cursor-error))))
  15436. (defun org-shiftmetaup (&optional arg)
  15437. "Move subtree up or kill table row.
  15438. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15439. `org-move-item-up' depending on context. See the individual commands
  15440. for more information."
  15441. (interactive "P")
  15442. (cond
  15443. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15444. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15445. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15446. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15447. (t (org-modifier-cursor-error))))
  15448. (defun org-shiftmetadown (&optional arg)
  15449. "Move subtree down or insert table row.
  15450. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15451. `org-move-item-down', depending on context. See the individual
  15452. commands for more information."
  15453. (interactive "P")
  15454. (cond
  15455. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15456. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15457. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15458. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15459. (t (org-modifier-cursor-error))))
  15460. (defsubst org-hidden-tree-error ()
  15461. (error
  15462. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15463. (defun org-metaleft (&optional arg)
  15464. "Promote heading or move table column to left.
  15465. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15466. With no specific context, calls the Emacs default `backward-word'.
  15467. See the individual commands for more information."
  15468. (interactive "P")
  15469. (cond
  15470. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15471. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15472. ((or (org-on-heading-p)
  15473. (and (org-region-active-p)
  15474. (save-excursion
  15475. (goto-char (region-beginning))
  15476. (org-on-heading-p))))
  15477. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15478. (call-interactively 'org-do-promote))
  15479. ((or (org-at-item-p)
  15480. (and (org-region-active-p)
  15481. (save-excursion
  15482. (goto-char (region-beginning))
  15483. (org-at-item-p))))
  15484. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15485. (call-interactively 'org-outdent-item))
  15486. (t (call-interactively 'backward-word))))
  15487. (defun org-metaright (&optional arg)
  15488. "Demote subtree or move table column to right.
  15489. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15490. With no specific context, calls the Emacs default `forward-word'.
  15491. See the individual commands for more information."
  15492. (interactive "P")
  15493. (cond
  15494. ((run-hook-with-args-until-success 'org-metaright-hook))
  15495. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15496. ((or (org-on-heading-p)
  15497. (and (org-region-active-p)
  15498. (save-excursion
  15499. (goto-char (region-beginning))
  15500. (org-on-heading-p))))
  15501. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15502. (call-interactively 'org-do-demote))
  15503. ((or (org-at-item-p)
  15504. (and (org-region-active-p)
  15505. (save-excursion
  15506. (goto-char (region-beginning))
  15507. (org-at-item-p))))
  15508. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15509. (call-interactively 'org-indent-item))
  15510. (t (call-interactively 'forward-word))))
  15511. (defun org-check-for-hidden (what)
  15512. "Check if there are hidden headlines/items in the current visual line.
  15513. WHAT can be either `headlines' or `items'. If the current line is
  15514. an outline or item heading and it has a folded subtree below it,
  15515. this function returns t, nil otherwise."
  15516. (let ((re (cond
  15517. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15518. ((eq what 'items) (concat "^" (org-item-re t)))
  15519. (t (error "This should not happen"))))
  15520. beg end)
  15521. (save-excursion
  15522. (catch 'exit
  15523. (unless (org-region-active-p)
  15524. (setq beg (point-at-bol))
  15525. (beginning-of-line 2)
  15526. (while (and (not (eobp)) ;; this is like `next-line'
  15527. (get-char-property (1- (point)) 'invisible))
  15528. (beginning-of-line 2))
  15529. (setq end (point))
  15530. (goto-char beg)
  15531. (goto-char (point-at-eol))
  15532. (setq end (max end (point)))
  15533. (while (re-search-forward re end t)
  15534. (if (get-char-property (match-beginning 0) 'invisible)
  15535. (throw 'exit t))))
  15536. nil))))
  15537. (defun org-metaup (&optional arg)
  15538. "Move subtree up or move table row up.
  15539. Calls `org-move-subtree-up' or `org-table-move-row' or
  15540. `org-move-item-up', depending on context. See the individual commands
  15541. for more information."
  15542. (interactive "P")
  15543. (cond
  15544. ((run-hook-with-args-until-success 'org-metaup-hook))
  15545. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15546. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15547. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15548. (t (transpose-lines 1) (beginning-of-line -1))))
  15549. (defun org-metadown (&optional arg)
  15550. "Move subtree down or move table row down.
  15551. Calls `org-move-subtree-down' or `org-table-move-row' or
  15552. `org-move-item-down', depending on context. See the individual
  15553. commands for more information."
  15554. (interactive "P")
  15555. (cond
  15556. ((run-hook-with-args-until-success 'org-metadown-hook))
  15557. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15558. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15559. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15560. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15561. (defun org-shiftup (&optional arg)
  15562. "Increase item in timestamp or increase priority of current headline.
  15563. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15564. depending on context. See the individual commands for more information."
  15565. (interactive "P")
  15566. (cond
  15567. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15568. ((and org-support-shift-select (org-region-active-p))
  15569. (org-call-for-shift-select 'previous-line))
  15570. ((org-at-timestamp-p t)
  15571. (call-interactively (if org-edit-timestamp-down-means-later
  15572. 'org-timestamp-down 'org-timestamp-up)))
  15573. ((and (not (eq org-support-shift-select 'always))
  15574. org-enable-priority-commands
  15575. (org-on-heading-p))
  15576. (call-interactively 'org-priority-up))
  15577. ((and (not org-support-shift-select) (org-at-item-p))
  15578. (call-interactively 'org-previous-item))
  15579. ((org-clocktable-try-shift 'up arg))
  15580. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15581. (org-support-shift-select
  15582. (org-call-for-shift-select 'previous-line))
  15583. (t (org-shiftselect-error))))
  15584. (defun org-shiftdown (&optional arg)
  15585. "Decrease item in timestamp or decrease priority of current headline.
  15586. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15587. depending on context. See the individual commands for more information."
  15588. (interactive "P")
  15589. (cond
  15590. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15591. ((and org-support-shift-select (org-region-active-p))
  15592. (org-call-for-shift-select 'next-line))
  15593. ((org-at-timestamp-p t)
  15594. (call-interactively (if org-edit-timestamp-down-means-later
  15595. 'org-timestamp-up 'org-timestamp-down)))
  15596. ((and (not (eq org-support-shift-select 'always))
  15597. org-enable-priority-commands
  15598. (org-on-heading-p))
  15599. (call-interactively 'org-priority-down))
  15600. ((and (not org-support-shift-select) (org-at-item-p))
  15601. (call-interactively 'org-next-item))
  15602. ((org-clocktable-try-shift 'down arg))
  15603. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15604. (org-support-shift-select
  15605. (org-call-for-shift-select 'next-line))
  15606. (t (org-shiftselect-error))))
  15607. (defun org-shiftright (&optional arg)
  15608. "Cycle the thing at point or in the current line, depending on context.
  15609. Depending on context, this does one of the following:
  15610. - switch a timestamp at point one day into the future
  15611. - on a headline, switch to the next TODO keyword.
  15612. - on an item, switch entire list to the next bullet type
  15613. - on a property line, switch to the next allowed value
  15614. - on a clocktable definition line, move time block into the future"
  15615. (interactive "P")
  15616. (cond
  15617. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15618. ((and org-support-shift-select (org-region-active-p))
  15619. (org-call-for-shift-select 'forward-char))
  15620. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15621. ((and (not (eq org-support-shift-select 'always))
  15622. (org-on-heading-p))
  15623. (let ((org-inhibit-logging
  15624. (not org-treat-S-cursor-todo-selection-as-state-change))
  15625. (org-inhibit-blocking
  15626. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15627. (org-call-with-arg 'org-todo 'right)))
  15628. ((or (and org-support-shift-select
  15629. (not (eq org-support-shift-select 'always))
  15630. (org-at-item-bullet-p))
  15631. (and (not org-support-shift-select) (org-at-item-p)))
  15632. (org-call-with-arg 'org-cycle-list-bullet nil))
  15633. ((and (not (eq org-support-shift-select 'always))
  15634. (org-at-property-p))
  15635. (call-interactively 'org-property-next-allowed-value))
  15636. ((org-clocktable-try-shift 'right arg))
  15637. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15638. (org-support-shift-select
  15639. (org-call-for-shift-select 'forward-char))
  15640. (t (org-shiftselect-error))))
  15641. (defun org-shiftleft (&optional arg)
  15642. "Cycle the thing at point or in the current line, depending on context.
  15643. Depending on context, this does one of the following:
  15644. - switch a timestamp at point one day into the past
  15645. - on a headline, switch to the previous TODO keyword.
  15646. - on an item, switch entire list to the previous bullet type
  15647. - on a property line, switch to the previous allowed value
  15648. - on a clocktable definition line, move time block into the past"
  15649. (interactive "P")
  15650. (cond
  15651. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15652. ((and org-support-shift-select (org-region-active-p))
  15653. (org-call-for-shift-select 'backward-char))
  15654. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15655. ((and (not (eq org-support-shift-select 'always))
  15656. (org-on-heading-p))
  15657. (let ((org-inhibit-logging
  15658. (not org-treat-S-cursor-todo-selection-as-state-change))
  15659. (org-inhibit-blocking
  15660. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15661. (org-call-with-arg 'org-todo 'left)))
  15662. ((or (and org-support-shift-select
  15663. (not (eq org-support-shift-select 'always))
  15664. (org-at-item-bullet-p))
  15665. (and (not org-support-shift-select) (org-at-item-p)))
  15666. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15667. ((and (not (eq org-support-shift-select 'always))
  15668. (org-at-property-p))
  15669. (call-interactively 'org-property-previous-allowed-value))
  15670. ((org-clocktable-try-shift 'left arg))
  15671. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15672. (org-support-shift-select
  15673. (org-call-for-shift-select 'backward-char))
  15674. (t (org-shiftselect-error))))
  15675. (defun org-shiftcontrolright ()
  15676. "Switch to next TODO set."
  15677. (interactive)
  15678. (cond
  15679. ((and org-support-shift-select (org-region-active-p))
  15680. (org-call-for-shift-select 'forward-word))
  15681. ((and (not (eq org-support-shift-select 'always))
  15682. (org-on-heading-p))
  15683. (org-call-with-arg 'org-todo 'nextset))
  15684. (org-support-shift-select
  15685. (org-call-for-shift-select 'forward-word))
  15686. (t (org-shiftselect-error))))
  15687. (defun org-shiftcontrolleft ()
  15688. "Switch to previous TODO set."
  15689. (interactive)
  15690. (cond
  15691. ((and org-support-shift-select (org-region-active-p))
  15692. (org-call-for-shift-select 'backward-word))
  15693. ((and (not (eq org-support-shift-select 'always))
  15694. (org-on-heading-p))
  15695. (org-call-with-arg 'org-todo 'previousset))
  15696. (org-support-shift-select
  15697. (org-call-for-shift-select 'backward-word))
  15698. (t (org-shiftselect-error))))
  15699. (defun org-ctrl-c-ret ()
  15700. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15701. (interactive)
  15702. (cond
  15703. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15704. (t (call-interactively 'org-insert-heading))))
  15705. (defun org-copy-special ()
  15706. "Copy region in table or copy current subtree.
  15707. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15708. See the individual commands for more information."
  15709. (interactive)
  15710. (call-interactively
  15711. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15712. (defun org-cut-special ()
  15713. "Cut region in table or cut current subtree.
  15714. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15715. See the individual commands for more information."
  15716. (interactive)
  15717. (call-interactively
  15718. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15719. (defun org-paste-special (arg)
  15720. "Paste rectangular region into table, or past subtree relative to level.
  15721. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15722. See the individual commands for more information."
  15723. (interactive "P")
  15724. (if (org-at-table-p)
  15725. (org-table-paste-rectangle)
  15726. (org-paste-subtree arg)))
  15727. (defun org-edit-special (&optional arg)
  15728. "Call a special editor for the stuff at point.
  15729. When at a table, call the formula editor with `org-table-edit-formulas'.
  15730. When at the first line of an src example, call `org-edit-src-code'.
  15731. When in an #+include line, visit the include file. Otherwise call
  15732. `ffap' to visit the file at point."
  15733. (interactive)
  15734. ;; possibly prep session before editing source
  15735. (when arg
  15736. (let* ((info (org-babel-get-src-block-info))
  15737. (lang (nth 0 info))
  15738. (params (nth 2 info))
  15739. (session (cdr (assoc :session params))))
  15740. (when (and info session) ;; we are in a source-code block with a session
  15741. (funcall
  15742. (intern (concat "org-babel-prep-session:" lang)) session params))))
  15743. (cond ;; proceed with `org-edit-special'
  15744. ((save-excursion
  15745. (beginning-of-line 1)
  15746. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15747. (find-file (org-trim (match-string 1))))
  15748. ((org-edit-src-code))
  15749. ((org-edit-fixed-width-region))
  15750. ((org-at-table.el-p)
  15751. (org-edit-src-code))
  15752. ((org-at-table-p)
  15753. (call-interactively 'org-table-edit-formulas))
  15754. (t (call-interactively 'ffap))))
  15755. (defun org-ctrl-c-ctrl-c (&optional arg)
  15756. "Set tags in headline, or update according to changed information at point.
  15757. This command does many different things, depending on context:
  15758. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15759. this is what we do.
  15760. - If the cursor is on a statistics cookie, update it.
  15761. - If the cursor is in a headline, prompt for tags and insert them
  15762. into the current line, aligned to `org-tags-column'. When called
  15763. with prefix arg, realign all tags in the current buffer.
  15764. - If the cursor is in one of the special #+KEYWORD lines, this
  15765. triggers scanning the buffer for these lines and updating the
  15766. information.
  15767. - If the cursor is inside a table, realign the table. This command
  15768. works even if the automatic table editor has been turned off.
  15769. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15770. the entire table.
  15771. - If the cursor is at a footnote reference or definition, jump to
  15772. the corresponding definition or references, respectively.
  15773. - If the cursor is a the beginning of a dynamic block, update it.
  15774. - If the current buffer is a capture buffer, close note and file it.
  15775. - If the cursor is on a <<<target>>>, update radio targets and
  15776. corresponding links in this buffer.
  15777. - If the cursor is on a numbered item in a plain list, renumber the
  15778. ordered list.
  15779. - If the cursor is on a checkbox, toggle it.
  15780. - If the cursor is on a code block, evaluate it. The variable
  15781. `org-confirm-babel-evaluate' can be used to control prompting
  15782. before code block evaluation, by default every code block
  15783. evaluation requires confirmation. Code block evaluation can be
  15784. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  15785. (interactive "P")
  15786. (let ((org-enable-table-editor t))
  15787. (cond
  15788. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15789. org-occur-highlights
  15790. org-latex-fragment-image-overlays)
  15791. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15792. (org-remove-occur-highlights)
  15793. (org-remove-latex-fragment-image-overlays)
  15794. (message "Temporary highlights/overlays removed from current buffer"))
  15795. ((and (local-variable-p 'org-finish-function (current-buffer))
  15796. (fboundp org-finish-function))
  15797. (funcall org-finish-function))
  15798. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15799. ((or (looking-at org-property-start-re)
  15800. (org-at-property-p))
  15801. (call-interactively 'org-property-action))
  15802. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15803. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15804. (or (org-on-heading-p) (org-at-item-p)))
  15805. (call-interactively 'org-update-statistics-cookies))
  15806. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15807. ((org-at-table.el-p)
  15808. (message "Use C-c ' to edit table.el tables"))
  15809. ((org-at-table-p)
  15810. (org-table-maybe-eval-formula)
  15811. (if arg
  15812. (call-interactively 'org-table-recalculate)
  15813. (org-table-maybe-recalculate-line))
  15814. (call-interactively 'org-table-align))
  15815. ((or (org-footnote-at-reference-p)
  15816. (org-footnote-at-definition-p))
  15817. (call-interactively 'org-footnote-action))
  15818. ((org-at-item-checkbox-p)
  15819. (call-interactively 'org-toggle-checkbox)
  15820. (org-list-send-list 'maybe))
  15821. ((org-at-item-p)
  15822. (if arg
  15823. (call-interactively 'org-toggle-checkbox)
  15824. (call-interactively 'org-maybe-renumber-ordered-list))
  15825. (org-list-send-list 'maybe))
  15826. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15827. ;; Dynamic block
  15828. (beginning-of-line 1)
  15829. (save-excursion (org-update-dblock)))
  15830. ((save-excursion
  15831. (beginning-of-line 1)
  15832. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15833. (cond
  15834. ((equal (match-string 1) "TBLFM")
  15835. ;; Recalculate the table before this line
  15836. (save-excursion
  15837. (beginning-of-line 1)
  15838. (skip-chars-backward " \r\n\t")
  15839. (if (org-at-table-p)
  15840. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15841. (t
  15842. (let ((org-inhibit-startup-visibility-stuff t)
  15843. (org-startup-align-all-tables nil))
  15844. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15845. (message "Local setup has been refreshed"))))
  15846. ((org-clock-update-time-maybe))
  15847. (t (error "C-c C-c can do nothing useful at this location")))))
  15848. (defun org-mode-restart ()
  15849. "Restart Org-mode, to scan again for special lines.
  15850. Also updates the keyword regular expressions."
  15851. (interactive)
  15852. (org-mode)
  15853. (message "Org-mode restarted"))
  15854. (defun org-kill-note-or-show-branches ()
  15855. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15856. (interactive)
  15857. (if (not org-finish-function)
  15858. (progn
  15859. (hide-subtree)
  15860. (call-interactively 'show-branches))
  15861. (let ((org-note-abort t))
  15862. (funcall org-finish-function))))
  15863. (defun org-return (&optional indent)
  15864. "Goto next table row or insert a newline.
  15865. Calls `org-table-next-row' or `newline', depending on context.
  15866. See the individual commands for more information."
  15867. (interactive)
  15868. (cond
  15869. ((bobp) (if indent (newline-and-indent) (newline)))
  15870. ((org-at-table-p)
  15871. (org-table-justify-field-maybe)
  15872. (call-interactively 'org-table-next-row))
  15873. ((and org-return-follows-link
  15874. (eq (get-text-property (point) 'face) 'org-link))
  15875. (call-interactively 'org-open-at-point))
  15876. ((and (org-at-heading-p)
  15877. (looking-at
  15878. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15879. (org-show-entry)
  15880. (end-of-line 1)
  15881. (newline))
  15882. (t (if indent (newline-and-indent) (newline)))))
  15883. (defun org-return-indent ()
  15884. "Goto next table row or insert a newline and indent.
  15885. Calls `org-table-next-row' or `newline-and-indent', depending on
  15886. context. See the individual commands for more information."
  15887. (interactive)
  15888. (org-return t))
  15889. (defun org-ctrl-c-star ()
  15890. "Compute table, or change heading status of lines.
  15891. Calls `org-table-recalculate' or `org-toggle-heading',
  15892. depending on context."
  15893. (interactive)
  15894. (cond
  15895. ((org-at-table-p)
  15896. (call-interactively 'org-table-recalculate))
  15897. (t
  15898. ;; Convert all lines in region to list items
  15899. (call-interactively 'org-toggle-heading))))
  15900. (defun org-ctrl-c-minus ()
  15901. "Insert separator line in table or modify bullet status of line.
  15902. Also turns a plain line or a region of lines into list items.
  15903. Calls `org-table-insert-hline', `org-toggle-item', or
  15904. `org-cycle-list-bullet', depending on context."
  15905. (interactive)
  15906. (cond
  15907. ((org-at-table-p)
  15908. (call-interactively 'org-table-insert-hline))
  15909. ((org-region-active-p)
  15910. (call-interactively 'org-toggle-item))
  15911. ((org-in-item-p)
  15912. (call-interactively 'org-cycle-list-bullet))
  15913. (t
  15914. (call-interactively 'org-toggle-item))))
  15915. (defun org-toggle-item ()
  15916. "Convert headings or normal lines to items, items to normal lines.
  15917. If there is no active region, only the current line is considered.
  15918. If the first line in the region is a headline, convert all headlines to items.
  15919. If the first line in the region is an item, convert all items to normal lines.
  15920. If the first line is normal text, add an item bullet to each line."
  15921. (interactive)
  15922. (let (l2 l beg end)
  15923. (if (org-region-active-p)
  15924. (setq beg (region-beginning) end (region-end))
  15925. (setq beg (point-at-bol)
  15926. end (min (1+ (point-at-eol)) (point-max))))
  15927. (save-excursion
  15928. (goto-char end)
  15929. (setq l2 (org-current-line))
  15930. (goto-char beg)
  15931. (beginning-of-line 1)
  15932. (setq l (1- (org-current-line)))
  15933. (if (org-at-item-p)
  15934. ;; We already have items, de-itemize
  15935. (while (< (setq l (1+ l)) l2)
  15936. (when (org-at-item-p)
  15937. (goto-char (match-beginning 2))
  15938. (delete-region (match-beginning 2) (match-end 2))
  15939. (and (looking-at "[ \t]+") (replace-match "")))
  15940. (beginning-of-line 2))
  15941. (if (org-on-heading-p)
  15942. ;; Headings, convert to items
  15943. (while (< (setq l (1+ l)) l2)
  15944. (if (looking-at org-outline-regexp)
  15945. (replace-match "- " t t))
  15946. (beginning-of-line 2))
  15947. ;; normal lines, turn them into items
  15948. (while (< (setq l (1+ l)) l2)
  15949. (unless (org-at-item-p)
  15950. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15951. (replace-match "\\1- \\2")))
  15952. (beginning-of-line 2)))))))
  15953. (defun org-toggle-heading (&optional nstars)
  15954. "Convert headings to normal text, or items or text to headings.
  15955. If there is no active region, only the current line is considered.
  15956. If the first line is a heading, remove the stars from all headlines
  15957. in the region.
  15958. If the first line is a plain list item, turn all plain list items
  15959. into headings.
  15960. If the first line is a normal line, turn each and every line in the
  15961. region into a heading.
  15962. When converting a line into a heading, the number of stars is chosen
  15963. such that the lines become children of the current entry. However,
  15964. when a prefix argument is given, its value determines the number of
  15965. stars to add."
  15966. (interactive "P")
  15967. (let (l2 l itemp beg end)
  15968. (if (org-region-active-p)
  15969. (setq beg (region-beginning) end (region-end))
  15970. (setq beg (point-at-bol)
  15971. end (min (1+ (point-at-eol)) (point-max))))
  15972. (save-excursion
  15973. (goto-char end)
  15974. (setq l2 (org-current-line))
  15975. (goto-char beg)
  15976. (beginning-of-line 1)
  15977. (setq l (1- (org-current-line)))
  15978. (if (org-on-heading-p)
  15979. ;; We already have headlines, de-star them
  15980. (while (< (setq l (1+ l)) l2)
  15981. (when (org-on-heading-p t)
  15982. (and (looking-at outline-regexp) (replace-match "")))
  15983. (beginning-of-line 2))
  15984. (setq itemp (org-at-item-p))
  15985. (let* ((stars
  15986. (if nstars
  15987. (make-string (prefix-numeric-value current-prefix-arg)
  15988. ?*)
  15989. (save-excursion
  15990. (if (re-search-backward org-complex-heading-regexp nil t)
  15991. (match-string 1) ""))))
  15992. (add-stars (cond (nstars "")
  15993. ((equal stars "") "*")
  15994. (org-odd-levels-only "**")
  15995. (t "*")))
  15996. (rpl (concat stars add-stars " ")))
  15997. (while (< (setq l (1+ l)) l2)
  15998. (if itemp
  15999. (and (org-at-item-p) (replace-match rpl t t))
  16000. (unless (org-on-heading-p)
  16001. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16002. (replace-match (concat rpl (match-string 2))))))
  16003. (beginning-of-line 2)))))))
  16004. (defun org-meta-return (&optional arg)
  16005. "Insert a new heading or wrap a region in a table.
  16006. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16007. See the individual commands for more information."
  16008. (interactive "P")
  16009. (cond
  16010. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16011. ((org-at-table-p)
  16012. (call-interactively 'org-table-wrap-region))
  16013. (t (call-interactively 'org-insert-heading))))
  16014. ;;; Menu entries
  16015. ;; Define the Org-mode menus
  16016. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16017. '("Tbl"
  16018. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16019. ["Next Field" org-cycle (org-at-table-p)]
  16020. ["Previous Field" org-shifttab (org-at-table-p)]
  16021. ["Next Row" org-return (org-at-table-p)]
  16022. "--"
  16023. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16024. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16025. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16026. "--"
  16027. ("Column"
  16028. ["Move Column Left" org-metaleft (org-at-table-p)]
  16029. ["Move Column Right" org-metaright (org-at-table-p)]
  16030. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16031. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16032. ("Row"
  16033. ["Move Row Up" org-metaup (org-at-table-p)]
  16034. ["Move Row Down" org-metadown (org-at-table-p)]
  16035. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16036. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16037. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16038. "--"
  16039. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16040. ("Rectangle"
  16041. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16042. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16043. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16044. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16045. "--"
  16046. ("Calculate"
  16047. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16048. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16049. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16050. "--"
  16051. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16052. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16053. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16054. "--"
  16055. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16056. "--"
  16057. ["Sum Column/Rectangle" org-table-sum
  16058. (or (org-at-table-p) (org-region-active-p))]
  16059. ["Which Column?" org-table-current-column (org-at-table-p)])
  16060. ["Debug Formulas"
  16061. org-table-toggle-formula-debugger
  16062. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16063. ["Show Col/Row Numbers"
  16064. org-table-toggle-coordinate-overlays
  16065. :style toggle
  16066. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16067. "--"
  16068. ["Create" org-table-create (and (not (org-at-table-p))
  16069. org-enable-table-editor)]
  16070. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16071. ["Import from File" org-table-import (not (org-at-table-p))]
  16072. ["Export to File" org-table-export (org-at-table-p)]
  16073. "--"
  16074. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16075. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16076. '("Org"
  16077. ("Show/Hide"
  16078. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16079. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16080. ["Sparse Tree..." org-sparse-tree t]
  16081. ["Reveal Context" org-reveal t]
  16082. ["Show All" show-all t]
  16083. "--"
  16084. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16085. "--"
  16086. ["New Heading" org-insert-heading t]
  16087. ("Navigate Headings"
  16088. ["Up" outline-up-heading t]
  16089. ["Next" outline-next-visible-heading t]
  16090. ["Previous" outline-previous-visible-heading t]
  16091. ["Next Same Level" outline-forward-same-level t]
  16092. ["Previous Same Level" outline-backward-same-level t]
  16093. "--"
  16094. ["Jump" org-goto t])
  16095. ("Edit Structure"
  16096. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16097. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16098. "--"
  16099. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16100. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16101. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16102. "--"
  16103. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16104. "--"
  16105. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16106. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16107. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16108. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16109. "--"
  16110. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16111. "--"
  16112. ["Convert to odd levels" org-convert-to-odd-levels t]
  16113. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16114. ("Editing"
  16115. ["Emphasis..." org-emphasize t]
  16116. ["Edit Source Example" org-edit-special t]
  16117. "--"
  16118. ["Footnote new/jump" org-footnote-action t]
  16119. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16120. ("Archive"
  16121. ["Archive (default method)" org-archive-subtree-default t]
  16122. "--"
  16123. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16124. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16125. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16126. )
  16127. "--"
  16128. ("Hyperlinks"
  16129. ["Store Link (Global)" org-store-link t]
  16130. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16131. ["Insert Link" org-insert-link t]
  16132. ["Follow Link" org-open-at-point t]
  16133. "--"
  16134. ["Next link" org-next-link t]
  16135. ["Previous link" org-previous-link t]
  16136. "--"
  16137. ["Descriptive Links"
  16138. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16139. :style radio
  16140. :selected (member '(org-link) buffer-invisibility-spec)]
  16141. ["Literal Links"
  16142. (progn
  16143. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16144. :style radio
  16145. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16146. "--"
  16147. ("TODO Lists"
  16148. ["TODO/DONE/-" org-todo t]
  16149. ("Select keyword"
  16150. ["Next keyword" org-shiftright (org-on-heading-p)]
  16151. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16152. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  16153. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16154. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16155. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16156. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16157. "--"
  16158. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16159. :selected org-enforce-todo-dependencies :style toggle :active t]
  16160. "Settings for tree at point"
  16161. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16162. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  16163. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16164. ["Do Children parallel" org-toggle-ordered-property :style radio
  16165. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  16166. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16167. "--"
  16168. ["Set Priority" org-priority t]
  16169. ["Priority Up" org-shiftup t]
  16170. ["Priority Down" org-shiftdown t]
  16171. "--"
  16172. ["Get news from all feeds" org-feed-update-all t]
  16173. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16174. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16175. ("TAGS and Properties"
  16176. ["Set Tags" org-set-tags-command t]
  16177. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16178. "--"
  16179. ["Set property" org-set-property t]
  16180. ["Column view of properties" org-columns t]
  16181. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16182. ("Dates and Scheduling"
  16183. ["Timestamp" org-time-stamp t]
  16184. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16185. ("Change Date"
  16186. ["1 Day Later" org-shiftright t]
  16187. ["1 Day Earlier" org-shiftleft t]
  16188. ["1 ... Later" org-shiftup t]
  16189. ["1 ... Earlier" org-shiftdown t])
  16190. ["Compute Time Range" org-evaluate-time-range t]
  16191. ["Schedule Item" org-schedule t]
  16192. ["Deadline" org-deadline t]
  16193. "--"
  16194. ["Custom time format" org-toggle-time-stamp-overlays
  16195. :style radio :selected org-display-custom-times]
  16196. "--"
  16197. ["Goto Calendar" org-goto-calendar t]
  16198. ["Date from Calendar" org-date-from-calendar t]
  16199. "--"
  16200. ["Start/Restart Timer" org-timer-start t]
  16201. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16202. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16203. ["Insert Timer String" org-timer t]
  16204. ["Insert Timer Item" org-timer-item t])
  16205. ("Logging work"
  16206. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16207. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16208. ["Clock out" org-clock-out t]
  16209. ["Clock cancel" org-clock-cancel t]
  16210. "--"
  16211. ["Mark as default task" org-clock-mark-default-task t]
  16212. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16213. ["Goto running clock" org-clock-goto t]
  16214. "--"
  16215. ["Display times" org-clock-display t]
  16216. ["Create clock table" org-clock-report t]
  16217. "--"
  16218. ["Record DONE time"
  16219. (progn (setq org-log-done (not org-log-done))
  16220. (message "Switching to %s will %s record a timestamp"
  16221. (car org-done-keywords)
  16222. (if org-log-done "automatically" "not")))
  16223. :style toggle :selected org-log-done])
  16224. "--"
  16225. ["Agenda Command..." org-agenda t]
  16226. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16227. ("File List for Agenda")
  16228. ("Special views current file"
  16229. ["TODO Tree" org-show-todo-tree t]
  16230. ["Check Deadlines" org-check-deadlines t]
  16231. ["Timeline" org-timeline t]
  16232. ["Tags/Property tree" org-match-sparse-tree t])
  16233. "--"
  16234. ["Export/Publish..." org-export t]
  16235. ("LaTeX"
  16236. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16237. :selected org-cdlatex-mode]
  16238. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16239. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16240. ["Modify math symbol" org-cdlatex-math-modify
  16241. (org-inside-LaTeX-fragment-p)]
  16242. ["Insert citation" org-reftex-citation t]
  16243. "--"
  16244. ["Template for BEAMER" org-insert-beamer-options-template t])
  16245. "--"
  16246. ("MobileOrg"
  16247. ["Push Files and Views" org-mobile-push t]
  16248. ["Get Captured and Flagged" org-mobile-pull t]
  16249. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16250. "--"
  16251. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16252. "--"
  16253. ("Documentation"
  16254. ["Show Version" org-version t]
  16255. ["Info Documentation" org-info t])
  16256. ("Customize"
  16257. ["Browse Org Group" org-customize t]
  16258. "--"
  16259. ["Expand This Menu" org-create-customize-menu
  16260. (fboundp 'customize-menu-create)])
  16261. ["Send bug report" org-submit-bug-report t]
  16262. "--"
  16263. ("Refresh/Reload"
  16264. ["Refresh setup current buffer" org-mode-restart t]
  16265. ["Reload Org (after update)" org-reload t]
  16266. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16267. ))
  16268. (defun org-info (&optional node)
  16269. "Read documentation for Org-mode in the info system.
  16270. With optional NODE, go directly to that node."
  16271. (interactive)
  16272. (info (format "(org)%s" (or node ""))))
  16273. ;;;###autoload
  16274. (defun org-submit-bug-report ()
  16275. "Submit a bug report on Org-mode via mail.
  16276. Don't hesitate to report any problems or inaccurate documentation.
  16277. If you don't have setup sending mail from (X)Emacs, please copy the
  16278. output buffer into your mail program, as it gives us important
  16279. information about your Org-mode version and configuration."
  16280. (interactive)
  16281. (require 'reporter)
  16282. (org-load-modules-maybe)
  16283. (org-require-autoloaded-modules)
  16284. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16285. (reporter-submit-bug-report
  16286. "emacs-orgmode@gnu.org"
  16287. (org-version)
  16288. (let (list)
  16289. (save-window-excursion
  16290. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16291. (delete-other-windows)
  16292. (erase-buffer)
  16293. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16294. We would like to add your full Org-mode and Outline configuration to the
  16295. bug report. This greatly simplifies the work of the maintainer and
  16296. other experts on the mailing list.
  16297. HOWEVER, some variables you have customized may contain private
  16298. information. The names of customers, colleagues, or friends, might
  16299. appear in the form of file names, tags, todo states, or search strings.
  16300. If you answer yes to the prompt, you might want to check and remove
  16301. such private information before sending the email.")
  16302. (add-text-properties (point-min) (point-max) '(face org-warning))
  16303. (when (yes-or-no-p "Include your Org-mode configuration ")
  16304. (mapatoms
  16305. (lambda (v)
  16306. (and (boundp v)
  16307. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16308. (or (and (symbol-value v)
  16309. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16310. (and
  16311. (get v 'custom-type) (get v 'standard-value)
  16312. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16313. (push v list)))))
  16314. (kill-buffer (get-buffer "*Warn about privacy*"))
  16315. list))
  16316. nil nil
  16317. "Remember to cover the basics, that is, what you expected to happen and
  16318. what in fact did happen. You don't know how to make a good report? See
  16319. http://orgmode.org/manual/Feedback.html#Feedback
  16320. Your bug report will be posted to the Org-mode mailing list.
  16321. ------------------------------------------------------------------------")
  16322. (save-excursion
  16323. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16324. (replace-match "\\1Bug: \\3 [\\2]")))))
  16325. (defun org-install-agenda-files-menu ()
  16326. (let ((bl (buffer-list)))
  16327. (save-excursion
  16328. (while bl
  16329. (set-buffer (pop bl))
  16330. (if (org-mode-p) (setq bl nil)))
  16331. (when (org-mode-p)
  16332. (easy-menu-change
  16333. '("Org") "File List for Agenda"
  16334. (append
  16335. (list
  16336. ["Edit File List" (org-edit-agenda-file-list) t]
  16337. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16338. ["Remove Current File from List" org-remove-file t]
  16339. ["Cycle through agenda files" org-cycle-agenda-files t]
  16340. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16341. "--")
  16342. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16343. ;;;; Documentation
  16344. ;;;###autoload
  16345. (defun org-require-autoloaded-modules ()
  16346. (interactive)
  16347. (mapc 'require
  16348. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16349. org-docbook org-exp org-html org-icalendar
  16350. org-id org-latex
  16351. org-publish org-remember org-table
  16352. org-timer org-xoxo)))
  16353. ;;;###autoload
  16354. (defun org-reload (&optional uncompiled)
  16355. "Reload all org lisp files.
  16356. With prefix arg UNCOMPILED, load the uncompiled versions."
  16357. (interactive "P")
  16358. (require 'find-func)
  16359. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16360. (dir-org (file-name-directory (org-find-library-name "org")))
  16361. (dir-org-contrib (ignore-errors
  16362. (file-name-directory
  16363. (org-find-library-name "org-contribdir"))))
  16364. (babel-files
  16365. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16366. (append (list nil "comint" "eval" "exp" "keys"
  16367. "lob" "ref" "table" "tangle")
  16368. (delq nil
  16369. (mapcar
  16370. (lambda (lang)
  16371. (when (cdr lang) (symbol-name (car lang))))
  16372. org-babel-load-languages)))))
  16373. (files
  16374. (append (directory-files dir-org t file-re)
  16375. babel-files
  16376. (and dir-org-contrib
  16377. (directory-files dir-org-contrib t file-re))))
  16378. (remove-re (concat (if (featurep 'xemacs)
  16379. "org-colview" "org-colview-xemacs")
  16380. "\\'")))
  16381. (setq files (mapcar 'file-name-sans-extension files))
  16382. (setq files (mapcar
  16383. (lambda (x) (if (string-match remove-re x) nil x))
  16384. files))
  16385. (setq files (delq nil files))
  16386. (mapc
  16387. (lambda (f)
  16388. (when (featurep (intern (file-name-nondirectory f)))
  16389. (if (and (not uncompiled)
  16390. (file-exists-p (concat f ".elc")))
  16391. (load (concat f ".elc") nil nil t)
  16392. (load (concat f ".el") nil nil t))))
  16393. files))
  16394. (org-version))
  16395. ;;;###autoload
  16396. (defun org-customize ()
  16397. "Call the customize function with org as argument."
  16398. (interactive)
  16399. (org-load-modules-maybe)
  16400. (org-require-autoloaded-modules)
  16401. (customize-browse 'org))
  16402. (defun org-create-customize-menu ()
  16403. "Create a full customization menu for Org-mode, insert it into the menu."
  16404. (interactive)
  16405. (org-load-modules-maybe)
  16406. (org-require-autoloaded-modules)
  16407. (if (fboundp 'customize-menu-create)
  16408. (progn
  16409. (easy-menu-change
  16410. '("Org") "Customize"
  16411. `(["Browse Org group" org-customize t]
  16412. "--"
  16413. ,(customize-menu-create 'org)
  16414. ["Set" Custom-set t]
  16415. ["Save" Custom-save t]
  16416. ["Reset to Current" Custom-reset-current t]
  16417. ["Reset to Saved" Custom-reset-saved t]
  16418. ["Reset to Standard Settings" Custom-reset-standard t]))
  16419. (message "\"Org\"-menu now contains full customization menu"))
  16420. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16421. ;;;; Miscellaneous stuff
  16422. ;;; Generally useful functions
  16423. (defun org-get-at-bol (property)
  16424. "Get text property PROPERTY at beginning of line."
  16425. (get-text-property (point-at-bol) property))
  16426. (defun org-find-text-property-in-string (prop s)
  16427. "Return the first non-nil value of property PROP in string S."
  16428. (or (get-text-property 0 prop s)
  16429. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16430. prop s)))
  16431. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16432. "Display the given MESSAGE as a warning."
  16433. (if (fboundp 'display-warning)
  16434. (display-warning 'org message
  16435. (if (featurep 'xemacs) 'warning :warning))
  16436. (let ((buf (get-buffer-create "*Org warnings*")))
  16437. (with-current-buffer buf
  16438. (goto-char (point-max))
  16439. (insert "Warning (Org): " message)
  16440. (unless (bolp)
  16441. (newline)))
  16442. (display-buffer buf)
  16443. (sit-for 0))))
  16444. (defun org-in-commented-line ()
  16445. "Is point in a line starting with `#'?"
  16446. (equal (char-after (point-at-bol)) ?#))
  16447. (defun org-in-indented-comment-line ()
  16448. "Is point in a line starting with `#' after some white space?"
  16449. (save-excursion
  16450. (save-match-data
  16451. (goto-char (point-at-bol))
  16452. (looking-at "[ \t]*#"))))
  16453. (defun org-in-verbatim-emphasis ()
  16454. (save-match-data
  16455. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16456. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16457. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16458. (if (and marker (marker-buffer marker)
  16459. (buffer-live-p (marker-buffer marker)))
  16460. (progn
  16461. (switch-to-buffer (marker-buffer marker))
  16462. (if (or (> marker (point-max)) (< marker (point-min)))
  16463. (widen))
  16464. (goto-char marker)
  16465. (org-show-context 'org-goto))
  16466. (if bookmark
  16467. (bookmark-jump bookmark)
  16468. (error "Cannot find location"))))
  16469. (defun org-quote-csv-field (s)
  16470. "Quote field for inclusion in CSV material."
  16471. (if (string-match "[\",]" s)
  16472. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16473. s))
  16474. (defun org-plist-delete (plist property)
  16475. "Delete PROPERTY from PLIST.
  16476. This is in contrast to merely setting it to 0."
  16477. (let (p)
  16478. (while plist
  16479. (if (not (eq property (car plist)))
  16480. (setq p (plist-put p (car plist) (nth 1 plist))))
  16481. (setq plist (cddr plist)))
  16482. p))
  16483. (defun org-force-self-insert (N)
  16484. "Needed to enforce self-insert under remapping."
  16485. (interactive "p")
  16486. (self-insert-command N))
  16487. (defun org-string-width (s)
  16488. "Compute width of string, ignoring invisible characters.
  16489. This ignores character with invisibility property `org-link', and also
  16490. characters with property `org-cwidth', because these will become invisible
  16491. upon the next fontification round."
  16492. (let (b l)
  16493. (when (or (eq t buffer-invisibility-spec)
  16494. (assq 'org-link buffer-invisibility-spec))
  16495. (while (setq b (text-property-any 0 (length s)
  16496. 'invisible 'org-link s))
  16497. (setq s (concat (substring s 0 b)
  16498. (substring s (or (next-single-property-change
  16499. b 'invisible s) (length s)))))))
  16500. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16501. (setq s (concat (substring s 0 b)
  16502. (substring s (or (next-single-property-change
  16503. b 'org-cwidth s) (length s))))))
  16504. (setq l (string-width s) b -1)
  16505. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16506. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16507. l))
  16508. (defun org-get-indentation (&optional line)
  16509. "Get the indentation of the current line, interpreting tabs.
  16510. When LINE is given, assume it represents a line and compute its indentation."
  16511. (if line
  16512. (if (string-match "^ *" (org-remove-tabs line))
  16513. (match-end 0))
  16514. (save-excursion
  16515. (beginning-of-line 1)
  16516. (skip-chars-forward " \t")
  16517. (current-column))))
  16518. (defun org-remove-tabs (s &optional width)
  16519. "Replace tabulators in S with spaces.
  16520. Assumes that s is a single line, starting in column 0."
  16521. (setq width (or width tab-width))
  16522. (while (string-match "\t" s)
  16523. (setq s (replace-match
  16524. (make-string
  16525. (- (* width (/ (+ (match-beginning 0) width) width))
  16526. (match-beginning 0)) ?\ )
  16527. t t s)))
  16528. s)
  16529. (defun org-fix-indentation (line ind)
  16530. "Fix indentation in LINE.
  16531. IND is a cons cell with target and minimum indentation.
  16532. If the current indentation in LINE is smaller than the minimum,
  16533. leave it alone. If it is larger than ind, set it to the target."
  16534. (let* ((l (org-remove-tabs line))
  16535. (i (org-get-indentation l))
  16536. (i1 (car ind)) (i2 (cdr ind)))
  16537. (if (>= i i2) (setq l (substring line i2)))
  16538. (if (> i1 0)
  16539. (concat (make-string i1 ?\ ) l)
  16540. l)))
  16541. (defun org-remove-indentation (code &optional n)
  16542. "Remove the maximum common indentation from the lines in CODE.
  16543. N may optionally be the number of spaces to remove."
  16544. (with-temp-buffer
  16545. (insert code)
  16546. (org-do-remove-indentation n)
  16547. (buffer-string)))
  16548. (defun org-do-remove-indentation (&optional n)
  16549. "Remove the maximum common indentation from the buffer."
  16550. (untabify (point-min) (point-max))
  16551. (let ((min 10000) re)
  16552. (if n
  16553. (setq min n)
  16554. (goto-char (point-min))
  16555. (while (re-search-forward "^ *[^ \n]" nil t)
  16556. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16557. (unless (or (= min 0) (= min 10000))
  16558. (setq re (format "^ \\{%d\\}" min))
  16559. (goto-char (point-min))
  16560. (while (re-search-forward re nil t)
  16561. (replace-match "")
  16562. (end-of-line 1))
  16563. min)))
  16564. (defun org-fill-template (template alist)
  16565. "Find each %key of ALIST in TEMPLATE and replace it."
  16566. (let ((case-fold-search nil)
  16567. entry key value)
  16568. (setq alist (sort (copy-sequence alist)
  16569. (lambda (a b) (< (length (car a)) (length (car b))))))
  16570. (while (setq entry (pop alist))
  16571. (setq template
  16572. (replace-regexp-in-string
  16573. (concat "%" (regexp-quote (car entry)))
  16574. (cdr entry) template t t)))
  16575. template))
  16576. (defun org-base-buffer (buffer)
  16577. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16578. (if (not buffer)
  16579. buffer
  16580. (or (buffer-base-buffer buffer)
  16581. buffer)))
  16582. (defun org-trim (s)
  16583. "Remove whitespace at beginning and end of string."
  16584. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16585. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16586. s)
  16587. (defun org-wrap (string &optional width lines)
  16588. "Wrap string to either a number of lines, or a width in characters.
  16589. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16590. that costs. If there is a word longer than WIDTH, the text is actually
  16591. wrapped to the length of that word.
  16592. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16593. many lines, whatever width that takes.
  16594. The return value is a list of lines, without newlines at the end."
  16595. (let* ((words (org-split-string string "[ \t\n]+"))
  16596. (maxword (apply 'max (mapcar 'org-string-width words)))
  16597. w ll)
  16598. (cond (width
  16599. (org-do-wrap words (max maxword width)))
  16600. (lines
  16601. (setq w maxword)
  16602. (setq ll (org-do-wrap words maxword))
  16603. (if (<= (length ll) lines)
  16604. ll
  16605. (setq ll words)
  16606. (while (> (length ll) lines)
  16607. (setq w (1+ w))
  16608. (setq ll (org-do-wrap words w)))
  16609. ll))
  16610. (t (error "Cannot wrap this")))))
  16611. (defun org-do-wrap (words width)
  16612. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16613. (let (lines line)
  16614. (while words
  16615. (setq line (pop words))
  16616. (while (and words (< (+ (length line) (length (car words))) width))
  16617. (setq line (concat line " " (pop words))))
  16618. (setq lines (push line lines)))
  16619. (nreverse lines)))
  16620. (defun org-split-string (string &optional separators)
  16621. "Splits STRING into substrings at SEPARATORS.
  16622. No empty strings are returned if there are matches at the beginning
  16623. and end of string."
  16624. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16625. (start 0)
  16626. notfirst
  16627. (list nil))
  16628. (while (and (string-match rexp string
  16629. (if (and notfirst
  16630. (= start (match-beginning 0))
  16631. (< start (length string)))
  16632. (1+ start) start))
  16633. (< (match-beginning 0) (length string)))
  16634. (setq notfirst t)
  16635. (or (eq (match-beginning 0) 0)
  16636. (and (eq (match-beginning 0) (match-end 0))
  16637. (eq (match-beginning 0) start))
  16638. (setq list
  16639. (cons (substring string start (match-beginning 0))
  16640. list)))
  16641. (setq start (match-end 0)))
  16642. (or (eq start (length string))
  16643. (setq list
  16644. (cons (substring string start)
  16645. list)))
  16646. (nreverse list)))
  16647. (defun org-quote-vert (s)
  16648. "Replace \"|\" with \"\\vert\"."
  16649. (while (string-match "|" s)
  16650. (setq s (replace-match "\\vert" t t s)))
  16651. s)
  16652. (defun org-uuidgen-p (s)
  16653. "Is S an ID created by UUIDGEN?"
  16654. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16655. (defun org-context ()
  16656. "Return a list of contexts of the current cursor position.
  16657. If several contexts apply, all are returned.
  16658. Each context entry is a list with a symbol naming the context, and
  16659. two positions indicating start and end of the context. Possible
  16660. contexts are:
  16661. :headline anywhere in a headline
  16662. :headline-stars on the leading stars in a headline
  16663. :todo-keyword on a TODO keyword (including DONE) in a headline
  16664. :tags on the TAGS in a headline
  16665. :priority on the priority cookie in a headline
  16666. :item on the first line of a plain list item
  16667. :item-bullet on the bullet/number of a plain list item
  16668. :checkbox on the checkbox in a plain list item
  16669. :table in an org-mode table
  16670. :table-special on a special filed in a table
  16671. :table-table in a table.el table
  16672. :link on a hyperlink
  16673. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16674. :target on a <<target>>
  16675. :radio-target on a <<<radio-target>>>
  16676. :latex-fragment on a LaTeX fragment
  16677. :latex-preview on a LaTeX fragment with overlayed preview image
  16678. This function expects the position to be visible because it uses font-lock
  16679. faces as a help to recognize the following contexts: :table-special, :link,
  16680. and :keyword."
  16681. (let* ((f (get-text-property (point) 'face))
  16682. (faces (if (listp f) f (list f)))
  16683. (p (point)) clist o)
  16684. ;; First the large context
  16685. (cond
  16686. ((org-on-heading-p t)
  16687. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16688. (when (progn
  16689. (beginning-of-line 1)
  16690. (looking-at org-todo-line-tags-regexp))
  16691. (push (org-point-in-group p 1 :headline-stars) clist)
  16692. (push (org-point-in-group p 2 :todo-keyword) clist)
  16693. (push (org-point-in-group p 4 :tags) clist))
  16694. (goto-char p)
  16695. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16696. (if (looking-at "\\[#[A-Z0-9]\\]")
  16697. (push (org-point-in-group p 0 :priority) clist)))
  16698. ((org-at-item-p)
  16699. (push (org-point-in-group p 2 :item-bullet) clist)
  16700. (push (list :item (point-at-bol)
  16701. (save-excursion (org-end-of-item) (point)))
  16702. clist)
  16703. (and (org-at-item-checkbox-p)
  16704. (push (org-point-in-group p 0 :checkbox) clist)))
  16705. ((org-at-table-p)
  16706. (push (list :table (org-table-begin) (org-table-end)) clist)
  16707. (if (memq 'org-formula faces)
  16708. (push (list :table-special
  16709. (previous-single-property-change p 'face)
  16710. (next-single-property-change p 'face)) clist)))
  16711. ((org-at-table-p 'any)
  16712. (push (list :table-table) clist)))
  16713. (goto-char p)
  16714. ;; Now the small context
  16715. (cond
  16716. ((org-at-timestamp-p)
  16717. (push (org-point-in-group p 0 :timestamp) clist))
  16718. ((memq 'org-link faces)
  16719. (push (list :link
  16720. (previous-single-property-change p 'face)
  16721. (next-single-property-change p 'face)) clist))
  16722. ((memq 'org-special-keyword faces)
  16723. (push (list :keyword
  16724. (previous-single-property-change p 'face)
  16725. (next-single-property-change p 'face)) clist))
  16726. ((org-on-target-p)
  16727. (push (org-point-in-group p 0 :target) clist)
  16728. (goto-char (1- (match-beginning 0)))
  16729. (if (looking-at org-radio-target-regexp)
  16730. (push (org-point-in-group p 0 :radio-target) clist))
  16731. (goto-char p))
  16732. ((setq o (car (delq nil
  16733. (mapcar
  16734. (lambda (x)
  16735. (if (memq x org-latex-fragment-image-overlays) x))
  16736. (overlays-at (point))))))
  16737. (push (list :latex-fragment
  16738. (overlay-start o) (overlay-end o)) clist)
  16739. (push (list :latex-preview
  16740. (overlay-start o) (overlay-end o)) clist))
  16741. ((org-inside-LaTeX-fragment-p)
  16742. ;; FIXME: positions wrong.
  16743. (push (list :latex-fragment (point) (point)) clist)))
  16744. (setq clist (nreverse (delq nil clist)))
  16745. clist))
  16746. ;; FIXME: Compare with at-regexp-p Do we need both?
  16747. (defun org-in-regexp (re &optional nlines visually)
  16748. "Check if point is inside a match of regexp.
  16749. Normally only the current line is checked, but you can include NLINES extra
  16750. lines both before and after point into the search.
  16751. If VISUALLY is set, require that the cursor is not after the match but
  16752. really on, so that the block visually is on the match."
  16753. (catch 'exit
  16754. (let ((pos (point))
  16755. (eol (point-at-eol (+ 1 (or nlines 0))))
  16756. (inc (if visually 1 0)))
  16757. (save-excursion
  16758. (beginning-of-line (- 1 (or nlines 0)))
  16759. (while (re-search-forward re eol t)
  16760. (if (and (<= (match-beginning 0) pos)
  16761. (>= (+ inc (match-end 0)) pos))
  16762. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16763. (defun org-at-regexp-p (regexp)
  16764. "Is point inside a match of REGEXP in the current line?"
  16765. (catch 'exit
  16766. (save-excursion
  16767. (let ((pos (point)) (end (point-at-eol)))
  16768. (beginning-of-line 1)
  16769. (while (re-search-forward regexp end t)
  16770. (if (and (<= (match-beginning 0) pos)
  16771. (>= (match-end 0) pos))
  16772. (throw 'exit t)))
  16773. nil))))
  16774. (defun org-in-regexps-block-p (start-re end-re)
  16775. "Return t if the current point is between matches of START-RE and END-RE.
  16776. This will also return to if point is on one of the two matches."
  16777. (interactive)
  16778. (let ((p (point)))
  16779. (save-excursion
  16780. (and (or (org-at-regexp-p start-re)
  16781. (re-search-backward start-re nil t))
  16782. (re-search-forward end-re nil t)
  16783. (>= (point) p)))))
  16784. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16785. "Call `multi-occur' with buffers for all agenda files."
  16786. (interactive "sOrg-files matching: \np")
  16787. (let* ((files (org-agenda-files))
  16788. (tnames (mapcar 'file-truename files))
  16789. (extra org-agenda-text-search-extra-files)
  16790. f)
  16791. (when (eq (car extra) 'agenda-archives)
  16792. (setq extra (cdr extra))
  16793. (setq files (org-add-archive-files files)))
  16794. (while (setq f (pop extra))
  16795. (unless (member (file-truename f) tnames)
  16796. (add-to-list 'files f 'append)
  16797. (add-to-list 'tnames (file-truename f) 'append)))
  16798. (multi-occur
  16799. (mapcar (lambda (x)
  16800. (with-current-buffer
  16801. (or (get-file-buffer x) (find-file-noselect x))
  16802. (widen)
  16803. (current-buffer)))
  16804. files)
  16805. regexp)))
  16806. (if (boundp 'occur-mode-find-occurrence-hook)
  16807. ;; Emacs 23
  16808. (add-hook 'occur-mode-find-occurrence-hook
  16809. (lambda ()
  16810. (when (org-mode-p)
  16811. (org-reveal))))
  16812. ;; Emacs 22
  16813. (defadvice occur-mode-goto-occurrence
  16814. (after org-occur-reveal activate)
  16815. (and (org-mode-p) (org-reveal)))
  16816. (defadvice occur-mode-goto-occurrence-other-window
  16817. (after org-occur-reveal activate)
  16818. (and (org-mode-p) (org-reveal)))
  16819. (defadvice occur-mode-display-occurrence
  16820. (after org-occur-reveal activate)
  16821. (when (org-mode-p)
  16822. (let ((pos (occur-mode-find-occurrence)))
  16823. (with-current-buffer (marker-buffer pos)
  16824. (save-excursion
  16825. (goto-char pos)
  16826. (org-reveal)))))))
  16827. (defun org-occur-link-in-agenda-files ()
  16828. "Create a link and search for it in the agendas.
  16829. The link is not stored in `org-stored-links', it is just created
  16830. for the search purpose."
  16831. (interactive)
  16832. (let ((link (condition-case nil
  16833. (org-store-link nil)
  16834. (error "Unable to create a link to here"))))
  16835. (org-occur-in-agenda-files (regexp-quote link))))
  16836. (defun org-uniquify (list)
  16837. "Remove duplicate elements from LIST."
  16838. (let (res)
  16839. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16840. res))
  16841. (defun org-delete-all (elts list)
  16842. "Remove all elements in ELTS from LIST."
  16843. (while elts
  16844. (setq list (delete (pop elts) list)))
  16845. list)
  16846. (defun org-count (cl-item cl-seq)
  16847. "Count the number of occurrences of ITEM in SEQ.
  16848. Taken from `count' in cl-seq.el with all keyword arguments removed."
  16849. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  16850. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  16851. (while (< cl-start cl-end)
  16852. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  16853. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  16854. (setq cl-start (1+ cl-start)))
  16855. cl-count))
  16856. (defun org-remove-if (predicate seq)
  16857. "Remove everything from SEQ that fulfills PREDICATE."
  16858. (let (res e)
  16859. (while seq
  16860. (setq e (pop seq))
  16861. (if (not (funcall predicate e)) (push e res)))
  16862. (nreverse res)))
  16863. (defun org-remove-if-not (predicate seq)
  16864. "Remove everything from SEQ that does not fulfill PREDICATE."
  16865. (let (res e)
  16866. (while seq
  16867. (setq e (pop seq))
  16868. (if (funcall predicate e) (push e res)))
  16869. (nreverse res)))
  16870. (defun org-back-over-empty-lines ()
  16871. "Move backwards over whitespace, to the beginning of the first empty line.
  16872. Returns the number of empty lines passed."
  16873. (let ((pos (point)))
  16874. (skip-chars-backward " \t\n\r")
  16875. (beginning-of-line 2)
  16876. (goto-char (min (point) pos))
  16877. (count-lines (point) pos)))
  16878. (defun org-skip-whitespace ()
  16879. (skip-chars-forward " \t\n\r"))
  16880. (defun org-point-in-group (point group &optional context)
  16881. "Check if POINT is in match-group GROUP.
  16882. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16883. match. If the match group does not exist or point is not inside it,
  16884. return nil."
  16885. (and (match-beginning group)
  16886. (>= point (match-beginning group))
  16887. (<= point (match-end group))
  16888. (if context
  16889. (list context (match-beginning group) (match-end group))
  16890. t)))
  16891. (defun org-switch-to-buffer-other-window (&rest args)
  16892. "Switch to buffer in a second window on the current frame.
  16893. In particular, do not allow pop-up frames.
  16894. Returns the newly created buffer."
  16895. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16896. special-display-function)
  16897. (apply 'switch-to-buffer-other-window args)))
  16898. (defun org-combine-plists (&rest plists)
  16899. "Create a single property list from all plists in PLISTS.
  16900. The process starts by copying the first list, and then setting properties
  16901. from the other lists. Settings in the last list are the most significant
  16902. ones and overrule settings in the other lists."
  16903. (let ((rtn (copy-sequence (pop plists)))
  16904. p v ls)
  16905. (while plists
  16906. (setq ls (pop plists))
  16907. (while ls
  16908. (setq p (pop ls) v (pop ls))
  16909. (setq rtn (plist-put rtn p v))))
  16910. rtn))
  16911. (defun org-move-line-down (arg)
  16912. "Move the current line down. With prefix argument, move it past ARG lines."
  16913. (interactive "p")
  16914. (let ((col (current-column))
  16915. beg end pos)
  16916. (beginning-of-line 1) (setq beg (point))
  16917. (beginning-of-line 2) (setq end (point))
  16918. (beginning-of-line (+ 1 arg))
  16919. (setq pos (move-marker (make-marker) (point)))
  16920. (insert (delete-and-extract-region beg end))
  16921. (goto-char pos)
  16922. (org-move-to-column col)))
  16923. (defun org-move-line-up (arg)
  16924. "Move the current line up. With prefix argument, move it past ARG lines."
  16925. (interactive "p")
  16926. (let ((col (current-column))
  16927. beg end pos)
  16928. (beginning-of-line 1) (setq beg (point))
  16929. (beginning-of-line 2) (setq end (point))
  16930. (beginning-of-line (- arg))
  16931. (setq pos (move-marker (make-marker) (point)))
  16932. (insert (delete-and-extract-region beg end))
  16933. (goto-char pos)
  16934. (org-move-to-column col)))
  16935. (defun org-replace-escapes (string table)
  16936. "Replace %-escapes in STRING with values in TABLE.
  16937. TABLE is an association list with keys like \"%a\" and string values.
  16938. The sequences in STRING may contain normal field width and padding information,
  16939. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16940. so values can contain further %-escapes if they are define later in TABLE."
  16941. (let ((tbl (copy-alist table))
  16942. (case-fold-search nil)
  16943. (pchg 0)
  16944. e re rpl)
  16945. (while (setq e (pop tbl))
  16946. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16947. (when (and (cdr e) (string-match re (cdr e)))
  16948. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16949. (safe "SREF"))
  16950. (add-text-properties 0 3 (list 'sref sref) safe)
  16951. (setcdr e (replace-match safe t t (cdr e)))))
  16952. (while (string-match re string)
  16953. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16954. (cdr e)))
  16955. (setq string (replace-match rpl t t string))))
  16956. (while (setq pchg (next-property-change pchg string))
  16957. (let ((sref (get-text-property pchg 'sref string)))
  16958. (when (and sref (string-match "SREF" string pchg))
  16959. (setq string (replace-match sref t t string)))))
  16960. string))
  16961. (defun org-sublist (list start end)
  16962. "Return a section of LIST, from START to END.
  16963. Counting starts at 1."
  16964. (let (rtn (c start))
  16965. (setq list (nthcdr (1- start) list))
  16966. (while (and list (<= c end))
  16967. (push (pop list) rtn)
  16968. (setq c (1+ c)))
  16969. (nreverse rtn)))
  16970. (defun org-find-base-buffer-visiting (file)
  16971. "Like `find-buffer-visiting' but always return the base buffer and
  16972. not an indirect buffer."
  16973. (let ((buf (or (get-file-buffer file)
  16974. (find-buffer-visiting file))))
  16975. (if buf
  16976. (or (buffer-base-buffer buf) buf)
  16977. nil)))
  16978. (defun org-image-file-name-regexp (&optional extensions)
  16979. "Return regexp matching the file names of images.
  16980. If EXTENSIONS is given, only match these."
  16981. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16982. (image-file-name-regexp)
  16983. (let ((image-file-name-extensions
  16984. (or extensions
  16985. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16986. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16987. (concat "\\."
  16988. (regexp-opt (nconc (mapcar 'upcase
  16989. image-file-name-extensions)
  16990. image-file-name-extensions)
  16991. t)
  16992. "\\'"))))
  16993. (defun org-file-image-p (file &optional extensions)
  16994. "Return non-nil if FILE is an image."
  16995. (save-match-data
  16996. (string-match (org-image-file-name-regexp extensions) file)))
  16997. (defun org-get-cursor-date ()
  16998. "Return the date at cursor in as a time.
  16999. This works in the calendar and in the agenda, anywhere else it just
  17000. returns the current time."
  17001. (let (date day defd)
  17002. (cond
  17003. ((eq major-mode 'calendar-mode)
  17004. (setq date (calendar-cursor-to-date)
  17005. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17006. ((eq major-mode 'org-agenda-mode)
  17007. (setq day (get-text-property (point) 'day))
  17008. (if day
  17009. (setq date (calendar-gregorian-from-absolute day)
  17010. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17011. (nth 2 date))))))
  17012. (or defd (current-time))))
  17013. (defvar org-agenda-action-marker (make-marker)
  17014. "Marker pointing to the entry for the next agenda action.")
  17015. (defun org-mark-entry-for-agenda-action ()
  17016. "Mark the current entry as target of an agenda action.
  17017. Agenda actions are actions executed from the agenda with the key `k',
  17018. which make use of the date at the cursor."
  17019. (interactive)
  17020. (move-marker org-agenda-action-marker
  17021. (save-excursion (org-back-to-heading t) (point))
  17022. (current-buffer))
  17023. (message
  17024. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17025. ;;; Paragraph filling stuff.
  17026. ;; We want this to be just right, so use the full arsenal.
  17027. (defun org-indent-line-function ()
  17028. "Indent line like previous, but further if previous was headline or item."
  17029. (interactive)
  17030. (let* ((pos (point))
  17031. (itemp (org-at-item-p))
  17032. (case-fold-search t)
  17033. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17034. column bpos bcol tpos tcol bullet btype bullet-type)
  17035. ;; Find the previous relevant line
  17036. (beginning-of-line 1)
  17037. (cond
  17038. ((looking-at "#") (setq column 0))
  17039. ((looking-at "\\*+ ") (setq column 0))
  17040. ((and (looking-at "[ \t]*:END:")
  17041. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17042. (save-excursion
  17043. (goto-char (1- (match-beginning 1)))
  17044. (setq column (current-column))))
  17045. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  17046. (save-excursion
  17047. (re-search-backward
  17048. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17049. (setq column (org-get-indentation (match-string 0))))
  17050. (t
  17051. (beginning-of-line 0)
  17052. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  17053. (not (looking-at "[ \t]*:END:"))
  17054. (not (looking-at org-drawer-regexp)))
  17055. (beginning-of-line 0))
  17056. (cond
  17057. ((looking-at "\\*+[ \t]+")
  17058. (if (not org-adapt-indentation)
  17059. (setq column 0)
  17060. (goto-char (match-end 0))
  17061. (setq column (current-column))))
  17062. ((looking-at org-drawer-regexp)
  17063. (goto-char (1- (match-beginning 1)))
  17064. (setq column (current-column)))
  17065. ((looking-at "\\([ \t]*\\):END:")
  17066. (goto-char (match-end 1))
  17067. (setq column (current-column)))
  17068. ((org-in-item-p)
  17069. (org-beginning-of-item)
  17070. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  17071. (setq bpos (match-beginning 1) tpos (match-end 0)
  17072. bcol (progn (goto-char bpos) (current-column))
  17073. tcol (progn (goto-char tpos) (current-column))
  17074. bullet (match-string 1)
  17075. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  17076. (if (> tcol (+ bcol org-description-max-indent))
  17077. (setq tcol (+ bcol 5)))
  17078. (if (not itemp)
  17079. (setq column tcol)
  17080. (goto-char pos)
  17081. (beginning-of-line 1)
  17082. (if (looking-at "\\S-")
  17083. (progn
  17084. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  17085. (setq bullet (match-string 1)
  17086. btype (if (string-match "[0-9]" bullet) "n" bullet))
  17087. (setq column (if (equal btype bullet-type) bcol tcol)))
  17088. (setq column (org-get-indentation)))))
  17089. (t (setq column (org-get-indentation))))))
  17090. (goto-char pos)
  17091. (if (<= (current-column) (current-indentation))
  17092. (org-indent-line-to column)
  17093. (save-excursion (org-indent-line-to column)))
  17094. (setq column (current-column))
  17095. (beginning-of-line 1)
  17096. (if (looking-at
  17097. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17098. (replace-match (concat (match-string 1)
  17099. (format org-property-format
  17100. (match-string 2) (match-string 3)))
  17101. t t))
  17102. (org-move-to-column column)))
  17103. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17104. "Variable to store copy of `adaptive-fill-regexp'.
  17105. Since `adaptive-fill-regexp' is set to never match, we need to
  17106. store a backup of its value before entering `org-mode' so that
  17107. the functionality can be provided as a fall-back.")
  17108. (defun org-set-autofill-regexps ()
  17109. (interactive)
  17110. ;; In the paragraph separator we include headlines, because filling
  17111. ;; text in a line directly attached to a headline would otherwise
  17112. ;; fill the headline as well.
  17113. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17114. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17115. ;; The paragraph starter includes hand-formatted lists.
  17116. (org-set-local
  17117. 'paragraph-start
  17118. (concat
  17119. "\f" "\\|"
  17120. "[ ]*$" "\\|"
  17121. "\\*+ " "\\|"
  17122. "[ \t]*#" "\\|"
  17123. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  17124. "[ \t]*[:|]" "\\|"
  17125. "\\$\\$" "\\|"
  17126. "\\\\\\(begin\\|end\\|[][]\\)"))
  17127. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17128. ;; But only if the user has not turned off tables or fixed-width regions
  17129. (org-set-local
  17130. 'auto-fill-inhibit-regexp
  17131. (concat "\\*+ \\|#\\+"
  17132. "\\|[ \t]*" org-keyword-time-regexp
  17133. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17134. (concat
  17135. "\\|[ \t]*["
  17136. (if org-enable-table-editor "|" "")
  17137. (if org-enable-fixed-width-editor ":" "")
  17138. "]"))))
  17139. ;; We use our own fill-paragraph function, to make sure that tables
  17140. ;; and fixed-width regions are not wrapped. That function will pass
  17141. ;; through to `fill-paragraph' when appropriate.
  17142. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17143. ;; Adaptive filling: To get full control, first make sure that
  17144. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17145. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17146. (org-set-local 'org-adaptive-fill-regexp-backup
  17147. adaptive-fill-regexp))
  17148. (org-set-local 'adaptive-fill-regexp "\000")
  17149. (org-set-local 'adaptive-fill-function
  17150. 'org-adaptive-fill-function)
  17151. (org-set-local
  17152. 'align-mode-rules-list
  17153. '((org-in-buffer-settings
  17154. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17155. (modes . '(org-mode))))))
  17156. (defun org-fill-paragraph (&optional justify)
  17157. "Re-align a table, pass through to fill-paragraph if no table."
  17158. (let ((table-p (org-at-table-p))
  17159. (table.el-p (org-at-table.el-p)))
  17160. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17161. (save-excursion (goto-char (point-at-bol))
  17162. (looking-at outline-regexp)))
  17163. t) ; skip headlines
  17164. (table.el-p t) ; skip table.el tables
  17165. (table-p (org-table-align) t) ; align org-mode tables
  17166. (t nil)))) ; call paragraph-fill
  17167. ;; For reference, this is the default value of adaptive-fill-regexp
  17168. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17169. (defun org-adaptive-fill-function ()
  17170. "Return a fill prefix for org-mode files.
  17171. In particular, this makes sure hanging paragraphs for hand-formatted lists
  17172. work correctly."
  17173. (cond
  17174. ;; Comment line
  17175. ((looking-at "#[ \t]+")
  17176. (match-string-no-properties 0))
  17177. ;; Description list
  17178. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  17179. (save-excursion
  17180. (if (> (match-end 1) (+ (match-beginning 1)
  17181. org-description-max-indent))
  17182. (goto-char (+ (match-beginning 1) 5))
  17183. (goto-char (match-end 0)))
  17184. (make-string (current-column) ?\ )))
  17185. ;; Ordered or unordered list
  17186. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  17187. (save-excursion
  17188. (goto-char (match-end 0))
  17189. (make-string (current-column) ?\ )))
  17190. ;; Other text
  17191. ((looking-at org-adaptive-fill-regexp-backup)
  17192. (match-string-no-properties 0))))
  17193. ;;; Other stuff.
  17194. (defun org-toggle-fixed-width-section (arg)
  17195. "Toggle the fixed-width export.
  17196. If there is no active region, the QUOTE keyword at the current headline is
  17197. inserted or removed. When present, it causes the text between this headline
  17198. and the next to be exported as fixed-width text, and unmodified.
  17199. If there is an active region, this command adds or removes a colon as the
  17200. first character of this line. If the first character of a line is a colon,
  17201. this line is also exported in fixed-width font."
  17202. (interactive "P")
  17203. (let* ((cc 0)
  17204. (regionp (org-region-active-p))
  17205. (beg (if regionp (region-beginning) (point)))
  17206. (end (if regionp (region-end)))
  17207. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17208. (case-fold-search nil)
  17209. (re "[ \t]*\\(: \\)")
  17210. off)
  17211. (if regionp
  17212. (save-excursion
  17213. (goto-char beg)
  17214. (setq cc (current-column))
  17215. (beginning-of-line 1)
  17216. (setq off (looking-at re))
  17217. (while (> nlines 0)
  17218. (setq nlines (1- nlines))
  17219. (beginning-of-line 1)
  17220. (cond
  17221. (arg
  17222. (org-move-to-column cc t)
  17223. (insert ": \n")
  17224. (forward-line -1))
  17225. ((and off (looking-at re))
  17226. (replace-match "" t t nil 1))
  17227. ((not off) (org-move-to-column cc t) (insert ": ")))
  17228. (forward-line 1)))
  17229. (save-excursion
  17230. (org-back-to-heading)
  17231. (if (looking-at (concat outline-regexp
  17232. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17233. (replace-match "" t t nil 1)
  17234. (if (looking-at outline-regexp)
  17235. (progn
  17236. (goto-char (match-end 0))
  17237. (insert org-quote-string " "))))))))
  17238. (defun org-reftex-citation ()
  17239. "Use reftex-citation to insert a citation into the buffer.
  17240. This looks for a line like
  17241. #+BIBLIOGRAPHY: foo plain option:-d
  17242. and derives from it that foo.bib is the bibliography file relevant
  17243. for this document. It then installs the necessary environment for RefTeX
  17244. to work in this buffer and calls `reftex-citation' to insert a citation
  17245. into the buffer.
  17246. Export of such citations to both LaTeX and HTML is handled by the contributed
  17247. package org-exp-bibtex by Taru Karttunen."
  17248. (interactive)
  17249. (let ((reftex-docstruct-symbol 'rds)
  17250. (reftex-cite-format "\\cite{%l}")
  17251. rds bib)
  17252. (save-excursion
  17253. (save-restriction
  17254. (widen)
  17255. (let ((case-fold-search t)
  17256. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17257. (if (not (save-excursion
  17258. (or (re-search-forward re nil t)
  17259. (re-search-backward re nil t))))
  17260. (error "No bibliography defined in file")
  17261. (setq bib (concat (match-string 1) ".bib")
  17262. rds (list (list 'bib bib)))))))
  17263. (call-interactively 'reftex-citation)))
  17264. ;;;; Functions extending outline functionality
  17265. (defun org-beginning-of-line (&optional arg)
  17266. "Go to the beginning of the current line. If that is invisible, continue
  17267. to a visible line beginning. This makes the function of C-a more intuitive.
  17268. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17269. first attempt, and only move to after the tags when the cursor is already
  17270. beyond the end of the headline."
  17271. (interactive "P")
  17272. (let ((pos (point))
  17273. (special (if (consp org-special-ctrl-a/e)
  17274. (car org-special-ctrl-a/e)
  17275. org-special-ctrl-a/e))
  17276. refpos)
  17277. (if (org-bound-and-true-p line-move-visual)
  17278. (beginning-of-visual-line 1)
  17279. (beginning-of-line 1))
  17280. (if (and arg (fboundp 'move-beginning-of-line))
  17281. (call-interactively 'move-beginning-of-line)
  17282. (if (bobp)
  17283. nil
  17284. (backward-char 1)
  17285. (if (org-truely-invisible-p)
  17286. (while (and (not (bobp)) (org-truely-invisible-p))
  17287. (backward-char 1)
  17288. (beginning-of-line 1))
  17289. (forward-char 1))))
  17290. (when special
  17291. (cond
  17292. ((and (looking-at org-complex-heading-regexp)
  17293. (= (char-after (match-end 1)) ?\ ))
  17294. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17295. (point-at-eol)))
  17296. (goto-char
  17297. (if (eq special t)
  17298. (cond ((> pos refpos) refpos)
  17299. ((= pos (point)) refpos)
  17300. (t (point)))
  17301. (cond ((> pos (point)) (point))
  17302. ((not (eq last-command this-command)) (point))
  17303. (t refpos)))))
  17304. ((org-at-item-p)
  17305. (goto-char
  17306. (if (eq special t)
  17307. (cond ((> pos (match-end 4)) (match-end 4))
  17308. ((= pos (point)) (match-end 4))
  17309. (t (point)))
  17310. (cond ((> pos (point)) (point))
  17311. ((not (eq last-command this-command)) (point))
  17312. (t (match-end 4))))))))
  17313. (org-no-warnings
  17314. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17315. (defun org-end-of-line (&optional arg)
  17316. "Go to the end of the line.
  17317. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17318. first attempt, and only move to after the tags when the cursor is already
  17319. beyond the end of the headline."
  17320. (interactive "P")
  17321. (let ((special (if (consp org-special-ctrl-a/e)
  17322. (cdr org-special-ctrl-a/e)
  17323. org-special-ctrl-a/e)))
  17324. (if (or (not special)
  17325. (not (org-on-heading-p))
  17326. arg)
  17327. (call-interactively
  17328. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17329. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17330. (t 'end-of-line)))
  17331. (let ((pos (point)))
  17332. (beginning-of-line 1)
  17333. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  17334. (if (eq special t)
  17335. (if (or (< pos (match-beginning 1))
  17336. (= pos (match-end 0)))
  17337. (goto-char (match-beginning 1))
  17338. (goto-char (match-end 0)))
  17339. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17340. (goto-char (match-end 0))
  17341. (goto-char (match-beginning 1))))
  17342. (call-interactively (if (fboundp 'move-end-of-line)
  17343. 'move-end-of-line
  17344. 'end-of-line)))))
  17345. (org-no-warnings
  17346. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17347. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17348. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17349. (define-key org-mode-map [home] 'org-beginning-of-line)
  17350. (define-key org-mode-map [end] 'org-end-of-line)
  17351. (defun org-backward-sentence (&optional arg)
  17352. "Go to beginning of sentence, or beginning of table field.
  17353. This will call `backward-sentence' or `org-table-beginning-of-field',
  17354. depending on context."
  17355. (interactive "P")
  17356. (cond
  17357. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17358. (t (call-interactively 'backward-sentence))))
  17359. (defun org-forward-sentence (&optional arg)
  17360. "Go to end of sentence, or end of table field.
  17361. This will call `forward-sentence' or `org-table-end-of-field',
  17362. depending on context."
  17363. (interactive "P")
  17364. (cond
  17365. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17366. (t (call-interactively 'forward-sentence))))
  17367. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17368. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17369. (defun org-kill-line (&optional arg)
  17370. "Kill line, to tags or end of line."
  17371. (interactive "P")
  17372. (cond
  17373. ((or (not org-special-ctrl-k)
  17374. (bolp)
  17375. (not (org-on-heading-p)))
  17376. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17377. org-ctrl-k-protect-subtree)
  17378. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17379. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17380. (error "C-k aborted - would kill hidden subtree")))
  17381. (call-interactively 'kill-line))
  17382. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  17383. (kill-region (point) (match-beginning 1))
  17384. (org-set-tags nil t))
  17385. (t (kill-region (point) (point-at-eol)))))
  17386. (define-key org-mode-map "\C-k" 'org-kill-line)
  17387. (defun org-yank (&optional arg)
  17388. "Yank. If the kill is a subtree, treat it specially.
  17389. This command will look at the current kill and check if is a single
  17390. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17391. cursor is at the beginning of a line or after the stars of a currently
  17392. empty headline, then the yank is handled specially. How exactly depends
  17393. on the value of the following variables, both set by default.
  17394. org-yank-folded-subtrees
  17395. When set, the subtree(s) will be folded after insertion, but only
  17396. if doing so would now swallow text after the yanked text.
  17397. org-yank-adjusted-subtrees
  17398. When set, the subtree will be promoted or demoted in order to
  17399. fit into the local outline tree structure, which means that the level
  17400. will be adjusted so that it becomes the smaller one of the two
  17401. *visible* surrounding headings.
  17402. Any prefix to this command will cause `yank' to be called directly with
  17403. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17404. will just
  17405. plainly yank the text as it is.
  17406. \[1] The test checks if the first non-white line is a heading
  17407. and if there are no other headings with fewer stars."
  17408. (interactive "P")
  17409. (org-yank-generic 'yank arg))
  17410. (defun org-yank-generic (command arg)
  17411. "Perform some yank-like command.
  17412. This function implements the behavior described in the `org-yank'
  17413. documentation. However, it has been generalized to work for any
  17414. interactive command with similar behavior."
  17415. ;; pretend to be command COMMAND
  17416. (setq this-command command)
  17417. (if arg
  17418. (call-interactively command)
  17419. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17420. (and (org-kill-is-subtree-p)
  17421. (or (bolp)
  17422. (and (looking-at "[ \t]*$")
  17423. (string-match
  17424. "\\`\\*+\\'"
  17425. (buffer-substring (point-at-bol) (point)))))))
  17426. swallowp)
  17427. (cond
  17428. ((and subtreep org-yank-folded-subtrees)
  17429. (let ((beg (point))
  17430. end)
  17431. (if (and subtreep org-yank-adjusted-subtrees)
  17432. (org-paste-subtree nil nil 'for-yank)
  17433. (call-interactively command))
  17434. (setq end (point))
  17435. (goto-char beg)
  17436. (when (and (bolp) subtreep
  17437. (not (setq swallowp
  17438. (org-yank-folding-would-swallow-text beg end))))
  17439. (or (looking-at outline-regexp)
  17440. (re-search-forward (concat "^" outline-regexp) end t))
  17441. (while (and (< (point) end) (looking-at outline-regexp))
  17442. (hide-subtree)
  17443. (org-cycle-show-empty-lines 'folded)
  17444. (condition-case nil
  17445. (outline-forward-same-level 1)
  17446. (error (goto-char end)))))
  17447. (when swallowp
  17448. (message
  17449. "Inserted text not folded because that would swallow text"))
  17450. (goto-char end)
  17451. (skip-chars-forward " \t\n\r")
  17452. (beginning-of-line 1)
  17453. (push-mark beg 'nomsg)))
  17454. ((and subtreep org-yank-adjusted-subtrees)
  17455. (let ((beg (point-at-bol)))
  17456. (org-paste-subtree nil nil 'for-yank)
  17457. (push-mark beg 'nomsg)))
  17458. (t
  17459. (call-interactively command))))))
  17460. (defun org-yank-folding-would-swallow-text (beg end)
  17461. "Would hide-subtree at BEG swallow any text after END?"
  17462. (let (level)
  17463. (save-excursion
  17464. (goto-char beg)
  17465. (when (or (looking-at outline-regexp)
  17466. (re-search-forward (concat "^" outline-regexp) end t))
  17467. (setq level (org-outline-level)))
  17468. (goto-char end)
  17469. (skip-chars-forward " \t\r\n\v\f")
  17470. (if (or (eobp)
  17471. (and (bolp) (looking-at org-outline-regexp)
  17472. (<= (org-outline-level) level)))
  17473. nil ; Nothing would be swallowed
  17474. t)))) ; something would swallow
  17475. (define-key org-mode-map "\C-y" 'org-yank)
  17476. (defun org-invisible-p ()
  17477. "Check if point is at a character currently not visible."
  17478. ;; Early versions of noutline don't have `outline-invisible-p'.
  17479. (if (fboundp 'outline-invisible-p)
  17480. (outline-invisible-p)
  17481. (get-char-property (point) 'invisible)))
  17482. (defun org-truely-invisible-p ()
  17483. "Check if point is at a character currently not visible.
  17484. This version does not only check the character property, but also
  17485. `visible-mode'."
  17486. ;; Early versions of noutline don't have `outline-invisible-p'.
  17487. (if (org-bound-and-true-p visible-mode)
  17488. nil
  17489. (if (fboundp 'outline-invisible-p)
  17490. (outline-invisible-p)
  17491. (get-char-property (point) 'invisible))))
  17492. (defun org-invisible-p2 ()
  17493. "Check if point is at a character currently not visible."
  17494. (save-excursion
  17495. (if (and (eolp) (not (bobp))) (backward-char 1))
  17496. ;; Early versions of noutline don't have `outline-invisible-p'.
  17497. (if (fboundp 'outline-invisible-p)
  17498. (outline-invisible-p)
  17499. (get-char-property (point) 'invisible))))
  17500. (defun org-back-to-heading (&optional invisible-ok)
  17501. "Call `outline-back-to-heading', but provide a better error message."
  17502. (condition-case nil
  17503. (outline-back-to-heading invisible-ok)
  17504. (error (error "Before first headline at position %d in buffer %s"
  17505. (point) (current-buffer)))))
  17506. (defun org-beginning-of-defun ()
  17507. "Go to the beginning of the subtree, i.e. back to the heading."
  17508. (org-back-to-heading))
  17509. (defun org-end-of-defun ()
  17510. "Go to the end of the subtree."
  17511. (org-end-of-subtree nil t))
  17512. (defun org-before-first-heading-p ()
  17513. "Before first heading?"
  17514. (save-excursion
  17515. (null (re-search-backward "^\\*+ " nil t))))
  17516. (defun org-on-heading-p (&optional ignored)
  17517. (outline-on-heading-p t))
  17518. (defun org-at-heading-p (&optional ignored)
  17519. (outline-on-heading-p t))
  17520. (defun org-point-at-end-of-empty-headline ()
  17521. "If point is at the end of an empty headline, return t, else nil.
  17522. If the heading only contains a TODO keyword, it is still still considered
  17523. empty."
  17524. (and (looking-at "[ \t]*$")
  17525. (save-excursion
  17526. (beginning-of-line 1)
  17527. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17528. "\\)?[ \t]*$")))))
  17529. (defun org-at-heading-or-item-p ()
  17530. (or (org-on-heading-p) (org-at-item-p)))
  17531. (defun org-on-target-p ()
  17532. (or (org-in-regexp org-radio-target-regexp)
  17533. (org-in-regexp org-target-regexp)))
  17534. (defun org-up-heading-all (arg)
  17535. "Move to the heading line of which the present line is a subheading.
  17536. This function considers both visible and invisible heading lines.
  17537. With argument, move up ARG levels."
  17538. (if (fboundp 'outline-up-heading-all)
  17539. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17540. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17541. (defun org-up-heading-safe ()
  17542. "Move to the heading line of which the present line is a subheading.
  17543. This version will not throw an error. It will return the level of the
  17544. headline found, or nil if no higher level is found.
  17545. Also, this function will be a lot faster than `outline-up-heading',
  17546. because it relies on stars being the outline starters. This can really
  17547. make a significant difference in outlines with very many siblings."
  17548. (let (start-level re)
  17549. (org-back-to-heading t)
  17550. (setq start-level (funcall outline-level))
  17551. (if (equal start-level 1)
  17552. nil
  17553. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17554. (if (re-search-backward re nil t)
  17555. (funcall outline-level)))))
  17556. (defun org-first-sibling-p ()
  17557. "Is this heading the first child of its parents?"
  17558. (interactive)
  17559. (let ((re (concat "^" outline-regexp))
  17560. level l)
  17561. (unless (org-at-heading-p t)
  17562. (error "Not at a heading"))
  17563. (setq level (funcall outline-level))
  17564. (save-excursion
  17565. (if (not (re-search-backward re nil t))
  17566. t
  17567. (setq l (funcall outline-level))
  17568. (< l level)))))
  17569. (defun org-goto-sibling (&optional previous)
  17570. "Goto the next sibling, even if it is invisible.
  17571. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17572. when a sibling was found. When none is found, return nil and don't
  17573. move point."
  17574. (let ((fun (if previous 're-search-backward 're-search-forward))
  17575. (pos (point))
  17576. (re (concat "^" outline-regexp))
  17577. level l)
  17578. (when (condition-case nil (org-back-to-heading t) (error nil))
  17579. (setq level (funcall outline-level))
  17580. (catch 'exit
  17581. (or previous (forward-char 1))
  17582. (while (funcall fun re nil t)
  17583. (setq l (funcall outline-level))
  17584. (when (< l level) (goto-char pos) (throw 'exit nil))
  17585. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17586. (goto-char pos)
  17587. nil))))
  17588. (defun org-show-siblings ()
  17589. "Show all siblings of the current headline."
  17590. (save-excursion
  17591. (while (org-goto-sibling) (org-flag-heading nil)))
  17592. (save-excursion
  17593. (while (org-goto-sibling 'previous)
  17594. (org-flag-heading nil))))
  17595. (defun org-show-hidden-entry ()
  17596. "Show an entry where even the heading is hidden."
  17597. (save-excursion
  17598. (org-show-entry)))
  17599. (defun org-flag-heading (flag &optional entry)
  17600. "Flag the current heading. FLAG non-nil means make invisible.
  17601. When ENTRY is non-nil, show the entire entry."
  17602. (save-excursion
  17603. (org-back-to-heading t)
  17604. ;; Check if we should show the entire entry
  17605. (if entry
  17606. (progn
  17607. (org-show-entry)
  17608. (save-excursion
  17609. (and (outline-next-heading)
  17610. (org-flag-heading nil))))
  17611. (outline-flag-region (max (point-min) (1- (point)))
  17612. (save-excursion (outline-end-of-heading) (point))
  17613. flag))))
  17614. (defun org-get-next-sibling ()
  17615. "Move to next heading of the same level, and return point.
  17616. If there is no such heading, return nil.
  17617. This is like outline-next-sibling, but invisible headings are ok."
  17618. (let ((level (funcall outline-level)))
  17619. (outline-next-heading)
  17620. (while (and (not (eobp)) (> (funcall outline-level) level))
  17621. (outline-next-heading))
  17622. (if (or (eobp) (< (funcall outline-level) level))
  17623. nil
  17624. (point))))
  17625. (defun org-get-last-sibling ()
  17626. "Move to previous heading of the same level, and return point.
  17627. If there is no such heading, return nil."
  17628. (let ((opoint (point))
  17629. (level (funcall outline-level)))
  17630. (outline-previous-heading)
  17631. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17632. (while (and (> (funcall outline-level) level)
  17633. (not (bobp)))
  17634. (outline-previous-heading))
  17635. (if (< (funcall outline-level) level)
  17636. nil
  17637. (point)))))
  17638. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17639. ;; This contains an exact copy of the original function, but it uses
  17640. ;; `org-back-to-heading', to make it work also in invisible
  17641. ;; trees. And is uses an invisible-OK argument.
  17642. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17643. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17644. ;; with many children and grandchildren etc, this can be much faster
  17645. ;; than the outline version.
  17646. (org-back-to-heading invisible-OK)
  17647. (let ((first t)
  17648. (level (funcall outline-level)))
  17649. (if (and (org-mode-p) (< level 1000))
  17650. ;; A true heading (not a plain list item), in Org-mode
  17651. ;; This means we can easily find the end by looking
  17652. ;; only for the right number of stars. Using a regexp to do
  17653. ;; this is so much faster than using a Lisp loop.
  17654. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17655. (forward-char 1)
  17656. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17657. ;; something else, do it the slow way
  17658. (while (and (not (eobp))
  17659. (or first (> (funcall outline-level) level)))
  17660. (setq first nil)
  17661. (outline-next-heading)))
  17662. (unless to-heading
  17663. (if (memq (preceding-char) '(?\n ?\^M))
  17664. (progn
  17665. ;; Go to end of line before heading
  17666. (forward-char -1)
  17667. (if (memq (preceding-char) '(?\n ?\^M))
  17668. ;; leave blank line before heading
  17669. (forward-char -1))))))
  17670. (point))
  17671. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17672. "Use Org version in org-mode, for dramatic speed-up."
  17673. (if (eq major-mode 'org-mode)
  17674. (progn
  17675. (org-end-of-subtree nil t)
  17676. (unless (eobp) (backward-char 1)))
  17677. ad-do-it))
  17678. (defun org-forward-same-level (arg &optional invisible-ok)
  17679. "Move forward to the arg'th subheading at same level as this one.
  17680. Stop at the first and last subheadings of a superior heading."
  17681. (interactive "p")
  17682. (org-back-to-heading invisible-ok)
  17683. (org-on-heading-p)
  17684. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17685. (re (format "^\\*\\{1,%d\\} " level))
  17686. l)
  17687. (forward-char 1)
  17688. (while (> arg 0)
  17689. (while (and (re-search-forward re nil 'move)
  17690. (setq l (- (match-end 0) (match-beginning 0) 1))
  17691. (= l level)
  17692. (not invisible-ok)
  17693. (progn (backward-char 1) (org-invisible-p)))
  17694. (if (< l level) (setq arg 1)))
  17695. (setq arg (1- arg)))
  17696. (beginning-of-line 1)))
  17697. (defun org-backward-same-level (arg &optional invisible-ok)
  17698. "Move backward to the arg'th subheading at same level as this one.
  17699. Stop at the first and last subheadings of a superior heading."
  17700. (interactive "p")
  17701. (org-back-to-heading)
  17702. (org-on-heading-p)
  17703. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17704. (re (format "^\\*\\{1,%d\\} " level))
  17705. l)
  17706. (while (> arg 0)
  17707. (while (and (re-search-backward re nil 'move)
  17708. (setq l (- (match-end 0) (match-beginning 0) 1))
  17709. (= l level)
  17710. (not invisible-ok)
  17711. (org-invisible-p))
  17712. (if (< l level) (setq arg 1)))
  17713. (setq arg (1- arg)))))
  17714. (defun org-show-subtree ()
  17715. "Show everything after this heading at deeper levels."
  17716. (outline-flag-region
  17717. (point)
  17718. (save-excursion
  17719. (org-end-of-subtree t t))
  17720. nil))
  17721. (defun org-show-entry ()
  17722. "Show the body directly following this heading.
  17723. Show the heading too, if it is currently invisible."
  17724. (interactive)
  17725. (save-excursion
  17726. (condition-case nil
  17727. (progn
  17728. (org-back-to-heading t)
  17729. (outline-flag-region
  17730. (max (point-min) (1- (point)))
  17731. (save-excursion
  17732. (if (re-search-forward
  17733. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17734. (match-beginning 1)
  17735. (point-max)))
  17736. nil)
  17737. (org-cycle-hide-drawers 'children))
  17738. (error nil))))
  17739. (defun org-make-options-regexp (kwds &optional extra)
  17740. "Make a regular expression for keyword lines."
  17741. (concat
  17742. "^"
  17743. "#?[ \t]*\\+\\("
  17744. (mapconcat 'regexp-quote kwds "\\|")
  17745. (if extra (concat "\\|" extra))
  17746. "\\):[ \t]*"
  17747. "\\(.*\\)"))
  17748. ;; Make isearch reveal the necessary context
  17749. (defun org-isearch-end ()
  17750. "Reveal context after isearch exits."
  17751. (when isearch-success ; only if search was successful
  17752. (if (featurep 'xemacs)
  17753. ;; Under XEmacs, the hook is run in the correct place,
  17754. ;; we directly show the context.
  17755. (org-show-context 'isearch)
  17756. ;; In Emacs the hook runs *before* restoring the overlays.
  17757. ;; So we have to use a one-time post-command-hook to do this.
  17758. ;; (Emacs 22 has a special variable, see function `org-mode')
  17759. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17760. isearch-mode-end-hook-quit)
  17761. ;; Only when the isearch was not quitted.
  17762. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17763. 'append 'local)))))
  17764. (defun org-isearch-post-command ()
  17765. "Remove self from hook, and show context."
  17766. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17767. (org-show-context 'isearch))
  17768. ;;;; Integration with and fixes for other packages
  17769. ;;; Imenu support
  17770. (defvar org-imenu-markers nil
  17771. "All markers currently used by Imenu.")
  17772. (make-variable-buffer-local 'org-imenu-markers)
  17773. (defun org-imenu-new-marker (&optional pos)
  17774. "Return a new marker for use by Imenu, and remember the marker."
  17775. (let ((m (make-marker)))
  17776. (move-marker m (or pos (point)))
  17777. (push m org-imenu-markers)
  17778. m))
  17779. (defun org-imenu-get-tree ()
  17780. "Produce the index for Imenu."
  17781. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17782. (setq org-imenu-markers nil)
  17783. (let* ((n org-imenu-depth)
  17784. (re (concat "^" outline-regexp))
  17785. (subs (make-vector (1+ n) nil))
  17786. (last-level 0)
  17787. m level head)
  17788. (save-excursion
  17789. (save-restriction
  17790. (widen)
  17791. (goto-char (point-max))
  17792. (while (re-search-backward re nil t)
  17793. (setq level (org-reduced-level (funcall outline-level)))
  17794. (when (<= level n)
  17795. (looking-at org-complex-heading-regexp)
  17796. (setq head (org-link-display-format
  17797. (org-match-string-no-properties 4))
  17798. m (org-imenu-new-marker))
  17799. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17800. (if (>= level last-level)
  17801. (push (cons head m) (aref subs level))
  17802. (push (cons head (aref subs (1+ level))) (aref subs level))
  17803. (loop for i from (1+ level) to n do (aset subs i nil)))
  17804. (setq last-level level)))))
  17805. (aref subs 1)))
  17806. (eval-after-load "imenu"
  17807. '(progn
  17808. (add-hook 'imenu-after-jump-hook
  17809. (lambda ()
  17810. (if (eq major-mode 'org-mode)
  17811. (org-show-context 'org-goto))))))
  17812. (defun org-link-display-format (link)
  17813. "Replace a link with either the description, or the link target
  17814. if no description is present"
  17815. (save-match-data
  17816. (if (string-match org-bracket-link-analytic-regexp link)
  17817. (replace-match (if (match-end 5)
  17818. (match-string 5 link)
  17819. (concat (match-string 1 link)
  17820. (match-string 3 link)))
  17821. nil t link)
  17822. link)))
  17823. ;; Speedbar support
  17824. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17825. "Overlay marking the agenda restriction line in speedbar.")
  17826. (overlay-put org-speedbar-restriction-lock-overlay
  17827. 'face 'org-agenda-restriction-lock)
  17828. (overlay-put org-speedbar-restriction-lock-overlay
  17829. 'help-echo "Agendas are currently limited to this item.")
  17830. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17831. (defun org-speedbar-set-agenda-restriction ()
  17832. "Restrict future agenda commands to the location at point in speedbar.
  17833. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17834. (interactive)
  17835. (require 'org-agenda)
  17836. (let (p m tp np dir txt)
  17837. (cond
  17838. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17839. 'org-imenu t))
  17840. (setq m (get-text-property p 'org-imenu-marker))
  17841. (with-current-buffer (marker-buffer m)
  17842. (goto-char m)
  17843. (org-agenda-set-restriction-lock 'subtree)))
  17844. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17845. 'speedbar-function 'speedbar-find-file))
  17846. (setq tp (previous-single-property-change
  17847. (1+ p) 'speedbar-function)
  17848. np (next-single-property-change
  17849. tp 'speedbar-function)
  17850. dir (speedbar-line-directory)
  17851. txt (buffer-substring-no-properties (or tp (point-min))
  17852. (or np (point-max))))
  17853. (with-current-buffer (find-file-noselect
  17854. (let ((default-directory dir))
  17855. (expand-file-name txt)))
  17856. (unless (org-mode-p)
  17857. (error "Cannot restrict to non-Org-mode file"))
  17858. (org-agenda-set-restriction-lock 'file)))
  17859. (t (error "Don't know how to restrict Org-mode's agenda")))
  17860. (move-overlay org-speedbar-restriction-lock-overlay
  17861. (point-at-bol) (point-at-eol))
  17862. (setq current-prefix-arg nil)
  17863. (org-agenda-maybe-redo)))
  17864. (eval-after-load "speedbar"
  17865. '(progn
  17866. (speedbar-add-supported-extension ".org")
  17867. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17868. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17869. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17870. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17871. (add-hook 'speedbar-visiting-tag-hook
  17872. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17873. ;;; Fixes and Hacks for problems with other packages
  17874. ;; Make flyspell not check words in links, to not mess up our keymap
  17875. (defun org-mode-flyspell-verify ()
  17876. "Don't let flyspell put overlays at active buttons."
  17877. (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
  17878. (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
  17879. (defun org-remove-flyspell-overlays-in (beg end)
  17880. "Remove flyspell overlays in region."
  17881. (and (org-bound-and-true-p flyspell-mode)
  17882. (fboundp 'flyspell-delete-region-overlays)
  17883. (flyspell-delete-region-overlays beg end))
  17884. (add-text-properties beg end '(org-no-flyspell t)))
  17885. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17886. (eval-after-load "bookmark"
  17887. '(if (boundp 'bookmark-after-jump-hook)
  17888. ;; We can use the hook
  17889. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17890. ;; Hook not available, use advice
  17891. (defadvice bookmark-jump (after org-make-visible activate)
  17892. "Make the position visible."
  17893. (org-bookmark-jump-unhide))))
  17894. ;; Make sure saveplace shows the location if it was hidden
  17895. (eval-after-load "saveplace"
  17896. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17897. "Make the position visible."
  17898. (org-bookmark-jump-unhide)))
  17899. ;; Make sure ecb shows the location if it was hidden
  17900. (eval-after-load "ecb"
  17901. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17902. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17903. (if (eq major-mode 'org-mode)
  17904. (org-show-context))))
  17905. (defun org-bookmark-jump-unhide ()
  17906. "Unhide the current position, to show the bookmark location."
  17907. (and (org-mode-p)
  17908. (or (org-invisible-p)
  17909. (save-excursion (goto-char (max (point-min) (1- (point))))
  17910. (org-invisible-p)))
  17911. (org-show-context 'bookmark-jump)))
  17912. ;; Make session.el ignore our circular variable
  17913. (eval-after-load "session"
  17914. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17915. ;;;; Experimental code
  17916. (defun org-closed-in-range ()
  17917. "Sparse tree of items closed in a certain time range.
  17918. Still experimental, may disappear in the future."
  17919. (interactive)
  17920. ;; Get the time interval from the user.
  17921. (let* ((time1 (org-float-time
  17922. (org-read-date nil 'to-time nil "Starting date: ")))
  17923. (time2 (org-float-time
  17924. (org-read-date nil 'to-time nil "End date:")))
  17925. ;; callback function
  17926. (callback (lambda ()
  17927. (let ((time
  17928. (org-float-time
  17929. (apply 'encode-time
  17930. (org-parse-time-string
  17931. (match-string 1))))))
  17932. ;; check if time in interval
  17933. (and (>= time time1) (<= time time2))))))
  17934. ;; make tree, check each match with the callback
  17935. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17936. ;;;; Finish up
  17937. (provide 'org)
  17938. (run-hooks 'org-load-hook)
  17939. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17940. ;;; org.el ends here